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

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

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

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

340 {
341  /* no point in looking if we determined it doesn't return tuples */
342  if (portal->strategy == PORTAL_MULTI_QUERY)
343  return NIL;
344  /* get the primary statement and find out what it returns */
346 }
#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:361
PortalStrategy strategy
Definition: portal.h:143
List* FetchStatementTargetList ( Node stmt)

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

Referenced by CachedPlanGetTargetList(), and FetchPortalTargetList().

362 {
363  if (stmt == NULL)
364  return NIL;
365  if (IsA(stmt, Query))
366  {
367  Query *query = (Query *) stmt;
368 
369  if (query->commandType == CMD_UTILITY)
370  {
371  /* transfer attention to utility statement */
372  stmt = query->utilityStmt;
373  }
374  else
375  {
376  if (query->commandType == CMD_SELECT)
377  return query->targetList;
378  if (query->returningList)
379  return query->returningList;
380  return NIL;
381  }
382  }
383  if (IsA(stmt, PlannedStmt))
384  {
385  PlannedStmt *pstmt = (PlannedStmt *) stmt;
386 
387  if (pstmt->commandType == CMD_UTILITY)
388  {
389  /* transfer attention to utility statement */
390  stmt = pstmt->utilityStmt;
391  }
392  else
393  {
394  if (pstmt->commandType == CMD_SELECT)
395  return pstmt->planTree->targetlist;
396  if (pstmt->hasReturning)
397  return pstmt->planTree->targetlist;
398  return NIL;
399  }
400  }
401  if (IsA(stmt, FetchStmt))
402  {
403  FetchStmt *fstmt = (FetchStmt *) stmt;
404  Portal subportal;
405 
406  Assert(!fstmt->ismove);
407  subportal = GetPortalByName(fstmt->portalname);
408  Assert(PortalIsValid(subportal));
409  return FetchPortalTargetList(subportal);
410  }
411  if (IsA(stmt, ExecuteStmt))
412  {
413  ExecuteStmt *estmt = (ExecuteStmt *) stmt;
414  PreparedStatement *entry;
415 
416  entry = FetchPreparedStatement(estmt->name, true);
417  return FetchPreparedStatementTargetList(entry);
418  }
419  return NIL;
420 }
#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:84
List * returningList
Definition: parsenodes.h:144
char * portalname
Definition: parsenodes.h:2644
#define PortalIsValid(p)
Definition: portal.h:199
bool ismove
Definition: parsenodes.h:2645
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:134
char * name
Definition: parsenodes.h:3263
List * FetchPortalTargetList(Portal portal)
Definition: pquery.c:339
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 690 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().

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

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

629 {
630  int natts;
631  int i;
632 
633  /* Do nothing if portal won't return tuples */
634  if (portal->tupDesc == NULL)
635  return;
636  natts = portal->tupDesc->natts;
637  portal->formats = (int16 *)
639  natts * sizeof(int16));
640  if (nFormats > 1)
641  {
642  /* format specified for each column */
643  if (nFormats != natts)
644  ereport(ERROR,
645  (errcode(ERRCODE_PROTOCOL_VIOLATION),
646  errmsg("bind message has %d result formats but query has %d columns",
647  nFormats, natts)));
648  memcpy(portal->formats, formats, natts * sizeof(int16));
649  }
650  else if (nFormats > 0)
651  {
652  /* single format specified, use for all columns */
653  int16 format1 = formats[0];
654 
655  for (i = 0; i < natts; i++)
656  portal->formats[i] = format1;
657  }
658  else
659  {
660  /* use default format for all columns */
661  for (i = 0; i < natts; i++)
662  portal->formats[i] = 0;
663  }
664 }
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 446 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().

