pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix checking of query type in plpgsql's RETURN QUERY command.
Date: Sun, 03 Oct 2021 17:21:32 +0000
Message-ID: <E1mX5B6-00061p-RT@gemulon.postgresql.org> (raw)

Fix checking of query type in plpgsql's RETURN QUERY command.

Prior to v14, we insisted that the query in RETURN QUERY be of a type
that returns tuples.  (For instance, INSERT RETURNING was allowed,
but not plain INSERT.)  That happened indirectly because we opened a
cursor for the query, so spi.c checked SPI_is_cursor_plan().  As a
consequence, the error message wasn't terribly on-point, but at least
it was there.

Commit 2f48ede08 lost this detail.  Instead, plain RETURN QUERY
insisted that the query be a SELECT (by checking for SPI_OK_SELECT)
while RETURN QUERY EXECUTE failed to check the query type at all.
Neither of these changes was intended.

The only convenient place to check this in the EXECUTE case is inside
_SPI_execute_plan, because we haven't done parse analysis until then.
So we need to pass down a flag saying whether to enforce that the
query returns tuples.  Fortunately, we can squeeze another boolean
into struct SPIExecuteOptions without an ABI break, since there's
padding space there.  (It's unlikely that any extensions would
already be using this new struct, but preserving ABI in v14 seems
like a smart idea anyway.)

Within spi.c, it seemed like _SPI_execute_plan's parameter list
was already ridiculously long, and I didn't want to make it longer.
So I thought of passing SPIExecuteOptions down as-is, allowing that
parameter list to become much shorter.  This makes the patch a bit
more invasive than it might otherwise be, but it's all internal to
spi.c, so that seems fine.

Per report from Marc Bachmann.  Back-patch to v14 where the
faulty code came in.

Discussion: https://postgr.es/m/1F2F75F0-27DF-406F-848D-8B50C7EEF06A@gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/a0558cfa395b47adb245972f5eba7978461e7baa

Modified Files
--------------
doc/src/sgml/spi.sgml                 |  22 +++++
src/backend/executor/spi.c            | 170 ++++++++++++++++++++++------------
src/include/executor/spi.h            |   1 +
src/pl/plpgsql/src/pl_exec.c          |  22 +----
src/test/regress/expected/plpgsql.out |  27 +++++-
src/test/regress/sql/plpgsql.sql      |  24 ++++-
6 files changed, 186 insertions(+), 80 deletions(-)



view thread (2+ messages)  latest in thread

Message-ID: <E1mX5B6-00061p-RT@gemulon.postgresql.org>
Permalink:  ../E1mX5B6-00061p-RT@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1mX5B6-00061p-RT@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us
  Subject: Re: pgsql: Fix checking of query type in plpgsql's RETURN QUERY command.
  In-Reply-To: <E1mX5B6-00061p-RT@gemulon.postgresql.org>
  References: <E1mX5B6-00061p-RT@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox