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 206 of file pquery.c.

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 }
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
#define nodeTag(nodeptr)
Definition: nodes.h:544
@ CMD_UTILITY
Definition: nodes.h:690
@ CMD_SELECT
Definition: nodes.h:686
#define lfirst(lc)
Definition: pg_list.h:169
static int list_length(const List *l)
Definition: pg_list.h:149
#define NIL
Definition: pg_list.h:65
#define linitial(l)
Definition: pg_list.h:174
@ 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:540
bool hasModifyingCTE
Definition: plannodes.h:52
bool canSetTag
Definition: plannodes.h:54
bool hasReturning
Definition: plannodes.h:50
CmdType commandType
Definition: plannodes.h:46
Node * utilityStmt
Definition: plannodes.h:86
List * returningList
Definition: parsenodes.h:155
bool canSetTag
Definition: parsenodes.h:125
CmdType commandType
Definition: parsenodes.h:119
bool hasModifyingCTE
Definition: parsenodes.h:138
Node * utilityStmt
Definition: parsenodes.h:127
bool UtilityReturnsTuples(Node *parsetree)
Definition: utility.c:2005

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 1777 of file pquery.c.

1778 {
1779  Portal portal;
1780 
1781  /*
1782  * Nothing to do if a snapshot is set. (We take it on faith that the
1783  * outermost active snapshot belongs to some Portal; or if there is no
1784  * Portal, it's somebody else's responsibility to manage things.)
1785  */
1786  if (ActiveSnapshotSet())
1787  return;
1788 
1789  /* Otherwise, we'd better have an active Portal */
1790  portal = ActivePortal;
1791  if (unlikely(portal == NULL))
1792  elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1793  Assert(portal->portalSnapshot == NULL);
1794 
1795  /*
1796  * Create a new snapshot, make it active, and remember it in portal.
1797  * Because the portal now references the snapshot, we must tell snapmgr.c
1798  * that the snapshot belongs to the portal's transaction level, else we
1799  * risk portalSnapshot becoming a dangling pointer.
1800  */
1802  /* PushActiveSnapshotWithLevel might have copied the snapshot */
1803  portal->portalSnapshot = GetActiveSnapshot();
1804 }
#define unlikely(x)
Definition: c.h:273
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:813
void PushActiveSnapshotWithLevel(Snapshot snap, int snap_level)
Definition: snapmgr.c:694
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:801
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 323 of file pquery.c.

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 }
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:151
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:345
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 345 of file pquery.c.

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 }
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:468
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:523
#define PortalIsValid(p)
Definition: portal.h:212
Portal GetPortalByName(const char *name)
Definition: portalmem.c:130
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:323
char * name
Definition: parsenodes.h:3566
bool ismove
Definition: parsenodes.h:2873
char * portalname
Definition: parsenodes.h:2872
List * targetlist
Definition: plannodes.h:141
struct Plan * planTree
Definition: plannodes.h:64
List * targetList
Definition: parsenodes.h:149

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 1729 of file pquery.c.

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

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 681 of file pquery.c.

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

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

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 620 of file pquery.c.

621 {
622  int natts;
623  int i;
624 
625  /* Do nothing if portal won't return tuples */
626  if (portal->tupDesc == NULL)
627  return;
628  natts = portal->tupDesc->natts;
629  portal->formats = (int16 *)
631  natts * sizeof(int16));
632  if (nFormats > 1)
633  {
634  /* format specified for each column */
635  if (nFormats != natts)
636  ereport(ERROR,
637  (errcode(ERRCODE_PROTOCOL_VIOLATION),
638  errmsg("bind message has %d result formats but query has %d columns",
639  nFormats, natts)));
640  memcpy(portal->formats, formats, natts * sizeof(int16));
641  }
642  else if (nFormats > 0)
643  {
644  /* single format specified, use for all columns */
645  int16 format1 = formats[0];
646 
647  for (i = 0; i < natts; i++)
648  portal->formats[i] = format1;
649  }
650  else
651  {
652  /* use default format for all columns */
653  for (i = 0; i < natts; i++)
654  portal->formats[i] = 0;
655  }
656 }
signed short int16
Definition: c.h:428
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 430 of file pquery.c.

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. Since
484  * we don't do that, there's also no need to force a
485  * non-default nesting level for the snapshot.
486  */
487 
488  /*
489  * Create QueryDesc in portal's context; for the moment, set
490  * the destination to DestNone.
491  */
492  queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
493  portal->sourceText,
497  params,
498  portal->queryEnv,
499  0);
500 
501  /*
502  * If it's a scrollable cursor, executor needs to support
503  * REWIND and backwards scan, as well as whatever the caller
504  * might've asked for.
505  */
506  if (portal->cursorOptions & CURSOR_OPT_SCROLL)
507  myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
508  else
509  myeflags = eflags;
510 
511  /*
512  * Call ExecutorStart to prepare the plan for execution
513  */
514  ExecutorStart(queryDesc, myeflags);
515 
516  /*
517  * This tells PortalCleanup to shut down the executor
518  */
519  portal->queryDesc = queryDesc;
520 
521  /*
522  * Remember tuple descriptor (computed by ExecutorStart)
523  */
524  portal->tupDesc = queryDesc->tupDesc;
525 
526  /*
527  * Reset cursor position data to "start of query"
528  */
529  portal->atStart = true;
530  portal->atEnd = false; /* allow fetches */
531  portal->portalPos = 0;
532 
534  break;
535 
537  case PORTAL_ONE_MOD_WITH:
538 
539  /*
540  * We don't start the executor until we are told to run the
541  * portal. We do need to set up the result tupdesc.
542  */
543  {
544  PlannedStmt *pstmt;
545 
546  pstmt = PortalGetPrimaryStmt(portal);
547  portal->tupDesc =
549  }
550 
551  /*
552  * Reset cursor position data to "start of query"
553  */
554  portal->atStart = true;
555  portal->atEnd = false; /* allow fetches */
556  portal->portalPos = 0;
557  break;
558 
559  case PORTAL_UTIL_SELECT:
560 
561  /*
562  * We don't set snapshot here, because PortalRunUtility will
563  * take care of it if needed.
564  */
565  {
566  PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
567 
568  Assert(pstmt->commandType == CMD_UTILITY);
569  portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
570  }
571 
572  /*
573  * Reset cursor position data to "start of query"
574  */
575  portal->atStart = true;
576  portal->atEnd = false; /* allow fetches */
577  portal->portalPos = 0;
578  break;
579 
580  case PORTAL_MULTI_QUERY:
581  /* Need do nothing now */
582  portal->tupDesc = NULL;
583  break;
584  }
585  }
586  PG_CATCH();
587  {
588  /* Uncaught error while executing portal: mark it dead */
589  MarkPortalFailed(portal);
590 
591  /* Restore global vars and propagate error */
592  ActivePortal = saveActivePortal;
593  CurrentResourceOwner = saveResourceOwner;
594  PortalContext = savePortalContext;
595 
596  PG_RE_THROW();
597  }
598  PG_END_TRY();
599 
600  MemoryContextSwitchTo(oldContext);
601 
602  ActivePortal = saveActivePortal;
603  CurrentResourceOwner = saveResourceOwner;
604  PortalContext = savePortalContext;
605 
606  portal->status = PORTAL_READY;
607 }
#define AssertState(condition)
Definition: c.h:807
DestReceiver * None_Receiver
Definition: dest.c:96
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
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:2821
#define linitial_node(type, l)
Definition: pg_list.h:177
@ PORTAL_DEFINED
Definition: portal.h:106
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition: pquery.c:206
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:774
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
#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:2061

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