1 /*-------------------------------------------------------------------------
4 * POSTGRES process query command code
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/tcop/pquery.c
13 *-------------------------------------------------------------------------
20 #include "access/xact.h"
21 #include "commands/prepare.h"
22 #include "executor/tstoreReceiver.h"
23 #include "miscadmin.h"
25 #include "tcop/pquery.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,
39 const char *sourceText,
41 QueryEnvironment *queryEnv,
44 static void FillPortalStore(Portal portal, bool isTopLevel);
45 static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
47 static uint64 PortalRunSelect(Portal portal, bool forward, long count,
49 static void PortalRunUtility(Portal portal, PlannedStmt *pstmt,
50 bool isTopLevel, bool setHoldSnapshot,
51 DestReceiver *dest, char *completionTag);
52 static void PortalRunMulti(Portal portal,
53 bool isTopLevel, bool setHoldSnapshot,
54 DestReceiver *dest, DestReceiver *altdest,
56 static uint64 DoPortalRunFetch(Portal portal,
57 FetchDirection fdirection,
60 static void DoPortalRewind(Portal portal);
67 CreateQueryDesc(PlannedStmt *plannedstmt,
68 const char *sourceText,
70 Snapshot crosscheck_snapshot,
73 QueryEnvironment *queryEnv,
74 int instrument_options)
76 QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
78 qd->operation = plannedstmt->commandType; /* operation */
79 qd->plannedstmt = plannedstmt; /* plan */
80 qd->sourceText = sourceText; /* query text */
81 qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
82 /* RI check snapshot */
83 qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
84 qd->dest = dest; /* output dest */
85 qd->params = params; /* parameter values passed into query */
86 qd->queryEnv = queryEnv;
87 qd->instrument_options = instrument_options; /* instrumentation wanted? */
89 /* null these fields until set by ExecutorStart */
95 /* not yet executed */
96 qd->already_executed = false;
105 FreeQueryDesc(QueryDesc *qdesc)
107 /* Can't be a live query */
108 Assert(qdesc->estate == NULL);
110 /* forget our snapshots */
111 UnregisterSnapshot(qdesc->snapshot);
112 UnregisterSnapshot(qdesc->crosscheck_snapshot);
114 /* Only the QueryDesc itself need be freed */
121 * Execute a single plannable query within a PORTAL_MULTI_QUERY,
122 * PORTAL_ONE_RETURNING, or PORTAL_ONE_MOD_WITH portal
124 * plan: the plan tree for the query
125 * sourceText: the source text of the query
126 * params: any parameters needed
127 * dest: where to send results
128 * completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
129 * in which to store a command completion status string.
131 * completionTag may be NULL if caller doesn't want a status string.
133 * Must be called in a memory context that will be reset or deleted on
134 * error; otherwise the executor's memory usage will be leaked.
137 ProcessQuery(PlannedStmt *plan,
138 const char *sourceText,
139 ParamListInfo params,
140 QueryEnvironment *queryEnv,
144 QueryDesc *queryDesc;
147 * Create the QueryDesc object
149 queryDesc = CreateQueryDesc(plan, sourceText,
150 GetActiveSnapshot(), InvalidSnapshot,
151 dest, params, queryEnv, 0);
154 * Call ExecutorStart to prepare the plan for execution
156 ExecutorStart(queryDesc, 0);
159 * Run the plan to completion.
161 ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
164 * Build command completion status string, if caller wants one.
170 switch (queryDesc->operation)
173 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
174 "SELECT " UINT64_FORMAT,
175 queryDesc->estate->es_processed);
178 /* lastoid doesn't exist anymore */
179 lastOid = InvalidOid;
180 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
181 "INSERT %u " UINT64_FORMAT,
182 lastOid, queryDesc->estate->es_processed);
185 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
186 "UPDATE " UINT64_FORMAT,
187 queryDesc->estate->es_processed);
190 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
191 "DELETE " UINT64_FORMAT,
192 queryDesc->estate->es_processed);
195 strcpy(completionTag, "???");
201 * Now, we close down all the scans and free allocated resources.
203 ExecutorFinish(queryDesc);
204 ExecutorEnd(queryDesc);
206 FreeQueryDesc(queryDesc);
210 * ChoosePortalStrategy
211 * Select portal execution strategy given the intended statement list.
213 * The list elements can be Querys or PlannedStmts.
214 * That's more general than portals need, but plancache.c uses this too.
216 * See the comments in portal.h.
219 ChoosePortalStrategy(List *stmts)
225 * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
226 * single-statement case, since there are no rewrite rules that can add
227 * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
228 * likewise allows only one top-level statement.
230 if (list_length(stmts) == 1)
232 Node *stmt = (Node *) linitial(stmts);
234 if (IsA(stmt, Query))
236 Query *query = (Query *) stmt;
238 if (query->canSetTag)
240 if (query->commandType == CMD_SELECT)
242 if (query->hasModifyingCTE)
243 return PORTAL_ONE_MOD_WITH;
245 return PORTAL_ONE_SELECT;
247 if (query->commandType == CMD_UTILITY)
249 if (UtilityReturnsTuples(query->utilityStmt))
250 return PORTAL_UTIL_SELECT;
251 /* it can't be ONE_RETURNING, so give up */
252 return PORTAL_MULTI_QUERY;
256 else if (IsA(stmt, PlannedStmt))
258 PlannedStmt *pstmt = (PlannedStmt *) stmt;
260 if (pstmt->canSetTag)
262 if (pstmt->commandType == CMD_SELECT)
264 if (pstmt->hasModifyingCTE)
265 return PORTAL_ONE_MOD_WITH;
267 return PORTAL_ONE_SELECT;
269 if (pstmt->commandType == CMD_UTILITY)
271 if (UtilityReturnsTuples(pstmt->utilityStmt))
272 return PORTAL_UTIL_SELECT;
273 /* it can't be ONE_RETURNING, so give up */
274 return PORTAL_MULTI_QUERY;
279 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
283 * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
284 * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
285 * it has a RETURNING list.
290 Node *stmt = (Node *) lfirst(lc);
292 if (IsA(stmt, Query))
294 Query *query = (Query *) stmt;
296 if (query->canSetTag)
299 return PORTAL_MULTI_QUERY; /* no need to look further */
300 if (query->commandType == CMD_UTILITY ||
301 query->returningList == NIL)
302 return PORTAL_MULTI_QUERY; /* no need to look further */
305 else if (IsA(stmt, PlannedStmt))
307 PlannedStmt *pstmt = (PlannedStmt *) stmt;
309 if (pstmt->canSetTag)
312 return PORTAL_MULTI_QUERY; /* no need to look further */
313 if (pstmt->commandType == CMD_UTILITY ||
314 !pstmt->hasReturning)
315 return PORTAL_MULTI_QUERY; /* no need to look further */
319 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
322 return PORTAL_ONE_RETURNING;
324 /* Else, it's the general case... */
325 return PORTAL_MULTI_QUERY;
329 * FetchPortalTargetList
330 * Given a portal that returns tuples, extract the query targetlist.
331 * Returns NIL if the portal doesn't have a determinable targetlist.
333 * Note: do not modify the result.
336 FetchPortalTargetList(Portal portal)
338 /* no point in looking if we determined it doesn't return tuples */
339 if (portal->strategy == PORTAL_MULTI_QUERY)
341 /* get the primary statement and find out what it returns */
342 return FetchStatementTargetList((Node *) PortalGetPrimaryStmt(portal));
346 * FetchStatementTargetList
347 * Given a statement that returns tuples, extract the query targetlist.
348 * Returns NIL if the statement doesn't have a determinable targetlist.
350 * This can be applied to a Query or a PlannedStmt.
351 * That's more general than portals need, but plancache.c uses this too.
353 * Note: do not modify the result.
355 * XXX be careful to keep this in sync with UtilityReturnsTuples.
358 FetchStatementTargetList(Node *stmt)
362 if (IsA(stmt, Query))
364 Query *query = (Query *) stmt;
366 if (query->commandType == CMD_UTILITY)
368 /* transfer attention to utility statement */
369 stmt = query->utilityStmt;
373 if (query->commandType == CMD_SELECT)
374 return query->targetList;
375 if (query->returningList)
376 return query->returningList;
380 if (IsA(stmt, PlannedStmt))
382 PlannedStmt *pstmt = (PlannedStmt *) stmt;
384 if (pstmt->commandType == CMD_UTILITY)
386 /* transfer attention to utility statement */
387 stmt = pstmt->utilityStmt;
391 if (pstmt->commandType == CMD_SELECT)
392 return pstmt->planTree->targetlist;
393 if (pstmt->hasReturning)
394 return pstmt->planTree->targetlist;
398 if (IsA(stmt, FetchStmt))
400 FetchStmt *fstmt = (FetchStmt *) stmt;
403 Assert(!fstmt->ismove);
404 subportal = GetPortalByName(fstmt->portalname);
405 Assert(PortalIsValid(subportal));
406 return FetchPortalTargetList(subportal);
408 if (IsA(stmt, ExecuteStmt))
410 ExecuteStmt *estmt = (ExecuteStmt *) stmt;
411 PreparedStatement *entry;
413 entry = FetchPreparedStatement(estmt->name, true);
414 return FetchPreparedStatementTargetList(entry);
421 * Prepare a portal for execution.
423 * Caller must already have created the portal, done PortalDefineQuery(),
424 * and adjusted portal options if needed.
426 * If parameters are needed by the query, they must be passed in "params"
427 * (caller is responsible for giving them appropriate lifetime).
429 * The caller can also provide an initial set of "eflags" to be passed to
430 * ExecutorStart (but note these can be modified internally, and they are
431 * currently only honored for PORTAL_ONE_SELECT portals). Most callers
432 * should simply pass zero.
434 * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
435 * for the normal behavior of setting a new snapshot. This parameter is
436 * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
437 * to be used for cursors).
439 * On return, portal is ready to accept PortalRun() calls, and the result
440 * tupdesc (if any) is known.
443 PortalStart(Portal portal, ParamListInfo params,
444 int eflags, Snapshot snapshot)
446 Portal saveActivePortal;
447 ResourceOwner saveResourceOwner;
448 MemoryContext savePortalContext;
449 MemoryContext oldContext;
450 QueryDesc *queryDesc;
453 AssertArg(PortalIsValid(portal));
454 AssertState(portal->status == PORTAL_DEFINED);
457 * Set up global portal context pointers.
459 saveActivePortal = ActivePortal;
460 saveResourceOwner = CurrentResourceOwner;
461 savePortalContext = PortalContext;
464 ActivePortal = portal;
465 if (portal->resowner)
466 CurrentResourceOwner = portal->resowner;
467 PortalContext = portal->portalContext;
469 oldContext = MemoryContextSwitchTo(PortalContext);
471 /* Must remember portal param list, if any */
472 portal->portalParams = params;
475 * Determine the portal execution strategy
477 portal->strategy = ChoosePortalStrategy(portal->stmts);
480 * Fire her up according to the strategy
482 switch (portal->strategy)
484 case PORTAL_ONE_SELECT:
486 /* Must set snapshot before starting executor. */
488 PushActiveSnapshot(snapshot);
490 PushActiveSnapshot(GetTransactionSnapshot());
493 * Create QueryDesc in portal's context; for the moment, set
494 * the destination to DestNone.
496 queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
506 * If it's a scrollable cursor, executor needs to support
507 * REWIND and backwards scan, as well as whatever the caller
508 * might've asked for.
510 if (portal->cursorOptions & CURSOR_OPT_SCROLL)
511 myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
516 * Call ExecutorStart to prepare the plan for execution
518 ExecutorStart(queryDesc, myeflags);
521 * This tells PortalCleanup to shut down the executor
523 portal->queryDesc = queryDesc;
526 * Remember tuple descriptor (computed by ExecutorStart)
528 portal->tupDesc = queryDesc->tupDesc;
531 * Reset cursor position data to "start of query"
533 portal->atStart = true;
534 portal->atEnd = false; /* allow fetches */
535 portal->portalPos = 0;
540 case PORTAL_ONE_RETURNING:
541 case PORTAL_ONE_MOD_WITH:
544 * We don't start the executor until we are told to run the
545 * portal. We do need to set up the result tupdesc.
550 pstmt = PortalGetPrimaryStmt(portal);
552 ExecCleanTypeFromTL(pstmt->planTree->targetlist);
556 * Reset cursor position data to "start of query"
558 portal->atStart = true;
559 portal->atEnd = false; /* allow fetches */
560 portal->portalPos = 0;
563 case PORTAL_UTIL_SELECT:
566 * We don't set snapshot here, because PortalRunUtility will
567 * take care of it if needed.
570 PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
572 Assert(pstmt->commandType == CMD_UTILITY);
573 portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
577 * Reset cursor position data to "start of query"
579 portal->atStart = true;
580 portal->atEnd = false; /* allow fetches */
581 portal->portalPos = 0;
584 case PORTAL_MULTI_QUERY:
585 /* Need do nothing now */
586 portal->tupDesc = NULL;
592 /* Uncaught error while executing portal: mark it dead */
593 MarkPortalFailed(portal);
595 /* Restore global vars and propagate error */
596 ActivePortal = saveActivePortal;
597 CurrentResourceOwner = saveResourceOwner;
598 PortalContext = savePortalContext;
604 MemoryContextSwitchTo(oldContext);
606 ActivePortal = saveActivePortal;
607 CurrentResourceOwner = saveResourceOwner;
608 PortalContext = savePortalContext;
610 portal->status = PORTAL_READY;
614 * PortalSetResultFormat
615 * Select the format codes for a portal's output.
617 * This must be run after PortalStart for a portal that will be read by
618 * a DestRemote or DestRemoteExecute destination. It is not presently needed
619 * for other destination types.
621 * formats[] is the client format request, as per Bind message conventions.
624 PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
629 /* Do nothing if portal won't return tuples */
630 if (portal->tupDesc == NULL)
632 natts = portal->tupDesc->natts;
633 portal->formats = (int16 *)
634 MemoryContextAlloc(portal->portalContext,
635 natts * sizeof(int16));
638 /* format specified for each column */
639 if (nFormats != natts)
641 (errcode(ERRCODE_PROTOCOL_VIOLATION),
642 errmsg("bind message has %d result formats but query has %d columns",
644 memcpy(portal->formats, formats, natts * sizeof(int16));
646 else if (nFormats > 0)
648 /* single format specified, use for all columns */
649 int16 format1 = formats[0];
651 for (i = 0; i < natts; i++)
652 portal->formats[i] = format1;
656 /* use default format for all columns */
657 for (i = 0; i < natts; i++)
658 portal->formats[i] = 0;
664 * Run a portal's query or queries.
666 * count <= 0 is interpreted as a no-op: the destination gets started up
667 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
668 * interpreted as "all rows". Note that count is ignored in multi-query
669 * situations, where we always run the portal to completion.
671 * isTopLevel: true if query is being executed at backend "top level"
672 * (that is, directly from a client command message)
674 * dest: where to send output of primary (canSetTag) query
676 * altdest: where to send output of non-primary queries
678 * completionTag: points to a buffer of size COMPLETION_TAG_BUFSIZE
679 * in which to store a command completion status string.
680 * May be NULL if caller doesn't want a status string.
682 * Returns true if the portal's execution is complete, false if it was
683 * suspended due to exhaustion of the count parameter.
686 PortalRun(Portal portal, long count, bool isTopLevel, bool run_once,
687 DestReceiver *dest, DestReceiver *altdest,
692 ResourceOwner saveTopTransactionResourceOwner;
693 MemoryContext saveTopTransactionContext;
694 Portal saveActivePortal;
695 ResourceOwner saveResourceOwner;
696 MemoryContext savePortalContext;
697 MemoryContext saveMemoryContext;
699 AssertArg(PortalIsValid(portal));
701 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
703 /* Initialize completion tag to empty string */
705 completionTag[0] = '\0';
707 if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
709 elog(DEBUG3, "PortalRun");
710 /* PORTAL_MULTI_QUERY logs its own stats per query */
715 * Check for improper portal use, and mark portal active.
717 MarkPortalActive(portal);
719 /* Set run_once flag. Shouldn't be clear if previously set. */
720 Assert(!portal->run_once || run_once);
721 portal->run_once = run_once;
724 * Set up global portal context pointers.
726 * We have to play a special game here to support utility commands like
727 * VACUUM and CLUSTER, which internally start and commit transactions.
728 * When we are called to execute such a command, CurrentResourceOwner will
729 * be pointing to the TopTransactionResourceOwner --- which will be
730 * destroyed and replaced in the course of the internal commit and
731 * restart. So we need to be prepared to restore it as pointing to the
732 * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
733 * internally starting whole new transactions is not good.)
734 * CurrentMemoryContext has a similar problem, but the other pointers we
735 * save here will be NULL or pointing to longer-lived objects.
737 saveTopTransactionResourceOwner = TopTransactionResourceOwner;
738 saveTopTransactionContext = TopTransactionContext;
739 saveActivePortal = ActivePortal;
740 saveResourceOwner = CurrentResourceOwner;
741 savePortalContext = PortalContext;
742 saveMemoryContext = CurrentMemoryContext;
745 ActivePortal = portal;
746 if (portal->resowner)
747 CurrentResourceOwner = portal->resowner;
748 PortalContext = portal->portalContext;
750 MemoryContextSwitchTo(PortalContext);
752 switch (portal->strategy)
754 case PORTAL_ONE_SELECT:
755 case PORTAL_ONE_RETURNING:
756 case PORTAL_ONE_MOD_WITH:
757 case PORTAL_UTIL_SELECT:
760 * If we have not yet run the command, do so, storing its
761 * results in the portal's tuplestore. But we don't do that
762 * for the PORTAL_ONE_SELECT case.
764 if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
765 FillPortalStore(portal, isTopLevel);
768 * Now fetch desired portion of results.
770 nprocessed = PortalRunSelect(portal, true, count, dest);
773 * If the portal result contains a command tag and the caller
774 * gave us a pointer to store it, copy it. Patch the "SELECT"
775 * tag to also provide the rowcount.
777 if (completionTag && portal->commandTag)
779 if (strcmp(portal->commandTag, "SELECT") == 0)
780 snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
781 "SELECT " UINT64_FORMAT, nprocessed);
783 strcpy(completionTag, portal->commandTag);
786 /* Mark portal not active */
787 portal->status = PORTAL_READY;
790 * Since it's a forward fetch, say DONE iff atEnd is now true.
792 result = portal->atEnd;
795 case PORTAL_MULTI_QUERY:
796 PortalRunMulti(portal, isTopLevel, false,
797 dest, altdest, completionTag);
799 /* Prevent portal's commands from being re-executed */
800 MarkPortalDone(portal);
802 /* Always complete at end of RunMulti */
807 elog(ERROR, "unrecognized portal strategy: %d",
808 (int) portal->strategy);
809 result = false; /* keep compiler quiet */
815 /* Uncaught error while executing portal: mark it dead */
816 MarkPortalFailed(portal);
818 /* Restore global vars and propagate error */
819 if (saveMemoryContext == saveTopTransactionContext)
820 MemoryContextSwitchTo(TopTransactionContext);
822 MemoryContextSwitchTo(saveMemoryContext);
823 ActivePortal = saveActivePortal;
824 if (saveResourceOwner == saveTopTransactionResourceOwner)
825 CurrentResourceOwner = TopTransactionResourceOwner;
827 CurrentResourceOwner = saveResourceOwner;
828 PortalContext = savePortalContext;
834 if (saveMemoryContext == saveTopTransactionContext)
835 MemoryContextSwitchTo(TopTransactionContext);
837 MemoryContextSwitchTo(saveMemoryContext);
838 ActivePortal = saveActivePortal;
839 if (saveResourceOwner == saveTopTransactionResourceOwner)
840 CurrentResourceOwner = TopTransactionResourceOwner;
842 CurrentResourceOwner = saveResourceOwner;
843 PortalContext = savePortalContext;
845 if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
846 ShowUsage("EXECUTOR STATISTICS");
848 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
855 * Execute a portal's query in PORTAL_ONE_SELECT mode, and also
856 * when fetching from a completed holdStore in PORTAL_ONE_RETURNING,
857 * PORTAL_ONE_MOD_WITH, and PORTAL_UTIL_SELECT cases.
859 * This handles simple N-rows-forward-or-backward cases. For more complex
860 * nonsequential access to a portal, see PortalRunFetch.
862 * count <= 0 is interpreted as a no-op: the destination gets started up
863 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
864 * interpreted as "all rows". (cf FetchStmt.howMany)
866 * Caller must already have validated the Portal and done appropriate
867 * setup (cf. PortalRun).
869 * Returns number of rows processed (suitable for use in result tag)
872 PortalRunSelect(Portal portal,
877 QueryDesc *queryDesc;
878 ScanDirection direction;
882 * NB: queryDesc will be NULL if we are fetching from a held cursor or a
883 * completed utility query; can't use it in that path.
885 queryDesc = portal->queryDesc;
887 /* Caller messed up if we have neither a ready query nor held data. */
888 Assert(queryDesc || portal->holdStore);
891 * Force the queryDesc destination to the right thing. This supports
892 * MOVE, for example, which will pass in dest = DestNone. This is okay to
893 * change as long as we do it on every fetch. (The Executor must not
894 * assume that dest never changes.)
897 queryDesc->dest = dest;
900 * Determine which direction to go in, and check to see if we're already
901 * at the end of the available tuples in that direction. If so, set the
902 * direction to NoMovement to avoid trying to fetch any tuples. (This
903 * check exists because not all plan node types are robust about being
904 * called again if they've already returned NULL once.) Then call the
905 * executor (we must not skip this, because the destination needs to see a
906 * setup and shutdown even if no tuples are available). Finally, update
907 * the portal position state depending on the number of tuples that were
912 if (portal->atEnd || count <= 0)
914 direction = NoMovementScanDirection;
915 count = 0; /* don't pass negative count to executor */
918 direction = ForwardScanDirection;
920 /* In the executor, zero count processes all rows */
921 if (count == FETCH_ALL)
924 if (portal->holdStore)
925 nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
928 PushActiveSnapshot(queryDesc->snapshot);
929 ExecutorRun(queryDesc, direction, (uint64) count,
931 nprocessed = queryDesc->estate->es_processed;
935 if (!ScanDirectionIsNoMovement(direction))
938 portal->atStart = false; /* OK to go backward now */
939 if (count == 0 || nprocessed < (uint64) count)
940 portal->atEnd = true; /* we retrieved 'em all */
941 portal->portalPos += nprocessed;
946 if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
948 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
949 errmsg("cursor can only scan forward"),
950 errhint("Declare it with SCROLL option to enable backward scan.")));
952 if (portal->atStart || count <= 0)
954 direction = NoMovementScanDirection;
955 count = 0; /* don't pass negative count to executor */
958 direction = BackwardScanDirection;
960 /* In the executor, zero count processes all rows */
961 if (count == FETCH_ALL)
964 if (portal->holdStore)
965 nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
968 PushActiveSnapshot(queryDesc->snapshot);
969 ExecutorRun(queryDesc, direction, (uint64) count,
971 nprocessed = queryDesc->estate->es_processed;
975 if (!ScanDirectionIsNoMovement(direction))
977 if (nprocessed > 0 && portal->atEnd)
979 portal->atEnd = false; /* OK to go forward now */
980 portal->portalPos++; /* adjust for endpoint case */
982 if (count == 0 || nprocessed < (uint64) count)
984 portal->atStart = true; /* we retrieved 'em all */
985 portal->portalPos = 0;
989 portal->portalPos -= nprocessed;
999 * Run the query and load result tuples into the portal's tuple store.
1001 * This is used for PORTAL_ONE_RETURNING, PORTAL_ONE_MOD_WITH, and
1002 * PORTAL_UTIL_SELECT cases only.
1005 FillPortalStore(Portal portal, bool isTopLevel)
1007 DestReceiver *treceiver;
1008 char completionTag[COMPLETION_TAG_BUFSIZE];
1010 PortalCreateHoldStore(portal);
1011 treceiver = CreateDestReceiver(DestTuplestore);
1012 SetTuplestoreDestReceiverParams(treceiver,
1014 portal->holdContext,
1017 completionTag[0] = '\0';
1019 switch (portal->strategy)
1021 case PORTAL_ONE_RETURNING:
1022 case PORTAL_ONE_MOD_WITH:
1025 * Run the portal to completion just as for the default
1026 * PORTAL_MULTI_QUERY case, but send the primary query's output to
1027 * the tuplestore. Auxiliary query outputs are discarded. Set the
1028 * portal's holdSnapshot to the snapshot used (or a copy of it).
1030 PortalRunMulti(portal, isTopLevel, true,
1031 treceiver, None_Receiver, completionTag);
1034 case PORTAL_UTIL_SELECT:
1035 PortalRunUtility(portal, linitial_node(PlannedStmt, portal->stmts),
1036 isTopLevel, true, treceiver, completionTag);
1040 elog(ERROR, "unsupported portal strategy: %d",
1041 (int) portal->strategy);
1045 /* Override default completion tag with actual command result */
1046 if (completionTag[0] != '\0')
1047 portal->commandTag = pstrdup(completionTag);
1049 treceiver->rDestroy(treceiver);
1054 * Fetch tuples from the portal's tuple store.
1056 * Calling conventions are similar to ExecutorRun, except that we
1057 * do not depend on having a queryDesc or estate. Therefore we return the
1058 * number of tuples processed as the result, not in estate->es_processed.
1060 * One difference from ExecutorRun is that the destination receiver functions
1061 * are run in the caller's memory context (since we have no estate). Watch
1062 * out for memory leaks.
1065 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
1068 uint64 current_tuple_count = 0;
1069 TupleTableSlot *slot;
1071 slot = MakeSingleTupleTableSlot(portal->tupDesc, &TTSOpsMinimalTuple);
1073 dest->rStartup(dest, CMD_SELECT, portal->tupDesc);
1075 if (ScanDirectionIsNoMovement(direction))
1077 /* do nothing except start/stop the destination */
1081 bool forward = ScanDirectionIsForward(direction);
1085 MemoryContext oldcontext;
1088 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1090 ok = tuplestore_gettupleslot(portal->holdStore, forward, false,
1093 MemoryContextSwitchTo(oldcontext);
1099 * If we are not able to send the tuple, we assume the destination
1100 * has closed and no more tuples can be sent. If that's the case,
1103 if (!dest->receiveSlot(slot, dest))
1106 ExecClearTuple(slot);
1109 * check our tuple count.. if we've processed the proper number
1110 * then quit, else loop again and process more tuples. Zero count
1113 current_tuple_count++;
1114 if (count && count == current_tuple_count)
1119 dest->rShutdown(dest);
1121 ExecDropSingleTupleTableSlot(slot);
1123 return current_tuple_count;
1128 * Execute a utility statement inside a portal.
1131 PortalRunUtility(Portal portal, PlannedStmt *pstmt,
1132 bool isTopLevel, bool setHoldSnapshot,
1133 DestReceiver *dest, char *completionTag)
1135 Node *utilityStmt = pstmt->utilityStmt;
1139 * Set snapshot if utility stmt needs one. Most reliable way to do this
1140 * seems to be to enumerate those that do not need one; this is a short
1141 * list. Transaction control, LOCK, and SET must *not* set a snapshot
1142 * since they need to be executable at the start of a transaction-snapshot
1143 * mode transaction without freezing a snapshot. By extension we allow
1144 * SHOW not to set a snapshot. The other stmts listed are just efficiency
1145 * hacks. Beware of listing anything that can modify the database --- if,
1146 * say, it has to update an index with expressions that invoke
1147 * user-defined functions, then it had better have a snapshot.
1149 if (!(IsA(utilityStmt, TransactionStmt) ||
1150 IsA(utilityStmt, LockStmt) ||
1151 IsA(utilityStmt, VariableSetStmt) ||
1152 IsA(utilityStmt, VariableShowStmt) ||
1153 IsA(utilityStmt, ConstraintsSetStmt) ||
1154 /* efficiency hacks from here down */
1155 IsA(utilityStmt, FetchStmt) ||
1156 IsA(utilityStmt, ListenStmt) ||
1157 IsA(utilityStmt, NotifyStmt) ||
1158 IsA(utilityStmt, UnlistenStmt) ||
1159 IsA(utilityStmt, CheckPointStmt)))
1161 snapshot = GetTransactionSnapshot();
1162 /* If told to, register the snapshot we're using and save in portal */
1163 if (setHoldSnapshot)
1165 snapshot = RegisterSnapshot(snapshot);
1166 portal->holdSnapshot = snapshot;
1168 PushActiveSnapshot(snapshot);
1169 /* PushActiveSnapshot might have copied the snapshot */
1170 snapshot = GetActiveSnapshot();
1175 ProcessUtility(pstmt,
1177 isTopLevel ? PROCESS_UTILITY_TOPLEVEL : PROCESS_UTILITY_QUERY,
1178 portal->portalParams,
1183 /* Some utility statements may change context on us */
1184 MemoryContextSwitchTo(portal->portalContext);
1187 * Some utility commands may pop the ActiveSnapshot stack from under us,
1188 * so be careful to only pop the stack if our snapshot is still at the
1191 if (snapshot != NULL && ActiveSnapshotSet() &&
1192 snapshot == GetActiveSnapshot())
1193 PopActiveSnapshot();
1198 * Execute a portal's queries in the general case (multi queries
1199 * or non-SELECT-like queries)
1202 PortalRunMulti(Portal portal,
1203 bool isTopLevel, bool setHoldSnapshot,
1204 DestReceiver *dest, DestReceiver *altdest,
1205 char *completionTag)
1207 bool active_snapshot_set = false;
1208 ListCell *stmtlist_item;
1211 * If the destination is DestRemoteExecute, change to DestNone. The
1212 * reason is that the client won't be expecting any tuples, and indeed has
1213 * no way to know what they are, since there is no provision for Describe
1214 * to send a RowDescription message when this portal execution strategy is
1215 * in effect. This presently will only affect SELECT commands added to
1216 * non-SELECT queries by rewrite rules: such commands will be executed,
1217 * but the results will be discarded unless you use "simple Query"
1220 if (dest->mydest == DestRemoteExecute)
1221 dest = None_Receiver;
1222 if (altdest->mydest == DestRemoteExecute)
1223 altdest = None_Receiver;
1226 * Loop to handle the individual queries generated from a single parsetree
1227 * by analysis and rewrite.
1229 foreach(stmtlist_item, portal->stmts)
1231 PlannedStmt *pstmt = lfirst_node(PlannedStmt, stmtlist_item);
1234 * If we got a cancel signal in prior command, quit
1236 CHECK_FOR_INTERRUPTS();
1238 if (pstmt->utilityStmt == NULL)
1241 * process a plannable query.
1243 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1245 if (log_executor_stats)
1249 * Must always have a snapshot for plannable queries. First time
1250 * through, take a new snapshot; for subsequent queries in the
1251 * same portal, just update the snapshot's copy of the command
1254 if (!active_snapshot_set)
1256 Snapshot snapshot = GetTransactionSnapshot();
1258 /* If told to, register the snapshot and save in portal */
1259 if (setHoldSnapshot)
1261 snapshot = RegisterSnapshot(snapshot);
1262 portal->holdSnapshot = snapshot;
1266 * We can't have the holdSnapshot also be the active one,
1267 * because UpdateActiveSnapshotCommandId would complain. So
1268 * force an extra snapshot copy. Plain PushActiveSnapshot
1269 * would have copied the transaction snapshot anyway, so this
1270 * only adds a copy step when setHoldSnapshot is true. (It's
1271 * okay for the command ID of the active snapshot to diverge
1272 * from what holdSnapshot has.)
1274 PushCopiedSnapshot(snapshot);
1275 active_snapshot_set = true;
1278 UpdateActiveSnapshotCommandId();
1280 if (pstmt->canSetTag)
1282 /* statement can set tag string */
1285 portal->portalParams,
1287 dest, completionTag);
1291 /* stmt added by rewrite cannot set tag */
1294 portal->portalParams,
1299 if (log_executor_stats)
1300 ShowUsage("EXECUTOR STATISTICS");
1302 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
1307 * process utility functions (create, destroy, etc..)
1309 * We must not set a snapshot here for utility commands (if one is
1310 * needed, PortalRunUtility will do it). If a utility command is
1311 * alone in a portal then everything's fine. The only case where
1312 * a utility command can be part of a longer list is that rules
1313 * are allowed to include NotifyStmt. NotifyStmt doesn't care
1314 * whether it has a snapshot or not, so we just leave the current
1315 * snapshot alone if we have one.
1317 if (pstmt->canSetTag)
1319 Assert(!active_snapshot_set);
1320 /* statement can set tag string */
1321 PortalRunUtility(portal, pstmt, isTopLevel, false,
1322 dest, completionTag);
1326 Assert(IsA(pstmt->utilityStmt, NotifyStmt));
1327 /* stmt added by rewrite cannot set tag */
1328 PortalRunUtility(portal, pstmt, isTopLevel, false,
1334 * Increment command counter between queries, but not after the last
1337 if (lnext(portal->stmts, stmtlist_item) != NULL)
1338 CommandCounterIncrement();
1341 * Clear subsidiary contexts to recover temporary memory.
1343 Assert(portal->portalContext == CurrentMemoryContext);
1345 MemoryContextDeleteChildren(portal->portalContext);
1348 /* Pop the snapshot if we pushed one. */
1349 if (active_snapshot_set)
1350 PopActiveSnapshot();
1353 * If a command completion tag was supplied, use it. Otherwise use the
1354 * portal's commandTag as the default completion tag.
1356 * Exception: Clients expect INSERT/UPDATE/DELETE tags to have counts, so
1357 * fake them with zeros. This can happen with DO INSTEAD rules if there
1358 * is no replacement query of the same type as the original. We print "0
1359 * 0" here because technically there is no query of the matching tag type,
1360 * and printing a non-zero count for a different query type seems wrong,
1361 * e.g. an INSERT that does an UPDATE instead should not print "0 1" if
1362 * one row was updated. See QueryRewrite(), step 3, for details.
1364 if (completionTag && completionTag[0] == '\0')
1366 if (portal->commandTag)
1367 strcpy(completionTag, portal->commandTag);
1368 if (strcmp(completionTag, "SELECT") == 0)
1369 sprintf(completionTag, "SELECT 0 0");
1370 else if (strcmp(completionTag, "INSERT") == 0)
1371 strcpy(completionTag, "INSERT 0 0");
1372 else if (strcmp(completionTag, "UPDATE") == 0)
1373 strcpy(completionTag, "UPDATE 0");
1374 else if (strcmp(completionTag, "DELETE") == 0)
1375 strcpy(completionTag, "DELETE 0");
1381 * Variant form of PortalRun that supports SQL FETCH directions.
1383 * Note: we presently assume that no callers of this want isTopLevel = true.
1385 * count <= 0 is interpreted as a no-op: the destination gets started up
1386 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
1387 * interpreted as "all rows". (cf FetchStmt.howMany)
1389 * Returns number of rows processed (suitable for use in result tag)
1392 PortalRunFetch(Portal portal,
1393 FetchDirection fdirection,
1398 Portal saveActivePortal;
1399 ResourceOwner saveResourceOwner;
1400 MemoryContext savePortalContext;
1401 MemoryContext oldContext;
1403 AssertArg(PortalIsValid(portal));
1406 * Check for improper portal use, and mark portal active.
1408 MarkPortalActive(portal);
1410 /* If supporting FETCH, portal can't be run-once. */
1411 Assert(!portal->run_once);
1414 * Set up global portal context pointers.
1416 saveActivePortal = ActivePortal;
1417 saveResourceOwner = CurrentResourceOwner;
1418 savePortalContext = PortalContext;
1421 ActivePortal = portal;
1422 if (portal->resowner)
1423 CurrentResourceOwner = portal->resowner;
1424 PortalContext = portal->portalContext;
1426 oldContext = MemoryContextSwitchTo(PortalContext);
1428 switch (portal->strategy)
1430 case PORTAL_ONE_SELECT:
1431 result = DoPortalRunFetch(portal, fdirection, count, dest);
1434 case PORTAL_ONE_RETURNING:
1435 case PORTAL_ONE_MOD_WITH:
1436 case PORTAL_UTIL_SELECT:
1439 * If we have not yet run the command, do so, storing its
1440 * results in the portal's tuplestore.
1442 if (!portal->holdStore)
1443 FillPortalStore(portal, false /* isTopLevel */ );
1446 * Now fetch desired portion of results.
1448 result = DoPortalRunFetch(portal, fdirection, count, dest);
1452 elog(ERROR, "unsupported portal strategy");
1453 result = 0; /* keep compiler quiet */
1459 /* Uncaught error while executing portal: mark it dead */
1460 MarkPortalFailed(portal);
1462 /* Restore global vars and propagate error */
1463 ActivePortal = saveActivePortal;
1464 CurrentResourceOwner = saveResourceOwner;
1465 PortalContext = savePortalContext;
1471 MemoryContextSwitchTo(oldContext);
1473 /* Mark portal not active */
1474 portal->status = PORTAL_READY;
1476 ActivePortal = saveActivePortal;
1477 CurrentResourceOwner = saveResourceOwner;
1478 PortalContext = savePortalContext;
1485 * Guts of PortalRunFetch --- the portal context is already set up
1487 * count <= 0 is interpreted as a no-op: the destination gets started up
1488 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
1489 * interpreted as "all rows". (cf FetchStmt.howMany)
1491 * Returns number of rows processed (suitable for use in result tag)
1494 DoPortalRunFetch(Portal portal,
1495 FetchDirection fdirection,
1501 Assert(portal->strategy == PORTAL_ONE_SELECT ||
1502 portal->strategy == PORTAL_ONE_RETURNING ||
1503 portal->strategy == PORTAL_ONE_MOD_WITH ||
1504 portal->strategy == PORTAL_UTIL_SELECT);
1511 fdirection = FETCH_BACKWARD;
1514 /* fall out of switch to share code with FETCH_BACKWARD */
1516 case FETCH_BACKWARD:
1519 fdirection = FETCH_FORWARD;
1522 /* fall out of switch to share code with FETCH_FORWARD */
1524 case FETCH_ABSOLUTE:
1528 * Definition: Rewind to start, advance count-1 rows, return
1529 * next row (if any).
1531 * In practice, if the goal is less than halfway back to the
1532 * start, it's better to scan from where we are.
1534 * Also, if current portalPos is outside the range of "long",
1535 * do it the hard way to avoid possible overflow of the count
1536 * argument to PortalRunSelect. We must exclude exactly
1537 * LONG_MAX, as well, lest the count look like FETCH_ALL.
1539 * In any case, we arrange to fetch the target row going
1542 if ((uint64) (count - 1) <= portal->portalPos / 2 ||
1543 portal->portalPos >= (uint64) LONG_MAX)
1545 DoPortalRewind(portal);
1547 PortalRunSelect(portal, true, count - 1,
1552 long pos = (long) portal->portalPos;
1555 pos++; /* need one extra fetch if off end */
1557 PortalRunSelect(portal, false, pos - count + 1,
1559 else if (count > pos + 1)
1560 PortalRunSelect(portal, true, count - pos - 1,
1563 return PortalRunSelect(portal, true, 1L, dest);
1568 * Definition: Advance to end, back up abs(count)-1 rows,
1569 * return prior row (if any). We could optimize this if we
1570 * knew in advance where the end was, but typically we won't.
1571 * (Is it worth considering case where count > half of size of
1572 * query? We could rewind once we know the size ...)
1574 PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
1576 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1577 return PortalRunSelect(portal, false, 1L, dest);
1582 /* Rewind to start, return zero rows */
1583 DoPortalRewind(portal);
1584 return PortalRunSelect(portal, true, 0L, dest);
1587 case FETCH_RELATIVE:
1591 * Definition: advance count-1 rows, return next row (if any).
1594 PortalRunSelect(portal, true, count - 1, None_Receiver);
1595 return PortalRunSelect(portal, true, 1L, dest);
1600 * Definition: back up abs(count)-1 rows, return prior row (if
1604 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1605 return PortalRunSelect(portal, false, 1L, dest);
1610 /* Same as FETCH FORWARD 0, so fall out of switch */
1611 fdirection = FETCH_FORWARD;
1615 elog(ERROR, "bogus direction");
1620 * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
1623 forward = (fdirection == FETCH_FORWARD);
1626 * Zero count means to re-fetch the current row, if any (per SQL)
1632 /* Are we sitting on a row? */
1633 on_row = (!portal->atStart && !portal->atEnd);
1635 if (dest->mydest == DestNone)
1637 /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
1638 return on_row ? 1 : 0;
1643 * If we are sitting on a row, back up one so we can re-fetch it.
1644 * If we are not sitting on a row, we still have to start up and
1645 * shut down the executor so that the destination is initialized
1646 * and shut down correctly; so keep going. To PortalRunSelect,
1647 * count == 0 means we will retrieve no row.
1651 PortalRunSelect(portal, false, 1L, None_Receiver);
1652 /* Set up to fetch one row forward */
1660 * Optimize MOVE BACKWARD ALL into a Rewind.
1662 if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
1664 uint64 result = portal->portalPos;
1666 if (result > 0 && !portal->atEnd)
1668 DoPortalRewind(portal);
1672 return PortalRunSelect(portal, forward, count, dest);
1676 * DoPortalRewind - rewind a Portal to starting point
1679 DoPortalRewind(Portal portal)
1681 QueryDesc *queryDesc;
1683 /* Rewind holdStore, if we have one */
1684 if (portal->holdStore)
1686 MemoryContext oldcontext;
1688 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1689 tuplestore_rescan(portal->holdStore);
1690 MemoryContextSwitchTo(oldcontext);
1693 /* Rewind executor, if active */
1694 queryDesc = portal->queryDesc;
1697 PushActiveSnapshot(queryDesc->snapshot);
1698 ExecutorRewind(queryDesc);
1699 PopActiveSnapshot();
1702 portal->atStart = true;
1703 portal->atEnd = false;
1704 portal->portalPos = 0;