PostgreSQL Source Code  git master
pquery.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/xact.h"
#include "commands/prepare.h"
#include "executor/tstoreReceiver.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "tcop/pquery.h"
#include "tcop/utility.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
Include dependency graph for pquery.c:

Go to the source code of this file.

Functions

static void ProcessQuery (PlannedStmt *plan, const char *sourceText, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
 
static void FillPortalStore (Portal portal, bool isTopLevel)
 
static uint64 RunFromStore (Portal portal, ScanDirection direction, uint64 count, DestReceiver *dest)
 
static uint64 PortalRunSelect (Portal portal, bool forward, long count, DestReceiver *dest)
 
static void PortalRunUtility (Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, QueryCompletion *qc)
 
static void PortalRunMulti (Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
 
static uint64 DoPortalRunFetch (Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
 
static void DoPortalRewind (Portal portal)
 
QueryDescCreateQueryDesc (PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
 
void FreeQueryDesc (QueryDesc *qdesc)
 
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 (PlannedStmt *pstmt)
 
void EnsurePortalSnapshotExists (void)
 

Variables

Portal ActivePortal = NULL
 

Function Documentation

◆ ChoosePortalStrategy()

PortalStrategy ChoosePortalStrategy ( List stmts)

Definition at line 206 of file pquery.c.

References PlannedStmt::canSetTag, Query::canSetTag, CMD_SELECT, CMD_UTILITY, PlannedStmt::commandType, Query::commandType, elog, ERROR, PlannedStmt::hasModifyingCTE, Query::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(), PlannedStmt::utilityStmt, and Query::utilityStmt.

Referenced by PlanCacheComputeResultDesc(), and PortalStart().

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 NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
Node * utilityStmt
Definition: parsenodes.h:128
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
bool hasReturning
Definition: plannodes.h:50
Node * utilityStmt
Definition: plannodes.h:86
bool UtilityReturnsTuples(Node *parsetree)
Definition: utility.c:1987
List * returningList
Definition: parsenodes.h:156
bool canSetTag
Definition: plannodes.h:54
CmdType commandType
Definition: plannodes.h:46
CmdType commandType
Definition: parsenodes.h:120
#define lfirst(lc)
Definition: pg_list.h:169
bool hasModifyingCTE
Definition: plannodes.h:52
bool canSetTag
Definition: parsenodes.h:126
static int list_length(const List *l)
Definition: pg_list.h:149
#define nodeTag(nodeptr)
Definition: nodes.h:544
#define elog(elevel,...)
Definition: elog.h:232
bool hasModifyingCTE
Definition: parsenodes.h:139

◆ CreateQueryDesc()

QueryDesc* CreateQueryDesc ( PlannedStmt plannedstmt,
const char *  sourceText,
Snapshot  snapshot,
Snapshot  crosscheck_snapshot,
DestReceiver dest,
ParamListInfo  params,
QueryEnvironment queryEnv,
int  instrument_options 
)

Definition at line 67 of file pquery.c.

References QueryDesc::already_executed, PlannedStmt::commandType, QueryDesc::crosscheck_snapshot, QueryDesc::dest, generate_unaccent_rules::dest, QueryDesc::estate, QueryDesc::instrument_options, QueryDesc::operation, palloc(), QueryDesc::params, QueryDesc::plannedstmt, QueryDesc::planstate, QueryDesc::queryEnv, RegisterSnapshot(), QueryDesc::snapshot, QueryDesc::sourceText, QueryDesc::totaltime, and QueryDesc::tupDesc.

Referenced by _SPI_execute_plan(), BeginCopyTo(), ExecCreateTableAs(), ExecParallelGetQueryDesc(), execute_sql_string(), ExplainOnePlan(), PortalStart(), postquel_start(), ProcessQuery(), and refresh_matview_datafill().

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 }
EState * estate
Definition: execdesc.h:48
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
Snapshot snapshot
Definition: execdesc.h:39
int instrument_options
Definition: execdesc.h:44
QueryEnvironment * queryEnv
Definition: execdesc.h:43
PlanState * planstate
Definition: execdesc.h:49
ParamListInfo params
Definition: execdesc.h:42
TupleDesc tupDesc
Definition: execdesc.h:47
CmdType operation
Definition: execdesc.h:36
CmdType commandType
Definition: plannodes.h:46
bool already_executed
Definition: execdesc.h:52
Snapshot crosscheck_snapshot
Definition: execdesc.h:40
struct Instrumentation * totaltime
Definition: execdesc.h:55
const char * sourceText
Definition: execdesc.h:38
DestReceiver * dest
Definition: execdesc.h:41
void * palloc(Size size)
Definition: mcxt.c:1062
PlannedStmt * plannedstmt
Definition: execdesc.h:37

◆ DoPortalRewind()

static void DoPortalRewind ( Portal  portal)
static

Definition at line 1667 of file pquery.c.

References PortalData::atEnd, PortalData::atStart, ExecutorRewind(), PortalData::holdContext, PortalData::holdStore, MemoryContextSwitchTo(), PopActiveSnapshot(), PortalData::portalPos, PushActiveSnapshot(), PortalData::queryDesc, QueryDesc::snapshot, and tuplestore_rescan().

Referenced by DoPortalRunFetch().

1668 {
1669  QueryDesc *queryDesc;
1670 
1671  /* Rewind holdStore, if we have one */
1672  if (portal->holdStore)
1673  {
1674  MemoryContext oldcontext;
1675 
1676  oldcontext = MemoryContextSwitchTo(portal->holdContext);
1677  tuplestore_rescan(portal->holdStore);
1678  MemoryContextSwitchTo(oldcontext);
1679  }
1680 
1681  /* Rewind executor, if active */
1682  queryDesc = portal->queryDesc;
1683  if (queryDesc)
1684  {
1685  PushActiveSnapshot(queryDesc->snapshot);
1686  ExecutorRewind(queryDesc);
1688  }
1689 
1690  portal->atStart = true;
1691  portal->atEnd = false;
1692  portal->portalPos = 0;
1693 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
bool atEnd
Definition: portal.h:199
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext holdContext
Definition: portal.h:177
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
Snapshot snapshot
Definition: execdesc.h:39
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
QueryDesc * queryDesc
Definition: portal.h:156
void ExecutorRewind(QueryDesc *queryDesc)
Definition: execMain.c:525
bool atStart
Definition: portal.h:198
Tuplestorestate * holdStore
Definition: portal.h:176
uint64 portalPos
Definition: portal.h:200

◆ DoPortalRunFetch()

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

Definition at line 1482 of file pquery.c.

References Assert, PortalData::atEnd, PortalData::atStart, DestNone, DoPortalRewind(), elog, ERROR, FETCH_ABSOLUTE, FETCH_ALL, FETCH_BACKWARD, FETCH_FORWARD, FETCH_RELATIVE, _DestReceiver::mydest, None_Receiver, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_UTIL_SELECT, PortalData::portalPos, PortalRunSelect(), and PortalData::strategy.

Referenced by PortalRunFetch().

1486 {
1487  bool forward;
1488 
1489  Assert(portal->strategy == PORTAL_ONE_SELECT ||
1490  portal->strategy == PORTAL_ONE_RETURNING ||
1491  portal->strategy == PORTAL_ONE_MOD_WITH ||
1492  portal->strategy == PORTAL_UTIL_SELECT);
1493 
1494  switch (fdirection)
1495  {
1496  case FETCH_FORWARD:
1497  if (count < 0)
1498  {
1499  fdirection = FETCH_BACKWARD;
1500  count = -count;
1501  }
1502  /* fall out of switch to share code with FETCH_BACKWARD */
1503  break;
1504  case FETCH_BACKWARD:
1505  if (count < 0)
1506  {
1507  fdirection = FETCH_FORWARD;
1508  count = -count;
1509  }
1510  /* fall out of switch to share code with FETCH_FORWARD */
1511  break;
1512  case FETCH_ABSOLUTE:
1513  if (count > 0)
1514  {
1515  /*
1516  * Definition: Rewind to start, advance count-1 rows, return
1517  * next row (if any).
1518  *
1519  * In practice, if the goal is less than halfway back to the
1520  * start, it's better to scan from where we are.
1521  *
1522  * Also, if current portalPos is outside the range of "long",
1523  * do it the hard way to avoid possible overflow of the count
1524  * argument to PortalRunSelect. We must exclude exactly
1525  * LONG_MAX, as well, lest the count look like FETCH_ALL.
1526  *
1527  * In any case, we arrange to fetch the target row going
1528  * forwards.
1529  */
1530  if ((uint64) (count - 1) <= portal->portalPos / 2 ||
1531  portal->portalPos >= (uint64) LONG_MAX)
1532  {
1533  DoPortalRewind(portal);
1534  if (count > 1)
1535  PortalRunSelect(portal, true, count - 1,
1536  None_Receiver);
1537  }
1538  else
1539  {
1540  long pos = (long) portal->portalPos;
1541 
1542  if (portal->atEnd)
1543  pos++; /* need one extra fetch if off end */
1544  if (count <= pos)
1545  PortalRunSelect(portal, false, pos - count + 1,
1546  None_Receiver);
1547  else if (count > pos + 1)
1548  PortalRunSelect(portal, true, count - pos - 1,
1549  None_Receiver);
1550  }
1551  return PortalRunSelect(portal, true, 1L, dest);
1552  }
1553  else if (count < 0)
1554  {
1555  /*
1556  * Definition: Advance to end, back up abs(count)-1 rows,
1557  * return prior row (if any). We could optimize this if we
1558  * knew in advance where the end was, but typically we won't.
1559  * (Is it worth considering case where count > half of size of
1560  * query? We could rewind once we know the size ...)
1561  */
1562  PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
1563  if (count < -1)
1564  PortalRunSelect(portal, false, -count - 1, None_Receiver);
1565  return PortalRunSelect(portal, false, 1L, dest);
1566  }
1567  else
1568  {
1569  /* count == 0 */
1570  /* Rewind to start, return zero rows */
1571  DoPortalRewind(portal);
1572  return PortalRunSelect(portal, true, 0L, dest);
1573  }
1574  break;
1575  case FETCH_RELATIVE:
1576  if (count > 0)
1577  {
1578  /*
1579  * Definition: advance count-1 rows, return next row (if any).
1580  */
1581  if (count > 1)
1582  PortalRunSelect(portal, true, count - 1, None_Receiver);
1583  return PortalRunSelect(portal, true, 1L, dest);
1584  }
1585  else if (count < 0)
1586  {
1587  /*
1588  * Definition: back up abs(count)-1 rows, return prior row (if
1589  * any).
1590  */
1591  if (count < -1)
1592  PortalRunSelect(portal, false, -count - 1, None_Receiver);
1593  return PortalRunSelect(portal, false, 1L, dest);
1594  }
1595  else
1596  {
1597  /* count == 0 */
1598  /* Same as FETCH FORWARD 0, so fall out of switch */
1599  fdirection = FETCH_FORWARD;
1600  }
1601  break;
1602  default:
1603  elog(ERROR, "bogus direction");
1604  break;
1605  }
1606 
1607  /*
1608  * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
1609  * >= 0.
1610  */
1611  forward = (fdirection == FETCH_FORWARD);
1612 
1613  /*
1614  * Zero count means to re-fetch the current row, if any (per SQL)
1615  */
1616  if (count == 0)
1617  {
1618  bool on_row;
1619 
1620  /* Are we sitting on a row? */
1621  on_row = (!portal->atStart && !portal->atEnd);
1622 
1623  if (dest->mydest == DestNone)
1624  {
1625  /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
1626  return on_row ? 1 : 0;
1627  }
1628  else
1629  {
1630  /*
1631  * If we are sitting on a row, back up one so we can re-fetch it.
1632  * If we are not sitting on a row, we still have to start up and
1633  * shut down the executor so that the destination is initialized
1634  * and shut down correctly; so keep going. To PortalRunSelect,
1635  * count == 0 means we will retrieve no row.
1636  */
1637  if (on_row)
1638  {
1639  PortalRunSelect(portal, false, 1L, None_Receiver);
1640  /* Set up to fetch one row forward */
1641  count = 1;
1642  forward = true;
1643  }
1644  }
1645  }
1646 
1647  /*
1648  * Optimize MOVE BACKWARD ALL into a Rewind.
1649  */
1650  if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
1651  {
1652  uint64 result = portal->portalPos;
1653 
1654  if (result > 0 && !portal->atEnd)
1655  result--;
1656  DoPortalRewind(portal);
1657  return result;
1658  }
1659 
1660  return PortalRunSelect(portal, forward, count, dest);
1661 }
bool atEnd
Definition: portal.h:199
CommandDest mydest
Definition: dest.h:129
DestReceiver * None_Receiver
Definition: dest.c:96
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition: pquery.c:862
#define ERROR
Definition: elog.h:46
Definition: dest.h:89
PortalStrategy strategy
Definition: portal.h:145
#define Assert(condition)
Definition: c.h:804
bool atStart
Definition: portal.h:198
#define elog(elevel,...)
Definition: elog.h:232
static void DoPortalRewind(Portal portal)
Definition: pquery.c:1667
uint64 portalPos
Definition: portal.h:200
#define FETCH_ALL
Definition: parsenodes.h:2851

◆ EnsurePortalSnapshotExists()

void EnsurePortalSnapshotExists ( void  )

Definition at line 1747 of file pquery.c.

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

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

1748 {
1749  Portal portal;
1750 
1751  /*
1752  * Nothing to do if a snapshot is set. (We take it on faith that the
1753  * outermost active snapshot belongs to some Portal; or if there is no
1754  * Portal, it's somebody else's responsibility to manage things.)
1755  */
1756  if (ActiveSnapshotSet())
1757  return;
1758 
1759  /* Otherwise, we'd better have an active Portal */
1760  portal = ActivePortal;
1761  if (unlikely(portal == NULL))
1762  elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1763  Assert(portal->portalSnapshot == NULL);
1764 
1765  /* Create a new snapshot and make it active */
1767  /* PushActiveSnapshot might have copied the snapshot */
1768  portal->portalSnapshot = GetActiveSnapshot();
1769 }
Portal ActivePortal
Definition: pquery.c:35
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
#define ERROR
Definition: elog.h:46
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:798
#define Assert(condition)
Definition: c.h:804
Snapshot portalSnapshot
Definition: portal.h:169
#define elog(elevel,...)
Definition: elog.h:232
#define unlikely(x)
Definition: c.h:273

◆ FetchPortalTargetList()

List* FetchPortalTargetList ( Portal  portal)

Definition at line 323 of file pquery.c.

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

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

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 }
#define NIL
Definition: pg_list.h:65
Definition: nodes.h:539
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:151
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:345
PortalStrategy strategy
Definition: portal.h:145

◆ FetchStatementTargetList()

List* FetchStatementTargetList ( Node stmt)

Definition at line 345 of file pquery.c.

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

Referenced by CachedPlanGetTargetList(), and FetchPortalTargetList().

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 }
#define NIL
Definition: pg_list.h:65
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Portal GetPortalByName(const char *name)
Definition: portalmem.c:130
Node * utilityStmt
Definition: parsenodes.h:128
struct Plan * planTree
Definition: plannodes.h:64
List * targetList
Definition: parsenodes.h:150
bool hasReturning
Definition: plannodes.h:50
Node * utilityStmt
Definition: plannodes.h:86
List * returningList
Definition: parsenodes.h:156
char * portalname
Definition: parsenodes.h:2858
#define PortalIsValid(p)
Definition: portal.h:211
bool ismove
Definition: parsenodes.h:2859
CmdType commandType
Definition: plannodes.h:46
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:523
CmdType commandType
Definition: parsenodes.h:120
#define Assert(condition)
Definition: c.h:804
List * targetlist
Definition: plannodes.h:141
char * name
Definition: parsenodes.h:3552
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:323
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:468

◆ FillPortalStore()

static void FillPortalStore ( Portal  portal,
bool  isTopLevel 
)
static

Definition at line 995 of file pquery.c.

References QueryCompletion::commandTag, CopyQueryCompletion(), CreateDestReceiver(), DestTuplestore, elog, ERROR, PortalData::holdContext, PortalData::holdStore, InitializeQueryCompletion(), linitial_node, None_Receiver, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_UTIL_SELECT, PortalCreateHoldStore(), PortalRunMulti(), PortalRunUtility(), PortalData::qc, _DestReceiver::rDestroy, SetTuplestoreDestReceiverParams(), PortalData::stmts, and PortalData::strategy.

Referenced by PortalRun(), and PortalRunFetch().

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 }
void SetTuplestoreDestReceiverParams(DestReceiver *self, Tuplestorestate *tStore, MemoryContext tContext, bool detoast, TupleDesc target_tupdesc, const char *map_failure_msg)
MemoryContext holdContext
Definition: portal.h:177
List * stmts
Definition: portal.h:138
#define linitial_node(type, l)
Definition: pg_list.h:177
DestReceiver * None_Receiver
Definition: dest.c:96
static void PortalRunUtility(Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, QueryCompletion *qc)
Definition: pquery.c:1122
#define ERROR
Definition: elog.h:46
void InitializeQueryCompletion(QueryCompletion *qc)
Definition: cmdtag.c:38
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
PortalStrategy strategy
Definition: portal.h:145
CommandTag commandTag
Definition: cmdtag.h:30
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition: cmdtag.h:44
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:127
QueryCompletion qc
Definition: portal.h:137
void PortalCreateHoldStore(Portal portal)
Definition: portalmem.c:330
Tuplestorestate * holdStore
Definition: portal.h:176
#define elog(elevel,...)
Definition: elog.h:232
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
Definition: pquery.c:1179

◆ FreeQueryDesc()

void FreeQueryDesc ( QueryDesc qdesc)

Definition at line 105 of file pquery.c.

References Assert, QueryDesc::crosscheck_snapshot, QueryDesc::estate, pfree(), QueryDesc::snapshot, and UnregisterSnapshot().

Referenced by _SPI_execute_plan(), EndCopyTo(), ExecCreateTableAs(), execute_sql_string(), ExplainOnePlan(), ParallelQueryMain(), PersistHoldablePortal(), PortalCleanup(), postquel_end(), ProcessQuery(), and refresh_matview_datafill().

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 }
EState * estate
Definition: execdesc.h:48
Snapshot snapshot
Definition: execdesc.h:39
void pfree(void *pointer)
Definition: mcxt.c:1169
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:852
Snapshot crosscheck_snapshot
Definition: execdesc.h:40
#define Assert(condition)
Definition: c.h:804

