aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2021-09-09 13:36:31 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2021-09-09 13:36:31 -0400
commit7813451c20c3b341c8af542d80bb941c9c893825 (patch)
tree75b32c0d9531186dd985b54d8fe8a9665bd5c5e0 /src
parent1a23b669db576460a42b257379335a447229ffa6 (diff)
downloadpostgresql-7813451c20c3b341c8af542d80bb941c9c893825.tar.gz
postgresql-7813451c20c3b341c8af542d80bb941c9c893825.zip
Avoid fetching from an already-terminated plan.
Some plan node types don't react well to being called again after they've already returned NULL. PortalRunSelect() has long dealt with this by calling the executor with NoMovementScanDirection if it sees that we've already run the portal to the end. However, commit ba2c6d6ce overlooked this point, so that persisting an already-fully-fetched cursor would fail if it had such a plan. Per report from Tomas Barton. Back-patch to v11, as the faulty commit was. (I've omitted a test case because the type of plan that causes a problem isn't all that stable.) Discussion: https://postgr.es/m/CAPV2KRjd=ErgVGbvO2Ty20tKTEZZr6cYsYLxgN_W3eAo9pf5sw@mail.gmail.com
Diffstat (limited to 'src')
-rw-r--r--src/backend/commands/portalcmds.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/backend/commands/portalcmds.c b/src/backend/commands/portalcmds.c
index 5608045a18a..1bd2bf39b15 100644
--- a/src/backend/commands/portalcmds.c
+++ b/src/backend/commands/portalcmds.c
@@ -365,6 +365,8 @@ PersistHoldablePortal(Portal portal)
savePortalContext = PortalContext;
PG_TRY();
{
+ ScanDirection direction = ForwardScanDirection;
+
ActivePortal = portal;
if (portal->resowner)
CurrentResourceOwner = portal->resowner;
@@ -387,10 +389,20 @@ PersistHoldablePortal(Portal portal)
}
else
{
- /* We must reset the cursor state as though at start of query */
+ /* Disallow moving backwards from here */
portal->atStart = true;
- portal->atEnd = false;
portal->portalPos = 0;
+
+ /*
+ * If we already reached end-of-query, set the direction to
+ * NoMovement to avoid trying to fetch any tuples. (This check
+ * exists because not all plan node types are robust about being
+ * called again if they've already returned NULL once.) We'll
+ * still set up an empty tuplestore, though, to keep this from
+ * being a special case later.
+ */
+ if (portal->atEnd)
+ direction = NoMovementScanDirection;
}
/*
@@ -405,7 +417,7 @@ PersistHoldablePortal(Portal portal)
true);
/* Fetch the result set into the tuplestore */
- ExecutorRun(queryDesc, ForwardScanDirection, 0L, false);
+ ExecutorRun(queryDesc, direction, 0L, false);
queryDesc->dest->rDestroy(queryDesc->dest);
queryDesc->dest = NULL;