aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/spi.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-08-21 10:22:14 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-08-21 10:22:14 -0400
commit26ae66090398082c54ce046936fc41633dbfc41e (patch)
tree0aa6a3a94f8dfcae9e775dac560b843814fb70f5 /src/backend/executor/spi.c
parentfacce1da918a8bf55a8f54606512f944529cba85 (diff)
downloadpostgresql-26ae66090398082c54ce046936fc41633dbfc41e.tar.gz
postgresql-26ae66090398082c54ce046936fc41633dbfc41e.zip
Improve error messages about misuse of SELECT INTO.
Improve two places in plpgsql, and one in spi.c, where an error message would confusingly tell you that you couldn't use a SELECT query, when what you had written *was* a SELECT query. The actual problem is that you can't use SELECT ... INTO in these contexts, but the messages failed to make that apparent. Special-case SELECT INTO to make these errors more helpful. Also, fix the same spots in plpgsql, as well as several messages in exec_eval_expr(), to not quote the entire complained-of query or expression in the primary error message. That behavior very easily led to violating our message style guideline about keeping the primary error message short and single-line. Also, since the important part of the message was after the inserted text, it could make the real problem very hard to see. We can report the query or expression as the first line of errcontext instead. Per complaint from Roger Mason. Back-patch to v14, since (a) some of these messages are new in v14 and (b) v14's translatable strings are still somewhat in flux. The problem's older than that of course, but I'm hesitant to change the behavior further back. Discussion: https://postgr.es/m/1914708.1629474624@sss.pgh.pa.us
Diffstat (limited to 'src/backend/executor/spi.c')
-rw-r--r--src/backend/executor/spi.c10
1 files changed, 8 insertions, 2 deletions
diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index bf619d3a65a..a8d7fe6dabf 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -1489,16 +1489,22 @@ SPI_cursor_open_internal(const char *name, SPIPlanPtr plan,
if (!SPI_is_cursor_plan(plan))
{
/* try to give a good error message */
+ const char *cmdtag;
+
if (list_length(plan->plancache_list) != 1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
errmsg("cannot open multi-query plan as cursor")));
plansource = (CachedPlanSource *) linitial(plan->plancache_list);
+ /* A SELECT that fails SPI_is_cursor_plan() must be SELECT INTO */
+ if (plansource->commandTag == CMDTAG_SELECT)
+ cmdtag = "SELECT INTO";
+ else
+ cmdtag = GetCommandTagName(plansource->commandTag);
ereport(ERROR,
(errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
/* translator: %s is name of a SQL command, eg INSERT */
- errmsg("cannot open %s query as cursor",
- GetCommandTagName(plansource->commandTag))));
+ errmsg("cannot open %s query as cursor", cmdtag)));
}
Assert(list_length(plan->plancache_list) == 1);