◆ PlannedStmtRequiresSnapshot()

bool PlannedStmtRequiresSnapshot ( PlannedStmt pstmt)

Definition at line 1699 of file pquery.c.

References IsA, and PlannedStmt::utilityStmt.

Referenced by _SPI_execute_plan(), and PortalRunUtility().

1700 {
1701  Node *utilityStmt = pstmt->utilityStmt;
1702 
1703  /* If it's not a utility statement, it definitely needs a snapshot */
1704  if (utilityStmt == NULL)
1705  return true;
1706 
1707  /*
1708  * Most utility statements need a snapshot, and the default presumption
1709  * about new ones should be that they do too. Hence, enumerate those that
1710  * do not need one.
1711  *
1712  * Transaction control, LOCK, and SET must *not* set a snapshot, since
1713  * they need to be executable at the start of a transaction-snapshot-mode
1714  * transaction without freezing a snapshot. By extension we allow SHOW
1715  * not to set a snapshot. The other stmts listed are just efficiency
1716  * hacks. Beware of listing anything that can modify the database --- if,
1717  * say, it has to update an index with expressions that invoke
1718  * user-defined functions, then it had better have a snapshot.
1719  */
1720  if (IsA(utilityStmt, TransactionStmt) ||
1721  IsA(utilityStmt, LockStmt) ||
1722  IsA(utilityStmt, VariableSetStmt) ||
1723  IsA(utilityStmt, VariableShowStmt) ||
1724  IsA(utilityStmt, ConstraintsSetStmt) ||
1725  /* efficiency hacks from here down */
1726  IsA(utilityStmt, FetchStmt) ||
1727  IsA(utilityStmt, ListenStmt) ||
1728  IsA(utilityStmt, NotifyStmt) ||
1729  IsA(utilityStmt, UnlistenStmt) ||
1730  IsA(utilityStmt, CheckPointStmt))
1731  return false;
1732 
1733  return true;
1734 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Definition: nodes.h:539
Node * utilityStmt
Definition: plannodes.h:86

◆ PortalRun()

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

Definition at line 679 of file pquery.c.

References ActivePortal, Assert, AssertArg, PortalData::atEnd, QueryCompletion::commandTag, CopyQueryCompletion(), CurrentMemoryContext, CurrentResourceOwner, DEBUG3, 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().

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 }
bool atEnd
Definition: portal.h:199
MemoryContext TopTransactionContext
Definition: mcxt.c:53
#define DEBUG3
Definition: elog.h:23
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:148
void ShowUsage(const char *title)
Definition: postgres.c:4795
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void MarkPortalActive(Portal portal)
Definition: portalmem.c:394
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext portalContext
Definition: portal.h:120
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition: pquery.c:862
bool run_once
Definition: portal.h:147
MemoryContext PortalContext
Definition: mcxt.c:57
void ResetUsage(void)
Definition: postgres.c:4788
#define ERROR
Definition: elog.h:46
uint64 nprocessed
Definition: cmdtag.h:31
void InitializeQueryCompletion(QueryCompletion *qc)
Definition: cmdtag.c:38
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:995
void MarkPortalDone(Portal portal)
Definition: portalmem.c:413
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
PortalStrategy strategy
Definition: portal.h:145
#define AssertArg(condition)
Definition: c.h:806
#define PortalIsValid(p)
Definition: portal.h:211
CommandTag commandTag
Definition: cmdtag.h:30
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition: cmdtag.h:44
#define PG_CATCH()
Definition: elog.h:323
QueryCompletion qc
Definition: portal.h:137
#define Assert(condition)
Definition: c.h:804
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:441
PortalStatus status
Definition: portal.h:150
#define PG_RE_THROW()
Definition: elog.h:354
ResourceOwner resowner
Definition: portal.h:121
Tuplestorestate * holdStore
Definition: portal.h:176
#define elog(elevel,...)
Definition: elog.h:232
bool log_executor_stats
Definition: guc.c:579
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
#define PG_END_TRY()
Definition: elog.h:338

