PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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, char *completionTag)
 
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, char *completionTag)
 
static void PortalRunMulti (Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, char *completionTag)
 
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, char *completionTag)
 
uint64 PortalRunFetch (Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
 

Variables

Portal ActivePortal = NULL
 

Function Documentation

PortalStrategy ChoosePortalStrategy ( List stmts)

Definition at line 221 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().

222 {
223  int nSetTag;
224  ListCell *lc;
225 
226  /*
227  * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
228  * single-statement case, since there are no rewrite rules that can add
229  * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
230  * likewise allows only one top-level statement.
231  */
232  if (list_length(stmts) == 1)
233  {
234  Node *stmt = (Node *) linitial(stmts);
235 
236  if (IsA(stmt, Query))
237  {
238  Query *query = (Query *) stmt;
239 
240  if (query->canSetTag)
241  {
242  if (query->commandType == CMD_SELECT)
243  {
244  if (query->hasModifyingCTE)
245  return PORTAL_ONE_MOD_WITH;
246  else
247  return PORTAL_ONE_SELECT;
248  }
249  if (query->commandType == CMD_UTILITY)
250  {
251  if (UtilityReturnsTuples(query->utilityStmt))
252  return PORTAL_UTIL_SELECT;
253  /* it can't be ONE_RETURNING, so give up */
254  return PORTAL_MULTI_QUERY;
255  }
256  }
257  }
258  else if (IsA(stmt, PlannedStmt))
259  {
260  PlannedStmt *pstmt = (PlannedStmt *) stmt;
261 
262  if (pstmt->canSetTag)
263  {
264  if (pstmt->commandType == CMD_SELECT)
265  {
266  if (pstmt->hasModifyingCTE)
267  return PORTAL_ONE_MOD_WITH;
268  else
269  return PORTAL_ONE_SELECT;
270  }
271  if (pstmt->commandType == CMD_UTILITY)
272  {
273  if (UtilityReturnsTuples(pstmt->utilityStmt))
274  return PORTAL_UTIL_SELECT;
275  /* it can't be ONE_RETURNING, so give up */
276  return PORTAL_MULTI_QUERY;
277  }
278  }
279  }
280  else
281  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
282  }
283 
284  /*
285  * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
286  * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
287  * it has a RETURNING list.
288  */
289  nSetTag = 0;
290  foreach(lc, stmts)
291  {
292  Node *stmt = (Node *) lfirst(lc);
293 
294  if (IsA(stmt, Query))
295  {
296  Query *query = (Query *) stmt;
297 
298  if (query->canSetTag)
299  {
300  if (++nSetTag > 1)
301  return PORTAL_MULTI_QUERY; /* no need to look further */
302  if (query->commandType == CMD_UTILITY ||
303  query->returningList == NIL)
304  return PORTAL_MULTI_QUERY; /* no need to look further */
305  }
306  }
307  else if (IsA(stmt, PlannedStmt))
308  {
309  PlannedStmt *pstmt = (PlannedStmt *) stmt;
310 
311  if (pstmt->canSetTag)
312  {
313  if (++nSetTag > 1)
314  return PORTAL_MULTI_QUERY; /* no need to look further */
315  if (pstmt->commandType == CMD_UTILITY ||
316  !pstmt->hasReturning)
317  return PORTAL_MULTI_QUERY; /* no need to look further */
318  }
319  }
320  else
321  elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
322  }
323  if (nSetTag == 1)
324  return PORTAL_ONE_RETURNING;
325 
326  /* Else, it's the general case... */
327  return PORTAL_MULTI_QUERY;
328 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Definition: nodes.h:510
Node * utilityStmt
Definition: parsenodes.h:118
#define linitial(l)
Definition: pg_list.h:111
#define ERROR
Definition: elog.h:43
bool hasReturning
Definition: plannodes.h:49
Node * utilityStmt
Definition: plannodes.h:94
bool UtilityReturnsTuples(Node *parsetree)
Definition: utility.c:1715
List * returningList
Definition: parsenodes.h:144
bool canSetTag
Definition: plannodes.h:53
CmdType commandType
Definition: plannodes.h:45
CmdType commandType
Definition: parsenodes.h:110
#define lfirst(lc)
Definition: pg_list.h:106
bool hasModifyingCTE
Definition: plannodes.h:51
bool canSetTag
Definition: parsenodes.h:116
static int list_length(const List *l)
Definition: pg_list.h:89
#define nodeTag(nodeptr)
Definition: nodes.h:515
bool hasModifyingCTE
Definition: parsenodes.h:129
#define elog
Definition: elog.h:219
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, 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(), BeginCopy(), 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:863
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:45
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:848
PlannedStmt * plannedstmt
Definition: execdesc.h:37
static void DoPortalRewind ( Portal  portal)
static

Definition at line 1682 of file pquery.c.

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

Referenced by DoPortalRunFetch().

1683 {
1684  QueryDesc *queryDesc;
1685 
1686  /* Rewind holdStore, if we have one */
1687  if (portal->holdStore)
1688  {
1689  MemoryContext oldcontext;
1690 
1691  oldcontext = MemoryContextSwitchTo(portal->holdContext);
1692  tuplestore_rescan(portal->holdStore);
1693  MemoryContextSwitchTo(oldcontext);
1694  }
1695 
1696  /* Rewind executor, if active */
1697  queryDesc = PortalGetQueryDesc(portal);
1698  if (queryDesc)
1699  {
1700  PushActiveSnapshot(queryDesc->snapshot);
1701  ExecutorRewind(queryDesc);
1703  }
1704 
1705  portal->atStart = true;
1706  portal->atEnd = false;
1707  portal->portalPos = 0;
1708 }
void tuplestore_rescan(Tuplestorestate *state)
Definition: tuplestore.c:1233
bool atEnd
Definition: portal.h:187
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext holdContext
Definition: portal.h:165
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
Snapshot snapshot
Definition: execdesc.h:39
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
void ExecutorRewind(QueryDesc *queryDesc)
Definition: execMain.c:527
#define PortalGetQueryDesc(portal)
Definition: portal.h:204
bool atStart
Definition: portal.h:186
Tuplestorestate * holdStore
Definition: portal.h:164
uint64 portalPos
Definition: portal.h:188
static uint64 DoPortalRunFetch ( Portal  portal,
FetchDirection  fdirection,
long  count,
DestReceiver dest 
)
static

Definition at line 1497 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().

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

Definition at line 338 of file pquery.c.

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

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

339 {
340  /* no point in looking if we determined it doesn't return tuples */
341  if (portal->strategy == PORTAL_MULTI_QUERY)
342  return NIL;
343  /* get the primary statement and find out what it returns */
345 }
#define NIL
Definition: pg_list.h:69
Definition: nodes.h:510
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:150
List * FetchStatementTargetList(Node *stmt)
Definition: pquery.c:360
PortalStrategy strategy
Definition: portal.h:143
List* FetchStatementTargetList ( Node stmt)

Definition at line 360 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, Query::targetList, Plan::targetlist, PlannedStmt::utilityStmt, and Query::utilityStmt.

Referenced by CachedPlanGetTargetList(), and FetchPortalTargetList().

361 {
362  if (stmt == NULL)
363  return NIL;
364  if (IsA(stmt, Query))
365  {
366  Query *query = (Query *) stmt;
367 
368  if (query->commandType == CMD_UTILITY)
369  {
370  /* transfer attention to utility statement */
371  stmt = query->utilityStmt;
372  }
373  else
374  {
375  if (query->commandType == CMD_SELECT)
376  return query->targetList;
377  if (query->returningList)
378  return query->returningList;
379  return NIL;
380  }
381  }
382  if (IsA(stmt, PlannedStmt))
383  {
384  PlannedStmt *pstmt = (PlannedStmt *) stmt;
385 
386  if (pstmt->commandType == CMD_UTILITY)
387  {
388  /* transfer attention to utility statement */
389  stmt = pstmt->utilityStmt;
390  }
391  else
392  {
393  if (pstmt->commandType == CMD_SELECT)
394  return pstmt->planTree->targetlist;
395  if (pstmt->hasReturning)
396  return pstmt->planTree->targetlist;
397  return NIL;
398  }
399  }
400  if (IsA(stmt, FetchStmt))
401  {
402  FetchStmt *fstmt = (FetchStmt *) stmt;
403  Portal subportal;
404 
405  Assert(!fstmt->ismove);
406  subportal = GetPortalByName(fstmt->portalname);
407  Assert(PortalIsValid(subportal));
408  return FetchPortalTargetList(subportal);
409  }
410  if (IsA(stmt, ExecuteStmt))
411  {
412  ExecuteStmt *estmt = (ExecuteStmt *) stmt;
413  PreparedStatement *entry;
414 
415  entry = FetchPreparedStatement(estmt->name, true);
416  return FetchPreparedStatementTargetList(entry);
417  }
418  return NIL;
419 }
#define NIL
Definition: pg_list.h:69
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Portal GetPortalByName(const char *name)
Definition: portalmem.c:129
Node * utilityStmt
Definition: parsenodes.h:118
struct Plan * planTree
Definition: plannodes.h:61
List * targetList
Definition: parsenodes.h:138
bool hasReturning
Definition: plannodes.h:49
Node * utilityStmt
Definition: plannodes.h:94
List * returningList
Definition: parsenodes.h:144
char * portalname
Definition: parsenodes.h:2681
#define PortalIsValid(p)
Definition: portal.h:199
bool ismove
Definition: parsenodes.h:2682
CmdType commandType
Definition: plannodes.h:45
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:549
CmdType commandType
Definition: parsenodes.h:110
#define Assert(condition)
Definition: c.h:681
List * targetlist
Definition: plannodes.h:144
char * name
Definition: parsenodes.h:3314
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:338
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:494
static void FillPortalStore ( Portal  portal,
bool  isTopLevel 
)
static

Definition at line 1008 of file pquery.c.

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

Referenced by PortalRun(), and PortalRunFetch().

1009 {
1010  DestReceiver *treceiver;
1011  char completionTag[COMPLETION_TAG_BUFSIZE];
1012 
1013  PortalCreateHoldStore(portal);
1014  treceiver = CreateDestReceiver(DestTuplestore);
1016  portal->holdStore,
1017  portal->holdContext,
1018  false);
1019 
1020  completionTag[0] = '\0';
1021 
1022  switch (portal->strategy)
1023  {
1024  case PORTAL_ONE_RETURNING:
1025  case PORTAL_ONE_MOD_WITH:
1026 
1027  /*
1028  * Run the portal to completion just as for the default
1029  * MULTI_QUERY case, but send the primary query's output to the
1030  * tuplestore. Auxiliary query outputs are discarded. Set the
1031  * portal's holdSnapshot to the snapshot used (or a copy of it).
1032  */
1033  PortalRunMulti(portal, isTopLevel, true,
1034  treceiver, None_Receiver, completionTag);
1035  break;
1036 
1037  case PORTAL_UTIL_SELECT:
1038  PortalRunUtility(portal, linitial_node(PlannedStmt, portal->stmts),
1039  isTopLevel, true, treceiver, completionTag);
1040  break;
1041 
1042  default:
1043  elog(ERROR, "unsupported portal strategy: %d",
1044  (int) portal->strategy);
1045  break;
1046  }
1047 
1048  /* Override default completion tag with actual command result */
1049  if (completionTag[0] != '\0')
1050  portal->commandTag = pstrdup(completionTag);
1051 
1052  treceiver->rDestroy(treceiver);
1053 }
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
char * pstrdup(const char *in)
Definition: mcxt.c:1076
void SetTuplestoreDestReceiverParams(DestReceiver *self, Tuplestorestate *tStore, MemoryContext tContext, bool detoast)
MemoryContext holdContext
Definition: portal.h:165
List * stmts
Definition: portal.h:136
#define linitial_node(type, l)
Definition: pg_list.h:114
DestReceiver * None_Receiver
Definition: dest.c:91
#define ERROR
Definition: elog.h:43
const char * commandTag
Definition: portal.h:135
static void PortalRunUtility(Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, char *completionTag)
Definition: pquery.c:1134
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:109
PortalStrategy strategy
Definition: portal.h:143
#define COMPLETION_TAG_BUFSIZE
Definition: dest.h:74
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, char *completionTag)
Definition: pquery.c:1205
void PortalCreateHoldStore(Portal portal)
Definition: portalmem.c:324
Tuplestorestate * holdStore
Definition: portal.h:164
#define elog
Definition: elog.h:219
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:949
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:905
Snapshot crosscheck_snapshot
Definition: execdesc.h:40
#define Assert(condition)
Definition: c.h:681
bool PortalRun ( Portal  portal,
long  count,
bool  isTopLevel,
bool  run_once,
DestReceiver dest,
DestReceiver altdest,
char *  completionTag 
)

