Fix an error in the original coding of holdable cursors: PersistHoldablePortal
authorTom Lane <[email protected]>
Tue, 6 Feb 2007 22:49:30 +0000 (22:49 +0000)
committerTom Lane <[email protected]>
Tue, 6 Feb 2007 22:49:30 +0000 (22:49 +0000)
thought that it didn't have to reposition the underlying tuplestore if the
portal is atEnd.  But this is not so, because tuplestores have separate read
and write cursors ... and the read cursor hasn't moved from the start.
This mistake explains bug #2970 from William Zhang.

Note: the coding here is pretty inefficient, but given that no one has noticed
this bug until now, I'd say hardly anyone uses the case where the cursor has
been advanced before being persisted.  So maybe it's not worth worrying about.

src/backend/commands/portalcmds.c

index 9989e513f0fcbb59ed0c05c7ab702f69ba52496e..98ea764bf19797c53633f18bd31f9a7e40d23318 100644 (file)
@@ -392,15 +392,23 @@ PersistHoldablePortal(Portal portal)
                ExecutorEnd(queryDesc);
 
                /*
-                * Reset the position in the result set: ideally, this could be
+                * Set the position in the result set: ideally, this could be
                 * implemented by just skipping straight to the tuple # that we need
                 * to be at, but the tuplestore API doesn't support that. So we start
                 * at the beginning of the tuplestore and iterate through it until we
-                * reach where we need to be.  FIXME someday?
+                * reach where we need to be.  FIXME someday?  (Fortunately, the
+                * typical case is that we're supposed to be at or near the start
+                * of the result set, so this isn't as bad as it sounds.)
                 */
                MemoryContextSwitchTo(portal->holdContext);
 
-               if (!portal->atEnd)
+               if (portal->atEnd)
+               {
+                       /* we can handle this case even if posOverflow */
+                       while (tuplestore_advance(portal->holdStore, true))
+                               /* continue */ ;
+               }
+               else
                {
                        long            store_pos;