◆ PortalRunFetch()

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

Definition at line 1380 of file pquery.c.

References ActivePortal, Assert, AssertArg, CurrentResourceOwner, 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().

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 }
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void MarkPortalActive(Portal portal)
Definition: portalmem.c:394
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext portalContext
Definition: portal.h:120
static uint64 DoPortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1482
bool run_once
Definition: portal.h:147
MemoryContext PortalContext
Definition: mcxt.c:57
#define ERROR
Definition: elog.h:46
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:995
PortalStrategy strategy
Definition: portal.h:145
#define AssertArg(condition)
Definition: c.h:806
#define PortalIsValid(p)
Definition: portal.h:211
#define PG_CATCH()
Definition: elog.h:323
#define Assert(condition)
Definition: c.h:804
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:441
PortalStatus status
Definition: portal.h:150
#define PG_RE_THROW()
Definition: elog.h:354
ResourceOwner resowner
Definition: portal.h:121
Tuplestorestate * holdStore
Definition: portal.h:176
#define elog(elevel,...)
Definition: elog.h:232
#define PG_TRY()
Definition: elog.h:313
#define PG_END_TRY()
Definition: elog.h:338

◆ PortalRunMulti()

static void PortalRunMulti ( Portal  portal,
bool  isTopLevel,
bool  setHoldSnapshot,
DestReceiver dest,
DestReceiver altdest,
QueryCompletion qc 
)
static

