PostgreSQL 函数:关系不存在错误

PostgreSQL function : relation does not exist error

我在使用包含点 (.) 运算符的参数调用 Postgresql 函数时出现以下错误。

SQL Error [42P01]: ERROR: relation "es.article_data" does not exist Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE....

查询:select es.getrowcount(schemawithtable :='es.article_data');

函数:

CREATE FUNCTION es.getrowcount (schemawithtable text)
RETURNS VARCHAR(50) AS $msg$
declare
    msg VARCHAR(50);
    total integer;
begin
  execute format('select count(*) from %I where until_ts is null', schemawithtable) into total;
   msg := CONCAT(total, ' records are there in ',schemawithtable);
   RETURN msg;
END;
$msg$ LANGUAGE plpgsql;

来自 DBeaver 的错误跟踪:

org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [42P01]: ERROR: relation "es.article_data" does not exist
  Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE

    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:134)

    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:487)

    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda[=12=](SQLQueryJob.java:424)

    at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:164)

    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:416)

    at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:774)

    at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:2914)

    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda[=12=](ResultSetJobDataRead.java:110)

    at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:164)

    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:108)

    at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer.run(ResultSetViewer.java:3421)

    at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:103)

    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

Caused by: org.postgresql.util.PSQLException: ERROR: relation "es.article_data" does not exist
  Where: PL/pgSQL function es.getrowcount(text) line 6 at EXECUTE

    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)

    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)

    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)

    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)

    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)

    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:307)

    at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:293)

    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:270)

    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:266)

    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:338)

    at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)

    ... 12 more

我已经通过单独传递架构名称找到解决方法。

工作查询:select es.getrowcount(schemaname :='es',tablename :='article_data');

CREATE FUNCTION es.getrowcount (schemaname text, tablename text)
...
begin
  execute format('select count(*) from %I.%I where until_ts is null',schemaname, tablename) into total;
...
$msg$ LANGUAGE plpgsql;

版本: PostgreSQL - 10.12 DBeaver (client) - 6.3.5

但我想知道为什么对于包含点 (.) 运算符的参数会出错?

当您使用 format%I 创建查询时。它正在创建您的查询,如下所示

单参数:

select count(*) from "es.article_data" where until_ts is null

双参数:

select count(*) from "es"."article_data" where until_ts is null

在第一种情况下,它会显示错误,因为您不能将 table 名称与这样的架构一起使用。但在第二种情况下它工作得很好,因为这是使用命名约定的正确方法。

如果只想使用第一种方法。您应该使用 %s 而不是 %I,如下所示

CREATE FUNCTION es.getrowcount (schemawithtable text)
RETURNS VARCHAR(50) AS $msg$
declare
    msg VARCHAR(50);
    total integer;
begin
  execute format('select count(*) from %s where until_ts is null', schemawithtable) into total;
   msg := CONCAT(total, ' records are there in ',tablename);
   RETURN msg;
END;
$msg$ LANGUAGE plpgsql;

注意:如果您的 table 名称和架构名称仅采用小写形式,这将正常工作