Definition at line 689 of file pquery.c.

References ActivePortal, Assert, AssertArg, PortalData::atEnd, PortalData::commandTag, COMPLETION_TAG_BUFSIZE, CurrentMemoryContext, CurrentResourceOwner, DEBUG3, elog, ERROR, FillPortalStore(), PortalData::holdStore, log_executor_stats, MarkPortalActive(), MarkPortalDone(), MarkPortalFailed(), MemoryContextSwitchTo(), 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, PortalGetHeapMemory, PortalIsValid, PortalRunMulti(), PortalRunSelect(), ResetUsage(), PortalData::resowner, PortalData::run_once, ShowUsage(), snprintf(), PortalData::status, PortalData::strategy, TopTransactionContext, TopTransactionResourceOwner, and UINT64_FORMAT.

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

692 {
693  bool result;
694  uint64 nprocessed;
695  ResourceOwner saveTopTransactionResourceOwner;
696  MemoryContext saveTopTransactionContext;
697  Portal saveActivePortal;
698  ResourceOwner saveResourceOwner;
699  MemoryContext savePortalContext;
700  MemoryContext saveMemoryContext;
701 
702  AssertArg(PortalIsValid(portal));
703 
704  TRACE_POSTGRESQL_QUERY_EXECUTE_START();
705 
706  /* Initialize completion tag to empty string */
707  if (completionTag)
708  completionTag[0] = '\0';
709 
711  {
712  elog(DEBUG3, "PortalRun");
713  /* PORTAL_MULTI_QUERY logs its own stats per query */
714  ResetUsage();
715  }
716 
717  /*
718  * Check for improper portal use, and mark portal active.
719  */
720  MarkPortalActive(portal);
721 
722  /* Set run_once flag. Shouldn't be clear if previously set. */
723  Assert(!portal->run_once || run_once);
724  portal->run_once = run_once;
725 
726  /*
727  * Set up global portal context pointers.
728  *
729  * We have to play a special game here to support utility commands like
730  * VACUUM and CLUSTER, which internally start and commit transactions.
731  * When we are called to execute such a command, CurrentResourceOwner will
732  * be pointing to the TopTransactionResourceOwner --- which will be
733  * destroyed and replaced in the course of the internal commit and
734  * restart. So we need to be prepared to restore it as pointing to the
735  * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
736  * internally starting whole new transactions is not good.)
737  * CurrentMemoryContext has a similar problem, but the other pointers we
738  * save here will be NULL or pointing to longer-lived objects.
739  */
740  saveTopTransactionResourceOwner = TopTransactionResourceOwner;
741  saveTopTransactionContext = TopTransactionContext;
742  saveActivePortal = ActivePortal;
743  saveResourceOwner = CurrentResourceOwner;
744  savePortalContext = PortalContext;
745  saveMemoryContext = CurrentMemoryContext;
746  PG_TRY();
747  {
748  ActivePortal = portal;
749  if (portal->resowner)
750  CurrentResourceOwner = portal->resowner;
752 
754 
755  switch (portal->strategy)
756  {
757  case PORTAL_ONE_SELECT:
759  case PORTAL_ONE_MOD_WITH:
760  case PORTAL_UTIL_SELECT:
761 
762  /*
763  * If we have not yet run the command, do so, storing its
764  * results in the portal's tuplestore. But we don't do that
765  * for the PORTAL_ONE_SELECT case.
766  */
767  if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
768  FillPortalStore(portal, isTopLevel);
769 
770  /*
771  * Now fetch desired portion of results.
772  */
773  nprocessed = PortalRunSelect(portal, true, count, dest);
774 
775  /*
776  * If the portal result contains a command tag and the caller
777  * gave us a pointer to store it, copy it. Patch the "SELECT"
778  * tag to also provide the rowcount.
779  */
780  if (completionTag && portal->commandTag)
781  {
782  if (strcmp(portal->commandTag, "SELECT") == 0)
783  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
784  "SELECT " UINT64_FORMAT, nprocessed);
785  else
786  strcpy(completionTag, portal->commandTag);
787  }
788 
789  /* Mark portal not active */
790  portal->status = PORTAL_READY;
791 
792  /*
793  * Since it's a forward fetch, say DONE iff atEnd is now true.
794  */
795  result = portal->atEnd;
796  break;
797 
798  case PORTAL_MULTI_QUERY:
799  PortalRunMulti(portal, isTopLevel, false,
800  dest, altdest, completionTag);
801 
802  /* Prevent portal's commands from being re-executed */
803  MarkPortalDone(portal);
804 
805  /* Always complete at end of RunMulti */
806  result = true;
807  break;
808 
809  default:
810  elog(ERROR, "unrecognized portal strategy: %d",
811  (int) portal->strategy);
812  result = false; /* keep compiler quiet */
813  break;
814  }
815  }
816  PG_CATCH();
817  {
818  /* Uncaught error while executing portal: mark it dead */
819  MarkPortalFailed(portal);
820 
821  /* Restore global vars and propagate error */
822  if (saveMemoryContext == saveTopTransactionContext)
824  else
825  MemoryContextSwitchTo(saveMemoryContext);
826  ActivePortal = saveActivePortal;
827  if (saveResourceOwner == saveTopTransactionResourceOwner)
829  else
830  CurrentResourceOwner = saveResourceOwner;
831  PortalContext = savePortalContext;
832 
833  PG_RE_THROW();
834  }
835  PG_END_TRY();
836 
837  if (saveMemoryContext == saveTopTransactionContext)
839  else
840  MemoryContextSwitchTo(saveMemoryContext);
841  ActivePortal = saveActivePortal;
842  if (saveResourceOwner == saveTopTransactionResourceOwner)
844  else
845  CurrentResourceOwner = saveResourceOwner;
846  PortalContext = savePortalContext;
847 
849  ShowUsage("EXECUTOR STATISTICS");
850 
851  TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
852 
853  return result;
854 }
bool atEnd
Definition: portal.h:187
MemoryContext TopTransactionContext
Definition: mcxt.c:48
#define DEBUG3
Definition: elog.h:23
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:140
void ShowUsage(const char *title)
Definition: postgres.c:4444
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void MarkPortalActive(Portal portal)
Definition: portalmem.c:388
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition: pquery.c:875
bool run_once
Definition: portal.h:145
MemoryContext PortalContext
Definition: mcxt.c:52
void ResetUsage(void)
Definition: postgres.c:4437
#define ERROR
Definition: elog.h:43
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:1008
const char * commandTag
Definition: portal.h:135
void MarkPortalDone(Portal portal)
Definition: portalmem.c:407
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
PortalStrategy strategy
Definition: portal.h:143
#define AssertArg(condition)
Definition: c.h:683
#define PortalIsValid(p)
Definition: portal.h:199
#define PG_CATCH()
Definition: elog.h:293
#define COMPLETION_TAG_BUFSIZE
Definition: dest.h:74
#define Assert(condition)
Definition: c.h:681
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, char *completionTag)
Definition: pquery.c:1205
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:435
PortalStatus status
Definition: portal.h:148
#define PG_RE_THROW()
Definition: elog.h:314
ResourceOwner resowner
Definition: portal.h:120
Tuplestorestate * holdStore
Definition: portal.h:164
bool log_executor_stats
Definition: guc.c:440
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define UINT64_FORMAT
Definition: c.h:301
#define PG_END_TRY()
Definition: elog.h:300
uint64 PortalRunFetch ( Portal  portal,
FetchDirection  fdirection,
long  count,
DestReceiver dest 
)

