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: Avoid misbehavior when persisting a non-stable cursor.
Date: Tue, 08 Jun 2021 21:50:54 +0000
Message-ID: <E1lqjcc-00062U-QV@gemulon.postgresql.org> (raw)

Avoid misbehavior when persisting a non-stable cursor.

PersistHoldablePortal has long assumed that it should store the
entire output of the query-to-be-persisted, which requires rewinding
and re-reading the output.  This is problematic if the query is not
stable: we might get different row contents, or even a different
number of rows, which'd confuse the cursor state mightily.

In the case where the cursor is NO SCROLL, this is very easy to
solve: just store the remaining query output, without any rewinding,
and tweak the portal's cursor state to match.  Aside from removing
the semantic problem, this could be significantly more efficient
than storing the whole output.

If the cursor is scrollable, there's not much we can do, but it
was already the case that scrolling a volatile query's result was
pretty unsafe.  We can just document more clearly that getting
correct results from that is not guaranteed.

There are already prohibitions in place on using SCROLL with
FOR UPDATE/SHARE, which is one way for a SELECT query to have
non-stable results.  We could imagine prohibiting SCROLL when
the query contains volatile functions, but that would be
expensive to enforce.  Moreover, it could break applications
that work just fine, if they have functions that are in fact
stable but the user neglected to mark them so.  So settle for
documenting the hazard.

While this problem has existed in some guise for a long time,
it got a lot worse in v11, which introduced the possibility
of persisting plpgsql cursors (perhaps implicit ones) even
when they violate the rules for what can be marked WITH HOLD.
Hence, I've chosen to back-patch to v11 but not further.

Per bug #17050 from Алексей Булгаков.

Discussion: https://postgr.es/m/17050-f77aa827dc85247c@postgresql.org

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/2757865fa7fb213eb83088927d5c8d5a9425e555

Modified Files
--------------
doc/src/sgml/plpgsql.sgml                          |  9 ++++
doc/src/sgml/ref/declare.sgml                      |  8 ++--
src/backend/commands/portalcmds.c                  | 19 ++++++--
.../plpgsql/src/expected/plpgsql_transaction.out   | 51 ++++++++++++++++++++++
src/pl/plpgsql/src/sql/plpgsql_transaction.sql     | 41 +++++++++++++++++
5 files changed, 122 insertions(+), 6 deletions(-)



view thread (4+ messages)

Message-ID: <E1lqjcc-00062U-QV@gemulon.postgresql.org>
Permalink:  ../E1lqjcc-00062U-QV@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1lqjcc-00062U-QV@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: Avoid misbehavior when persisting a non-stable cursor.
  In-Reply-To: <E1lqjcc-00062U-QV@gemulon.postgresql.org>
  References: <E1lqjcc-00062U-QV@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