1 /*-------------------------------------------------------------------------
4 * POSTGRES process query command code
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/tcop/pquery.c,v 1.127 2008/12/01 17:06:21 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/xact.h"
19 #include "commands/prepare.h"
20 #include "commands/trigger.h"
21 #include "executor/tstoreReceiver.h"
22 #include "miscadmin.h"
24 #include "tcop/pquery.h"
25 #include "tcop/tcopprot.h"
26 #include "tcop/utility.h"
27 #include "utils/memutils.h"
28 #include "utils/snapmgr.h"
32 * ActivePortal is the currently executing Portal (the most closely nested,
33 * if there are several).
35 Portal ActivePortal = NULL;
38 static void ProcessQuery(PlannedStmt *plan,
42 static void FillPortalStore(Portal portal, bool isTopLevel);
43 static uint32 RunFromStore(Portal portal, ScanDirection direction, long count,
45 static long PortalRunSelect(Portal portal, bool forward, long count,
47 static void PortalRunUtility(Portal portal, Node *utilityStmt, bool isTopLevel,
48 DestReceiver *dest, char *completionTag);
49 static void PortalRunMulti(Portal portal, bool isTopLevel,
50 DestReceiver *dest, DestReceiver *altdest,
52 static long DoPortalRunFetch(Portal portal,
53 FetchDirection fdirection,
56 static void DoPortalRewind(Portal portal);
63 CreateQueryDesc(PlannedStmt *plannedstmt,
65 Snapshot crosscheck_snapshot,
70 QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
72 qd->operation = plannedstmt->commandType; /* operation */
73 qd->plannedstmt = plannedstmt; /* plan */
74 qd->utilitystmt = plannedstmt->utilityStmt; /* in case DECLARE CURSOR */
75 qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
76 /* RI check snapshot */
77 qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
78 qd->dest = dest; /* output dest */
79 qd->params = params; /* parameter values passed into query */
80 qd->doInstrument = doInstrument; /* instrumentation wanted? */
82 /* null these fields until set by ExecutorStart */
92 * CreateUtilityQueryDesc
95 CreateUtilityQueryDesc(Node *utilitystmt,
100 QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
102 qd->operation = CMD_UTILITY; /* operation */
103 qd->plannedstmt = NULL;
104 qd->utilitystmt = utilitystmt; /* utility command */
105 qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
106 qd->crosscheck_snapshot = InvalidSnapshot; /* RI check snapshot */
107 qd->dest = dest; /* output dest */
108 qd->params = params; /* parameter values passed into query */
109 qd->doInstrument = false; /* uninteresting for utilities */
111 /* null these fields until set by ExecutorStart */
114 qd->planstate = NULL;
115 qd->totaltime = NULL;
124 FreeQueryDesc(QueryDesc *qdesc)
126 /* Can't be a live query */
127 Assert(qdesc->estate == NULL);
129 /* forget our snapshots */
130 UnregisterSnapshot(qdesc->snapshot);
131 UnregisterSnapshot(qdesc->crosscheck_snapshot);
133 /* Only the QueryDesc itself need be freed */
140 * Execute a single plannable query within a PORTAL_MULTI_QUERY
141 * or PORTAL_ONE_RETURNING portal
143 * plan: the plan tree for the query
144 * params: any parameters needed
145 * dest: where to send results
146 * completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
147 * in which to store a command completion status string.
149 * completionTag may be NULL if caller doesn't want a status string.
151 * Must be called in a memory context that will be reset or deleted on
152 * error; otherwise the executor's memory usage will be leaked.
155 ProcessQuery(PlannedStmt *plan,
156 ParamListInfo params,
160 QueryDesc *queryDesc;
162 elog(DEBUG3, "ProcessQuery");
165 * Must always set a snapshot for plannable queries.
167 PushActiveSnapshot(GetTransactionSnapshot());
170 * Create the QueryDesc object
172 queryDesc = CreateQueryDesc(plan,
173 GetActiveSnapshot(), InvalidSnapshot,
174 dest, params, false);
177 * Set up to collect AFTER triggers
179 AfterTriggerBeginQuery();
182 * Call ExecutorStart to prepare the plan for execution
184 ExecutorStart(queryDesc, 0);
187 * Run the plan to completion.
189 ExecutorRun(queryDesc, ForwardScanDirection, 0L);
192 * Build command completion status string, if caller wants one.
198 switch (queryDesc->operation)
201 strcpy(completionTag, "SELECT");
204 if (queryDesc->estate->es_processed == 1)
205 lastOid = queryDesc->estate->es_lastoid;
207 lastOid = InvalidOid;
208 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
209 "INSERT %u %u", lastOid, queryDesc->estate->es_processed);
212 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
213 "UPDATE %u", queryDesc->estate->es_processed);
216 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
217 "DELETE %u", queryDesc->estate->es_processed);
220 strcpy(completionTag, "???");
225 /* Now take care of any queued AFTER triggers */
226 AfterTriggerEndQuery(queryDesc->estate);
231 * Now, we close down all the scans and free allocated resources.
233 ExecutorEnd(queryDesc);
235 FreeQueryDesc(queryDesc);
239 * ChoosePortalStrategy
240 * Select portal execution strategy given the intended statement list.
242 * The list elements can be Querys, PlannedStmts, or utility statements.
243 * That's more general than portals need, but plancache.c uses this too.
245 * See the comments in portal.h.
248 ChoosePortalStrategy(List *stmts)
254 * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
255 * single-statement case, since there are no rewrite rules that can add
256 * auxiliary queries to a SELECT or a utility command.
258 if (list_length(stmts) == 1)
260 Node *stmt = (Node *) linitial(stmts);
262 if (IsA(stmt, Query))
264 Query *query = (Query *) stmt;
266 if (query->canSetTag)
268 if (query->commandType == CMD_SELECT &&
269 query->utilityStmt == NULL &&
270 query->intoClause == NULL)
271 return PORTAL_ONE_SELECT;
272 if (query->commandType == CMD_UTILITY &&
273 query->utilityStmt != NULL)
275 if (UtilityReturnsTuples(query->utilityStmt))
276 return PORTAL_UTIL_SELECT;
277 /* it can't be ONE_RETURNING, so give up */
278 return PORTAL_MULTI_QUERY;
282 else if (IsA(stmt, PlannedStmt))
284 PlannedStmt *pstmt = (PlannedStmt *) stmt;
286 if (pstmt->canSetTag)
288 if (pstmt->commandType == CMD_SELECT &&
289 pstmt->utilityStmt == NULL &&
290 pstmt->intoClause == NULL)
291 return PORTAL_ONE_SELECT;
296 /* must be a utility command; assume it's canSetTag */
297 if (UtilityReturnsTuples(stmt))
298 return PORTAL_UTIL_SELECT;
299 /* it can't be ONE_RETURNING, so give up */
300 return PORTAL_MULTI_QUERY;
305 * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
306 * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
307 * it has a RETURNING list.
312 Node *stmt = (Node *) lfirst(lc);
314 if (IsA(stmt, Query))
316 Query *query = (Query *) stmt;
318 if (query->canSetTag)
321 return PORTAL_MULTI_QUERY; /* no need to look further */
322 if (query->returningList == NIL)
323 return PORTAL_MULTI_QUERY; /* no need to look further */
326 else if (IsA(stmt, PlannedStmt))
328 PlannedStmt *pstmt = (PlannedStmt *) stmt;
330 if (pstmt->canSetTag)
333 return PORTAL_MULTI_QUERY; /* no need to look further */
334 if (pstmt->returningLists == NIL)
335 return PORTAL_MULTI_QUERY; /* no need to look further */
338 /* otherwise, utility command, assumed not canSetTag */
341 return PORTAL_ONE_RETURNING;
343 /* Else, it's the general case... */
344 return PORTAL_MULTI_QUERY;
348 * FetchPortalTargetList
349 * Given a portal that returns tuples, extract the query targetlist.
350 * Returns NIL if the portal doesn't have a determinable targetlist.
352 * Note: do not modify the result.
355 FetchPortalTargetList(Portal portal)
357 /* no point in looking if we determined it doesn't return tuples */
358 if (portal->strategy == PORTAL_MULTI_QUERY)
360 /* get the primary statement and find out what it returns */
361 return FetchStatementTargetList(PortalGetPrimaryStmt(portal));
365 * FetchStatementTargetList
366 * Given a statement that returns tuples, extract the query targetlist.
367 * Returns NIL if the statement doesn't have a determinable targetlist.
369 * This can be applied to a Query, a PlannedStmt, or a utility statement.
370 * That's more general than portals need, but plancache.c uses this too.
372 * Note: do not modify the result.
374 * XXX be careful to keep this in sync with UtilityReturnsTuples.
377 FetchStatementTargetList(Node *stmt)
381 if (IsA(stmt, Query))
383 Query *query = (Query *) stmt;
385 if (query->commandType == CMD_UTILITY &&
386 query->utilityStmt != NULL)
388 /* transfer attention to utility statement */
389 stmt = query->utilityStmt;
393 if (query->commandType == CMD_SELECT &&
394 query->utilityStmt == NULL &&
395 query->intoClause == NULL)
396 return query->targetList;
397 if (query->returningList)
398 return query->returningList;
402 if (IsA(stmt, PlannedStmt))
404 PlannedStmt *pstmt = (PlannedStmt *) stmt;
406 if (pstmt->commandType == CMD_SELECT &&
407 pstmt->utilityStmt == NULL &&
408 pstmt->intoClause == NULL)
409 return pstmt->planTree->targetlist;
410 if (pstmt->returningLists)
411 return (List *) linitial(pstmt->returningLists);
414 if (IsA(stmt, FetchStmt))
416 FetchStmt *fstmt = (FetchStmt *) stmt;
419 Assert(!fstmt->ismove);
420 subportal = GetPortalByName(fstmt->portalname);
421 Assert(PortalIsValid(subportal));
422 return FetchPortalTargetList(subportal);
424 if (IsA(stmt, ExecuteStmt))
426 ExecuteStmt *estmt = (ExecuteStmt *) stmt;
427 PreparedStatement *entry;
429 Assert(!estmt->into);
430 entry = FetchPreparedStatement(estmt->name, true);
431 return FetchPreparedStatementTargetList(entry);
438 * Prepare a portal for execution.
440 * Caller must already have created the portal, done PortalDefineQuery(),
441 * and adjusted portal options if needed. If parameters are needed by
442 * the query, they must be passed in here (caller is responsible for
443 * giving them appropriate lifetime).
445 * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
446 * for the normal behavior of setting a new snapshot. This parameter is
447 * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
448 * to be used for cursors).
450 * On return, portal is ready to accept PortalRun() calls, and the result
451 * tupdesc (if any) is known.
454 PortalStart(Portal portal, ParamListInfo params, Snapshot snapshot)
456 Portal saveActivePortal;
457 ResourceOwner saveResourceOwner;
458 MemoryContext savePortalContext;
459 MemoryContext oldContext;
460 QueryDesc *queryDesc;
463 AssertArg(PortalIsValid(portal));
464 AssertState(portal->status == PORTAL_DEFINED);
467 * Set up global portal context pointers.
469 saveActivePortal = ActivePortal;
470 saveResourceOwner = CurrentResourceOwner;
471 savePortalContext = PortalContext;
474 ActivePortal = portal;
475 CurrentResourceOwner = portal->resowner;
476 PortalContext = PortalGetHeapMemory(portal);
478 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
480 /* Must remember portal param list, if any */
481 portal->portalParams = params;
484 * Determine the portal execution strategy
486 portal->strategy = ChoosePortalStrategy(portal->stmts);
489 * Fire her up according to the strategy
491 switch (portal->strategy)
493 case PORTAL_ONE_SELECT:
495 /* Must set snapshot before starting executor. */
497 PushActiveSnapshot(snapshot);
499 PushActiveSnapshot(GetTransactionSnapshot());
502 * Create QueryDesc in portal's context; for the moment, set
503 * the destination to DestNone.
505 queryDesc = CreateQueryDesc((PlannedStmt *) linitial(portal->stmts),
513 * We do *not* call AfterTriggerBeginQuery() here. We assume
514 * that a SELECT cannot queue any triggers. It would be messy
515 * to support triggers since the execution of the portal may
516 * be interleaved with other queries.
520 * If it's a scrollable cursor, executor needs to support
521 * REWIND and backwards scan.
523 if (portal->cursorOptions & CURSOR_OPT_SCROLL)
524 eflags = EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
526 eflags = 0; /* default run-to-completion flags */
529 * Call ExecutorStart to prepare the plan for execution
531 ExecutorStart(queryDesc, eflags);
534 * This tells PortalCleanup to shut down the executor
536 portal->queryDesc = queryDesc;
539 * Remember tuple descriptor (computed by ExecutorStart)
541 portal->tupDesc = queryDesc->tupDesc;
544 * Reset cursor position data to "start of query"
546 portal->atStart = true;
547 portal->atEnd = false; /* allow fetches */
548 portal->portalPos = 0;
549 portal->posOverflow = false;
554 case PORTAL_ONE_RETURNING:
557 * We don't start the executor until we are told to run the
558 * portal. We do need to set up the result tupdesc.
563 pstmt = (PlannedStmt *) PortalGetPrimaryStmt(portal);
564 Assert(IsA(pstmt, PlannedStmt));
565 Assert(pstmt->returningLists);
567 ExecCleanTypeFromTL((List *) linitial(pstmt->returningLists),
572 * Reset cursor position data to "start of query"
574 portal->atStart = true;
575 portal->atEnd = false; /* allow fetches */
576 portal->portalPos = 0;
577 portal->posOverflow = false;
580 case PORTAL_UTIL_SELECT:
583 * We don't set snapshot here, because PortalRunUtility will
584 * take care of it if needed.
587 Node *ustmt = PortalGetPrimaryStmt(portal);
589 Assert(!IsA(ustmt, PlannedStmt));
590 portal->tupDesc = UtilityTupleDescriptor(ustmt);
594 * Reset cursor position data to "start of query"
596 portal->atStart = true;
597 portal->atEnd = false; /* allow fetches */
598 portal->portalPos = 0;
599 portal->posOverflow = false;
602 case PORTAL_MULTI_QUERY:
603 /* Need do nothing now */
604 portal->tupDesc = NULL;
610 /* Uncaught error while executing portal: mark it dead */
611 portal->status = PORTAL_FAILED;
613 /* Restore global vars and propagate error */
614 ActivePortal = saveActivePortal;
615 CurrentResourceOwner = saveResourceOwner;
616 PortalContext = savePortalContext;
622 MemoryContextSwitchTo(oldContext);
624 ActivePortal = saveActivePortal;
625 CurrentResourceOwner = saveResourceOwner;
626 PortalContext = savePortalContext;
628 portal->status = PORTAL_READY;
632 * PortalSetResultFormat
633 * Select the format codes for a portal's output.
635 * This must be run after PortalStart for a portal that will be read by
636 * a DestRemote or DestRemoteExecute destination. It is not presently needed
637 * for other destination types.
639 * formats[] is the client format request, as per Bind message conventions.
642 PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
647 /* Do nothing if portal won't return tuples */
648 if (portal->tupDesc == NULL)
650 natts = portal->tupDesc->natts;
651 portal->formats = (int16 *)
652 MemoryContextAlloc(PortalGetHeapMemory(portal),
653 natts * sizeof(int16));
656 /* format specified for each column */
657 if (nFormats != natts)
659 (errcode(ERRCODE_PROTOCOL_VIOLATION),
660 errmsg("bind message has %d result formats but query has %d columns",
662 memcpy(portal->formats, formats, natts * sizeof(int16));
664 else if (nFormats > 0)
666 /* single format specified, use for all columns */
667 int16 format1 = formats[0];
669 for (i = 0; i < natts; i++)
670 portal->formats[i] = format1;
674 /* use default format for all columns */
675 for (i = 0; i < natts; i++)
676 portal->formats[i] = 0;
682 * Run a portal's query or queries.
684 * count <= 0 is interpreted as a no-op: the destination gets started up
685 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
686 * interpreted as "all rows". Note that count is ignored in multi-query
687 * situations, where we always run the portal to completion.
689 * isTopLevel: true if query is being executed at backend "top level"
690 * (that is, directly from a client command message)
692 * dest: where to send output of primary (canSetTag) query
694 * altdest: where to send output of non-primary queries
696 * completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
697 * in which to store a command completion status string.
698 * May be NULL if caller doesn't want a status string.
700 * Returns TRUE if the portal's execution is complete, FALSE if it was
701 * suspended due to exhaustion of the count parameter.
704 PortalRun(Portal portal, long count, bool isTopLevel,
705 DestReceiver *dest, DestReceiver *altdest,
709 ResourceOwner saveTopTransactionResourceOwner;
710 MemoryContext saveTopTransactionContext;
711 Portal saveActivePortal;
712 ResourceOwner saveResourceOwner;
713 MemoryContext savePortalContext;
714 MemoryContext saveMemoryContext;
716 AssertArg(PortalIsValid(portal));
718 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
720 /* Initialize completion tag to empty string */
722 completionTag[0] = '\0';
724 if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
726 elog(DEBUG3, "PortalRun");
727 /* PORTAL_MULTI_QUERY logs its own stats per query */
732 * Check for improper portal use, and mark portal active.
734 if (portal->status != PORTAL_READY)
736 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
737 errmsg("portal \"%s\" cannot be run", portal->name)));
738 portal->status = PORTAL_ACTIVE;
741 * Set up global portal context pointers.
743 * We have to play a special game here to support utility commands like
744 * VACUUM and CLUSTER, which internally start and commit transactions.
745 * When we are called to execute such a command, CurrentResourceOwner will
746 * be pointing to the TopTransactionResourceOwner --- which will be
747 * destroyed and replaced in the course of the internal commit and
748 * restart. So we need to be prepared to restore it as pointing to the
749 * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
750 * internally starting whole new transactions is not good.)
751 * CurrentMemoryContext has a similar problem, but the other pointers we
752 * save here will be NULL or pointing to longer-lived objects.
754 saveTopTransactionResourceOwner = TopTransactionResourceOwner;
755 saveTopTransactionContext = TopTransactionContext;
756 saveActivePortal = ActivePortal;
757 saveResourceOwner = CurrentResourceOwner;
758 savePortalContext = PortalContext;
759 saveMemoryContext = CurrentMemoryContext;
762 ActivePortal = portal;
763 CurrentResourceOwner = portal->resowner;
764 PortalContext = PortalGetHeapMemory(portal);
766 MemoryContextSwitchTo(PortalContext);
768 switch (portal->strategy)
770 case PORTAL_ONE_SELECT:
771 (void) PortalRunSelect(portal, true, count, dest);
773 /* we know the query is supposed to set the tag */
774 if (completionTag && portal->commandTag)
775 strcpy(completionTag, portal->commandTag);
777 /* Mark portal not active */
778 portal->status = PORTAL_READY;
781 * Since it's a forward fetch, say DONE iff atEnd is now true.
783 result = portal->atEnd;
786 case PORTAL_ONE_RETURNING:
787 case PORTAL_UTIL_SELECT:
790 * If we have not yet run the command, do so, storing its
791 * results in the portal's tuplestore.
793 if (!portal->holdStore)
794 FillPortalStore(portal, isTopLevel);
797 * Now fetch desired portion of results.
799 (void) PortalRunSelect(portal, true, count, dest);
801 /* we know the query is supposed to set the tag */
802 if (completionTag && portal->commandTag)
803 strcpy(completionTag, portal->commandTag);
805 /* Mark portal not active */
806 portal->status = PORTAL_READY;
809 * Since it's a forward fetch, say DONE iff atEnd is now true.
811 result = portal->atEnd;
814 case PORTAL_MULTI_QUERY:
815 PortalRunMulti(portal, isTopLevel,
816 dest, altdest, completionTag);
818 /* Prevent portal's commands from being re-executed */
819 portal->status = PORTAL_DONE;
821 /* Always complete at end of RunMulti */
826 elog(ERROR, "unrecognized portal strategy: %d",
827 (int) portal->strategy);
828 result = false; /* keep compiler quiet */
834 /* Uncaught error while executing portal: mark it dead */
835 portal->status = PORTAL_FAILED;
837 /* Restore global vars and propagate error */
838 if (saveMemoryContext == saveTopTransactionContext)
839 MemoryContextSwitchTo(TopTransactionContext);
841 MemoryContextSwitchTo(saveMemoryContext);
842 ActivePortal = saveActivePortal;
843 if (saveResourceOwner == saveTopTransactionResourceOwner)
844 CurrentResourceOwner = TopTransactionResourceOwner;
846 CurrentResourceOwner = saveResourceOwner;
847 PortalContext = savePortalContext;
853 if (saveMemoryContext == saveTopTransactionContext)
854 MemoryContextSwitchTo(TopTransactionContext);
856 MemoryContextSwitchTo(saveMemoryContext);
857 ActivePortal = saveActivePortal;
858 if (saveResourceOwner == saveTopTransactionResourceOwner)
859 CurrentResourceOwner = TopTransactionResourceOwner;
861 CurrentResourceOwner = saveResourceOwner;
862 PortalContext = savePortalContext;
864 if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
865 ShowUsage("EXECUTOR STATISTICS");
867 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
874 * Execute a portal's query in PORTAL_ONE_SELECT mode, and also
875 * when fetching from a completed holdStore in PORTAL_ONE_RETURNING
876 * and PORTAL_UTIL_SELECT cases.
878 * This handles simple N-rows-forward-or-backward cases. For more complex
879 * nonsequential access to a portal, see PortalRunFetch.
881 * count <= 0 is interpreted as a no-op: the destination gets started up
882 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
883 * interpreted as "all rows".
885 * Caller must already have validated the Portal and done appropriate
886 * setup (cf. PortalRun).
888 * Returns number of rows processed (suitable for use in result tag)
891 PortalRunSelect(Portal portal,
896 QueryDesc *queryDesc;
897 ScanDirection direction;
901 * NB: queryDesc will be NULL if we are fetching from a held cursor or a
902 * completed utility query; can't use it in that path.
904 queryDesc = PortalGetQueryDesc(portal);
906 /* Caller messed up if we have neither a ready query nor held data. */
907 Assert(queryDesc || portal->holdStore);
910 * Force the queryDesc destination to the right thing. This supports
911 * MOVE, for example, which will pass in dest = DestNone. This is okay to
912 * change as long as we do it on every fetch. (The Executor must not
913 * assume that dest never changes.)
916 queryDesc->dest = dest;
919 * Determine which direction to go in, and check to see if we're already
920 * at the end of the available tuples in that direction. If so, set the
921 * direction to NoMovement to avoid trying to fetch any tuples. (This
922 * check exists because not all plan node types are robust about being
923 * called again if they've already returned NULL once.) Then call the
924 * executor (we must not skip this, because the destination needs to see a
925 * setup and shutdown even if no tuples are available). Finally, update
926 * the portal position state depending on the number of tuples that were
931 if (portal->atEnd || count <= 0)
932 direction = NoMovementScanDirection;
934 direction = ForwardScanDirection;
936 /* In the executor, zero count processes all rows */
937 if (count == FETCH_ALL)
940 if (portal->holdStore)
941 nprocessed = RunFromStore(portal, direction, count, dest);
944 PushActiveSnapshot(queryDesc->snapshot);
945 ExecutorRun(queryDesc, direction, count);
946 nprocessed = queryDesc->estate->es_processed;
950 if (!ScanDirectionIsNoMovement(direction))
955 portal->atStart = false; /* OK to go backward now */
957 (unsigned long) nprocessed < (unsigned long) count)
958 portal->atEnd = true; /* we retrieved 'em all */
959 oldPos = portal->portalPos;
960 portal->portalPos += nprocessed;
961 /* portalPos doesn't advance when we fall off the end */
962 if (portal->portalPos < oldPos)
963 portal->posOverflow = true;
968 if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
970 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
971 errmsg("cursor can only scan forward"),
972 errhint("Declare it with SCROLL option to enable backward scan.")));
974 if (portal->atStart || count <= 0)
975 direction = NoMovementScanDirection;
977 direction = BackwardScanDirection;
979 /* In the executor, zero count processes all rows */
980 if (count == FETCH_ALL)
983 if (portal->holdStore)
984 nprocessed = RunFromStore(portal, direction, count, dest);
987 PushActiveSnapshot(queryDesc->snapshot);
988 ExecutorRun(queryDesc, direction, count);
989 nprocessed = queryDesc->estate->es_processed;
993 if (!ScanDirectionIsNoMovement(direction))
995 if (nprocessed > 0 && portal->atEnd)
997 portal->atEnd = false; /* OK to go forward now */
998 portal->portalPos++; /* adjust for endpoint case */
1001 (unsigned long) nprocessed < (unsigned long) count)
1003 portal->atStart = true; /* we retrieved 'em all */
1004 portal->portalPos = 0;
1005 portal->posOverflow = false;
1011 oldPos = portal->portalPos;
1012 portal->portalPos -= nprocessed;
1013 if (portal->portalPos > oldPos ||
1014 portal->portalPos <= 0)
1015 portal->posOverflow = true;
1025 * Run the query and load result tuples into the portal's tuple store.
1027 * This is used for PORTAL_ONE_RETURNING and PORTAL_UTIL_SELECT cases only.
1030 FillPortalStore(Portal portal, bool isTopLevel)
1032 DestReceiver *treceiver;
1033 char completionTag[COMPLETION_TAG_BUFSIZE];
1035 PortalCreateHoldStore(portal);
1036 treceiver = CreateDestReceiver(DestTuplestore);
1037 SetTuplestoreDestReceiverParams(treceiver,
1039 portal->holdContext,
1042 completionTag[0] = '\0';
1044 switch (portal->strategy)
1046 case PORTAL_ONE_RETURNING:
1049 * Run the portal to completion just as for the default
1050 * MULTI_QUERY case, but send the primary query's output to the
1051 * tuplestore. Auxiliary query outputs are discarded.
1053 PortalRunMulti(portal, isTopLevel,
1054 treceiver, None_Receiver, completionTag);
1057 case PORTAL_UTIL_SELECT:
1058 PortalRunUtility(portal, (Node *) linitial(portal->stmts),
1059 isTopLevel, treceiver, completionTag);
1063 elog(ERROR, "unsupported portal strategy: %d",
1064 (int) portal->strategy);
1068 /* Override default completion tag with actual command result */
1069 if (completionTag[0] != '\0')
1070 portal->commandTag = pstrdup(completionTag);
1072 (*treceiver->rDestroy) (treceiver);
1077 * Fetch tuples from the portal's tuple store.
1079 * Calling conventions are similar to ExecutorRun, except that we
1080 * do not depend on having a queryDesc or estate. Therefore we return the
1081 * number of tuples processed as the result, not in estate->es_processed.
1083 * One difference from ExecutorRun is that the destination receiver functions
1084 * are run in the caller's memory context (since we have no estate). Watch
1085 * out for memory leaks.
1088 RunFromStore(Portal portal, ScanDirection direction, long count,
1091 long current_tuple_count = 0;
1092 TupleTableSlot *slot;
1094 slot = MakeSingleTupleTableSlot(portal->tupDesc);
1096 (*dest->rStartup) (dest, CMD_SELECT, portal->tupDesc);
1098 if (ScanDirectionIsNoMovement(direction))
1100 /* do nothing except start/stop the destination */
1104 bool forward = ScanDirectionIsForward(direction);
1108 MemoryContext oldcontext;
1111 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1113 ok = tuplestore_gettupleslot(portal->holdStore, forward, slot);
1115 MemoryContextSwitchTo(oldcontext);
1120 (*dest->receiveSlot) (slot, dest);
1122 ExecClearTuple(slot);
1125 * check our tuple count.. if we've processed the proper number
1126 * then quit, else loop again and process more tuples. Zero count
1129 current_tuple_count++;
1130 if (count && count == current_tuple_count)
1135 (*dest->rShutdown) (dest);
1137 ExecDropSingleTupleTableSlot(slot);
1139 return (uint32) current_tuple_count;
1144 * Execute a utility statement inside a portal.
1147 PortalRunUtility(Portal portal, Node *utilityStmt, bool isTopLevel,
1148 DestReceiver *dest, char *completionTag)
1150 bool active_snapshot_set;
1152 elog(DEBUG3, "ProcessUtility");
1155 * Set snapshot if utility stmt needs one. Most reliable way to do this
1156 * seems to be to enumerate those that do not need one; this is a short
1157 * list. Transaction control, LOCK, and SET must *not* set a snapshot
1158 * since they need to be executable at the start of a serializable
1159 * transaction without freezing a snapshot. By extension we allow SHOW
1160 * not to set a snapshot. The other stmts listed are just efficiency
1161 * hacks. Beware of listing anything that can modify the database --- if,
1162 * say, it has to update an index with expressions that invoke
1163 * user-defined functions, then it had better have a snapshot.
1165 if (!(IsA(utilityStmt, TransactionStmt) ||
1166 IsA(utilityStmt, LockStmt) ||
1167 IsA(utilityStmt, VariableSetStmt) ||
1168 IsA(utilityStmt, VariableShowStmt) ||
1169 IsA(utilityStmt, ConstraintsSetStmt) ||
1170 /* efficiency hacks from here down */
1171 IsA(utilityStmt, FetchStmt) ||
1172 IsA(utilityStmt, ListenStmt) ||
1173 IsA(utilityStmt, NotifyStmt) ||
1174 IsA(utilityStmt, UnlistenStmt) ||
1175 IsA(utilityStmt, CheckPointStmt)))
1177 PushActiveSnapshot(GetTransactionSnapshot());
1178 active_snapshot_set = true;
1181 active_snapshot_set = false;
1183 ProcessUtility(utilityStmt,
1185 portal->portalParams,
1190 /* Some utility statements may change context on us */
1191 MemoryContextSwitchTo(PortalGetHeapMemory(portal));
1194 * Some utility commands may pop the ActiveSnapshot stack from under us,
1195 * so we only pop the stack if we actually see a snapshot set. Note that
1196 * the set of utility commands that do this must be the same set
1197 * disallowed to run inside a transaction; otherwise, we could be popping
1198 * a snapshot that belongs to some other operation.
1200 if (active_snapshot_set && ActiveSnapshotSet())
1201 PopActiveSnapshot();
1206 * Execute a portal's queries in the general case (multi queries
1207 * or non-SELECT-like queries)
1210 PortalRunMulti(Portal portal, bool isTopLevel,
1211 DestReceiver *dest, DestReceiver *altdest,
1212 char *completionTag)
1214 ListCell *stmtlist_item;
1217 * If the destination is DestRemoteExecute, change to DestNone. The
1218 * reason is that the client won't be expecting any tuples, and indeed has
1219 * no way to know what they are, since there is no provision for Describe
1220 * to send a RowDescription message when this portal execution strategy is
1221 * in effect. This presently will only affect SELECT commands added to
1222 * non-SELECT queries by rewrite rules: such commands will be executed,
1223 * but the results will be discarded unless you use "simple Query"
1226 if (dest->mydest == DestRemoteExecute)
1227 dest = None_Receiver;
1228 if (altdest->mydest == DestRemoteExecute)
1229 altdest = None_Receiver;
1232 * Loop to handle the individual queries generated from a single parsetree
1233 * by analysis and rewrite.
1235 foreach(stmtlist_item, portal->stmts)
1237 Node *stmt = (Node *) lfirst(stmtlist_item);
1240 * If we got a cancel signal in prior command, quit
1242 CHECK_FOR_INTERRUPTS();
1244 if (IsA(stmt, PlannedStmt) &&
1245 ((PlannedStmt *) stmt)->utilityStmt == NULL)
1248 * process a plannable query.
1250 PlannedStmt *pstmt = (PlannedStmt *) stmt;
1252 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1254 if (log_executor_stats)
1257 if (pstmt->canSetTag)
1259 /* statement can set tag string */
1261 portal->portalParams,
1262 dest, completionTag);
1266 /* stmt added by rewrite cannot set tag */
1268 portal->portalParams,
1272 if (log_executor_stats)
1273 ShowUsage("EXECUTOR STATISTICS");
1275 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
1280 * process utility functions (create, destroy, etc..)
1282 * These are assumed canSetTag if they're the only stmt in the
1285 if (list_length(portal->stmts) == 1)
1286 PortalRunUtility(portal, stmt, isTopLevel, dest, completionTag);
1288 PortalRunUtility(portal, stmt, isTopLevel, altdest, NULL);
1292 * Increment command counter between queries, but not after the last
1295 if (lnext(stmtlist_item) != NULL)
1296 CommandCounterIncrement();
1299 * Clear subsidiary contexts to recover temporary memory.
1301 Assert(PortalGetHeapMemory(portal) == CurrentMemoryContext);
1303 MemoryContextDeleteChildren(PortalGetHeapMemory(portal));
1307 * If a command completion tag was supplied, use it. Otherwise use the
1308 * portal's commandTag as the default completion tag.
1310 * Exception: clients will expect INSERT/UPDATE/DELETE tags to have
1311 * counts, so fake something up if necessary. (This could happen if the
1312 * original query was replaced by a DO INSTEAD rule.)
1314 if (completionTag && completionTag[0] == '\0')
1316 if (portal->commandTag)
1317 strcpy(completionTag, portal->commandTag);
1318 if (strcmp(completionTag, "INSERT") == 0)
1319 strcpy(completionTag, "INSERT 0 0");
1320 else if (strcmp(completionTag, "UPDATE") == 0)
1321 strcpy(completionTag, "UPDATE 0");
1322 else if (strcmp(completionTag, "DELETE") == 0)
1323 strcpy(completionTag, "DELETE 0");
1329 * Variant form of PortalRun that supports SQL FETCH directions.
1331 * Note: we presently assume that no callers of this want isTopLevel = true.
1333 * Returns number of rows processed (suitable for use in result tag)
1336 PortalRunFetch(Portal portal,
1337 FetchDirection fdirection,
1342 Portal saveActivePortal;
1343 ResourceOwner saveResourceOwner;
1344 MemoryContext savePortalContext;
1345 MemoryContext oldContext;
1347 AssertArg(PortalIsValid(portal));
1350 * Check for improper portal use, and mark portal active.
1352 if (portal->status != PORTAL_READY)
1354 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1355 errmsg("portal \"%s\" cannot be run", portal->name)));
1356 portal->status = PORTAL_ACTIVE;
1359 * Set up global portal context pointers.
1361 saveActivePortal = ActivePortal;
1362 saveResourceOwner = CurrentResourceOwner;
1363 savePortalContext = PortalContext;
1366 ActivePortal = portal;
1367 CurrentResourceOwner = portal->resowner;
1368 PortalContext = PortalGetHeapMemory(portal);
1370 oldContext = MemoryContextSwitchTo(PortalContext);
1372 switch (portal->strategy)
1374 case PORTAL_ONE_SELECT:
1375 result = DoPortalRunFetch(portal, fdirection, count, dest);
1378 case PORTAL_ONE_RETURNING:
1379 case PORTAL_UTIL_SELECT:
1382 * If we have not yet run the command, do so, storing its
1383 * results in the portal's tuplestore.
1385 if (!portal->holdStore)
1386 FillPortalStore(portal, false /* isTopLevel */ );
1389 * Now fetch desired portion of results.
1391 result = DoPortalRunFetch(portal, fdirection, count, dest);
1395 elog(ERROR, "unsupported portal strategy");
1396 result = 0; /* keep compiler quiet */
1402 /* Uncaught error while executing portal: mark it dead */
1403 portal->status = PORTAL_FAILED;
1405 /* Restore global vars and propagate error */
1406 ActivePortal = saveActivePortal;
1407 CurrentResourceOwner = saveResourceOwner;
1408 PortalContext = savePortalContext;
1414 MemoryContextSwitchTo(oldContext);
1416 /* Mark portal not active */
1417 portal->status = PORTAL_READY;
1419 ActivePortal = saveActivePortal;
1420 CurrentResourceOwner = saveResourceOwner;
1421 PortalContext = savePortalContext;
1428 * Guts of PortalRunFetch --- the portal context is already set up
1430 * Returns number of rows processed (suitable for use in result tag)
1433 DoPortalRunFetch(Portal portal,
1434 FetchDirection fdirection,
1440 Assert(portal->strategy == PORTAL_ONE_SELECT ||
1441 portal->strategy == PORTAL_ONE_RETURNING ||
1442 portal->strategy == PORTAL_UTIL_SELECT);
1449 fdirection = FETCH_BACKWARD;
1452 /* fall out of switch to share code with FETCH_BACKWARD */
1454 case FETCH_BACKWARD:
1457 fdirection = FETCH_FORWARD;
1460 /* fall out of switch to share code with FETCH_FORWARD */
1462 case FETCH_ABSOLUTE:
1466 * Definition: Rewind to start, advance count-1 rows, return
1467 * next row (if any). In practice, if the goal is less than
1468 * halfway back to the start, it's better to scan from where
1469 * we are. In any case, we arrange to fetch the target row
1472 if (portal->posOverflow || portal->portalPos == LONG_MAX ||
1473 count - 1 <= portal->portalPos / 2)
1475 DoPortalRewind(portal);
1477 PortalRunSelect(portal, true, count - 1,
1482 long pos = portal->portalPos;
1485 pos++; /* need one extra fetch if off end */
1487 PortalRunSelect(portal, false, pos - count + 1,
1489 else if (count > pos + 1)
1490 PortalRunSelect(portal, true, count - pos - 1,
1493 return PortalRunSelect(portal, true, 1L, dest);
1498 * Definition: Advance to end, back up abs(count)-1 rows,
1499 * return prior row (if any). We could optimize this if we
1500 * knew in advance where the end was, but typically we won't.
1501 * (Is it worth considering case where count > half of size of
1502 * query? We could rewind once we know the size ...)
1504 PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
1506 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1507 return PortalRunSelect(portal, false, 1L, dest);
1512 /* Rewind to start, return zero rows */
1513 DoPortalRewind(portal);
1514 return PortalRunSelect(portal, true, 0L, dest);
1517 case FETCH_RELATIVE:
1521 * Definition: advance count-1 rows, return next row (if any).
1524 PortalRunSelect(portal, true, count - 1, None_Receiver);
1525 return PortalRunSelect(portal, true, 1L, dest);
1530 * Definition: back up abs(count)-1 rows, return prior row (if
1534 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1535 return PortalRunSelect(portal, false, 1L, dest);
1540 /* Same as FETCH FORWARD 0, so fall out of switch */
1541 fdirection = FETCH_FORWARD;
1545 elog(ERROR, "bogus direction");
1550 * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
1553 forward = (fdirection == FETCH_FORWARD);
1556 * Zero count means to re-fetch the current row, if any (per SQL92)
1562 /* Are we sitting on a row? */
1563 on_row = (!portal->atStart && !portal->atEnd);
1565 if (dest->mydest == DestNone)
1567 /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
1568 return on_row ? 1L : 0L;
1573 * If we are sitting on a row, back up one so we can re-fetch it.
1574 * If we are not sitting on a row, we still have to start up and
1575 * shut down the executor so that the destination is initialized
1576 * and shut down correctly; so keep going. To PortalRunSelect,
1577 * count == 0 means we will retrieve no row.
1581 PortalRunSelect(portal, false, 1L, None_Receiver);
1582 /* Set up to fetch one row forward */
1590 * Optimize MOVE BACKWARD ALL into a Rewind.
1592 if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
1594 long result = portal->portalPos;
1596 if (result > 0 && !portal->atEnd)
1598 DoPortalRewind(portal);
1599 /* result is bogus if pos had overflowed, but it's best we can do */
1603 return PortalRunSelect(portal, forward, count, dest);
1607 * DoPortalRewind - rewind a Portal to starting point
1610 DoPortalRewind(Portal portal)
1612 if (portal->holdStore)
1614 MemoryContext oldcontext;
1616 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1617 tuplestore_rescan(portal->holdStore);
1618 MemoryContextSwitchTo(oldcontext);
1620 if (PortalGetQueryDesc(portal))
1621 ExecutorRewind(PortalGetQueryDesc(portal));
1623 portal->atStart = true;
1624 portal->atEnd = false;
1625 portal->portalPos = 0;
1626 portal->posOverflow = false;