Definition at line 1395 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, PortalGetHeapMemory, PortalIsValid, PortalData::resowner, PortalData::run_once, PortalData::status, and PortalData::strategy.

Referenced by _SPI_cursor_operation(), and PerformPortalFetch().

1399 {
1400  uint64 result;
1401  Portal saveActivePortal;
1402  ResourceOwner saveResourceOwner;
1403  MemoryContext savePortalContext;
1404  MemoryContext oldContext;
1405 
1406  AssertArg(PortalIsValid(portal));
1407 
1408  /*
1409  * Check for improper portal use, and mark portal active.
1410  */
1411  MarkPortalActive(portal);
1412 
1413  /* If supporting FETCH, portal can't be run-once. */
1414  Assert(!portal->run_once);
1415 
1416  /*
1417  * Set up global portal context pointers.
1418  */
1419  saveActivePortal = ActivePortal;
1420  saveResourceOwner = CurrentResourceOwner;
1421  savePortalContext = PortalContext;
1422  PG_TRY();
1423  {
1424  ActivePortal = portal;
1425  if (portal->resowner)
1426  CurrentResourceOwner = portal->resowner;
1428 
1429  oldContext = MemoryContextSwitchTo(PortalContext);
1430 
1431  switch (portal->strategy)
1432  {
1433  case PORTAL_ONE_SELECT:
1434  result = DoPortalRunFetch(portal, fdirection, count, dest);
1435  break;
1436 
1437  case PORTAL_ONE_RETURNING:
1438  case PORTAL_ONE_MOD_WITH:
1439  case PORTAL_UTIL_SELECT:
1440 
1441  /*
1442  * If we have not yet run the command, do so, storing its
1443  * results in the portal's tuplestore.
1444  */
1445  if (!portal->holdStore)
1446  FillPortalStore(portal, false /* isTopLevel */ );
1447 
1448  /*
1449  * Now fetch desired portion of results.
1450  */
1451  result = DoPortalRunFetch(portal, fdirection, count, dest);
1452  break;
1453 
1454  default:
1455  elog(ERROR, "unsupported portal strategy");
1456  result = 0; /* keep compiler quiet */
1457  break;
1458  }
1459  }
1460  PG_CATCH();
1461  {
1462  /* Uncaught error while executing portal: mark it dead */
1463  MarkPortalFailed(portal);
1464 
1465  /* Restore global vars and propagate error */
1466  ActivePortal = saveActivePortal;
1467  CurrentResourceOwner = saveResourceOwner;
1468  PortalContext = savePortalContext;
1469 
1470  PG_RE_THROW();
1471  }
1472  PG_END_TRY();
1473 
1474  MemoryContextSwitchTo(oldContext);
1475 
1476  /* Mark portal not active */
1477  portal->status = PORTAL_READY;
1478 
1479  ActivePortal = saveActivePortal;
1480  CurrentResourceOwner = saveResourceOwner;
1481  PortalContext = savePortalContext;
1482 
1483  return result;
1484 }
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void MarkPortalActive(Portal portal)
Definition: portalmem.c:388
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static uint64 DoPortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1497
bool run_once
Definition: portal.h:145
MemoryContext PortalContext
Definition: mcxt.c:52
#define ERROR
Definition: elog.h:43
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition: pquery.c:1008
PortalStrategy strategy
Definition: portal.h:143
#define AssertArg(condition)
Definition: c.h:683
#define PortalIsValid(p)
Definition: portal.h:199
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:681
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:435
PortalStatus status
Definition: portal.h:148
#define PG_RE_THROW()
Definition: elog.h:314
ResourceOwner resowner
Definition: portal.h:120
Tuplestorestate * holdStore
Definition: portal.h:164
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
static void PortalRunMulti ( Portal  portal,
bool  isTopLevel,
bool  setHoldSnapshot,
DestReceiver dest,
DestReceiver altdest,
char *  completionTag 
)
static