448 {
449  Portal saveActivePortal;
450  ResourceOwner saveResourceOwner;
451  MemoryContext savePortalContext;
452  MemoryContext oldContext;
453  QueryDesc *queryDesc;
454  int myeflags;
455 
456  AssertArg(PortalIsValid(portal));
457  AssertState(portal->status == PORTAL_DEFINED);
458 
459  /*
460  * Set up global portal context pointers.
461  */
462  saveActivePortal = ActivePortal;
463  saveResourceOwner = CurrentResourceOwner;
464  savePortalContext = PortalContext;
465  PG_TRY();
466  {
467  ActivePortal = portal;
468  if (portal->resowner)
469  CurrentResourceOwner = portal->resowner;
471 
472  oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
473 
474  /* Must remember portal param list, if any */
475  portal->portalParams = params;
476 
477  /*
478  * Determine the portal execution strategy
479  */
480  portal->strategy = ChoosePortalStrategy(portal->stmts);
481 
482  /*
483  * Fire her up according to the strategy
484  */
485  switch (portal->strategy)
486  {
487  case PORTAL_ONE_SELECT:
488 
489  /* Must set snapshot before starting executor. */
490  if (snapshot)
491  PushActiveSnapshot(snapshot);
492  else
494 
495  /*
496  * Create QueryDesc in portal's context; for the moment, set
497  * the destination to DestNone.
498  */
499  queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
500  portal->sourceText,
504  params,
505  portal->queryEnv,
506  0);
507 
508  /*
509  * If it's a scrollable cursor, executor needs to support
510  * REWIND and backwards scan, as well as whatever the caller
511  * might've asked for.
512  */
513  if (portal->cursorOptions & CURSOR_OPT_SCROLL)
514  myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
515  else
516  myeflags = eflags;
517 
518  /*
519  * Call ExecutorStart to prepare the plan for execution
520  */
521  ExecutorStart(queryDesc, myeflags);
522 
523  /*
524  * This tells PortalCleanup to shut down the executor
525  */
526  portal->queryDesc = queryDesc;
527 
528  /*
529  * Remember tuple descriptor (computed by ExecutorStart)
530  */
531  portal->tupDesc = queryDesc->tupDesc;
532 
533  /*
534  * Reset cursor position data to "start of query"
535  */
536  portal->atStart = true;
537  portal->atEnd = false; /* allow fetches */
538  portal->portalPos = 0;
539 
541  break;
542 
544  case PORTAL_ONE_MOD_WITH:
545 
546  /*
547  * We don't start the executor until we are told to run the
548  * portal. We do need to set up the result tupdesc.
549  */
550  {
551  PlannedStmt *pstmt;
552 
553  pstmt = PortalGetPrimaryStmt(portal);
554  portal->tupDesc =
556  false);
557  }
558 
559  /*
560  * Reset cursor position data to "start of query"
561  */
562  portal->atStart = true;
563  portal->atEnd = false; /* allow fetches */
564  portal->portalPos = 0;
565  break;
566 
567  case PORTAL_UTIL_SELECT:
568 
569  /*
570  * We don't set snapshot here, because PortalRunUtility will
571  * take care of it if needed.
572  */
573  {
574  PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
575 
576  Assert(pstmt->commandType == CMD_UTILITY);
577  portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
578  }
579 
580  /*
581  * Reset cursor position data to "start of query"
582  */
583  portal->atStart = true;
584  portal->atEnd = false; /* allow fetches */
585  portal->portalPos = 0;
586  break;
587 
588  case PORTAL_MULTI_QUERY:
589  /* Need do nothing now */
590  portal->tupDesc = NULL;
591  break;
592  }
593  }
594  PG_CATCH();
595  {
596  /* Uncaught error while executing portal: mark it dead */
597  MarkPortalFailed(portal);
598 
599  /* Restore global vars and propagate error */
600  ActivePortal = saveActivePortal;
601  CurrentResourceOwner = saveResourceOwner;
602  PortalContext = savePortalContext;
603 
604  PG_RE_THROW();
605  }
606  PG_END_TRY();
607 
608  MemoryContextSwitchTo(oldContext);
609 
610  ActivePortal = saveActivePortal;
611  CurrentResourceOwner = saveResourceOwner;
612  PortalContext = savePortalContext;
613 
614  portal->status = PORTAL_READY;
615 }
#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:144
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
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:300
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:728
Node * utilityStmt
Definition: plannodes.h:84
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:222
List * targetlist
Definition: plannodes.h:134
ResourceOwner resowner
Definition: portal.h:120
bool atStart
Definition: portal.h:186
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:2594
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:1762
#define PG_END_TRY()
Definition: elog.h:300
uint64 portalPos
Definition: portal.h:188

Variable Documentation