PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pquery.h File Reference
#include "nodes/parsenodes.h"
#include "utils/portal.h"
Include dependency graph for pquery.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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

Variables

PGDLLIMPORT Portal ActivePortal
 

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:560
Definition: nodes.h:509
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:514
bool hasModifyingCTE
Definition: parsenodes.h:129
#define elog
Definition: elog.h:219
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:509
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, NULL, 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:560
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:2665
#define PortalIsValid(p)
Definition: portal.h:199
bool ismove
Definition: parsenodes.h:2666
CmdType commandType
Definition: plannodes.h:45
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition: prepare.c:549
CmdType commandType
Definition: parsenodes.h:110
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
List * targetlist
Definition: plannodes.h:144
char * name
Definition: parsenodes.h:3284
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:338
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition: prepare.c:494
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, result, 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:4395
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void MarkPortalActive(Portal portal)
Definition: portalmem.c:388
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
return result
Definition: formatting.c:1633
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:4388
#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:677
#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:675
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:316
#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, result, 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
return result
Definition: formatting.c:1633
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:677
#define PortalIsValid(p)
Definition: portal.h:199
#define PG_CATCH()
Definition: elog.h:293
#define Assert(condition)
Definition: c.h:675
#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
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, NULL, 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:255
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 NULL
Definition: c.h:229
#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:707
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, NULL, 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:678
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:146
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:677
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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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:2615
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

Variable Documentation