Definition at line 1205 of file pquery.c.

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

Referenced by FillPortalStore(), and PortalRun().

1209 {
1210  bool active_snapshot_set = false;
1211  ListCell *stmtlist_item;
1212 
1213  /*
1214  * If the destination is DestRemoteExecute, change to DestNone. The
1215  * reason is that the client won't be expecting any tuples, and indeed has
1216  * no way to know what they are, since there is no provision for Describe
1217  * to send a RowDescription message when this portal execution strategy is
1218  * in effect. This presently will only affect SELECT commands added to
1219  * non-SELECT queries by rewrite rules: such commands will be executed,
1220  * but the results will be discarded unless you use "simple Query"
1221  * protocol.
1222  */
1223  if (dest->mydest == DestRemoteExecute)
1224  dest = None_Receiver;
1225  if (altdest->mydest == DestRemoteExecute)
1226  altdest = None_Receiver;
1227 
1228  /*
1229  * Loop to handle the individual queries generated from a single parsetree
1230  * by analysis and rewrite.
1231  */
1232  foreach(stmtlist_item, portal->stmts)
1233  {
1234  PlannedStmt *pstmt = lfirst_node(PlannedStmt, stmtlist_item);
1235 
1236  /*
1237  * If we got a cancel signal in prior command, quit
1238  */
1240 
1241  if (pstmt->utilityStmt == NULL)
1242  {
1243  /*
1244  * process a plannable query.
1245  */
1246  TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1247 
1248  if (log_executor_stats)
1249  ResetUsage();
1250 
1251  /*
1252  * Must always have a snapshot for plannable queries. First time
1253  * through, take a new snapshot; for subsequent queries in the
1254  * same portal, just update the snapshot's copy of the command
1255  * counter.
1256  */
1257  if (!active_snapshot_set)
1258  {
1259  Snapshot snapshot = GetTransactionSnapshot();
1260 
1261  /* If told to, register the snapshot and save in portal */
1262  if (setHoldSnapshot)
1263  {
1264  snapshot = RegisterSnapshot(snapshot);
1265  portal->holdSnapshot = snapshot;
1266  }
1267 
1268  /*
1269  * We can't have the holdSnapshot also be the active one,
1270  * because UpdateActiveSnapshotCommandId would complain. So
1271  * force an extra snapshot copy. Plain PushActiveSnapshot
1272  * would have copied the transaction snapshot anyway, so this
1273  * only adds a copy step when setHoldSnapshot is true. (It's
1274  * okay for the command ID of the active snapshot to diverge
1275  * from what holdSnapshot has.)
1276  */
1277  PushCopiedSnapshot(snapshot);
1278  active_snapshot_set = true;
1279  }
1280  else
1282 
1283  if (pstmt->canSetTag)
1284  {
1285  /* statement can set tag string */
1286  ProcessQuery(pstmt,
1287  portal->sourceText,
1288  portal->portalParams,
1289  portal->queryEnv,
1290  dest, completionTag);
1291  }
1292  else
1293  {
1294  /* stmt added by rewrite cannot set tag */
1295  ProcessQuery(pstmt,
1296  portal->sourceText,
1297  portal->portalParams,
1298  portal->queryEnv,
1299  altdest, NULL);
1300  }
1301 
1302  if (log_executor_stats)
1303  ShowUsage("EXECUTOR STATISTICS");
1304 
1305  TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
1306  }
1307  else
1308  {
1309  /*
1310  * process utility functions (create, destroy, etc..)
1311  *
1312  * We must not set a snapshot here for utility commands (if one is
1313  * needed, PortalRunUtility will do it). If a utility command is
1314  * alone in a portal then everything's fine. The only case where
1315  * a utility command can be part of a longer list is that rules
1316  * are allowed to include NotifyStmt. NotifyStmt doesn't care
1317  * whether it has a snapshot or not, so we just leave the current
1318  * snapshot alone if we have one.
1319  */
1320  if (pstmt->canSetTag)
1321  {
1322  Assert(!active_snapshot_set);
1323  /* statement can set tag string */
1324  PortalRunUtility(portal, pstmt, isTopLevel, false,
1325  dest, completionTag);
1326  }
1327  else
1328  {
1329  Assert(IsA(pstmt->utilityStmt, NotifyStmt));
1330  /* stmt added by rewrite cannot set tag */
1331  PortalRunUtility(portal, pstmt, isTopLevel, false,
1332  altdest, NULL);
1333  }
1334  }
1335 
1336  /*
1337  * Increment command counter between queries, but not after the last
1338  * one.
1339  */
1340  if (lnext(stmtlist_item) != NULL)
1342 
1343  /*
1344  * Clear subsidiary contexts to recover temporary memory.
1345  */
1347 
1349  }
1350 
1351  /* Pop the snapshot if we pushed one. */
1352  if (active_snapshot_set)
1354 
1355  /*
1356  * If a command completion tag was supplied, use it. Otherwise use the
1357  * portal's commandTag as the default completion tag.
1358  *
1359  * Exception: Clients expect INSERT/UPDATE/DELETE tags to have counts, so
1360  * fake them with zeros. This can happen with DO INSTEAD rules if there
1361  * is no replacement query of the same type as the original. We print "0
1362  * 0" here because technically there is no query of the matching tag type,
1363  * and printing a non-zero count for a different query type seems wrong,
1364  * e.g. an INSERT that does an UPDATE instead should not print "0 1" if
1365  * one row was updated. See QueryRewrite(), step 3, for details.
1366  */
1367  if (completionTag && completionTag[0] == '\0')
1368  {
1369  if (portal->commandTag)
1370  strcpy(completionTag, portal->commandTag);
1371  if (strcmp(completionTag, "SELECT") == 0)
1372  sprintf(completionTag, "SELECT 0 0");
1373  else if (strcmp(completionTag, "INSERT") == 0)
1374  strcpy(completionTag, "INSERT 0 0");
1375  else if (strcmp(completionTag, "UPDATE") == 0)
1376  strcpy(completionTag, "UPDATE 0");
1377  else if (strcmp(completionTag, "DELETE") == 0)
1378  strcpy(completionTag, "DELETE 0");
1379  }
1380 }
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:781
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:863
void ShowUsage(const char *title)
Definition: postgres.c:4444
CommandDest mydest
Definition: dest.h:128
static void ProcessQuery(PlannedStmt *plan, const char *sourceText, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, char *completionTag)
Definition: pquery.c:137
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
List * stmts
Definition: portal.h:136
DestReceiver * None_Receiver
Definition: dest.c:91
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
void ResetUsage(void)
Definition: postgres.c:4437
ParamListInfo portalParams
Definition: portal.h:139
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:769
const char * commandTag
Definition: portal.h:135
#define lfirst_node(type, lc)
Definition: pg_list.h:109
static void PortalRunUtility(Portal portal, PlannedStmt *pstmt, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, char *completionTag)
Definition: pquery.c:1134
Node * utilityStmt
Definition: plannodes.h:94
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define lnext(lc)
Definition: pg_list.h:105
void MemoryContextDeleteChildren(MemoryContext context)
Definition: mcxt.c:236
bool canSetTag
Definition: plannodes.h:53
void CommandCounterIncrement(void)
Definition: xact.c:915
const char * sourceText
Definition: portal.h:134
#define Assert(condition)
Definition: c.h:681
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
Snapshot holdSnapshot
Definition: portal.h:175
QueryEnvironment * queryEnv
Definition: portal.h:140
bool log_executor_stats
Definition: guc.c:440
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
static uint64 PortalRunSelect ( Portal  portal,
bool  forward,
long  count,
DestReceiver dest 
)
static

