From 3a2f9b2baf0c3095d818f1563c52bec5a9878345 Mon Sep 17 00:00:00 2001 From: Kirill Reshke Date: Thu, 26 Nov 2026 11:53:31 +0500 Subject: [PATCH v2 2/2] Add psql commands to select and close a destination portal Use \portal after \bind_named to choose the portal for the pending execution. Allow \close_portal to send a protocol Close within a pipeline or transaction. Exercise portal replacement, visibility and lifetime through psql. --- doc/src/sgml/ref/psql-ref.sgml | 44 ++++++++++++++++- src/bin/psql/command.c | 50 ++++++++++++++++--- src/bin/psql/common.c | 20 +++++--- src/bin/psql/help.c | 2 + src/bin/psql/settings.h | 1 + src/bin/psql/tab-complete.in.c | 6 +-- src/test/regress/expected/psql.out | 53 +++++++++++++++++++++ src/test/regress/expected/psql_pipeline.out | 30 ++++++++++++ src/test/regress/sql/psql.sql | 21 ++++++++ src/test/regress/sql/psql_pipeline.sql | 11 +++++ 10 files changed, 219 insertions(+), 19 deletions(-) diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index b0f86e931ab..8dfdeef42f1 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -932,7 +932,8 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g \bind_named is equivalent to \bind, except that it takes the name of an existing prepared statement as first parameter. An empty string denotes the unnamed prepared - statement. + statement. To choose the destination portal, follow this command + with \portal. @@ -1067,6 +1068,18 @@ INSERT INTO tbls1 VALUES ($1, $2) \parse stmt1 + + \close_portal portal_name + + + + Sends an extended-protocol Close message for the specified portal. + An empty string selects the unnamed portal. Closing a portal that + does not exist succeeds. + + + + \close_prepared prepared_statement_name @@ -3010,6 +3023,32 @@ SELECT $1 \parse stmt1 + + \portal portal_name + + + + Selects the destination portal for a pending \bind_named. + Use it after \bind_named and before the command + that sends the query. If used more than once, the last name wins. + An empty string selects the unnamed portal, which is the default. + The command has no effect without a pending \bind_named. + + + A named portal lasts until the transaction ends or it is closed. + For example, to inspect and then close a portal: + +BEGIN; +SELECT $1::integer \parse stmt1 +\bind_named stmt1 42 \portal p1 \g +SELECT name FROM pg_cursors WHERE name = 'p1'; +\close_portal p1 +COMMIT; + + + + + \prompt [ text ] name @@ -3755,7 +3794,8 @@ testdb=> \setenv LESS -imx4F All queries executed while a pipeline is ongoing use the extended query protocol. Queries are appended to the pipeline when ending with a semicolon. The meta-commands \bind, - \bind_named, \close_prepared or + \bind_named, \portal, + \close_portal, \close_prepared or \parse can be used in an ongoing pipeline. While a pipeline is ongoing, \sendpipeline will append the current query buffer to the pipeline. Other meta-commands like diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 407d263fb20..54286c816d3 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -67,8 +67,8 @@ static backslashResult exec_command_C(PsqlScanState scan_state, bool active_bran static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd); -static backslashResult exec_command_close_prepared(PsqlScanState scan_state, - bool active_branch, const char *cmd); +static backslashResult exec_command_close(PsqlScanState scan_state, + bool active_branch, const char *cmd); static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch); @@ -124,6 +124,7 @@ static backslashResult exec_command_print(PsqlScanState scan_state, bool active_ static backslashResult exec_command_parse(PsqlScanState scan_state, bool active_branch, const char *cmd); static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch); +static backslashResult exec_command_portal(PsqlScanState scan_state, bool active_branch); static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch, const char *cmd); static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch); @@ -349,8 +350,8 @@ exec_command(const char *cmd, status = exec_command_connect(scan_state, active_branch); else if (strcmp(cmd, "cd") == 0) status = exec_command_cd(scan_state, active_branch, cmd); - else if (strcmp(cmd, "close_prepared") == 0) - status = exec_command_close_prepared(scan_state, active_branch, cmd); + else if (strcmp(cmd, "close_portal") == 0 || strcmp(cmd, "close_prepared") == 0) + status = exec_command_close(scan_state, active_branch, cmd); else if (strcmp(cmd, "conninfo") == 0) status = exec_command_conninfo(scan_state, active_branch); else if (pg_strcasecmp(cmd, "copy") == 0) @@ -427,6 +428,8 @@ exec_command(const char *cmd, status = exec_command_parse(scan_state, active_branch, cmd); else if (strcmp(cmd, "password") == 0) status = exec_command_password(scan_state, active_branch); + else if (strcmp(cmd, "portal") == 0) + status = exec_command_portal(scan_state, active_branch); else if (strcmp(cmd, "prompt") == 0) status = exec_command_prompt(scan_state, active_branch, cmd); else if (strcmp(cmd, "pset") == 0) @@ -751,10 +754,10 @@ exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd) } /* - * \close_prepared -- close a previously prepared statement + * \close_portal, \close_prepared -- close a portal or a prepared statement */ static backslashResult -exec_command_close_prepared(PsqlScanState scan_state, bool active_branch, const char *cmd) +exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd) { backslashResult status = PSQL_CMD_SKIP_LINE; @@ -772,7 +775,10 @@ exec_command_close_prepared(PsqlScanState scan_state, bool active_branch, const } else { - pset.stmtName = opt; + if (strcmp(cmd, "close_portal") == 0) + pset.portalName = opt; + else + pset.stmtName = opt; pset.send_mode = PSQL_SEND_EXTENDED_CLOSE; status = PSQL_CMD_SEND; } @@ -2614,6 +2620,36 @@ exec_command_password(PsqlScanState scan_state, bool active_branch) return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR; } +/* + * \portal -- select the destination portal for a pending \bind_named + */ +static backslashResult +exec_command_portal(PsqlScanState scan_state, bool active_branch) +{ + if (active_branch) + { + char *name = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false); + + if (!name) + { + pg_log_error("\\%s: missing required argument", "portal"); + return PSQL_CMD_ERROR; + } + + if (pset.send_mode == PSQL_SEND_EXTENDED_QUERY_PREPARED) + { + free(pset.portalName); + pset.portalName = name; + } + else + free(name); + } + else + ignore_slash_options(scan_state); + + return PSQL_CMD_SKIP_LINE; +} + /* * \prompt -- prompt and set variable */ diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index becb22ea7e2..cc3fd6a0c08 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1602,7 +1602,10 @@ ExecQueryAndProcessResults(const char *query, switch (pset.send_mode) { case PSQL_SEND_EXTENDED_CLOSE: - success = PQsendClosePrepared(pset.db, pset.stmtName); + if (pset.portalName) + success = PQsendClosePortal(pset.db, pset.portalName); + else + success = PQsendClosePrepared(pset.db, pset.stmtName); if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF) pset.piped_commands++; break; @@ -1622,10 +1625,11 @@ ExecQueryAndProcessResults(const char *query, break; case PSQL_SEND_EXTENDED_QUERY_PREPARED: Assert(pset.stmtName != NULL); - success = PQsendQueryPrepared(pset.db, pset.stmtName, - pset.bind_nparams, - (const char *const *) pset.bind_params, - NULL, NULL, 0); + success = PQsendQueryPreparedPortal(pset.db, pset.stmtName, + pset.portalName ? pset.portalName : "", + pset.bind_nparams, + (const char *const *) pset.bind_params, + NULL, NULL, 0); if (success && PQpipelineStatus(pset.db) != PQ_PIPELINE_OFF) pset.piped_commands++; break; @@ -2838,7 +2842,7 @@ uri_prefix_length(const char *connstr) /* * Reset state related to extended query protocol * - * Clean up any state related to bind parameters, statement name and + * Clean up any state related to bind parameters, statement and portal names, and * PSQL_SEND_MODE. This needs to be called after processing a query or when * running a new meta-command that uses the extended query protocol, like * \parse, \bind, etc. @@ -2850,7 +2854,7 @@ clean_extended_state(void) switch (pset.send_mode) { - case PSQL_SEND_EXTENDED_CLOSE: /* \close_prepared */ + case PSQL_SEND_EXTENDED_CLOSE: /* \close_prepared or \close_portal */ free(pset.stmtName); break; case PSQL_SEND_EXTENDED_PARSE: /* \parse */ @@ -2874,6 +2878,8 @@ clean_extended_state(void) break; } + free(pset.portalName); + pset.portalName = NULL; pset.stmtName = NULL; pset.send_mode = PSQL_SEND_QUERY; } diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index 0def23418d5..91c7caf7efa 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -331,6 +331,7 @@ slashUsage(unsigned short int pager) HELP0(" \\bind [PARAM]... set query parameters\n"); HELP0(" \\bind_named STMT_NAME [PARAM]...\n" " set query parameters for an existing prepared statement\n"); + HELP0(" \\close_portal NAME close a portal\n"); HELP0(" \\close_prepared STMT_NAME\n" " close an existing prepared statement\n"); HELP0(" \\endpipeline exit pipeline mode\n"); @@ -338,6 +339,7 @@ slashUsage(unsigned short int pager) HELP0(" \\flushrequest send request to the server to flush its output buffer\n"); HELP0(" \\getresults [NUM_RES] read NUM_RES pending results, or all if no argument\n"); HELP0(" \\parse STMT_NAME create a prepared statement\n"); + HELP0(" \\portal NAME select the destination portal for \\bind_named\n"); HELP0(" \\sendpipeline send an extended query to an ongoing pipeline\n"); HELP0(" \\startpipeline enter pipeline mode\n"); HELP0(" \\syncpipeline add a synchronisation point to an ongoing pipeline\n"); diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index a3d088d80cd..5af89e542d9 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -123,6 +123,7 @@ typedef struct _psqlSettings char **bind_params; /* parameters for extended query protocol call */ char *stmtName; /* prepared statement name used for extended * query protocol commands */ + char *portalName; /* portal for \bind_named or \close_portal */ int piped_commands; /* number of piped commands */ int piped_syncs; /* number of piped syncs */ int available_results; /* number of results available to get */ diff --git a/src/bin/psql/tab-complete.in.c b/src/bin/psql/tab-complete.in.c index 96de78cd087..9774587d7e3 100644 --- a/src/bin/psql/tab-complete.in.c +++ b/src/bin/psql/tab-complete.in.c @@ -1911,8 +1911,8 @@ psql_completion(const char *text, int start, int end) static const char *const backslash_commands[] = { "\\a", "\\bind", "\\bind_named", - "\\connect", "\\conninfo", "\\C", "\\cd", "\\close_prepared", "\\copy", - "\\copyright", "\\crosstabview", + "\\connect", "\\conninfo", "\\C", "\\cd", "\\close_portal", "\\close_prepared", + "\\copy", "\\copyright", "\\crosstabview", "\\d", "\\da", "\\dA", "\\dAc", "\\dAf", "\\dAo", "\\dAp", "\\db", "\\dc", "\\dconfig", "\\dC", "\\dd", "\\ddp", "\\dD", "\\des", "\\det", "\\deu", "\\dew", "\\dE", "\\df", @@ -1928,7 +1928,7 @@ psql_completion(const char *text, int start, int end) "\\if", "\\include", "\\include_relative", "\\ir", "\\list", "\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink", "\\out", - "\\parse", "\\password", "\\print", "\\prompt", "\\pset", + "\\parse", "\\password", "\\portal", "\\print", "\\prompt", "\\pset", "\\qecho", "\\quit", "\\reset", "\\restrict", "\\s", "\\sendpipeline", "\\set", "\\setenv", "\\sf", diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out index e8605dd041f..1aaede18e62 100644 --- a/src/test/regress/expected/psql.out +++ b/src/test/regress/expected/psql.out @@ -160,6 +160,57 @@ LINE 1: SELECT $1, $2 foo4 | bar4 (1 row) +-- Destination portal for a pending bind +\portal +\portal: missing required argument +\close_portal +\close_portal: missing required argument +SELECT name = '' AS unnamed FROM pg_cursors WHERE statement = current_query() \parse inspect_portal +BEGIN; +\portal unused +\bind_named inspect_portal \g + unnamed +--------- + t +(1 row) + +\bind_named stmt2 'named' \portal unused \portal chosen \g + ?column? +---------- + named +(1 row) + +SELECT name FROM pg_cursors WHERE name IN ('unused', 'chosen') ORDER BY name; + name +-------- + chosen +(1 row) + +\close_portal chosen +SELECT count(*) FROM pg_cursors WHERE name = 'chosen'; + count +------- + 0 +(1 row) + +\close_portal chosen +\bind_named inspect_portal \portal overwritten \portal '' \g + unnamed +--------- + t +(1 row) + +\close_portal '' +\bind_named stmt2 'first' \portal chosen \g + ?column? +---------- + first +(1 row) + +\bind_named stmt2 'duplicate' \portal chosen \g +ERROR: cursor "chosen" already exists +ROLLBACK; +\close_prepared inspect_portal -- \close_prepared (extended query protocol) \close_prepared \close_prepared: missing required argument @@ -4724,6 +4775,8 @@ bar 'bar' "bar" \C arg1 \c arg1 arg2 arg3 arg4 \cd arg1 + \portal ignored + \close_portal ignored \close_prepared stmt1 \conninfo \copy arg1 arg2 arg3 arg4 arg5 arg6 diff --git a/src/test/regress/expected/psql_pipeline.out b/src/test/regress/expected/psql_pipeline.out index 6fc5aea49d9..00bee71a9fa 100644 --- a/src/test/regress/expected/psql_pipeline.out +++ b/src/test/regress/expected/psql_pipeline.out @@ -417,6 +417,36 @@ SELECT $1 \bind 3 \sendpipeline (1 row) \endpipeline +-- Inspect and close a named portal in the pipeline's implicit transaction. +\startpipeline +SELECT $1::integer AS n \parse portal_stmt +\bind_named portal_stmt 42 \portal portal_stmt \sendpipeline +SELECT name FROM pg_cursors WHERE name = 'portal_stmt'; +\close_portal portal_stmt +SELECT count(*) FROM pg_cursors WHERE name = 'portal_stmt'; +\bind_named portal_stmt 43 \portal portal_stmt \sendpipeline +\close_prepared portal_stmt +\endpipeline + n +---- + 42 +(1 row) + + name +------------- + portal_stmt +(1 row) + + count +------- + 0 +(1 row) + + n +---- + 43 +(1 row) + -- -- Pipeline errors -- diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql index cd685b43e0f..713c2deb92a 100644 --- a/src/test/regress/sql/psql.sql +++ b/src/test/regress/sql/psql.sql @@ -68,6 +68,25 @@ SELECT $1, $2 \parse stmt3 -- Multiple \g calls mean multiple executions \bind_named stmt2 'foo3' \g \bind_named stmt3 'foo4' 'bar4' \g +-- Destination portal for a pending bind +\portal +\close_portal +SELECT name = '' AS unnamed FROM pg_cursors WHERE statement = current_query() \parse inspect_portal +BEGIN; +\portal unused +\bind_named inspect_portal \g +\bind_named stmt2 'named' \portal unused \portal chosen \g +SELECT name FROM pg_cursors WHERE name IN ('unused', 'chosen') ORDER BY name; +\close_portal chosen +SELECT count(*) FROM pg_cursors WHERE name = 'chosen'; +\close_portal chosen +\bind_named inspect_portal \portal overwritten \portal '' \g +\close_portal '' +\bind_named stmt2 'first' \portal chosen \g +\bind_named stmt2 'duplicate' \portal chosen \g +ROLLBACK; +\close_prepared inspect_portal + -- \close_prepared (extended query protocol) \close_prepared \close_prepared '' @@ -1062,6 +1081,8 @@ select \if false \\ (bogus \else \\ 42 \endif \\ forty_two; \C arg1 \c arg1 arg2 arg3 arg4 \cd arg1 + \portal ignored + \close_portal ignored \close_prepared stmt1 \conninfo \copy arg1 arg2 arg3 arg4 arg5 arg6 diff --git a/src/test/regress/sql/psql_pipeline.sql b/src/test/regress/sql/psql_pipeline.sql index b6bd917c90b..5ef28f89b46 100644 --- a/src/test/regress/sql/psql_pipeline.sql +++ b/src/test/regress/sql/psql_pipeline.sql @@ -200,6 +200,17 @@ SELECT $1 \bind 3 \sendpipeline \getresults 0 \endpipeline +-- Inspect and close a named portal in the pipeline's implicit transaction. +\startpipeline +SELECT $1::integer AS n \parse portal_stmt +\bind_named portal_stmt 42 \portal portal_stmt \sendpipeline +SELECT name FROM pg_cursors WHERE name = 'portal_stmt'; +\close_portal portal_stmt +SELECT count(*) FROM pg_cursors WHERE name = 'portal_stmt'; +\bind_named portal_stmt 43 \portal portal_stmt \sendpipeline +\close_prepared portal_stmt +\endpipeline + -- -- Pipeline errors -- -- 2.50.1 (Apple Git-155)