在pg的日志中经常会看到unname statement。它通常指的是unnamed prepared statement,由支持wire protocol的客户端(使用libpq的库)发起。通常是调用PQprepare方法,如下:
PGresult *PQprepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes);
stmtName
can be ""
to create an unnamed statement, in which case any pre-existing unnamed statement is automatically replaced。所以每次新的事务开始时,都会判断有没有unamed stmt,有的话清理掉,它不保存在公共的事务表,所以任何时候只存在一个。如下:
static void exec_simple_query(const char *query_string) { ... /* * Zap any pre-existing unnamed statement. (While not strictly necessary, * it seems best to define simple-Query mode as if it used the unnamed * statement and portal; this ensures we recover any storage used by prior * unnamed operations.) */ drop_unnamed_stmt(); ... }
https://www.postgresql.org/docs/current/libpq-exec.html
https://www.pgcon.org/2014/schedule/attachments/330_postgres-for-the-wire.pdf
https://stackoverflow.com/questions/46810606/postgres-can-i-prepare-unnamed-statement-from-sql