Definition at line 875 of file pquery.c.

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

Referenced by DoPortalRunFetch(), and PortalRun().

879 {
880  QueryDesc *queryDesc;
881  ScanDirection direction;
882  uint64 nprocessed;
883 
884  /*
885  * NB: queryDesc will be NULL if we are fetching from a held cursor or a
886  * completed utility query; can't use it in that path.
887  */
888  queryDesc = PortalGetQueryDesc(portal);
889 
890  /* Caller messed up if we have neither a ready query nor held data. */
891  Assert(queryDesc || portal->holdStore);
892 
893  /*
894  * Force the queryDesc destination to the right thing. This supports
895  * MOVE, for example, which will pass in dest = DestNone. This is okay to
896  * change as long as we do it on every fetch. (The Executor must not
897  * assume that dest never changes.)
898  */
899  if (queryDesc)
900  queryDesc->dest = dest;
901 
902  /*
903  * Determine which direction to go in, and check to see if we're already
904  * at the end of the available tuples in that direction. If so, set the
905  * direction to NoMovement to avoid trying to fetch any tuples. (This
906  * check exists because not all plan node types are robust about being
907  * called again if they've already returned NULL once.) Then call the
908  * executor (we must not skip this, because the destination needs to see a
909  * setup and shutdown even if no tuples are available). Finally, update
910  * the portal position state depending on the number of tuples that were
911  * retrieved.
912  */
913  if (forward)
914  {
915  if (portal->atEnd || count <= 0)
916  {
917  direction = NoMovementScanDirection;
918  count = 0; /* don't pass negative count to executor */
919  }
920  else
921  direction = ForwardScanDirection;
922 
923  /* In the executor, zero count processes all rows */
924  if (count == FETCH_ALL)
925  count = 0;
926 
927  if (portal->holdStore)
928  nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
929  else
930  {
931  PushActiveSnapshot(queryDesc->snapshot);
932  ExecutorRun(queryDesc, direction, (uint64) count,
933  portal->run_once);
934  nprocessed = queryDesc->estate->es_processed;
936  }
937 
938  if (!ScanDirectionIsNoMovement(direction))
939  {
940  if (nprocessed > 0)
941  portal->atStart = false; /* OK to go backward now */
942  if (count == 0 || nprocessed < (uint64) count)
943  portal->atEnd = true; /* we retrieved 'em all */
944  portal->portalPos += nprocessed;
945  }
946  }
947  else
948  {
949  if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
950  ereport(ERROR,
951  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
952  errmsg("cursor can only scan forward"),
953  errhint("Declare it with SCROLL option to enable backward scan.")));
954 
955  if (portal->atStart || count <= 0)
956  {
957  direction = NoMovementScanDirection;
958  count = 0; /* don't pass negative count to executor */
959  }
960  else
961  direction = BackwardScanDirection;
962 
963  /* In the executor, zero count processes all rows */
964  if (count == FETCH_ALL)
965  count = 0;
966 
967  if (portal->holdStore)
968  nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
969  else
970  {
971  PushActiveSnapshot(queryDesc->snapshot);
972  ExecutorRun(queryDesc, direction, (uint64) count,
973  portal->run_once);
974  nprocessed = queryDesc->estate->es_processed;
976  }
977 
978  if (!ScanDirectionIsNoMovement(direction))
979  {
980  if (nprocessed > 0 && portal->atEnd)
981  {
982  portal->atEnd = false; /* OK to go forward now */
983  portal->portalPos++; /* adjust for endpoint case */
984  }
985  if (count == 0 || nprocessed < (uint64) count)
986  {
987  portal->atStart = true; /* we retrieved 'em all */
988  portal->portalPos = 0;
989  }
990  else
991  {
992  portal->portalPos -= nprocessed;
993  }
994  }
995  }
996 
997  return nprocessed;
998 }
static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count, DestReceiver *dest)
Definition: pquery.c:1068
int errhint(const char *fmt,...)
Definition: elog.c:987
bool atEnd
Definition: portal.h:187
EState * estate
Definition: execdesc.h:48
int errcode(int sqlerrcode)
Definition: elog.c:575
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
#define ScanDirectionIsNoMovement(direction)
Definition: sdir.h:48
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:2632
Snapshot snapshot
Definition: execdesc.h:39
bool run_once
Definition: portal.h:145
#define ERROR
Definition: elog.h:43
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
ScanDirection
Definition: sdir.h:22
#define ereport(elevel, rest)
Definition: elog.h:122
#define Assert(condition)
Definition: c.h:681
uint64 es_processed
Definition: execnodes.h:477
#define PortalGetQueryDesc(portal)
Definition: portal.h:204
DestReceiver * dest
Definition: execdesc.h:41
bool atStart
Definition: portal.h:186
Tuplestorestate * holdStore
Definition: portal.h:164
int errmsg(const char *fmt,...)
Definition: elog.c:797
int cursorOptions
Definition: portal.h:144
uint64 portalPos
Definition: portal.h:188
#define FETCH_ALL
Definition: parsenodes.h:2674
static void PortalRunUtility ( Portal  portal,
PlannedStmt pstmt,
bool  isTopLevel,
bool  setHoldSnapshot,
DestReceiver dest,
char *  completionTag 
)
static