Definition at line 1179 of file pquery.c.

References Assert, PlannedStmt::canSetTag, CHECK_FOR_INTERRUPTS, CommandCounterIncrement(), QueryCompletion::commandTag, CopyQueryCompletion(), CurrentMemoryContext, DestRemoteExecute, GetTransactionSnapshot(), PortalData::holdSnapshot, IsA, lfirst_node, lnext(), log_executor_stats, MemoryContextDeleteChildren(), _DestReceiver::mydest, NIL, None_Receiver, PopActiveSnapshot(), PortalData::portalContext, PortalData::portalParams, PortalRunUtility(), ProcessQuery(), PushCopiedSnapshot(), PortalData::qc, PortalData::queryEnv, RegisterSnapshot(), ResetUsage(), ShowUsage(), PortalData::sourceText, PortalData::stmts, UpdateActiveSnapshotCommandId(), and PlannedStmt::utilityStmt.

Referenced by FillPortalStore(), and PortalRun().

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 }
#define NIL
Definition: pg_list.h:65
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:728
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:322
void ShowUsage(const char *title)
Definition: postgres.c:4795
CommandDest mydest
Definition: dest.h:129
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
List * stmts
Definition: portal.h:138
DestReceiver * None_Receiver
Definition: dest.c:96
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
MemoryContext portalContext
Definition: portal.h:120
static void PortalRunUtility(Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, QueryCompletion *qc)
Definition: pquery.c:1122
void ResetUsage(void)
Definition: postgres.c:4788
ParamListInfo portalParams
Definition: portal.h:141
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:716
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Node * utilityStmt
Definition: plannodes.h:86
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void MemoryContextDeleteChildren(MemoryContext context)
Definition: mcxt.c:263
bool canSetTag
Definition: plannodes.h:54
void CommandCounterIncrement(void)
Definition: xact.c:1021
const char * sourceText
Definition: portal.h:135
CommandTag commandTag
Definition: cmdtag.h:30
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition: cmdtag.h:44
QueryCompletion qc
Definition: portal.h:137
#define Assert(condition)
Definition: c.h:804
Snapshot holdSnapshot
Definition: portal.h:187
QueryEnvironment * queryEnv
Definition: portal.h:142
bool log_executor_stats
Definition: guc.c:579
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

