summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2014-05-07 18:25:22 +0000
committerTom Lane2014-05-07 18:25:22 +0000
commit229101db4d7696c555b338264ef9c89f769c511d (patch)
treeaec01eb5c5c44eb059430784f1ace465b40dbe81
parent47e4309c07a399ce4bf896e0ba5edcdf1a691ada (diff)
Fix failure to set ActiveSnapshot while rewinding a cursor.
ActiveSnapshot needs to be set when we call ExecutorRewind because some plan node types may execute user-defined functions during their ReScan calls (nodeLimit.c does so, at least). The wisdom of that is somewhat debatable, perhaps, but for now the simplest fix is to make sure the required context is valid. Failure to do this typically led to a null-pointer-dereference core dump, though it's possible that in more complex cases a function could be executed with the wrong snapshot leading to very subtle misbehavior. Per report from Leif Jensen. It's been broken for a long time, so back-patch to all active branches.
-rw-r--r--src/backend/tcop/pquery.c14
-rw-r--r--src/test/regress/expected/portals.out24
-rw-r--r--src/test/regress/sql/portals.sql11
3 files changed, 47 insertions, 2 deletions
diff --git a/src/backend/tcop/pquery.c b/src/backend/tcop/pquery.c
index ef6520c5783..87ea6ce9e52 100644
--- a/src/backend/tcop/pquery.c
+++ b/src/backend/tcop/pquery.c
@@ -1660,6 +1660,9 @@ DoPortalRunFetch(Portal portal,
static void
DoPortalRewind(Portal portal)
{
+ QueryDesc *queryDesc;
+
+ /* Rewind holdStore, if we have one */
if (portal->holdStore)
{
MemoryContext oldcontext;
@@ -1668,8 +1671,15 @@ DoPortalRewind(Portal portal)
tuplestore_rescan(portal->holdStore);
MemoryContextSwitchTo(oldcontext);
}
- if (PortalGetQueryDesc(portal))
- ExecutorRewind(PortalGetQueryDesc(portal));
+
+ /* Rewind executor, if active */
+ queryDesc = PortalGetQueryDesc(portal);
+ if (queryDesc)
+ {
+ PushActiveSnapshot(queryDesc->snapshot);
+ ExecutorRewind(queryDesc);
+ PopActiveSnapshot();
+ }
portal->atStart = true;
portal->atEnd = false;
diff --git a/src/test/regress/expected/portals.out b/src/test/regress/expected/portals.out
index 01152a939d3..1dc22957d29 100644
--- a/src/test/regress/expected/portals.out
+++ b/src/test/regress/expected/portals.out
@@ -1257,3 +1257,27 @@ FETCH ALL FROM c1;
COMMIT;
DROP TABLE cursor;
+-- Check rewinding a cursor containing a stable function in LIMIT,
+-- per bug report in 8336843.9833.1399385291498.JavaMail.root@quick
+begin;
+create function nochange(int) returns int
+ as 'select $1 limit 1' language sql stable;
+declare c cursor for select * from int8_tbl limit nochange(3);
+fetch all from c;
+ q1 | q2
+------------------+------------------
+ 123 | 456
+ 123 | 4567890123456789
+ 4567890123456789 | 123
+(3 rows)
+
+move backward all in c;
+fetch all from c;
+ q1 | q2
+------------------+------------------
+ 123 | 456
+ 123 | 4567890123456789
+ 4567890123456789 | 123
+(3 rows)
+
+rollback;
diff --git a/src/test/regress/sql/portals.sql b/src/test/regress/sql/portals.sql
index 02286c4096e..3777c286870 100644
--- a/src/test/regress/sql/portals.sql
+++ b/src/test/regress/sql/portals.sql
@@ -470,3 +470,14 @@ UPDATE cursor SET a = 2;
FETCH ALL FROM c1;
COMMIT;
DROP TABLE cursor;
+
+-- Check rewinding a cursor containing a stable function in LIMIT,
+-- per bug report in 8336843.9833.1399385291498.JavaMail.root@quick
+begin;
+create function nochange(int) returns int
+ as 'select $1 limit 1' language sql stable;
+declare c cursor for select * from int8_tbl limit nochange(3);
+fetch all from c;
+move backward all in c;
+fetch all from c;
+rollback;