Definition at line 1134 of file pquery.c.

References ActiveSnapshotSet(), GetActiveSnapshot(), GetTransactionSnapshot(), PortalData::holdSnapshot, IsA, MemoryContextSwitchTo(), PopActiveSnapshot(), PortalGetHeapMemory, PortalData::portalParams, PROCESS_UTILITY_QUERY, PROCESS_UTILITY_TOPLEVEL, ProcessUtility(), PushActiveSnapshot(), PortalData::queryEnv, RegisterSnapshot(), PortalData::sourceText, and PlannedStmt::utilityStmt.

Referenced by FillPortalStore(), and PortalRunMulti().

1137 {
1138  Node *utilityStmt = pstmt->utilityStmt;
1139  Snapshot snapshot;
1140 
1141  /*
1142  * Set snapshot if utility stmt needs one. Most reliable way to do this
1143  * seems to be to enumerate those that do not need one; this is a short
1144  * list. Transaction control, LOCK, and SET must *not* set a snapshot
1145  * since they need to be executable at the start of a transaction-snapshot
1146  * mode transaction without freezing a snapshot. By extension we allow
1147  * SHOW not to set a snapshot. The other stmts listed are just efficiency
1148  * hacks. Beware of listing anything that can modify the database --- if,
1149  * say, it has to update an index with expressions that invoke
1150  * user-defined functions, then it had better have a snapshot.
1151  */
1152  if (!(IsA(utilityStmt, TransactionStmt) ||
1153  IsA(utilityStmt, LockStmt) ||
1154  IsA(utilityStmt, VariableSetStmt) ||
1155  IsA(utilityStmt, VariableShowStmt) ||
1156  IsA(utilityStmt, ConstraintsSetStmt) ||
1157  /* efficiency hacks from here down */
1158  IsA(utilityStmt, FetchStmt) ||
1159  IsA(utilityStmt, ListenStmt) ||
1160  IsA(utilityStmt, NotifyStmt) ||
1161  IsA(utilityStmt, UnlistenStmt) ||
1162  IsA(utilityStmt, CheckPointStmt)))
1163  {
1164  snapshot = GetTransactionSnapshot();
1165  /* If told to, register the snapshot we're using and save in portal */
1166  if (setHoldSnapshot)
1167  {
1168  snapshot = RegisterSnapshot(snapshot);
1169  portal->holdSnapshot = snapshot;
1170  }
1171  PushActiveSnapshot(snapshot);
1172  /* PushActiveSnapshot might have copied the snapshot */
1173  snapshot = GetActiveSnapshot();
1174  }
1175  else
1176  snapshot = NULL;
1177 
1178  ProcessUtility(pstmt,
1179  portal->sourceText,
1181  portal->portalParams,
1182  portal->queryEnv,
1183  dest,
1184  completionTag);
1185 
1186  /* Some utility statements may change context on us */
1188 
1189  /*
1190  * Some utility commands may pop the ActiveSnapshot stack from under us,
1191  * so be careful to only pop the stack if our snapshot is still at the
1192  * top.
1193  */
1194  if (snapshot != NULL && ActiveSnapshotSet() &&
1195  snapshot == GetActiveSnapshot())
1197 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
Snapshot RegisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:863
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
Definition: nodes.h:510
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, char *completionTag)
Definition: utility.c:335
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
ParamListInfo portalParams
Definition: portal.h:139
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
Node * utilityStmt
Definition: plannodes.h:94
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:851
const char * sourceText
Definition: portal.h:134
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
Snapshot holdSnapshot
Definition: portal.h:175
QueryEnvironment * queryEnv
Definition: portal.h:140
void PortalSetResultFormat ( Portal  portal,
int  nFormats,
int16 formats 
)