◆ PortalRunSelect()

static uint64 PortalRunSelect ( Portal  portal,
bool  forward,
long  count,
DestReceiver dest 
)
static

Definition at line 862 of file pquery.c.

References Assert, PortalData::atEnd, PortalData::atStart, BackwardScanDirection, CURSOR_OPT_NO_SCROLL, PortalData::cursorOptions, QueryDesc::dest, generate_unaccent_rules::dest, ereport, errcode(), errhint(), errmsg(), ERROR, EState::es_processed, QueryDesc::estate, ExecutorRun(), FETCH_ALL, ForwardScanDirection, PortalData::holdStore, NoMovementScanDirection, PopActiveSnapshot(), PortalData::portalPos, PushActiveSnapshot(), PortalData::queryDesc, PortalData::run_once, RunFromStore(), ScanDirectionIsNoMovement, and QueryDesc::snapshot.

Referenced by DoPortalRunFetch(), and PortalRun().

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 }
static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count, DestReceiver *dest)
Definition: pquery.c:1056
int errhint(const char *fmt,...)
Definition: elog.c:1156
bool atEnd
Definition: portal.h:199
EState * estate
Definition: execdesc.h:48
int errcode(int sqlerrcode)
Definition: elog.c:698
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
#define ScanDirectionIsNoMovement(direction)
Definition: sdir.h:48
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:2808
Snapshot snapshot
Definition: execdesc.h:39
bool run_once
Definition: portal.h:147
#define ERROR
Definition: elog.h:46
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
ScanDirection
Definition: sdir.h:22
QueryDesc * queryDesc
Definition: portal.h:156
#define ereport(elevel,...)
Definition: elog.h:157
#define Assert(condition)
Definition: c.h:804
uint64 es_processed
Definition: execnodes.h:604
DestReceiver * dest
Definition: execdesc.h:41
bool atStart
Definition: portal.h:198
Tuplestorestate * holdStore
Definition: portal.h:176
int errmsg(const char *fmt,...)
Definition: elog.c:909
int cursorOptions
Definition: portal.h:146
uint64 portalPos
Definition: portal.h:200
#define FETCH_ALL
Definition: parsenodes.h:2851

