{
QueryDesc *queryDesc = PortalGetQueryDesc(portal);
Portal saveActivePortal;
+ Snapshot saveActiveSnapshot;
ResourceOwner saveResourceOwner;
MemoryContext savePortalContext;
MemoryContext saveQueryContext;
* Set up global portal context pointers.
*/
saveActivePortal = ActivePortal;
+ saveActiveSnapshot = ActiveSnapshot;
saveResourceOwner = CurrentResourceOwner;
savePortalContext = PortalContext;
saveQueryContext = QueryContext;
PG_TRY();
{
ActivePortal = portal;
+ ActiveSnapshot = queryDesc->snapshot;
CurrentResourceOwner = portal->resowner;
PortalContext = PortalGetHeapMemory(portal);
QueryContext = portal->queryContext;
/* we do not need AfterTriggerEndQuery() here */
/*
- * Reset 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?
+ * 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? (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 */
+ HeapTuple tup;
+ bool should_free;
+
+ while ((tup = tuplestore_gettuple(portal->holdStore, true,
+ &should_free)) != NULL)
+ {
+ if (should_free)
+ pfree(tup);
+ }
+ }
+ else
{
long store_pos;
/* Restore global vars and propagate error */
ActivePortal = saveActivePortal;
+ ActiveSnapshot = saveActiveSnapshot;
CurrentResourceOwner = saveResourceOwner;
PortalContext = savePortalContext;
QueryContext = saveQueryContext;
portal->status = PORTAL_READY;
ActivePortal = saveActivePortal;
+ ActiveSnapshot = saveActiveSnapshot;
CurrentResourceOwner = saveResourceOwner;
PortalContext = savePortalContext;
QueryContext = saveQueryContext;