Definition at line 627 of file pquery.c.

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

Referenced by exec_bind_message(), and exec_simple_query().

628 {
629  int natts;
630  int i;
631 
632  /* Do nothing if portal won't return tuples */
633  if (portal->tupDesc == NULL)
634  return;
635  natts = portal->tupDesc->natts;
636  portal->formats = (int16 *)
638  natts * sizeof(int16));
639  if (nFormats > 1)
640  {
641  /* format specified for each column */
642  if (nFormats != natts)
643  ereport(ERROR,
644  (errcode(ERRCODE_PROTOCOL_VIOLATION),
645  errmsg("bind message has %d result formats but query has %d columns",
646  nFormats, natts)));
647  memcpy(portal->formats, formats, natts * sizeof(int16));
648  }
649  else if (nFormats > 0)
650  {
651  /* single format specified, use for all columns */
652  int16 format1 = formats[0];
653 
654  for (i = 0; i < natts; i++)
655  portal->formats[i] = format1;
656  }
657  else
658  {
659  /* use default format for all columns */
660  for (i = 0; i < natts; i++)
661  portal->formats[i] = 0;
662  }
663 }
signed short int16
Definition: c.h:245
int errcode(int sqlerrcode)
Definition: elog.c:575
int natts
Definition: tupdesc.h:73
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int16 * formats
Definition: portal.h:157
TupleDesc tupDesc
Definition: portal.h:155
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
int errmsg(const char *fmt,...)
Definition: elog.c:797
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
int i
void PortalStart ( Portal  portal,
ParamListInfo  params,
int  eflags,
Snapshot  snapshot 
)

Definition at line 445 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, PortalGetHeapMemory, 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().

447 {
448  Portal saveActivePortal;
449  ResourceOwner saveResourceOwner;
450  MemoryContext savePortalContext;
451  MemoryContext oldContext;
452  QueryDesc *queryDesc;
453  int myeflags;
454 
455  AssertArg(PortalIsValid(portal));
456  AssertState(portal->status == PORTAL_DEFINED);
457 
458  /*
459  * Set up global portal context pointers.
460  */
461  saveActivePortal = ActivePortal;
462  saveResourceOwner = CurrentResourceOwner;
463  savePortalContext = PortalContext;
464  PG_TRY();
465  {
466  ActivePortal = portal;
467  if (portal->resowner)
468  CurrentResourceOwner = portal->resowner;
470 
471  oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
472 
473  /* Must remember portal param list, if any */
474  portal->portalParams = params;
475 
476  /*
477  * Determine the portal execution strategy
478  */
479  portal->strategy = ChoosePortalStrategy(portal->stmts);
480 
481  /*
482  * Fire her up according to the strategy
483  */
484  switch (portal->strategy)
485  {
486  case PORTAL_ONE_SELECT:
487 
488  /* Must set snapshot before starting executor. */
489  if (snapshot)
490  PushActiveSnapshot(snapshot);
491  else
493 
494  /*
495  * Create QueryDesc in portal's context; for the moment, set
496  * the destination to DestNone.
497  */
498  queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
499  portal->sourceText,
503  params,
504  portal->queryEnv,
505  0);
506 
507  /*
508  * If it's a scrollable cursor, executor needs to support
509  * REWIND and backwards scan, as well as whatever the caller
510  * might've asked for.
511  */
512  if (portal->cursorOptions & CURSOR_OPT_SCROLL)
513  myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
514  else
515  myeflags = eflags;
516 
517  /*
518  * Call ExecutorStart to prepare the plan for execution
519  */
520  ExecutorStart(queryDesc, myeflags);
521 
522  /*
523  * This tells PortalCleanup to shut down the executor
524  */
525  portal->queryDesc = queryDesc;
526 
527  /*
528  * Remember tuple descriptor (computed by ExecutorStart)
529  */
530  portal->tupDesc = queryDesc->tupDesc;
531 
532  /*
533  * Reset cursor position data to "start of query"
534  */
535  portal->atStart = true;
536  portal->atEnd = false; /* allow fetches */
537  portal->portalPos = 0;
538 
540  break;
541 
543  case PORTAL_ONE_MOD_WITH:
544 
545  /*
546  * We don't start the executor until we are told to run the
547  * portal. We do need to set up the result tupdesc.
548  */
549  {
550  PlannedStmt *pstmt;
551 
552  pstmt = PortalGetPrimaryStmt(portal);
553  portal->tupDesc =
555  false);
556  }
557 
558  /*
559  * Reset cursor position data to "start of query"
560  */
561  portal->atStart = true;
562  portal->atEnd = false; /* allow fetches */
563  portal->portalPos = 0;
564  break;
565 
566  case PORTAL_UTIL_SELECT:
567 
568  /*
569  * We don't set snapshot here, because PortalRunUtility will
570  * take care of it if needed.
571  */
572  {
573  PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
574 
575  Assert(pstmt->commandType == CMD_UTILITY);
576  portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
577  }
578 
579  /*
580  * Reset cursor position data to "start of query"
581  */
582  portal->atStart = true;
583  portal->atEnd = false; /* allow fetches */
584  portal->portalPos = 0;
585  break;
586 
587  case PORTAL_MULTI_QUERY:
588  /* Need do nothing now */
589  portal->tupDesc = NULL;
590  break;
591  }
592  }
593  PG_CATCH();
594  {
595  /* Uncaught error while executing portal: mark it dead */
596  MarkPortalFailed(portal);
597 
598  /* Restore global vars and propagate error */
599  ActivePortal = saveActivePortal;
600  CurrentResourceOwner = saveResourceOwner;
601  PortalContext = savePortalContext;
602 
603  PG_RE_THROW();
604  }
605  PG_END_TRY();
606 
607  MemoryContextSwitchTo(oldContext);
608 
609  ActivePortal = saveActivePortal;
610  CurrentResourceOwner = saveResourceOwner;
611  PortalContext = savePortalContext;
612 
613  portal->status = PORTAL_READY;
614 }
#define AssertState(condition)
Definition: c.h:684
bool atEnd
Definition: portal.h:187
Portal ActivePortal
Definition: pquery.c:35
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:147
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
void PopActiveSnapshot(void)
Definition: snapmgr.c:812
List * stmts
Definition: portal.h:136
#define linitial_node(type, l)
Definition: pg_list.h:114
DestReceiver * None_Receiver
Definition: dest.c:91
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:304
struct Plan * planTree
Definition: plannodes.h:61
MemoryContext PortalContext
Definition: mcxt.c:52
ParamListInfo portalParams
Definition: portal.h:139
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition: portalmem.c:150
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:60
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:733
Node * utilityStmt
Definition: plannodes.h:94
PortalStrategy strategy
Definition: portal.h:143
#define EXEC_FLAG_REWIND
Definition: executor.h:59
#define AssertArg(condition)
Definition: c.h:683
QueryDesc * queryDesc
Definition: portal.h:152
TupleDesc tupDesc
Definition: execdesc.h:47
#define InvalidSnapshot
Definition: snapshot.h:25
#define PortalIsValid(p)
Definition: portal.h:199
CmdType commandType
Definition: plannodes.h:45
const char * sourceText
Definition: portal.h:134
TupleDesc tupDesc
Definition: portal.h:155
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:681
#define PortalGetHeapMemory(portal)
Definition: portal.h:205
void MarkPortalFailed(Portal portal)
Definition: portalmem.c:435
PortalStatus status
Definition: portal.h:148
#define PG_RE_THROW()
Definition: elog.h:314
QueryEnvironment * queryEnv
Definition: portal.h:140
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition: pquery.c:221
List * targetlist
Definition: plannodes.h:144
ResourceOwner resowner
Definition: portal.h:120
bool atStart
Definition: portal.h:186
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:2631
TupleDesc ExecCleanTypeFromTL(List *targetList, bool hasoid)
Definition: execTuples.c:900
int cursorOptions
Definition: portal.h:144
#define PG_TRY()
Definition: elog.h:284
TupleDesc UtilityTupleDescriptor(Node *parsetree)
Definition: utility.c:1765
#define PG_END_TRY()
Definition: elog.h:300
uint64 portalPos
Definition: portal.h:188
static void ProcessQuery ( PlannedStmt plan,
const char *  sourceText,
ParamListInfo  params,
QueryEnvironment queryEnv,
DestReceiver dest,
char *  completionTag 
)
static