◆ PortalRunUtility()

static void PortalRunUtility ( Portal  portal,
PlannedStmt pstmt,
bool  isTopLevel,
bool  setHoldSnapshot,
DestReceiver dest,
QueryCompletion qc 
)
static

Definition at line 1122 of file pquery.c.

References ActiveSnapshotSet(), Assert, PortalData::cplan, GetActiveSnapshot(), GetTransactionSnapshot(), PortalData::holdSnapshot, MemoryContextSwitchTo(), PlannedStmtRequiresSnapshot(), PopActiveSnapshot(), PortalData::portalContext, PortalData::portalParams, PortalData::portalSnapshot, PROCESS_UTILITY_QUERY, PROCESS_UTILITY_TOPLEVEL, ProcessUtility(), PushActiveSnapshot(), PortalData::queryEnv, RegisterSnapshot(), and PortalData::sourceText.

Referenced by FillPortalStore(), and PortalRunMulti().

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 }
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:810
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
CachedPlan * cplan
Definition: portal.h:139
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
MemoryContext portalContext
Definition: portal.h:120
ParamListInfo portalParams
Definition: portal.h:141
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
Definition: utility.c:503
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:798
const char * sourceText
Definition: portal.h:135
#define Assert(condition)
Definition: c.h:804
Snapshot portalSnapshot
Definition: portal.h:169
Snapshot holdSnapshot
Definition: portal.h:187
QueryEnvironment * queryEnv
Definition: portal.h:142
bool PlannedStmtRequiresSnapshot(PlannedStmt *pstmt)
Definition: pquery.c:1699

