Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Fix failure to set ActiveSnapshot while rewinding a cursor.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 May 2014 18:25:25 +0000 (14:25 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 7 May 2014 18:25:25 +0000 (14:25 -0400)
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.

src/backend/tcop/pquery.c
src/test/regress/expected/portals.out
src/test/regress/sql/portals.sql

index 13e7bf23f75a1a42b33de30aed69588ab8316021..09135370c785f6aea00d216a1e8257d9f40427af 100644 (file)
@@ -1628,6 +1628,9 @@ DoPortalRunFetch(Portal portal,
 static void
 DoPortalRewind(Portal portal)
 {
+   QueryDesc  *queryDesc;
+
+   /* Rewind holdStore, if we have one */
    if (portal->holdStore)
    {
        MemoryContext oldcontext;
@@ -1636,8 +1639,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;
index be7348d6b2adaba4f4357672011d9cffa48a827f..990bfac35d64564088e184c68594feed5032517f 100644 (file)
@@ -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;
index 585a7c25ea7514666d5ef395c5ba2e6efcf2a293..2bdad4b59fc75f36647b1f6fc7f981cd904be10c 100644 (file)
@@ -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;