PostgreSQL Source Code  git master
pquery.h File Reference
#include "nodes/parsenodes.h"
#include "utils/portal.h"
Include dependency graph for pquery.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

PortalStrategy ChoosePortalStrategy (List *stmts)
 
ListFetchPortalTargetList (Portal portal)
 
ListFetchStatementTargetList (Node *stmt)
 
void PortalStart (Portal portal, ParamListInfo params, int eflags, Snapshot snapshot)
 
void PortalSetResultFormat (Portal portal, int nFormats, int16 *formats)
 
bool PortalRun (Portal portal, long count, bool isTopLevel, bool run_once, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
 
uint64 PortalRunFetch (Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
 
bool PlannedStmtRequiresSnapshot (struct PlannedStmt *pstmt)
 
void EnsurePortalSnapshotExists (void)
 

Variables

PGDLLIMPORT Portal ActivePortal
 

Function Documentation

◆ ChoosePortalStrategy()

PortalStrategy ChoosePortalStrategy ( List stmts)

Definition at line 209 of file pquery.c.

210 {
211  int nSetTag;
212  ListCell *lc;
213 
214  /*
215  * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
216  * single-statement case, since there are no rewrite rules that can add
217  * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
218  * likewise allows only one top-level statement.
219  */
220  if (list_length(stmts) == 1)
221  {
222  Node *stmt = (Node *) linitial(stmts);
223 
224  if (IsA(stmt, Query))
225  {
226  Query *query = (Query *) stmt;
227 
228  if (query->canSetTag)
229  {
230  if (query->commandType == CMD_SELECT)
231  {
232  if (query->hasModifyingCTE)
233  return PORTAL_ONE_MOD_WITH;
234  else
235  return PORTAL_ONE_SELECT;
236  }
237  if (query->commandType == CMD_UTILITY)
238  {
239  if (UtilityReturnsTuples(query->utilityStmt))
240  return PORTAL_UTIL_SELECT;
241  /* it can't be ONE_RETURNING, so give up */
242  return PORTAL_MULTI_QUERY;
243  }
244  }
245  }
246  else if (IsA(stmt, PlannedStmt))
247  {
248  PlannedStmt *pstmt = (PlannedStmt *) stmt;
249 
250  if (pstmt->canSetTag)
251  {
252  if (pstmt->commandType == CMD_SELECT)
253  {
254  if (pstmt->hasModifyingCTE)
255  return PORTAL_ONE_MOD_WITH;
256  else
257  return PORTAL_ONE_SELECT;
258  }
259  if (pstmt->commandType == CMD_UTILITY)
260  {
261  if (UtilityReturnsTuples(pstmt->utilityStmt))
262  return PORTAL_UTIL_SELECT;
263  /* it can't be ONE_RETURNING, so give up */
264  return PORTAL_MULTI_QUERY;
265  }
266  }
267  }
268  else
269  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
270  }
271 
272  /*
273  * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
274  * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
275  * it has a RETURNING list.
276  */
277  nSetTag = 0;
278  foreach(lc, stmts)
279  {
280  Node *stmt = (Node *) lfirst(lc);
281 
282  if (IsA(stmt, Query))
283  {
284  Query *query = (Query *) stmt;
285 
286  if (query->canSetTag)
287  {
288  if (++nSetTag > 1)
289  return PORTAL_MULTI_QUERY; /* no need to look further */
290  if (query->commandType == CMD_UTILITY ||
291  query->returningList == NIL)
292  return PORTAL_MULTI_QUERY; /* no need to look further */
293  }
294  }
295  else if (IsA(stmt, PlannedStmt))
296  {
297  PlannedStmt *pstmt = (PlannedStmt *) stmt;
298 
299  if (pstmt->canSetTag)
300  {
301  if (++nSetTag > 1)
302  return PORTAL_MULTI_QUERY; /* no need to look further */
303  if (pstmt->commandType == CMD_UTILITY ||
304  !pstmt->hasReturning)
305  return PORTAL_MULTI_QUERY; /* no need to look further */
306  }
307  }
308  else
309  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
310  }
311  if (nSetTag == 1)
312  return PORTAL_ONE_RETURNING;
313 
314  /* Else, it's the general case... */
315  return PORTAL_MULTI_QUERY;
316 }
#define ERROR
Definition: elog.h:33
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
#define nodeTag(nodeptr)
Definition: nodes.h:579
@ CMD_UTILITY
Definition: nodes.h:727
@ CMD_SELECT
Definition: nodes.h:722
#define lfirst(lc)
Definition: pg_list.h:170
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define linitial(l)
Definition: pg_list.h:176
@ 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
Definition: nodes.h:575
bool hasModifyingCTE
Definition: plannodes.h:53
bool canSetTag
Definition: plannodes.h:55
bool hasReturning
Definition: plannodes.h:51
CmdType commandType
Definition: plannodes.h:47
Node * utilityStmt
Definition: plannodes.h:87
List * returningList
Definition: parsenodes.h:162
bool canSetTag
Definition: parsenodes.h:128
CmdType commandType
Definition: parsenodes.h:121
bool hasModifyingCTE
Definition: parsenodes.h:141
Node * utilityStmt
Definition: parsenodes.h:130
bool UtilityReturnsTuples(Node *parsetree)
Definition: utility.c:2018

References Query::canSetTag, PlannedStmt::canSetTag, CMD_SELECT, CMD_UTILITY, Query::commandType, PlannedStmt::commandType, elog(), ERROR, Query::hasModifyingCTE, PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, IsA, lfirst, linitial, list_length(), NIL, nodeTag, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_UTIL_SELECT, Query::returningList, UtilityReturnsTuples(), Query::utilityStmt, and PlannedStmt::utilityStmt.

Referenced by PlanCacheComputeResultDesc(), and PortalStart().

◆ EnsurePortalSnapshotExists()

void EnsurePortalSnapshotExists ( void  )

Definition at line 1780 of file pquery.c.

1781 {
1782  Portal portal;
1783 
1784  /*
1785  * Nothing to do if a snapshot is set. (We take it on faith that the
1786  * outermost active snapshot belongs to some Portal; or if there is no
1787  * Portal, it's somebody else's responsibility to manage things.)
1788  */
1789  if (ActiveSnapshotSet())
1790  return;
1791 
1792  /* Otherwise, we'd better have an active Portal */
1793  portal = ActivePortal;
1794  if (unlikely(portal == NULL))
1795  elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1796  Assert(portal->portalSnapshot == NULL);
1797 
1798  /*
1799  * Create a new snapshot, make it active, and remember it in portal.
1800  * Because the portal now references the snapshot, we must tell snapmgr.c
1801  * that the snapshot belongs to the portal's transaction level, else we
1802  * risk portalSnapshot becoming a dangling pointer.
1803  */
1805  /* PushActiveSnapshotWithLevel might have copied the snapshot */
1806  portal->portalSnapshot = GetActiveSnapshot();
1807 }
#define unlikely(x)
Definition: c.h:284
Assert(fmt[strlen(fmt) - 1] !='\n')
Portal ActivePortal
Definition: pquery.c:35
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:815
void PushActiveSnapshotWithLevel(Snapshot snap, int snap_level)
Definition: snapmgr.c:696
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:803
Snapshot portalSnapshot
Definition: portal.h:170
int createLevel
Definition: portal.h:133

References ActivePortal, ActiveSnapshotSet(), Assert(), PortalData::createLevel, elog(), ERROR, GetActiveSnapshot(), GetTransactionSnapshot(), PortalData::portalSnapshot, PushActiveSnapshotWithLevel(), and unlikely.

Referenced by _SPI_execute_plan(), exec_eval_simple_expr(), and ExecuteCallStmt().

◆ FetchPortalTargetList()

List* FetchPortalTargetList ( Portal  portal)

Definition at line 326 of file pquery.c.

327 {
328  /* no point in looking if we determined it doesn't return tuples */
329  if (portal->strategy == PORTAL_MULTI_QUERY)
330  return NIL;
331  /* get the primary statement and find out what it returns */
333 }
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:152
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:348
PortalStrategy strategy
Definition: portal.h:146

References FetchStatementTargetList(), NIL, PORTAL_MULTI_QUERY, PortalGetPrimaryStmt(), and PortalData::strategy.

Referenced by exec_describe_portal_message(), FetchStatementTargetList(), and printtup_startup().

◆ FetchStatementTargetList()

List* FetchStatementTargetList ( Node stmt)

Definition at line 348 of file pquery.c.

349 {
350  if (stmt == NULL)
351  return NIL;
352  if (IsA(stmt, Query))
353  {
354  Query *query = (Query *) stmt;
355 
356  if (query->commandType == CMD_UTILITY)
357  {
358  /* transfer attention to utility statement */
359  stmt = query->utilityStmt;
360  }
361  else
362  {
363  if (query->commandType == CMD_SELECT)
364  return query->targetList;
365  if (query->returningList)
366  return query->returningList;
367  return NIL;
368  }
369  }
370  if (IsA(stmt, PlannedStmt))
371  {
372  PlannedStmt *pstmt = (PlannedStmt *) stmt;
373 
374  if (pstmt->commandType == CMD_UTILITY)
375  {
376  /* transfer attention to utility statement */
377  stmt = pstmt->utilityStmt;
378  }
379  else
380  {
381  if (pstmt->commandType == CMD_SELECT)
382  return pstmt->planTree->targetlist;
383  if (pstmt->hasReturning)
384  return pstmt->planTree->targetlist;
385  return NIL;
386  }
387  }
388  if (IsA(stmt, FetchStmt))
389  {
390  FetchStmt *fstmt = (FetchStmt *) stmt;
391  Portal subportal;
392 
393  Assert(!fstmt->ismove);
394  subportal = GetPortalByName(fstmt->portalname);
395  Assert(PortalIsValid(subportal));
396  return FetchPortalTargetList(subportal);
397  }
398  if (IsA(stmt, ExecuteStmt))
399  {
400  ExecuteStmt *estmt = (ExecuteStmt *) stmt;
401  PreparedStatement *entry;
402 
403  entry = FetchPreparedStatement(estmt->name, true);
404  return FetchPreparedStatementTargetList(entry);
405  }
406  return NIL;
407 }
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:434
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:489
#define PortalIsValid(p)
Definition: portal.h:212
Portal GetPortalByName(const char *name)
Definition: portalmem.c:131
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:326
char * name
Definition: parsenodes.h:3921
bool ismove
Definition: parsenodes.h:3221
char * portalname
Definition: parsenodes.h:3220
List * targetlist
Definition: plannodes.h:142
struct Plan * planTree
Definition: plannodes.h:65
List * targetList
Definition: parsenodes.h:156

References Assert(), CMD_SELECT, CMD_UTILITY, Query::commandType, PlannedStmt::commandType, FetchPortalTargetList(), FetchPreparedStatement(), FetchPreparedStatementTargetList(), GetPortalByName(), PlannedStmt::hasReturning, IsA, FetchStmt::ismove, ExecuteStmt::name, NIL, PlannedStmt::planTree, PortalIsValid, FetchStmt::portalname, Query::returningList, Query::targetList, Plan::targetlist, Query::utilityStmt, and PlannedStmt::utilityStmt.

Referenced by CachedPlanGetTargetList(), and FetchPortalTargetList().

◆ PlannedStmtRequiresSnapshot()

bool PlannedStmtRequiresSnapshot ( struct PlannedStmt pstmt)

Definition at line 1732 of file pquery.c.

1733 {
1734  Node *utilityStmt = pstmt->utilityStmt;
1735 
1736  /* If it's not a utility statement, it definitely needs a snapshot */
1737  if (utilityStmt == NULL)
1738  return true;
1739 
1740  /*
1741  * Most utility statements need a snapshot, and the default presumption
1742  * about new ones should be that they do too. Hence, enumerate those that
1743  * do not need one.
1744  *
1745  * Transaction control, LOCK, and SET must *not* set a snapshot, since
1746  * they need to be executable at the start of a transaction-snapshot-mode
1747  * transaction without freezing a snapshot. By extension we allow SHOW
1748  * not to set a snapshot. The other stmts listed are just efficiency
1749  * hacks. Beware of listing anything that can modify the database --- if,
1750  * say, it has to update an index with expressions that invoke
1751  * user-defined functions, then it had better have a snapshot.
1752  */
1753  if (IsA(utilityStmt, TransactionStmt) ||
1754  IsA(utilityStmt, LockStmt) ||
1755  IsA(utilityStmt, VariableSetStmt) ||
1756  IsA(utilityStmt, VariableShowStmt) ||
1757  IsA(utilityStmt, ConstraintsSetStmt) ||
1758  /* efficiency hacks from here down */
1759  IsA(utilityStmt, FetchStmt) ||
1760  IsA(utilityStmt, ListenStmt) ||
1761  IsA(utilityStmt, NotifyStmt) ||
1762  IsA(utilityStmt, UnlistenStmt) ||
1763  IsA(utilityStmt, CheckPointStmt))
1764  return false;
1765 
1766  return true;
1767 }

References IsA, and PlannedStmt::utilityStmt.

Referenced by _SPI_execute_plan(), and PortalRunUtility().

◆ PortalRun()

bool PortalRun ( Portal  portal,
long  count,
bool  isTopLevel,
bool  run_once,
DestReceiver dest,
DestReceiver altdest,
QueryCompletion qc 
)

Definition at line 684 of file pquery.c.

687 {
688  bool result;
689  uint64 nprocessed;
690  ResourceOwner saveTopTransactionResourceOwner;
691  MemoryContext saveTopTransactionContext;
692  Portal saveActivePortal;
693  ResourceOwner saveResourceOwner;
694  MemoryContext savePortalContext;
695  MemoryContext saveMemoryContext;
696 
697  AssertArg(PortalIsValid(portal));
698 
699  TRACE_POSTGRESQL_QUERY_EXECUTE_START();
700 
701  /* Initialize empty completion data */
702  if (qc)
704 
706  {
707  elog(DEBUG3, "PortalRun");
708  /* PORTAL_MULTI_QUERY logs its own stats per query */
709  ResetUsage();
710  }
711 
712  /*
713  * Check for improper portal use, and mark portal active.
714  */
715  MarkPortalActive(portal);
716 
717  /* Set run_once flag. Shouldn't be clear if previously set. */
718  Assert(!portal->run_once || run_once);
719  portal->run_once = run_once;
720 
721  /*
722  * Set up global portal context pointers.
723  *
724  * We have to play a special game here to support utility commands like
725  * VACUUM and CLUSTER, which internally start and commit transactions.
726  * When we are called to execute such a command, CurrentResourceOwner will
727  * be pointing to the TopTransactionResourceOwner --- which will be
728  * destroyed and replaced in the course of the internal commit and
729  * restart. So we need to be prepared to restore it as pointing to the
730  * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
731  * internally starting whole new transactions is not good.)
732  * CurrentMemoryContext has a similar problem, but the other pointers we
733  * save here will be NULL or pointing to longer-lived objects.
734  */
735  saveTopTransactionResourceOwner = TopTransactionResourceOwner;
736  saveTopTransactionContext = TopTransactionContext;
737  saveActivePortal = ActivePortal;
738  saveResourceOwner = CurrentResourceOwner;
739  savePortalContext = PortalContext;
740  saveMemoryContext = CurrentMemoryContext;
741  PG_TRY();
742  {
743  ActivePortal = portal;
744  if (portal->resowner)
745  CurrentResourceOwner = portal->resowner;
746  PortalContext = portal->portalContext;
747 
749 
750  switch (portal->strategy)
751  {
752  case PORTAL_ONE_SELECT:
754  case PORTAL_ONE_MOD_WITH:
755  case PORTAL_UTIL_SELECT:
756 
757  /*
758  * If we have not yet run the command, do so, storing its
759  * results in the portal's tuplestore. But we don't do that
760  * for the PORTAL_ONE_SELECT case.
761  */
762  if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
763  FillPortalStore(portal, isTopLevel);
764 
765  /*
766  * Now fetch desired portion of results.
767  */
768  nprocessed = PortalRunSelect(portal, true, count, dest);
769 
770  /*
771  * If the portal result contains a command tag and the caller
772  * gave us a pointer to store it, copy it and update the
773  * rowcount.
774  */
775  if (qc && portal->qc.commandTag != CMDTAG_UNKNOWN)
776  {
777  CopyQueryCompletion(qc, &portal->qc);
778  qc->nprocessed = nprocessed;
779  }
780 
781  /* Mark portal not active */
782  portal->status = PORTAL_READY;
783 
784  /*
785  * Since it's a forward fetch, say DONE iff atEnd is now true.
786  */
787  result = portal->atEnd;
788  break;
789 
790  case PORTAL_MULTI_QUERY:
791  PortalRunMulti(portal, isTopLevel, false,
792  dest, altdest, qc);
793 
794  /* Prevent portal's commands from being re-executed */
795  MarkPortalDone(portal);
796 
797  /* Always complete at end of RunMulti */
798  result = true;
799  break;
800 
801  default:
802  elog(ERROR, "unrecognized portal strategy: %d",
803  (int) portal->strategy);
804  result = false; /* keep compiler quiet */
805  break;
806  }
807  }
808  PG_CATCH();
809  {
810  /* Uncaught error while executing portal: mark it dead */
811  MarkPortalFailed(portal);
812 
813  /* Restore global vars and propagate error */
814  if (saveMemoryContext == saveTopTransactionContext)
816  else
817  MemoryContextSwitchTo(saveMemoryContext);
818  ActivePortal = saveActivePortal;
819  if (saveResourceOwner == saveTopTransactionResourceOwner)
821  else
822  CurrentResourceOwner = saveResourceOwner;
823  PortalContext = savePortalContext;
824 
825  PG_RE_THROW();
826  }
827  PG_END_TRY();
828 
829  if (saveMemoryContext == saveTopTransactionContext)
831  else
832  MemoryContextSwitchTo(saveMemoryContext);
833  ActivePortal = saveActivePortal;
834  if (saveResourceOwner == saveTopTransactionResourceOwner)
836  else
837  CurrentResourceOwner = saveResourceOwner;
838  PortalContext = savePortalContext;
839 
841  ShowUsage("EXECUTOR STATISTICS");
842 
843  TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
844 
845  return result;
846 }
#define AssertArg(condition)
Definition: c.h:817
void InitializeQueryCompletion(QueryCompletion *qc)
Definition: cmdtag.c:38
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition: cmdtag.h:44
#define PG_RE_THROW()
Definition: elog.h:340
#define PG_END_TRY()
Definition: elog.h:324
#define DEBUG3
Definition: elog.h:22
#define PG_TRY()
Definition: elog.h:299
#define PG_CATCH()
Definition: elog.h:309
bool log_executor_stats
Definition: guc.c:619
MemoryContext TopTransactionContext
Definition: mcxt.c:53
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
MemoryContext PortalContext
Definition: mcxt.c:57
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
@ PORTAL_READY
Definition: portal.h:107
void MarkPortalDone(Portal portal)
Definition: portalmem.c:415
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:443
void MarkPortalActive(Portal portal)
Definition: portalmem.c:396
void ShowUsage(const char *title)
Definition: postgres.c:4866
void ResetUsage(void)
Definition: postgres.c:4859
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
Definition: pquery.c:1190
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:1000
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition: pquery.c:867
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
bool run_once
Definition: portal.h:148
bool atEnd
Definition: portal.h:200
ResourceOwner resowner
Definition: portal.h:121
QueryCompletion qc
Definition: portal.h:138
MemoryContext portalContext
Definition: portal.h:120
Tuplestorestate * holdStore
Definition: portal.h:177
PortalStatus status
Definition: portal.h:151
uint64 nprocessed
Definition: cmdtag.h:31
CommandTag commandTag
Definition: cmdtag.h:30

References ActivePortal, Assert(), AssertArg, PortalData::atEnd, QueryCompletion::commandTag, CopyQueryCompletion(), CurrentMemoryContext, CurrentResourceOwner, DEBUG3, generate_unaccent_rules::dest, elog(), ERROR, FillPortalStore(), PortalData::holdStore, InitializeQueryCompletion(), log_executor_stats, MarkPortalActive(), MarkPortalDone(), MarkPortalFailed(), MemoryContextSwitchTo(), QueryCompletion::nprocessed, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalIsValid, PortalRunMulti(), PortalRunSelect(), PortalData::qc, ResetUsage(), PortalData::resowner, PortalData::run_once, ShowUsage(), PortalData::status, PortalData::strategy, TopTransactionContext, and TopTransactionResourceOwner.

Referenced by exec_execute_message(), exec_simple_query(), and ExecuteQuery().

◆ PortalRunFetch()

uint64 PortalRunFetch ( Portal  portal,
FetchDirection  fdirection,
long  count,
DestReceiver dest 
)

Definition at line 1391 of file pquery.c.

1395 {
1396  uint64 result;
1397  Portal saveActivePortal;
1398  ResourceOwner saveResourceOwner;
1399  MemoryContext savePortalContext;
1400  MemoryContext oldContext;
1401 
1402  AssertArg(PortalIsValid(portal));
1403 
1404  /*
1405  * Check for improper portal use, and mark portal active.
1406  */
1407  MarkPortalActive(portal);
1408 
1409  /* If supporting FETCH, portal can't be run-once. */
1410  Assert(!portal->run_once);
1411 
1412  /*
1413  * Set up global portal context pointers.
1414  */
1415  saveActivePortal = ActivePortal;
1416  saveResourceOwner = CurrentResourceOwner;
1417  savePortalContext = PortalContext;
1418  PG_TRY();
1419  {
1420  ActivePortal = portal;
1421  if (portal->resowner)
1422  CurrentResourceOwner = portal->resowner;
1423  PortalContext = portal->portalContext;
1424 
1425  oldContext = MemoryContextSwitchTo(PortalContext);
1426 
1427  switch (portal->strategy)
1428  {
1429  case PORTAL_ONE_SELECT:
1430  result = DoPortalRunFetch(portal, fdirection, count, dest);
1431  break;
1432 
1433  case PORTAL_ONE_RETURNING:
1434  case PORTAL_ONE_MOD_WITH:
1435  case PORTAL_UTIL_SELECT:
1436 
1437  /*
1438  * If we have not yet run the command, do so, storing its
1439  * results in the portal's tuplestore.
1440  */
1441  if (!portal->holdStore)
1442  FillPortalStore(portal, false /* isTopLevel */ );
1443 
1444  /*
1445  * Now fetch desired portion of results.
1446  */
1447  result = DoPortalRunFetch(portal, fdirection, count, dest);
1448  break;
1449 
1450  default:
1451  elog(ERROR, "unsupported portal strategy");
1452  result = 0; /* keep compiler quiet */
1453  break;
1454  }
1455  }
1456  PG_CATCH();
1457  {
1458  /* Uncaught error while executing portal: mark it dead */
1459  MarkPortalFailed(portal);
1460 
1461  /* Restore global vars and propagate error */
1462  ActivePortal = saveActivePortal;
1463  CurrentResourceOwner = saveResourceOwner;
1464  PortalContext = savePortalContext;
1465 
1466  PG_RE_THROW();
1467  }
1468  PG_END_TRY();
1469 
1470  MemoryContextSwitchTo(oldContext);
1471 
1472  /* Mark portal not active */
1473  portal->status = PORTAL_READY;
1474 
1475  ActivePortal = saveActivePortal;
1476  CurrentResourceOwner = saveResourceOwner;
1477  PortalContext = savePortalContext;
1478 
1479  return result;
1480 }
static uint64 DoPortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1492

References ActivePortal, Assert(), AssertArg, CurrentResourceOwner, generate_unaccent_rules::dest, DoPortalRunFetch(), elog(), ERROR, FillPortalStore(), PortalData::holdStore, MarkPortalActive(), MarkPortalFailed(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalIsValid, PortalData::resowner, PortalData::run_once, PortalData::status, and PortalData::strategy.

Referenced by _SPI_cursor_operation(), and PerformPortalFetch().

◆ PortalSetResultFormat()

void PortalSetResultFormat ( Portal  portal,
int  nFormats,
int16 formats 
)

Definition at line 623 of file pquery.c.

624 {
625  int natts;
626  int i;
627 
628  /* Do nothing if portal won't return tuples */
629  if (portal->tupDesc == NULL)
630  return;
631  natts = portal->tupDesc->natts;
632  portal->formats = (int16 *)
634  natts * sizeof(int16));
635  if (nFormats > 1)
636  {
637  /* format specified for each column */
638  if (nFormats != natts)
639  ereport(ERROR,
640  (errcode(ERRCODE_PROTOCOL_VIOLATION),
641  errmsg("bind message has %d result formats but query has %d columns",
642  nFormats, natts)));
643  memcpy(portal->formats, formats, natts * sizeof(int16));
644  }
645  else if (nFormats > 0)
646  {
647  /* single format specified, use for all columns */
648  int16 format1 = formats[0];
649 
650  for (i = 0; i < natts; i++)
651  portal->formats[i] = format1;
652  }
653  else
654  {
655  /* use default format for all columns */
656  for (i = 0; i < natts; i++)
657  portal->formats[i] = 0;
658  }
659 }
signed short int16
Definition: c.h:439
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ereport(elevel,...)
Definition: elog.h:143
int i
Definition: isn.c:73
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
int16 * formats
Definition: portal.h:162
TupleDesc tupDesc
Definition: portal.h:160

References ereport, errcode(), errmsg(), ERROR, PortalData::formats, i, MemoryContextAlloc(), TupleDescData::natts, PortalData::portalContext, and PortalData::tupDesc.

Referenced by exec_bind_message(), and exec_simple_query().

◆ PortalStart()

void PortalStart ( Portal  portal,
ParamListInfo  params,
int  eflags,
Snapshot  snapshot 
)

Definition at line 433 of file pquery.c.

435 {
436  Portal saveActivePortal;
437  ResourceOwner saveResourceOwner;
438  MemoryContext savePortalContext;
439  MemoryContext oldContext;
440  QueryDesc *queryDesc;
441  int myeflags;
442 
443  AssertArg(PortalIsValid(portal));
444  AssertState(portal->status == PORTAL_DEFINED);
445 
446  /*
447  * Set up global portal context pointers.
448  */
449  saveActivePortal = ActivePortal;
450  saveResourceOwner = CurrentResourceOwner;
451  savePortalContext = PortalContext;
452  PG_TRY();
453  {
454  ActivePortal = portal;
455  if (portal->resowner)
456  CurrentResourceOwner = portal->resowner;
457  PortalContext = portal->portalContext;
458 
459  oldContext = MemoryContextSwitchTo(PortalContext);
460 
461  /* Must remember portal param list, if any */
462  portal->portalParams = params;
463 
464  /*
465  * Determine the portal execution strategy
466  */
467  portal->strategy = ChoosePortalStrategy(portal->stmts);
468 
469  /*
470  * Fire her up according to the strategy
471  */
472  switch (portal->strategy)
473  {
474  case PORTAL_ONE_SELECT:
475 
476  /* Must set snapshot before starting executor. */
477  if (snapshot)
478  PushActiveSnapshot(snapshot);
479  else
481 
482  /*
483  * We could remember the snapshot in portal->portalSnapshot,
484  * but presently there seems no need to, as this code path
485  * cannot be used for non-atomic execution. Hence there can't
486  * be any commit/abort that might destroy the snapshot. Since
487  * we don't do that, there's also no need to force a
488  * non-default nesting level for the snapshot.
489  */
490 
491  /*
492  * Create QueryDesc in portal's context; for the moment, set
493  * the destination to DestNone.
494  */
495  queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
496  portal->sourceText,
500  params,
501  portal->queryEnv,
502  0);
503 
504  /*
505  * If it's a scrollable cursor, executor needs to support
506  * REWIND and backwards scan, as well as whatever the caller
507  * might've asked for.
508  */
509  if (portal->cursorOptions & CURSOR_OPT_SCROLL)
510  myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
511  else
512  myeflags = eflags;
513 
514  /*
515  * Call ExecutorStart to prepare the plan for execution
516  */
517  ExecutorStart(queryDesc, myeflags);
518 
519  /*
520  * This tells PortalCleanup to shut down the executor
521  */
522  portal->queryDesc = queryDesc;
523 
524  /*
525  * Remember tuple descriptor (computed by ExecutorStart)
526  */
527  portal->tupDesc = queryDesc->tupDesc;
528 
529  /*
530  * Reset cursor position data to "start of query"
531  */
532  portal->atStart = true;
533  portal->atEnd = false; /* allow fetches */
534  portal->portalPos = 0;
535 
537  break;
538 
540  case PORTAL_ONE_MOD_WITH:
541 
542  /*
543  * We don't start the executor until we are told to run the
544  * portal. We do need to set up the result tupdesc.
545  */
546  {
547  PlannedStmt *pstmt;
548 
549  pstmt = PortalGetPrimaryStmt(portal);
550  portal->tupDesc =
552  }
553 
554  /*
555  * Reset cursor position data to "start of query"
556  */
557  portal->atStart = true;
558  portal->atEnd = false; /* allow fetches */
559  portal->portalPos = 0;
560  break;
561 
562  case PORTAL_UTIL_SELECT:
563 
564  /*
565  * We don't set snapshot here, because PortalRunUtility will
566  * take care of it if needed.
567  */
568  {
569  PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
570 
571  Assert(pstmt->commandType == CMD_UTILITY);
572  portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
573  }
574 
575  /*
576  * Reset cursor position data to "start of query"
577  */
578  portal->atStart = true;
579  portal->atEnd = false; /* allow fetches */
580  portal->portalPos = 0;
581  break;
582 
583  case PORTAL_MULTI_QUERY:
584  /* Need do nothing now */
585  portal->tupDesc = NULL;
586  break;
587  }
588  }
589  PG_CATCH();
590  {
591  /* Uncaught error while executing portal: mark it dead */
592  MarkPortalFailed(portal);
593 
594  /* Restore global vars and propagate error */
595  ActivePortal = saveActivePortal;
596  CurrentResourceOwner = saveResourceOwner;
597  PortalContext = savePortalContext;
598 
599  PG_RE_THROW();
600  }
601  PG_END_TRY();
602 
603  MemoryContextSwitchTo(oldContext);
604 
605  ActivePortal = saveActivePortal;
606  CurrentResourceOwner = saveResourceOwner;
607  PortalContext = savePortalContext;
608 
609  portal->status = PORTAL_READY;
610 }
#define AssertState(condition)
Definition: c.h:818
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:131
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:1950
#define EXEC_FLAG_BACKWARD
Definition: executor.h:58
#define EXEC_FLAG_REWIND
Definition: executor.h:57
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:3169
#define linitial_node(type, l)
Definition: pg_list.h:179
@ PORTAL_DEFINED
Definition: portal.h:106
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition: pquery.c:209
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
void PopActiveSnapshot(void)
Definition: snapmgr.c:776
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:682
#define InvalidSnapshot
Definition: snapshot.h:123
uint64 portalPos
Definition: portal.h:201
QueryDesc * queryDesc
Definition: portal.h:157
const char * sourceText
Definition: portal.h:136
bool atStart
Definition: portal.h:199
List * stmts
Definition: portal.h:139
QueryEnvironment * queryEnv
Definition: portal.h:143
ParamListInfo portalParams
Definition: portal.h:142
int cursorOptions
Definition: portal.h:147
TupleDesc tupDesc
Definition: execdesc.h:47
TupleDesc UtilityTupleDescriptor(Node *parsetree)
Definition: utility.c:2074

References ActivePortal, Assert(), AssertArg, AssertState, PortalData::atEnd, PortalData::atStart, ChoosePortalStrategy(), CMD_UTILITY, PlannedStmt::commandType, CreateQueryDesc(), CurrentResourceOwner, CURSOR_OPT_SCROLL, PortalData::cursorOptions, EXEC_FLAG_BACKWARD, EXEC_FLAG_REWIND, ExecCleanTypeFromTL(), ExecutorStart(), GetActiveSnapshot(), GetTransactionSnapshot(), InvalidSnapshot, linitial_node, MarkPortalFailed(), MemoryContextSwitchTo(), None_Receiver, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PlannedStmt::planTree, PopActiveSnapshot(), PORTAL_DEFINED, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalGetPrimaryStmt(), PortalIsValid, PortalData::portalParams, PortalData::portalPos, PushActiveSnapshot(), PortalData::queryDesc, PortalData::queryEnv, PortalData::resowner, PortalData::sourceText, PortalData::status, PortalData::stmts, PortalData::strategy, Plan::targetlist, QueryDesc::tupDesc, PortalData::tupDesc, PlannedStmt::utilityStmt, and UtilityTupleDescriptor().

Referenced by exec_bind_message(), exec_simple_query(), ExecuteQuery(), PerformCursorOpen(), and SPI_cursor_open_internal().

Variable Documentation

◆ ActivePortal