◆ PortalSetResultFormat()

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

Definition at line 618 of file pquery.c.

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().

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 }
signed short int16
Definition: c.h:428
int errcode(int sqlerrcode)
Definition: elog.c:698
MemoryContext portalContext
Definition: portal.h:120
#define ERROR
Definition: elog.h:46
int16 * formats
Definition: portal.h:161
TupleDesc tupDesc
Definition: portal.h:159
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
int i

◆ PortalStart()

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

Definition at line 430 of file pquery.c.

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().

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 }
#define AssertState(condition)
Definition: c.h:807
bool atEnd
Definition: portal.h:199
TupleDesc ExecCleanTypeFromTL(List *targetList)
Definition: execTuples.c:1950
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
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
void PopActiveSnapshot(void)
Definition: snapmgr.c:759
List * stmts
Definition: portal.h:138
#define linitial_node(type, l)
Definition: pg_list.h:177
DestReceiver * None_Receiver
Definition: dest.c:96
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
MemoryContext portalContext
Definition: portal.h:120
struct Plan * planTree
Definition: plannodes.h:64
MemoryContext PortalContext
Definition: mcxt.c:57
ParamListInfo portalParams
Definition: portal.h:141
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:151
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
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
Node * utilityStmt
Definition: plannodes.h:86
PortalStrategy strategy
Definition: portal.h:145
#define EXEC_FLAG_REWIND
Definition: executor.h:57
#define AssertArg(condition)
Definition: c.h:806
QueryDesc * queryDesc
Definition: portal.h:156
TupleDesc tupDesc
Definition: execdesc.h:47
#define InvalidSnapshot
Definition: snapshot.h:123
#define PortalIsValid(p)
Definition: portal.h:211
CmdType commandType
Definition: plannodes.h:46
const char * sourceText
Definition: portal.h:135
TupleDesc tupDesc
Definition: portal.h:159
#define PG_CATCH()
Definition: elog.h:323
#define Assert(condition)
Definition: c.h:804
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:441
PortalStatus status
Definition: portal.h:150
#define PG_RE_THROW()
Definition: elog.h:354
QueryEnvironment * queryEnv
Definition: portal.h:142
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition: pquery.c:206
List * targetlist
Definition: plannodes.h:141
ResourceOwner resowner
Definition: portal.h:121
bool atStart
Definition: portal.h:198
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:2807
int cursorOptions
Definition: portal.h:146
#define PG_TRY()
Definition: elog.h:313
TupleDesc UtilityTupleDescriptor(Node *parsetree)
Definition: utility.c:2043
#define PG_END_TRY()
Definition: elog.h:338
uint64 portalPos
Definition: portal.h:200

