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