Definition at line 137 of file pquery.c.

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

Referenced by PortalRunMulti().

143 {
144  QueryDesc *queryDesc;
145 
146  /*
147  * Create the QueryDesc object
148  */
149  queryDesc = CreateQueryDesc(plan, sourceText,
151  dest, params, queryEnv, 0);
152 
153  /*
154  * Call ExecutorStart to prepare the plan for execution
155  */
156  ExecutorStart(queryDesc, 0);
157 
158  /*
159  * Run the plan to completion.
160  */
161  ExecutorRun(queryDesc, ForwardScanDirection, 0L, true);
162 
163  /*
164  * Build command completion status string, if caller wants one.
165  */
166  if (completionTag)
167  {
168  Oid lastOid;
169 
170  switch (queryDesc->operation)
171  {
172  case CMD_SELECT:
173  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
174  "SELECT " UINT64_FORMAT,
175  queryDesc->estate->es_processed);
176  break;
177  case CMD_INSERT:
178  if (queryDesc->estate->es_processed == 1)
179  lastOid = queryDesc->estate->es_lastoid;
180  else
181  lastOid = InvalidOid;
182  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
183  "INSERT %u " UINT64_FORMAT,
184  lastOid, queryDesc->estate->es_processed);
185  break;
186  case CMD_UPDATE:
187  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
188  "UPDATE " UINT64_FORMAT,
189  queryDesc->estate->es_processed);
190  break;
191  case CMD_DELETE:
192  snprintf(completionTag, COMPLETION_TAG_BUFSIZE,
193  "DELETE " UINT64_FORMAT,
194  queryDesc->estate->es_processed);
195  break;
196  default:
197  strcpy(completionTag, "???");
198  break;
199  }
200  }
201 
202  /*
203  * Now, we close down all the scans and free allocated resources.
204  */
205  ExecutorFinish(queryDesc);
206  ExecutorEnd(queryDesc);
207 
208  FreeQueryDesc(queryDesc);
209 }
EState * estate
Definition: execdesc.h:48
Oid es_lastoid
Definition: execnodes.h:478
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:147
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:839
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
unsigned int Oid
Definition: postgres_ext.h:31
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:461
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:299
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:401
CmdType operation
Definition: execdesc.h:36
#define InvalidSnapshot
Definition: snapshot.h:25
#define InvalidOid
Definition: postgres_ext.h:36
#define COMPLETION_TAG_BUFSIZE
Definition: dest.h:74
uint64 es_processed
Definition: execnodes.h:477
#define UINT64_FORMAT
Definition: c.h:301
static uint64 RunFromStore ( Portal  portal,
ScanDirection  direction,
uint64  count,
DestReceiver dest 
)
static

Definition at line 1068 of file pquery.c.

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

Referenced by PortalRunSelect().

1070 {
1071  uint64 current_tuple_count = 0;
1072  TupleTableSlot *slot;
1073 
1074  slot = MakeSingleTupleTableSlot(portal->tupDesc);
1075 
1076  dest->rStartup(dest, CMD_SELECT, portal->tupDesc);
1077 
1078  if (ScanDirectionIsNoMovement(direction))
1079  {
1080  /* do nothing except start/stop the destination */
1081  }
1082  else
1083  {
1084  bool forward = ScanDirectionIsForward(direction);
1085 
1086  for (;;)
1087  {
1088  MemoryContext oldcontext;
1089  bool ok;
1090 
1091  oldcontext = MemoryContextSwitchTo(portal->holdContext);
1092 
1093  ok = tuplestore_gettupleslot(portal->holdStore, forward, false,
1094  slot);
1095 
1096  MemoryContextSwitchTo(oldcontext);
1097 
1098  if (!ok)
1099  break;
1100 
1101  /*
1102  * If we are not able to send the tuple, we assume the destination
1103  * has closed and no more tuples can be sent. If that's the case,
1104  * end the loop.
1105  */
1106  if (!dest->receiveSlot(slot, dest))
1107  break;
1108 
1109  ExecClearTuple(slot);
1110 
1111  /*
1112  * check our tuple count.. if we've processed the proper number
1113  * then quit, else loop again and process more tuples. Zero count
1114  * means no limit.
1115  */
1116  current_tuple_count++;
1117  if (count && count == current_tuple_count)
1118  break;
1119  }
1120  }
1121 
1122  dest->rShutdown(dest);
1123 
1125 
1126  return current_tuple_count;
1127 }
void(* rShutdown)(DestReceiver *self)
Definition: dest.h:124
void(* rStartup)(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: dest.h:121
#define ScanDirectionIsForward(direction)
Definition: sdir.h:55
TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: execTuples.c:439
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext holdContext
Definition: portal.h:165
#define ScanDirectionIsNoMovement(direction)
Definition: sdir.h:48
void ExecDropSingleTupleTableSlot(TupleTableSlot *slot)
Definition: execTuples.c:216
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc)
Definition: execTuples.c:199
TupleDesc tupDesc
Definition: portal.h:155
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1078
Tuplestorestate * holdStore
Definition: portal.h:164
bool(* receiveSlot)(TupleTableSlot *slot, DestReceiver *self)
Definition: dest.h:118

Variable Documentation

Portal ActivePortal = NULL