◆ ProcessQuery()

static void ProcessQuery ( PlannedStmt plan,
const char *  sourceText,
ParamListInfo  params,
QueryEnvironment queryEnv,
DestReceiver dest,
QueryCompletion qc 
)
static

Definition at line 136 of file pquery.c.

References CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, CreateQueryDesc(), EState::es_processed, QueryDesc::estate, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, FreeQueryDesc(), GetActiveSnapshot(), InvalidSnapshot, QueryDesc::operation, and SetQueryCompletion().

Referenced by PortalRunMulti().

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 }
EState * estate
Definition: execdesc.h:48
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:786
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
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 ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
CmdType operation
Definition: execdesc.h:36
#define InvalidSnapshot
Definition: snapshot.h:123
static void SetQueryCompletion(QueryCompletion *qc, CommandTag commandTag, uint64 nprocessed)
Definition: cmdtag.h:36
uint64 es_processed
Definition: execnodes.h:604

◆ RunFromStore()

static uint64 RunFromStore ( Portal  portal,
ScanDirection  direction,
uint64  count,
DestReceiver dest 
)
static

Definition at line 1056 of file pquery.c.

References CMD_SELECT, ExecClearTuple(), ExecDropSingleTupleTableSlot(), PortalData::holdContext, PortalData::holdStore, MakeSingleTupleTableSlot(), MemoryContextSwitchTo(), _DestReceiver::receiveSlot, _DestReceiver::rShutdown, _DestReceiver::rStartup, ScanDirectionIsForward, ScanDirectionIsNoMovement, TTSOpsMinimalTuple, PortalData::tupDesc, and tuplestore_gettupleslot().

Referenced by PortalRunSelect().

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 }
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:119
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:425
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1238
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext holdContext
Definition: portal.h:177
#define ScanDirectionIsNoMovement(direction)
Definition: sdir.h:48
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: dest.h:122
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:1254
TupleDesc tupDesc
Definition: portal.h:159
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
void(* rShutdown)(DestReceiver *self)
Definition: dest.h:125
Tuplestorestate * holdStore
Definition: portal.h:176
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:85

Variable Documentation

◆ ActivePortal