PostgreSQL Source Code git master
pquery.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pquery.c
4 * POSTGRES process query command code
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/tcop/pquery.c
12 *
13 *-------------------------------------------------------------------------
14 */
15
16#include "postgres.h"
17
18#include <limits.h>
19
20#include "access/xact.h"
21#include "commands/prepare.h"
22#include "executor/executor.h"
24#include "miscadmin.h"
25#include "pg_trace.h"
26#include "tcop/pquery.h"
27#include "tcop/utility.h"
28#include "utils/memutils.h"
29#include "utils/snapmgr.h"
30
31
32/*
33 * ActivePortal is the currently executing Portal (the most closely nested,
34 * if there are several).
35 */
37
38
39static void ProcessQuery(PlannedStmt *plan,
40 const char *sourceText,
41 ParamListInfo params,
42 QueryEnvironment *queryEnv,
44 QueryCompletion *qc);
45static void FillPortalStore(Portal portal, bool isTopLevel);
46static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
48static uint64 PortalRunSelect(Portal portal, bool forward, long count,
50static void PortalRunUtility(Portal portal, PlannedStmt *pstmt,
51 bool isTopLevel, bool setHoldSnapshot,
53static void PortalRunMulti(Portal portal,
54 bool isTopLevel, bool setHoldSnapshot,
56 QueryCompletion *qc);
57static uint64 DoPortalRunFetch(Portal portal,
58 FetchDirection fdirection,
59 long count,
61static void DoPortalRewind(Portal portal);
62
63
64/*
65 * CreateQueryDesc
66 */
69 const char *sourceText,
70 Snapshot snapshot,
71 Snapshot crosscheck_snapshot,
73 ParamListInfo params,
74 QueryEnvironment *queryEnv,
75 int instrument_options)
76{
77 QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
78
79 qd->operation = plannedstmt->commandType; /* operation */
80 qd->plannedstmt = plannedstmt; /* plan */
81 qd->sourceText = sourceText; /* query text */
82 qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
83 /* RI check snapshot */
84 qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
85 qd->dest = dest; /* output dest */
86 qd->params = params; /* parameter values passed into query */
87 qd->queryEnv = queryEnv;
88 qd->instrument_options = instrument_options; /* instrumentation wanted? */
89
90 /* null these fields until set by ExecutorStart */
91 qd->tupDesc = NULL;
92 qd->estate = NULL;
93 qd->planstate = NULL;
94 qd->totaltime = NULL;
95
96 /* not yet executed */
97 qd->already_executed = false;
98
99 return qd;
100}
101
102/*
103 * FreeQueryDesc
104 */
105void
107{
108 /* Can't be a live query */
109 Assert(qdesc->estate == NULL);
110
111 /* forget our snapshots */
114
115 /* Only the QueryDesc itself need be freed */
116 pfree(qdesc);
117}
118
119
120/*
121 * ProcessQuery
122 * Execute a single plannable query within a PORTAL_MULTI_QUERY,
123 * PORTAL_ONE_RETURNING, or PORTAL_ONE_MOD_WITH portal
124 *
125 * plan: the plan tree for the query
126 * sourceText: the source text of the query
127 * params: any parameters needed
128 * dest: where to send results
129 * qc: where to store the command completion status data.
130 *
131 * qc may be NULL if caller doesn't want a status string.
132 *
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.
135 */
136static void
138 const char *sourceText,
139 ParamListInfo params,
140 QueryEnvironment *queryEnv,
142 QueryCompletion *qc)
143{
144 QueryDesc *queryDesc;
145
146 /*
147 * Create the QueryDesc object
148 */
149 queryDesc = CreateQueryDesc(plan, sourceText,
151 dest, params, queryEnv, 0);
152
153 /*
154 * Call ExecutorStart to prepare the plan for execution
155 */
156 ExecutorStart(queryDesc, 0);
157
158 /*
159 * Run the plan to completion.
160 */
161 ExecutorRun(queryDesc, ForwardScanDirection, 0);
162
163 /*
164 * Build command completion status data, if caller wants one.
165 */
166 if (qc)
167 {
168 CommandTag tag;
169
170 if (queryDesc->operation == CMD_SELECT)
171 tag = CMDTAG_SELECT;
172 else if (queryDesc->operation == CMD_INSERT)
173 tag = CMDTAG_INSERT;
174 else if (queryDesc->operation == CMD_UPDATE)
175 tag = CMDTAG_UPDATE;
176 else if (queryDesc->operation == CMD_DELETE)
177 tag = CMDTAG_DELETE;
178 else if (queryDesc->operation == CMD_MERGE)
179 tag = CMDTAG_MERGE;
180 else
181 tag = CMDTAG_UNKNOWN;
182
183 SetQueryCompletion(qc, tag, queryDesc->estate->es_processed);
184 }
185
186 /*
187 * Now, we close down all the scans and free allocated resources.
188 */
189 ExecutorFinish(queryDesc);
190 ExecutorEnd(queryDesc);
191
192 FreeQueryDesc(queryDesc);
193}
194
195/*
196 * ChoosePortalStrategy
197 * Select portal execution strategy given the intended statement list.
198 *
199 * The list elements can be Querys or PlannedStmts.
200 * That's more general than portals need, but plancache.c uses this too.
201 *
202 * See the comments in portal.h.
203 */
206{
207 int nSetTag;
208 ListCell *lc;
209
210 /*
211 * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
212 * single-statement case, since there are no rewrite rules that can add
213 * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
214 * likewise allows only one top-level statement.
215 */
216 if (list_length(stmts) == 1)
217 {
218 Node *stmt = (Node *) linitial(stmts);
219
220 if (IsA(stmt, Query))
221 {
222 Query *query = (Query *) stmt;
223
224 if (query->canSetTag)
225 {
226 if (query->commandType == CMD_SELECT)
227 {
228 if (query->hasModifyingCTE)
229 return PORTAL_ONE_MOD_WITH;
230 else
231 return PORTAL_ONE_SELECT;
232 }
233 if (query->commandType == CMD_UTILITY)
234 {
236 return PORTAL_UTIL_SELECT;
237 /* it can't be ONE_RETURNING, so give up */
238 return PORTAL_MULTI_QUERY;
239 }
240 }
241 }
242 else if (IsA(stmt, PlannedStmt))
243 {
244 PlannedStmt *pstmt = (PlannedStmt *) stmt;
245
246 if (pstmt->canSetTag)
247 {
248 if (pstmt->commandType == CMD_SELECT)
249 {
250 if (pstmt->hasModifyingCTE)
251 return PORTAL_ONE_MOD_WITH;
252 else
253 return PORTAL_ONE_SELECT;
254 }
255 if (pstmt->commandType == CMD_UTILITY)
256 {
258 return PORTAL_UTIL_SELECT;
259 /* it can't be ONE_RETURNING, so give up */
260 return PORTAL_MULTI_QUERY;
261 }
262 }
263 }
264 else
265 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
266 }
267
268 /*
269 * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
270 * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
271 * it has a RETURNING list.
272 */
273 nSetTag = 0;
274 foreach(lc, stmts)
275 {
276 Node *stmt = (Node *) lfirst(lc);
277
278 if (IsA(stmt, Query))
279 {
280 Query *query = (Query *) stmt;
281
282 if (query->canSetTag)
283 {
284 if (++nSetTag > 1)
285 return PORTAL_MULTI_QUERY; /* no need to look further */
286 if (query->commandType == CMD_UTILITY ||
287 query->returningList == NIL)
288 return PORTAL_MULTI_QUERY; /* no need to look further */
289 }
290 }
291 else if (IsA(stmt, PlannedStmt))
292 {
293 PlannedStmt *pstmt = (PlannedStmt *) stmt;
294
295 if (pstmt->canSetTag)
296 {
297 if (++nSetTag > 1)
298 return PORTAL_MULTI_QUERY; /* no need to look further */
299 if (pstmt->commandType == CMD_UTILITY ||
300 !pstmt->hasReturning)
301 return PORTAL_MULTI_QUERY; /* no need to look further */
302 }
303 }
304 else
305 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
306 }
307 if (nSetTag == 1)
309
310 /* Else, it's the general case... */
311 return PORTAL_MULTI_QUERY;
312}
313
314/*
315 * FetchPortalTargetList
316 * Given a portal that returns tuples, extract the query targetlist.
317 * Returns NIL if the portal doesn't have a determinable targetlist.
318 *
319 * Note: do not modify the result.
320 */
321List *
323{
324 /* no point in looking if we determined it doesn't return tuples */
325 if (portal->strategy == PORTAL_MULTI_QUERY)
326 return NIL;
327 /* get the primary statement and find out what it returns */
329}
330
331/*
332 * FetchStatementTargetList
333 * Given a statement that returns tuples, extract the query targetlist.
334 * Returns NIL if the statement doesn't have a determinable targetlist.
335 *
336 * This can be applied to a Query or a PlannedStmt.
337 * That's more general than portals need, but plancache.c uses this too.
338 *
339 * Note: do not modify the result.
340 *
341 * XXX be careful to keep this in sync with UtilityReturnsTuples.
342 */
343List *
345{
346 if (stmt == NULL)
347 return NIL;
348 if (IsA(stmt, Query))
349 {
350 Query *query = (Query *) stmt;
351
352 if (query->commandType == CMD_UTILITY)
353 {
354 /* transfer attention to utility statement */
355 stmt = query->utilityStmt;
356 }
357 else
358 {
359 if (query->commandType == CMD_SELECT)
360 return query->targetList;
361 if (query->returningList)
362 return query->returningList;
363 return NIL;
364 }
365 }
366 if (IsA(stmt, PlannedStmt))
367 {
368 PlannedStmt *pstmt = (PlannedStmt *) stmt;
369
370 if (pstmt->commandType == CMD_UTILITY)
371 {
372 /* transfer attention to utility statement */
373 stmt = pstmt->utilityStmt;
374 }
375 else
376 {
377 if (pstmt->commandType == CMD_SELECT)
378 return pstmt->planTree->targetlist;
379 if (pstmt->hasReturning)
380 return pstmt->planTree->targetlist;
381 return NIL;
382 }
383 }
384 if (IsA(stmt, FetchStmt))
385 {
386 FetchStmt *fstmt = (FetchStmt *) stmt;
387 Portal subportal;
388
389 Assert(!fstmt->ismove);
390 subportal = GetPortalByName(fstmt->portalname);
391 Assert(PortalIsValid(subportal));
392 return FetchPortalTargetList(subportal);
393 }
394 if (IsA(stmt, ExecuteStmt))
395 {
396 ExecuteStmt *estmt = (ExecuteStmt *) stmt;
397 PreparedStatement *entry;
398
399 entry = FetchPreparedStatement(estmt->name, true);
401 }
402 return NIL;
403}
404
405/*
406 * PortalStart
407 * Prepare a portal for execution.
408 *
409 * Caller must already have created the portal, done PortalDefineQuery(),
410 * and adjusted portal options if needed.
411 *
412 * If parameters are needed by the query, they must be passed in "params"
413 * (caller is responsible for giving them appropriate lifetime).
414 *
415 * The caller can also provide an initial set of "eflags" to be passed to
416 * ExecutorStart (but note these can be modified internally, and they are
417 * currently only honored for PORTAL_ONE_SELECT portals). Most callers
418 * should simply pass zero.
419 *
420 * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
421 * for the normal behavior of setting a new snapshot. This parameter is
422 * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
423 * to be used for cursors).
424 *
425 * On return, portal is ready to accept PortalRun() calls, and the result
426 * tupdesc (if any) is known.
427 */
428void
430 int eflags, Snapshot snapshot)
431{
432 Portal saveActivePortal;
433 ResourceOwner saveResourceOwner;
434 MemoryContext savePortalContext;
435 MemoryContext oldContext;
436 QueryDesc *queryDesc;
437 int myeflags;
438
439 Assert(PortalIsValid(portal));
440 Assert(portal->status == PORTAL_DEFINED);
441
442 /*
443 * Set up global portal context pointers.
444 */
445 saveActivePortal = ActivePortal;
446 saveResourceOwner = CurrentResourceOwner;
447 savePortalContext = PortalContext;
448 PG_TRY();
449 {
450 ActivePortal = portal;
451 if (portal->resowner)
454
456
457 /* Must remember portal param list, if any */
458 portal->portalParams = params;
459
460 /*
461 * Determine the portal execution strategy
462 */
463 portal->strategy = ChoosePortalStrategy(portal->stmts);
464
465 /*
466 * Fire her up according to the strategy
467 */
468 switch (portal->strategy)
469 {
471
472 /* Must set snapshot before starting executor. */
473 if (snapshot)
474 PushActiveSnapshot(snapshot);
475 else
477
478 /*
479 * We could remember the snapshot in portal->portalSnapshot,
480 * but presently there seems no need to, as this code path
481 * cannot be used for non-atomic execution. Hence there can't
482 * be any commit/abort that might destroy the snapshot. Since
483 * we don't do that, there's also no need to force a
484 * non-default nesting level for the snapshot.
485 */
486
487 /*
488 * Create QueryDesc in portal's context; for the moment, set
489 * the destination to DestNone.
490 */
491 queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
492 portal->sourceText,
496 params,
497 portal->queryEnv,
498 0);
499
500 /*
501 * If it's a scrollable cursor, executor needs to support
502 * REWIND and backwards scan, as well as whatever the caller
503 * might've asked for.
504 */
505 if (portal->cursorOptions & CURSOR_OPT_SCROLL)
506 myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
507 else
508 myeflags = eflags;
509
510 /*
511 * Call ExecutorStart to prepare the plan for execution
512 */
513 ExecutorStart(queryDesc, myeflags);
514
515 /*
516 * This tells PortalCleanup to shut down the executor
517 */
518 portal->queryDesc = queryDesc;
519
520 /*
521 * Remember tuple descriptor (computed by ExecutorStart)
522 */
523 portal->tupDesc = queryDesc->tupDesc;
524
525 /*
526 * Reset cursor position data to "start of query"
527 */
528 portal->atStart = true;
529 portal->atEnd = false; /* allow fetches */
530 portal->portalPos = 0;
531
533 break;
534
537
538 /*
539 * We don't start the executor until we are told to run the
540 * portal. We do need to set up the result tupdesc.
541 */
542 {
543 PlannedStmt *pstmt;
544
545 pstmt = PortalGetPrimaryStmt(portal);
546 portal->tupDesc =
548 }
549
550 /*
551 * Reset cursor position data to "start of query"
552 */
553 portal->atStart = true;
554 portal->atEnd = false; /* allow fetches */
555 portal->portalPos = 0;
556 break;
557
559
560 /*
561 * We don't set snapshot here, because PortalRunUtility will
562 * take care of it if needed.
563 */
564 {
565 PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
566
567 Assert(pstmt->commandType == CMD_UTILITY);
569 }
570
571 /*
572 * Reset cursor position data to "start of query"
573 */
574 portal->atStart = true;
575 portal->atEnd = false; /* allow fetches */
576 portal->portalPos = 0;
577 break;
578
580 /* Need do nothing now */
581 portal->tupDesc = NULL;
582 break;
583 }
584 }
585 PG_CATCH();
586 {
587 /* Uncaught error while executing portal: mark it dead */
588 MarkPortalFailed(portal);
589
590 /* Restore global vars and propagate error */
591 ActivePortal = saveActivePortal;
592 CurrentResourceOwner = saveResourceOwner;
593 PortalContext = savePortalContext;
594
595 PG_RE_THROW();
596 }
597 PG_END_TRY();
598
599 MemoryContextSwitchTo(oldContext);
600
601 ActivePortal = saveActivePortal;
602 CurrentResourceOwner = saveResourceOwner;
603 PortalContext = savePortalContext;
604
605 portal->status = PORTAL_READY;
606}
607
608/*
609 * PortalSetResultFormat
610 * Select the format codes for a portal's output.
611 *
612 * This must be run after PortalStart for a portal that will be read by
613 * a DestRemote or DestRemoteExecute destination. It is not presently needed
614 * for other destination types.
615 *
616 * formats[] is the client format request, as per Bind message conventions.
617 */
618void
619PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
620{
621 int natts;
622 int i;
623
624 /* Do nothing if portal won't return tuples */
625 if (portal->tupDesc == NULL)
626 return;
627 natts = portal->tupDesc->natts;
628 portal->formats = (int16 *)
630 natts * sizeof(int16));
631 if (nFormats > 1)
632 {
633 /* format specified for each column */
634 if (nFormats != natts)
636 (errcode(ERRCODE_PROTOCOL_VIOLATION),
637 errmsg("bind message has %d result formats but query has %d columns",
638 nFormats, natts)));
639 memcpy(portal->formats, formats, natts * sizeof(int16));
640 }
641 else if (nFormats > 0)
642 {
643 /* single format specified, use for all columns */
644 int16 format1 = formats[0];
645
646 for (i = 0; i < natts; i++)
647 portal->formats[i] = format1;
648 }
649 else
650 {
651 /* use default format for all columns */
652 for (i = 0; i < natts; i++)
653 portal->formats[i] = 0;
654 }
655}
656
657/*
658 * PortalRun
659 * Run a portal's query or queries.
660 *
661 * count <= 0 is interpreted as a no-op: the destination gets started up
662 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
663 * interpreted as "all rows". Note that count is ignored in multi-query
664 * situations, where we always run the portal to completion.
665 *
666 * isTopLevel: true if query is being executed at backend "top level"
667 * (that is, directly from a client command message)
668 *
669 * dest: where to send output of primary (canSetTag) query
670 *
671 * altdest: where to send output of non-primary queries
672 *
673 * qc: where to store command completion status data.
674 * May be NULL if caller doesn't want status data.
675 *
676 * Returns true if the portal's execution is complete, false if it was
677 * suspended due to exhaustion of the count parameter.
678 */
679bool
680PortalRun(Portal portal, long count, bool isTopLevel,
681 DestReceiver *dest, DestReceiver *altdest,
682 QueryCompletion *qc)
683{
684 bool result;
685 uint64 nprocessed;
686 ResourceOwner saveTopTransactionResourceOwner;
687 MemoryContext saveTopTransactionContext;
688 Portal saveActivePortal;
689 ResourceOwner saveResourceOwner;
690 MemoryContext savePortalContext;
691 MemoryContext saveMemoryContext;
692
693 Assert(PortalIsValid(portal));
694
695 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
696
697 /* Initialize empty completion data */
698 if (qc)
700
702 {
703 elog(DEBUG3, "PortalRun");
704 /* PORTAL_MULTI_QUERY logs its own stats per query */
705 ResetUsage();
706 }
707
708 /*
709 * Check for improper portal use, and mark portal active.
710 */
711 MarkPortalActive(portal);
712
713 /*
714 * Set up global portal context pointers.
715 *
716 * We have to play a special game here to support utility commands like
717 * VACUUM and CLUSTER, which internally start and commit transactions.
718 * When we are called to execute such a command, CurrentResourceOwner will
719 * be pointing to the TopTransactionResourceOwner --- which will be
720 * destroyed and replaced in the course of the internal commit and
721 * restart. So we need to be prepared to restore it as pointing to the
722 * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
723 * internally starting whole new transactions is not good.)
724 * CurrentMemoryContext has a similar problem, but the other pointers we
725 * save here will be NULL or pointing to longer-lived objects.
726 */
727 saveTopTransactionResourceOwner = TopTransactionResourceOwner;
728 saveTopTransactionContext = TopTransactionContext;
729 saveActivePortal = ActivePortal;
730 saveResourceOwner = CurrentResourceOwner;
731 savePortalContext = PortalContext;
732 saveMemoryContext = CurrentMemoryContext;
733 PG_TRY();
734 {
735 ActivePortal = portal;
736 if (portal->resowner)
739
741
742 switch (portal->strategy)
743 {
748
749 /*
750 * If we have not yet run the command, do so, storing its
751 * results in the portal's tuplestore. But we don't do that
752 * for the PORTAL_ONE_SELECT case.
753 */
754 if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
755 FillPortalStore(portal, isTopLevel);
756
757 /*
758 * Now fetch desired portion of results.
759 */
760 nprocessed = PortalRunSelect(portal, true, count, dest);
761
762 /*
763 * If the portal result contains a command tag and the caller
764 * gave us a pointer to store it, copy it and update the
765 * rowcount.
766 */
767 if (qc && portal->qc.commandTag != CMDTAG_UNKNOWN)
768 {
769 CopyQueryCompletion(qc, &portal->qc);
770 qc->nprocessed = nprocessed;
771 }
772
773 /* Mark portal not active */
774 portal->status = PORTAL_READY;
775
776 /*
777 * Since it's a forward fetch, say DONE iff atEnd is now true.
778 */
779 result = portal->atEnd;
780 break;
781
783 PortalRunMulti(portal, isTopLevel, false,
784 dest, altdest, qc);
785
786 /* Prevent portal's commands from being re-executed */
787 MarkPortalDone(portal);
788
789 /* Always complete at end of RunMulti */
790 result = true;
791 break;
792
793 default:
794 elog(ERROR, "unrecognized portal strategy: %d",
795 (int) portal->strategy);
796 result = false; /* keep compiler quiet */
797 break;
798 }
799 }
800 PG_CATCH();
801 {
802 /* Uncaught error while executing portal: mark it dead */
803 MarkPortalFailed(portal);
804
805 /* Restore global vars and propagate error */
806 if (saveMemoryContext == saveTopTransactionContext)
808 else
809 MemoryContextSwitchTo(saveMemoryContext);
810 ActivePortal = saveActivePortal;
811 if (saveResourceOwner == saveTopTransactionResourceOwner)
813 else
814 CurrentResourceOwner = saveResourceOwner;
815 PortalContext = savePortalContext;
816
817 PG_RE_THROW();
818 }
819 PG_END_TRY();
820
821 if (saveMemoryContext == saveTopTransactionContext)
823 else
824 MemoryContextSwitchTo(saveMemoryContext);
825 ActivePortal = saveActivePortal;
826 if (saveResourceOwner == saveTopTransactionResourceOwner)
828 else
829 CurrentResourceOwner = saveResourceOwner;
830 PortalContext = savePortalContext;
831
833 ShowUsage("EXECUTOR STATISTICS");
834
835 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
836
837 return result;
838}
839
840/*
841 * PortalRunSelect
842 * Execute a portal's query in PORTAL_ONE_SELECT mode, and also
843 * when fetching from a completed holdStore in PORTAL_ONE_RETURNING,
844 * PORTAL_ONE_MOD_WITH, and PORTAL_UTIL_SELECT cases.
845 *
846 * This handles simple N-rows-forward-or-backward cases. For more complex
847 * nonsequential access to a portal, see PortalRunFetch.
848 *
849 * count <= 0 is interpreted as a no-op: the destination gets started up
850 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
851 * interpreted as "all rows". (cf FetchStmt.howMany)
852 *
853 * Caller must already have validated the Portal and done appropriate
854 * setup (cf. PortalRun).
855 *
856 * Returns number of rows processed (suitable for use in result tag)
857 */
858static uint64
860 bool forward,
861 long count,
863{
864 QueryDesc *queryDesc;
865 ScanDirection direction;
866 uint64 nprocessed;
867
868 /*
869 * NB: queryDesc will be NULL if we are fetching from a held cursor or a
870 * completed utility query; can't use it in that path.
871 */
872 queryDesc = portal->queryDesc;
873
874 /* Caller messed up if we have neither a ready query nor held data. */
875 Assert(queryDesc || portal->holdStore);
876
877 /*
878 * Force the queryDesc destination to the right thing. This supports
879 * MOVE, for example, which will pass in dest = DestNone. This is okay to
880 * change as long as we do it on every fetch. (The Executor must not
881 * assume that dest never changes.)
882 */
883 if (queryDesc)
884 queryDesc->dest = dest;
885
886 /*
887 * Determine which direction to go in, and check to see if we're already
888 * at the end of the available tuples in that direction. If so, set the
889 * direction to NoMovement to avoid trying to fetch any tuples. (This
890 * check exists because not all plan node types are robust about being
891 * called again if they've already returned NULL once.) Then call the
892 * executor (we must not skip this, because the destination needs to see a
893 * setup and shutdown even if no tuples are available). Finally, update
894 * the portal position state depending on the number of tuples that were
895 * retrieved.
896 */
897 if (forward)
898 {
899 if (portal->atEnd || count <= 0)
900 {
901 direction = NoMovementScanDirection;
902 count = 0; /* don't pass negative count to executor */
903 }
904 else
905 direction = ForwardScanDirection;
906
907 /* In the executor, zero count processes all rows */
908 if (count == FETCH_ALL)
909 count = 0;
910
911 if (portal->holdStore)
912 nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
913 else
914 {
915 PushActiveSnapshot(queryDesc->snapshot);
916 ExecutorRun(queryDesc, direction, (uint64) count);
917 nprocessed = queryDesc->estate->es_processed;
919 }
920
921 if (!ScanDirectionIsNoMovement(direction))
922 {
923 if (nprocessed > 0)
924 portal->atStart = false; /* OK to go backward now */
925 if (count == 0 || nprocessed < (uint64) count)
926 portal->atEnd = true; /* we retrieved 'em all */
927 portal->portalPos += nprocessed;
928 }
929 }
930 else
931 {
934 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
935 errmsg("cursor can only scan forward"),
936 errhint("Declare it with SCROLL option to enable backward scan.")));
937
938 if (portal->atStart || count <= 0)
939 {
940 direction = NoMovementScanDirection;
941 count = 0; /* don't pass negative count to executor */
942 }
943 else
944 direction = BackwardScanDirection;
945
946 /* In the executor, zero count processes all rows */
947 if (count == FETCH_ALL)
948 count = 0;
949
950 if (portal->holdStore)
951 nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
952 else
953 {
954 PushActiveSnapshot(queryDesc->snapshot);
955 ExecutorRun(queryDesc, direction, (uint64) count);
956 nprocessed = queryDesc->estate->es_processed;
958 }
959
960 if (!ScanDirectionIsNoMovement(direction))
961 {
962 if (nprocessed > 0 && portal->atEnd)
963 {
964 portal->atEnd = false; /* OK to go forward now */
965 portal->portalPos++; /* adjust for endpoint case */
966 }
967 if (count == 0 || nprocessed < (uint64) count)
968 {
969 portal->atStart = true; /* we retrieved 'em all */
970 portal->portalPos = 0;
971 }
972 else
973 {
974 portal->portalPos -= nprocessed;
975 }
976 }
977 }
978
979 return nprocessed;
980}
981
982/*
983 * FillPortalStore
984 * Run the query and load result tuples into the portal's tuple store.
985 *
986 * This is used for PORTAL_ONE_RETURNING, PORTAL_ONE_MOD_WITH, and
987 * PORTAL_UTIL_SELECT cases only.
988 */
989static void
990FillPortalStore(Portal portal, bool isTopLevel)
991{
992 DestReceiver *treceiver;
994
996 PortalCreateHoldStore(portal);
999 portal->holdStore,
1000 portal->holdContext,
1001 false,
1002 NULL,
1003 NULL);
1004
1005 switch (portal->strategy)
1006 {
1009
1010 /*
1011 * Run the portal to completion just as for the default
1012 * PORTAL_MULTI_QUERY case, but send the primary query's output to
1013 * the tuplestore. Auxiliary query outputs are discarded. Set the
1014 * portal's holdSnapshot to the snapshot used (or a copy of it).
1015 */
1016 PortalRunMulti(portal, isTopLevel, true,
1017 treceiver, None_Receiver, &qc);
1018 break;
1019
1020 case PORTAL_UTIL_SELECT:
1022 isTopLevel, true, treceiver, &qc);
1023 break;
1024
1025 default:
1026 elog(ERROR, "unsupported portal strategy: %d",
1027 (int) portal->strategy);
1028 break;
1029 }
1030
1031 /* Override portal completion data with actual command results */
1032 if (qc.commandTag != CMDTAG_UNKNOWN)
1033 CopyQueryCompletion(&portal->qc, &qc);
1034
1035 treceiver->rDestroy(treceiver);
1036}
1037
1038/*
1039 * RunFromStore
1040 * Fetch tuples from the portal's tuple store.
1041 *
1042 * Calling conventions are similar to ExecutorRun, except that we
1043 * do not depend on having a queryDesc or estate. Therefore we return the
1044 * number of tuples processed as the result, not in estate->es_processed.
1045 *
1046 * One difference from ExecutorRun is that the destination receiver functions
1047 * are run in the caller's memory context (since we have no estate). Watch
1048 * out for memory leaks.
1049 */
1050static uint64
1051RunFromStore(Portal portal, ScanDirection direction, uint64 count,
1053{
1054 uint64 current_tuple_count = 0;
1055 TupleTableSlot *slot;
1056
1058
1059 dest->rStartup(dest, CMD_SELECT, portal->tupDesc);
1060
1061 if (ScanDirectionIsNoMovement(direction))
1062 {
1063 /* do nothing except start/stop the destination */
1064 }
1065 else
1066 {
1067 bool forward = ScanDirectionIsForward(direction);
1068
1069 for (;;)
1070 {
1071 MemoryContext oldcontext;
1072 bool ok;
1073
1074 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1075
1076 ok = tuplestore_gettupleslot(portal->holdStore, forward, false,
1077 slot);
1078
1079 MemoryContextSwitchTo(oldcontext);
1080
1081 if (!ok)
1082 break;
1083
1084 /*
1085 * If we are not able to send the tuple, we assume the destination
1086 * has closed and no more tuples can be sent. If that's the case,
1087 * end the loop.
1088 */
1089 if (!dest->receiveSlot(slot, dest))
1090 break;
1091
1092 ExecClearTuple(slot);
1093
1094 /*
1095 * check our tuple count.. if we've processed the proper number
1096 * then quit, else loop again and process more tuples. Zero count
1097 * means no limit.
1098 */
1099 current_tuple_count++;
1100 if (count && count == current_tuple_count)
1101 break;
1102 }
1103 }
1104
1105 dest->rShutdown(dest);
1106
1108
1109 return current_tuple_count;
1110}
1111
1112/*
1113 * PortalRunUtility
1114 * Execute a utility statement inside a portal.
1115 */
1116static void
1118 bool isTopLevel, bool setHoldSnapshot,
1120{
1121 /*
1122 * Set snapshot if utility stmt needs one.
1123 */
1124 if (PlannedStmtRequiresSnapshot(pstmt))
1125 {
1126 Snapshot snapshot = GetTransactionSnapshot();
1127
1128 /* If told to, register the snapshot we're using and save in portal */
1129 if (setHoldSnapshot)
1130 {
1131 snapshot = RegisterSnapshot(snapshot);
1132 portal->holdSnapshot = snapshot;
1133 }
1134
1135 /*
1136 * In any case, make the snapshot active and remember it in portal.
1137 * Because the portal now references the snapshot, we must tell
1138 * snapmgr.c that the snapshot belongs to the portal's transaction
1139 * level, else we risk portalSnapshot becoming a dangling pointer.
1140 */
1141 PushActiveSnapshotWithLevel(snapshot, portal->createLevel);
1142 /* PushActiveSnapshotWithLevel might have copied the snapshot */
1144 }
1145 else
1146 portal->portalSnapshot = NULL;
1147
1148 ProcessUtility(pstmt,
1149 portal->sourceText,
1150 (portal->cplan != NULL), /* protect tree if in plancache */
1152 portal->portalParams,
1153 portal->queryEnv,
1154 dest,
1155 qc);
1156
1157 /* Some utility statements may change context on us */
1159
1160 /*
1161 * Some utility commands (e.g., VACUUM) pop the ActiveSnapshot stack from
1162 * under us, so don't complain if it's now empty. Otherwise, our snapshot
1163 * should be the top one; pop it. Note that this could be a different
1164 * snapshot from the one we made above; see EnsurePortalSnapshotExists.
1165 */
1166 if (portal->portalSnapshot != NULL && ActiveSnapshotSet())
1167 {
1170 }
1171 portal->portalSnapshot = NULL;
1172}
1173
1174/*
1175 * PortalRunMulti
1176 * Execute a portal's queries in the general case (multi queries
1177 * or non-SELECT-like queries)
1178 */
1179static void
1181 bool isTopLevel, bool setHoldSnapshot,
1182 DestReceiver *dest, DestReceiver *altdest,
1183 QueryCompletion *qc)
1184{
1185 bool active_snapshot_set = false;
1186 ListCell *stmtlist_item;
1187
1188 /*
1189 * If the destination is DestRemoteExecute, change to DestNone. The
1190 * reason is that the client won't be expecting any tuples, and indeed has
1191 * no way to know what they are, since there is no provision for Describe
1192 * to send a RowDescription message when this portal execution strategy is
1193 * in effect. This presently will only affect SELECT commands added to
1194 * non-SELECT queries by rewrite rules: such commands will be executed,
1195 * but the results will be discarded unless you use "simple Query"
1196 * protocol.
1197 */
1198 if (dest->mydest == DestRemoteExecute)
1200 if (altdest->mydest == DestRemoteExecute)
1201 altdest = None_Receiver;
1202
1203 /*
1204 * Loop to handle the individual queries generated from a single parsetree
1205 * by analysis and rewrite.
1206 */
1207 foreach(stmtlist_item, portal->stmts)
1208 {
1209 PlannedStmt *pstmt = lfirst_node(PlannedStmt, stmtlist_item);
1210
1211 /*
1212 * If we got a cancel signal in prior command, quit
1213 */
1215
1216 if (pstmt->utilityStmt == NULL)
1217 {
1218 /*
1219 * process a plannable query.
1220 */
1221 TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1222
1224 ResetUsage();
1225
1226 /*
1227 * Must always have a snapshot for plannable queries. First time
1228 * through, take a new snapshot; for subsequent queries in the
1229 * same portal, just update the snapshot's copy of the command
1230 * counter.
1231 */
1232 if (!active_snapshot_set)
1233 {
1234 Snapshot snapshot = GetTransactionSnapshot();
1235
1236 /* If told to, register the snapshot and save in portal */
1237 if (setHoldSnapshot)
1238 {
1239 snapshot = RegisterSnapshot(snapshot);
1240 portal->holdSnapshot = snapshot;
1241 }
1242
1243 /*
1244 * We can't have the holdSnapshot also be the active one,
1245 * because UpdateActiveSnapshotCommandId would complain. So
1246 * force an extra snapshot copy. Plain PushActiveSnapshot
1247 * would have copied the transaction snapshot anyway, so this
1248 * only adds a copy step when setHoldSnapshot is true. (It's
1249 * okay for the command ID of the active snapshot to diverge
1250 * from what holdSnapshot has.)
1251 */
1252 PushCopiedSnapshot(snapshot);
1253
1254 /*
1255 * As for PORTAL_ONE_SELECT portals, it does not seem
1256 * necessary to maintain portal->portalSnapshot here.
1257 */
1258
1259 active_snapshot_set = true;
1260 }
1261 else
1263
1264 if (pstmt->canSetTag)
1265 {
1266 /* statement can set tag string */
1267 ProcessQuery(pstmt,
1268 portal->sourceText,
1269 portal->portalParams,
1270 portal->queryEnv,
1271 dest, qc);
1272 }
1273 else
1274 {
1275 /* stmt added by rewrite cannot set tag */
1276 ProcessQuery(pstmt,
1277 portal->sourceText,
1278 portal->portalParams,
1279 portal->queryEnv,
1280 altdest, NULL);
1281 }
1282
1284 ShowUsage("EXECUTOR STATISTICS");
1285
1286 TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
1287 }
1288 else
1289 {
1290 /*
1291 * process utility functions (create, destroy, etc..)
1292 *
1293 * We must not set a snapshot here for utility commands (if one is
1294 * needed, PortalRunUtility will do it). If a utility command is
1295 * alone in a portal then everything's fine. The only case where
1296 * a utility command can be part of a longer list is that rules
1297 * are allowed to include NotifyStmt. NotifyStmt doesn't care
1298 * whether it has a snapshot or not, so we just leave the current
1299 * snapshot alone if we have one.
1300 */
1301 if (pstmt->canSetTag)
1302 {
1303 Assert(!active_snapshot_set);
1304 /* statement can set tag string */
1305 PortalRunUtility(portal, pstmt, isTopLevel, false,
1306 dest, qc);
1307 }
1308 else
1309 {
1310 Assert(IsA(pstmt->utilityStmt, NotifyStmt));
1311 /* stmt added by rewrite cannot set tag */
1312 PortalRunUtility(portal, pstmt, isTopLevel, false,
1313 altdest, NULL);
1314 }
1315 }
1316
1317 /*
1318 * Clear subsidiary contexts to recover temporary memory.
1319 */
1321
1323
1324 /*
1325 * Avoid crashing if portal->stmts has been reset. This can only
1326 * occur if a CALL or DO utility statement executed an internal
1327 * COMMIT/ROLLBACK (cf PortalReleaseCachedPlan). The CALL or DO must
1328 * have been the only statement in the portal, so there's nothing left
1329 * for us to do; but we don't want to dereference a now-dangling list
1330 * pointer.
1331 */
1332 if (portal->stmts == NIL)
1333 break;
1334
1335 /*
1336 * Increment command counter between queries, but not after the last
1337 * one.
1338 */
1339 if (lnext(portal->stmts, stmtlist_item) != NULL)
1341 }
1342
1343 /* Pop the snapshot if we pushed one. */
1344 if (active_snapshot_set)
1346
1347 /*
1348 * If a command tag was requested and we did not fill in a run-time-
1349 * determined tag above, copy the parse-time tag from the Portal. (There
1350 * might not be any tag there either, in edge cases such as empty prepared
1351 * statements. That's OK.)
1352 */
1353 if (qc &&
1354 qc->commandTag == CMDTAG_UNKNOWN &&
1355 portal->qc.commandTag != CMDTAG_UNKNOWN)
1356 CopyQueryCompletion(qc, &portal->qc);
1357}
1358
1359/*
1360 * PortalRunFetch
1361 * Variant form of PortalRun that supports SQL FETCH directions.
1362 *
1363 * Note: we presently assume that no callers of this want isTopLevel = true.
1364 *
1365 * count <= 0 is interpreted as a no-op: the destination gets started up
1366 * and shut down, but nothing else happens. Also, count == FETCH_ALL is
1367 * interpreted as "all rows". (cf FetchStmt.howMany)
1368 *
1369 * Returns number of rows processed (suitable for use in result tag)
1370 */
1371uint64
1373 FetchDirection fdirection,
1374 long count,
1376{
1377 uint64 result;
1378 Portal saveActivePortal;
1379 ResourceOwner saveResourceOwner;
1380 MemoryContext savePortalContext;
1381 MemoryContext oldContext;
1382
1383 Assert(PortalIsValid(portal));
1384
1385 /*
1386 * Check for improper portal use, and mark portal active.
1387 */
1388 MarkPortalActive(portal);
1389
1390 /*
1391 * Set up global portal context pointers.
1392 */
1393 saveActivePortal = ActivePortal;
1394 saveResourceOwner = CurrentResourceOwner;
1395 savePortalContext = PortalContext;
1396 PG_TRY();
1397 {
1398 ActivePortal = portal;
1399 if (portal->resowner)
1401 PortalContext = portal->portalContext;
1402
1404
1405 switch (portal->strategy)
1406 {
1407 case PORTAL_ONE_SELECT:
1408 result = DoPortalRunFetch(portal, fdirection, count, dest);
1409 break;
1410
1413 case PORTAL_UTIL_SELECT:
1414
1415 /*
1416 * If we have not yet run the command, do so, storing its
1417 * results in the portal's tuplestore.
1418 */
1419 if (!portal->holdStore)
1420 FillPortalStore(portal, false /* isTopLevel */ );
1421
1422 /*
1423 * Now fetch desired portion of results.
1424 */
1425 result = DoPortalRunFetch(portal, fdirection, count, dest);
1426 break;
1427
1428 default:
1429 elog(ERROR, "unsupported portal strategy");
1430 result = 0; /* keep compiler quiet */
1431 break;
1432 }
1433 }
1434 PG_CATCH();
1435 {
1436 /* Uncaught error while executing portal: mark it dead */
1437 MarkPortalFailed(portal);
1438
1439 /* Restore global vars and propagate error */
1440 ActivePortal = saveActivePortal;
1441 CurrentResourceOwner = saveResourceOwner;
1442 PortalContext = savePortalContext;
1443
1444 PG_RE_THROW();
1445 }
1446 PG_END_TRY();
1447
1448 MemoryContextSwitchTo(oldContext);
1449
1450 /* Mark portal not active */
1451 portal->status = PORTAL_READY;
1452
1453 ActivePortal = saveActivePortal;
1454 CurrentResourceOwner = saveResourceOwner;
1455 PortalContext = savePortalContext;
1456
1457 return result;
1458}
1459
1460/*
1461 * DoPortalRunFetch
1462 * Guts of PortalRunFetch --- the portal context is already set up
1463 *
1464 * Here, count < 0 typically reverses the direction. Also, count == FETCH_ALL
1465 * is interpreted as "all rows". (cf FetchStmt.howMany)
1466 *
1467 * Returns number of rows processed (suitable for use in result tag)
1468 */
1469static uint64
1471 FetchDirection fdirection,
1472 long count,
1474{
1475 bool forward;
1476
1477 Assert(portal->strategy == PORTAL_ONE_SELECT ||
1478 portal->strategy == PORTAL_ONE_RETURNING ||
1479 portal->strategy == PORTAL_ONE_MOD_WITH ||
1480 portal->strategy == PORTAL_UTIL_SELECT);
1481
1482 /*
1483 * Note: we disallow backwards fetch (including re-fetch of current row)
1484 * for NO SCROLL cursors, but we interpret that very loosely: you can use
1485 * any of the FetchDirection options, so long as the end result is to move
1486 * forwards by at least one row. Currently it's sufficient to check for
1487 * NO SCROLL in DoPortalRewind() and in the forward == false path in
1488 * PortalRunSelect(); but someday we might prefer to account for that
1489 * restriction explicitly here.
1490 */
1491 switch (fdirection)
1492 {
1493 case FETCH_FORWARD:
1494 if (count < 0)
1495 {
1496 fdirection = FETCH_BACKWARD;
1497 count = -count;
1498 }
1499 /* fall out of switch to share code with FETCH_BACKWARD */
1500 break;
1501 case FETCH_BACKWARD:
1502 if (count < 0)
1503 {
1504 fdirection = FETCH_FORWARD;
1505 count = -count;
1506 }
1507 /* fall out of switch to share code with FETCH_FORWARD */
1508 break;
1509 case FETCH_ABSOLUTE:
1510 if (count > 0)
1511 {
1512 /*
1513 * Definition: Rewind to start, advance count-1 rows, return
1514 * next row (if any).
1515 *
1516 * In practice, if the goal is less than halfway back to the
1517 * start, it's better to scan from where we are.
1518 *
1519 * Also, if current portalPos is outside the range of "long",
1520 * do it the hard way to avoid possible overflow of the count
1521 * argument to PortalRunSelect. We must exclude exactly
1522 * LONG_MAX, as well, lest the count look like FETCH_ALL.
1523 *
1524 * In any case, we arrange to fetch the target row going
1525 * forwards.
1526 */
1527 if ((uint64) (count - 1) <= portal->portalPos / 2 ||
1528 portal->portalPos >= (uint64) LONG_MAX)
1529 {
1530 DoPortalRewind(portal);
1531 if (count > 1)
1532 PortalRunSelect(portal, true, count - 1,
1534 }
1535 else
1536 {
1537 long pos = (long) portal->portalPos;
1538
1539 if (portal->atEnd)
1540 pos++; /* need one extra fetch if off end */
1541 if (count <= pos)
1542 PortalRunSelect(portal, false, pos - count + 1,
1544 else if (count > pos + 1)
1545 PortalRunSelect(portal, true, count - pos - 1,
1547 }
1548 return PortalRunSelect(portal, true, 1L, dest);
1549 }
1550 else if (count < 0)
1551 {
1552 /*
1553 * Definition: Advance to end, back up abs(count)-1 rows,
1554 * return prior row (if any). We could optimize this if we
1555 * knew in advance where the end was, but typically we won't.
1556 * (Is it worth considering case where count > half of size of
1557 * query? We could rewind once we know the size ...)
1558 */
1559 PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
1560 if (count < -1)
1561 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1562 return PortalRunSelect(portal, false, 1L, dest);
1563 }
1564 else
1565 {
1566 /* count == 0 */
1567 /* Rewind to start, return zero rows */
1568 DoPortalRewind(portal);
1569 return PortalRunSelect(portal, true, 0L, dest);
1570 }
1571 break;
1572 case FETCH_RELATIVE:
1573 if (count > 0)
1574 {
1575 /*
1576 * Definition: advance count-1 rows, return next row (if any).
1577 */
1578 if (count > 1)
1579 PortalRunSelect(portal, true, count - 1, None_Receiver);
1580 return PortalRunSelect(portal, true, 1L, dest);
1581 }
1582 else if (count < 0)
1583 {
1584 /*
1585 * Definition: back up abs(count)-1 rows, return prior row (if
1586 * any).
1587 */
1588 if (count < -1)
1589 PortalRunSelect(portal, false, -count - 1, None_Receiver);
1590 return PortalRunSelect(portal, false, 1L, dest);
1591 }
1592 else
1593 {
1594 /* count == 0 */
1595 /* Same as FETCH FORWARD 0, so fall out of switch */
1596 fdirection = FETCH_FORWARD;
1597 }
1598 break;
1599 default:
1600 elog(ERROR, "bogus direction");
1601 break;
1602 }
1603
1604 /*
1605 * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
1606 * >= 0.
1607 */
1608 forward = (fdirection == FETCH_FORWARD);
1609
1610 /*
1611 * Zero count means to re-fetch the current row, if any (per SQL)
1612 */
1613 if (count == 0)
1614 {
1615 bool on_row;
1616
1617 /* Are we sitting on a row? */
1618 on_row = (!portal->atStart && !portal->atEnd);
1619
1620 if (dest->mydest == DestNone)
1621 {
1622 /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
1623 return on_row ? 1 : 0;
1624 }
1625 else
1626 {
1627 /*
1628 * If we are sitting on a row, back up one so we can re-fetch it.
1629 * If we are not sitting on a row, we still have to start up and
1630 * shut down the executor so that the destination is initialized
1631 * and shut down correctly; so keep going. To PortalRunSelect,
1632 * count == 0 means we will retrieve no row.
1633 */
1634 if (on_row)
1635 {
1636 PortalRunSelect(portal, false, 1L, None_Receiver);
1637 /* Set up to fetch one row forward */
1638 count = 1;
1639 forward = true;
1640 }
1641 }
1642 }
1643
1644 /*
1645 * Optimize MOVE BACKWARD ALL into a Rewind.
1646 */
1647 if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
1648 {
1649 uint64 result = portal->portalPos;
1650
1651 if (result > 0 && !portal->atEnd)
1652 result--;
1653 DoPortalRewind(portal);
1654 return result;
1655 }
1656
1657 return PortalRunSelect(portal, forward, count, dest);
1658}
1659
1660/*
1661 * DoPortalRewind - rewind a Portal to starting point
1662 */
1663static void
1665{
1666 QueryDesc *queryDesc;
1667
1668 /*
1669 * No work is needed if we've not advanced nor attempted to advance the
1670 * cursor (and we don't want to throw a NO SCROLL error in this case).
1671 */
1672 if (portal->atStart && !portal->atEnd)
1673 return;
1674
1675 /* Otherwise, cursor must allow scrolling */
1676 if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
1677 ereport(ERROR,
1678 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1679 errmsg("cursor can only scan forward"),
1680 errhint("Declare it with SCROLL option to enable backward scan.")));
1681
1682 /* Rewind holdStore, if we have one */
1683 if (portal->holdStore)
1684 {
1685 MemoryContext oldcontext;
1686
1687 oldcontext = MemoryContextSwitchTo(portal->holdContext);
1689 MemoryContextSwitchTo(oldcontext);
1690 }
1691
1692 /* Rewind executor, if active */
1693 queryDesc = portal->queryDesc;
1694 if (queryDesc)
1695 {
1696 PushActiveSnapshot(queryDesc->snapshot);
1697 ExecutorRewind(queryDesc);
1699 }
1700
1701 portal->atStart = true;
1702 portal->atEnd = false;
1703 portal->portalPos = 0;
1704}
1705
1706/*
1707 * PlannedStmtRequiresSnapshot - what it says on the tin
1708 */
1709bool
1711{
1712 Node *utilityStmt = pstmt->utilityStmt;
1713
1714 /* If it's not a utility statement, it definitely needs a snapshot */
1715 if (utilityStmt == NULL)
1716 return true;
1717
1718 /*
1719 * Most utility statements need a snapshot, and the default presumption
1720 * about new ones should be that they do too. Hence, enumerate those that
1721 * do not need one.
1722 *
1723 * Transaction control, LOCK, and SET must *not* set a snapshot, since
1724 * they need to be executable at the start of a transaction-snapshot-mode
1725 * transaction without freezing a snapshot. By extension we allow SHOW
1726 * not to set a snapshot. The other stmts listed are just efficiency
1727 * hacks. Beware of listing anything that can modify the database --- if,
1728 * say, it has to update an index with expressions that invoke
1729 * user-defined functions, then it had better have a snapshot.
1730 */
1731 if (IsA(utilityStmt, TransactionStmt) ||
1732 IsA(utilityStmt, LockStmt) ||
1733 IsA(utilityStmt, VariableSetStmt) ||
1734 IsA(utilityStmt, VariableShowStmt) ||
1735 IsA(utilityStmt, ConstraintsSetStmt) ||
1736 /* efficiency hacks from here down */
1737 IsA(utilityStmt, FetchStmt) ||
1738 IsA(utilityStmt, ListenStmt) ||
1739 IsA(utilityStmt, NotifyStmt) ||
1740 IsA(utilityStmt, UnlistenStmt) ||
1741 IsA(utilityStmt, CheckPointStmt))
1742 return false;
1743
1744 return true;
1745}
1746
1747/*
1748 * EnsurePortalSnapshotExists - recreate Portal-level snapshot, if needed
1749 *
1750 * Generally, we will have an active snapshot whenever we are executing
1751 * inside a Portal, unless the Portal's query is one of the utility
1752 * statements exempted from that rule (see PlannedStmtRequiresSnapshot).
1753 * However, procedures and DO blocks can commit or abort the transaction,
1754 * and thereby destroy all snapshots. This function can be called to
1755 * re-establish the Portal-level snapshot when none exists.
1756 */
1757void
1759{
1760 Portal portal;
1761
1762 /*
1763 * Nothing to do if a snapshot is set. (We take it on faith that the
1764 * outermost active snapshot belongs to some Portal; or if there is no
1765 * Portal, it's somebody else's responsibility to manage things.)
1766 */
1767 if (ActiveSnapshotSet())
1768 return;
1769
1770 /* Otherwise, we'd better have an active Portal */
1771 portal = ActivePortal;
1772 if (unlikely(portal == NULL))
1773 elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1774 Assert(portal->portalSnapshot == NULL);
1775
1776 /*
1777 * Create a new snapshot, make it active, and remember it in portal.
1778 * Because the portal now references the snapshot, we must tell snapmgr.c
1779 * that the snapshot belongs to the portal's transaction level, else we
1780 * risk portalSnapshot becoming a dangling pointer.
1781 */
1783 /* PushActiveSnapshotWithLevel might have copied the snapshot */
1785}
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:434
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:489
int16_t int16
Definition: c.h:537
uint64_t uint64
Definition: c.h:543
#define unlikely(x)
Definition: c.h:406
void InitializeQueryCompletion(QueryCompletion *qc)
Definition: cmdtag.c:40
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:37
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition: cmdtag.h:45
CommandTag
Definition: cmdtag.h:23
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
DestReceiver * None_Receiver
Definition: dest.c:96
@ DestTuplestore
Definition: dest.h:93
@ DestRemoteExecute
Definition: dest.h:90
@ DestNone
Definition: dest.h:87
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define PG_RE_THROW()
Definition: elog.h:405
#define DEBUG3
Definition: elog.h:28
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:466
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:406
void ExecutorRewind(QueryDesc *queryDesc)
Definition: execMain.c:536
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:122
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition: execMain.c:297
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1427
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1443
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:2139
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
#define EXEC_FLAG_BACKWARD
Definition: executor.h:69
#define EXEC_FLAG_REWIND
Definition: executor.h:68
bool log_executor_stats
Definition: guc_tables.c:522
Assert(PointerIsAligned(start, uint64))
#define stmt
Definition: indent_codes.h:59
int i
Definition: isn.c:77
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
MemoryContext TopTransactionContext
Definition: mcxt.c:171
void pfree(void *pointer)
Definition: mcxt.c:1594
void MemoryContextDeleteChildren(MemoryContext context)
Definition: mcxt.c:552
void * palloc(Size size)
Definition: mcxt.c:1365
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
MemoryContext PortalContext
Definition: mcxt.c:175
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define nodeTag(nodeptr)
Definition: nodes.h:139
@ CMD_MERGE
Definition: nodes.h:279
@ CMD_UTILITY
Definition: nodes.h:280
@ CMD_INSERT
Definition: nodes.h:277
@ CMD_DELETE
Definition: nodes.h:278
@ CMD_UPDATE
Definition: nodes.h:276
@ CMD_SELECT
Definition: nodes.h:275
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:3387
#define FETCH_ALL
Definition: parsenodes.h:3447
FetchDirection
Definition: parsenodes.h:3422
@ FETCH_RELATIVE
Definition: parsenodes.h:3428
@ FETCH_ABSOLUTE
Definition: parsenodes.h:3427
@ FETCH_FORWARD
Definition: parsenodes.h:3424
@ FETCH_BACKWARD
Definition: parsenodes.h:3425
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:3388
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_node(type, lc)
Definition: pg_list.h:176
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
#define plan(x)
Definition: pg_regress.c:161
@ PORTAL_READY
Definition: portal.h:107
@ PORTAL_DEFINED
Definition: portal.h:106
PortalStrategy
Definition: portal.h:90
@ PORTAL_ONE_RETURNING
Definition: portal.h:92
@ PORTAL_MULTI_QUERY
Definition: portal.h:95
@ PORTAL_ONE_SELECT
Definition: portal.h:91
@ PORTAL_ONE_MOD_WITH
Definition: portal.h:93
@ PORTAL_UTIL_SELECT
Definition: portal.h:94
#define PortalIsValid(p)
Definition: portal.h:211
void MarkPortalDone(Portal portal)
Definition: portalmem.c:414
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:442
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:151
void MarkPortalActive(Portal portal)
Definition: portalmem.c:395
Portal GetPortalByName(const char *name)
Definition: portalmem.c:130
void PortalCreateHoldStore(Portal portal)
Definition: portalmem.c:331
void ShowUsage(const char *title)
Definition: postgres.c:5068
void ResetUsage(void)
Definition: postgres.c:5061
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:106
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:68
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition: pquery.c:205
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
Definition: pquery.c:1180
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:990
bool PlannedStmtRequiresSnapshot(PlannedStmt *pstmt)
Definition: pquery.c:1710
void EnsurePortalSnapshotExists(void)
Definition: pquery.c:1758
Portal ActivePortal
Definition: pquery.c:36
void PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
Definition: pquery.c:619
void PortalStart(Portal portal, ParamListInfo params, int eflags, Snapshot snapshot)
Definition: pquery.c:429
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition: pquery.c:859
static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count, DestReceiver *dest)
Definition: pquery.c:1051
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:344
uint64 PortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1372
static void ProcessQuery(PlannedStmt *plan, const char *sourceText, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
Definition: pquery.c:137
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:322
bool PortalRun(Portal portal, long count, bool isTopLevel, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
Definition: pquery.c:680
static uint64 DoPortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1470
static void DoPortalRewind(Portal portal)
Definition: pquery.c:1664
static void PortalRunUtility(Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, QueryCompletion *qc)
Definition: pquery.c:1117
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:175
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
#define ScanDirectionIsForward(direction)
Definition: sdir.h:64
#define ScanDirectionIsNoMovement(direction)
Definition: sdir.h:57
ScanDirection
Definition: sdir.h:25
@ NoMovementScanDirection
Definition: sdir.h:27
@ BackwardScanDirection
Definition: sdir.h:26
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:271
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:864
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:680
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:742
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:810
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:822
void PopActiveSnapshot(void)
Definition: snapmgr.c:773
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:730
void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
Definition: snapmgr.c:694
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:798
#define InvalidSnapshot
Definition: snapshot.h:119
uint64 es_processed
Definition: execnodes.h:714
char * name
Definition: parsenodes.h:4184
bool ismove
Definition: parsenodes.h:3458
char * portalname
Definition: parsenodes.h:3456
Definition: pg_list.h:54
Definition: nodes.h:135
List * targetlist
Definition: plannodes.h:229
struct Plan * planTree
Definition: plannodes.h:101
bool hasModifyingCTE
Definition: plannodes.h:83
bool canSetTag
Definition: plannodes.h:86
bool hasReturning
Definition: plannodes.h:80
CmdType commandType
Definition: plannodes.h:68
Node * utilityStmt
Definition: plannodes.h:150
Snapshot portalSnapshot
Definition: portal.h:169
uint64 portalPos
Definition: portal.h:200
QueryDesc * queryDesc
Definition: portal.h:156
const char * sourceText
Definition: portal.h:136
bool atEnd
Definition: portal.h:199
bool atStart
Definition: portal.h:198
List * stmts
Definition: portal.h:139
ResourceOwner resowner
Definition: portal.h:121
int createLevel
Definition: portal.h:133
MemoryContext holdContext
Definition: portal.h:177
QueryEnvironment * queryEnv
Definition: portal.h:143
QueryCompletion qc
Definition: portal.h:138
MemoryContext portalContext
Definition: portal.h:120
int16 * formats
Definition: portal.h:161
ParamListInfo portalParams
Definition: portal.h:142
Snapshot holdSnapshot
Definition: portal.h:187
TupleDesc tupDesc
Definition: portal.h:159
CachedPlan * cplan
Definition: portal.h:140
Tuplestorestate * holdStore
Definition: portal.h:176
int cursorOptions
Definition: portal.h:147
PortalStrategy strategy
Definition: portal.h:146
PortalStatus status
Definition: portal.h:150
uint64 nprocessed
Definition: cmdtag.h:32
CommandTag commandTag
Definition: cmdtag.h:31
const char * sourceText
Definition: execdesc.h:38
ParamListInfo params
Definition: execdesc.h:42
DestReceiver * dest
Definition: execdesc.h:41
int instrument_options
Definition: execdesc.h:44
EState * estate
Definition: execdesc.h:48
CmdType operation
Definition: execdesc.h:36
Snapshot snapshot
Definition: execdesc.h:39
bool already_executed
Definition: execdesc.h:52
PlannedStmt * plannedstmt
Definition: execdesc.h:37
struct Instrumentation * totaltime
Definition: execdesc.h:55
QueryEnvironment * queryEnv
Definition: execdesc.h:43
TupleDesc tupDesc
Definition: execdesc.h:47
Snapshot crosscheck_snapshot
Definition: execdesc.h:40
PlanState * planstate
Definition: execdesc.h:49
List * returningList
Definition: parsenodes.h:214
CmdType commandType
Definition: parsenodes.h:121
Node * utilityStmt
Definition: parsenodes.h:141
List * targetList
Definition: parsenodes.h:198
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
CommandDest mydest
Definition: dest.h:128
void SetTuplestoreDestReceiverParams(DestReceiver *self, Tuplestorestate *tStore, MemoryContext tContext, bool detoast, TupleDesc target_tupdesc, const char *map_failure_msg)
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1130
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1285
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:457
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
Definition: utility.c:499
bool UtilityReturnsTuples(Node *parsetree)
Definition: utility.c:2020
TupleDesc UtilityTupleDescriptor(Node *parsetree)
Definition: utility.c:2076
@ PROCESS_UTILITY_TOPLEVEL
Definition: utility.h:22
@ PROCESS_UTILITY_QUERY
Definition: utility.h:23
void CommandCounterIncrement(void)
Definition: xact.c:1100