diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 180781ecd05..4d2c81aa753 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -471,7 +471,7 @@ exec_command_bind(PsqlScanState scan_state, bool active_branch) int nparams = 0; int nalloc = 0; - pset.bind_params = NULL; + clean_bind_state(); while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false))) { diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index fe8e049c4c1..4a50eb1e672 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -1275,14 +1275,7 @@ sendquery_cleanup: } /* clean up after \bind */ - if (pset.bind_flag) - { - for (i = 0; i < pset.bind_nparams; i++) - free(pset.bind_params[i]); - free(pset.bind_params); - pset.bind_params = NULL; - pset.bind_flag = false; - } + clean_bind_state(); /* reset \gset trigger */ if (pset.gset_prefix) @@ -2252,6 +2245,26 @@ uri_prefix_length(const char *connstr) return 0; } +/* + * Reset state related to \bind + * + * Clean up any state related to bind parameters and bind_flag. This needs + * to be called after processing a query or when running \bind. + */ +void +clean_bind_state(void) +{ + if (pset.bind_flag) + { + for (int i = 0; i < pset.bind_nparams; i++) + free(pset.bind_params[i]); + free(pset.bind_params); + } + + pset.bind_params = NULL; + pset.bind_flag = false; +} + /* * Recognized connection string either starts with a valid URI prefix or * contains a "=" in it. diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h index 6efe12274fe..9baa1e89064 100644 --- a/src/bin/psql/common.h +++ b/src/bin/psql/common.h @@ -41,6 +41,7 @@ extern bool standard_strings(void); extern const char *session_username(void); extern void expand_tilde(char **filename); +extern void clean_bind_state(void); extern bool recognized_connection_string(const char *connstr);