PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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.

Typedefs

typedef struct PlannedStmt PlannedStmt
 

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, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
 
uint64 PortalRunFetch (Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
 
bool PlannedStmtRequiresSnapshot (PlannedStmt *pstmt)
 
void EnsurePortalSnapshotExists (void)
 

Variables

PGDLLIMPORT Portal ActivePortal
 

Typedef Documentation

◆ PlannedStmt

Definition at line 20 of file pquery.h.

Function Documentation

◆ ChoosePortalStrategy()

PortalStrategy ChoosePortalStrategy ( List stmts)
extern

Definition at line 206 of file pquery.c.

207{
208 int nSetTag;
209 ListCell *lc;
210
211 /*
212 * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
213 * single-statement case, since there are no rewrite rules that can add
214 * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
215 * likewise allows only one top-level statement.
216 */
217 if (list_length(stmts) == 1)
218 {
219 Node *stmt = (Node *) linitial(stmts);
220
221 if (IsA(stmt, Query))
222 {
223 Query *query = (Query *) stmt;
224
225 if (query->canSetTag)
226 {
227 if (query->commandType == CMD_SELECT)
228 {
229 if (query->hasModifyingCTE)
230 return PORTAL_ONE_MOD_WITH;
231 else
232 return PORTAL_ONE_SELECT;
233 }
234 if (query->commandType == CMD_UTILITY)
235 {
237 return PORTAL_UTIL_SELECT;
238 /* it can't be ONE_RETURNING, so give up */
239 return PORTAL_MULTI_QUERY;
240 }
241 }
242 }
243 else if (IsA(stmt, PlannedStmt))
244 {
245 PlannedStmt *pstmt = (PlannedStmt *) stmt;
246
247 if (pstmt->canSetTag)
248 {
249 if (pstmt->commandType == CMD_SELECT)
250 {
251 if (pstmt->hasModifyingCTE)
252 return PORTAL_ONE_MOD_WITH;
253 else
254 return PORTAL_ONE_SELECT;
255 }
256 if (pstmt->commandType == CMD_UTILITY)
257 {
259 return PORTAL_UTIL_SELECT;
260 /* it can't be ONE_RETURNING, so give up */
261 return PORTAL_MULTI_QUERY;
262 }
263 }
264 }
265 else
266 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
267 }
268
269 /*
270 * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
271 * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
272 * it has a RETURNING list.
273 */
274 nSetTag = 0;
275 foreach(lc, stmts)
276 {
277 Node *stmt = (Node *) lfirst(lc);
278
279 if (IsA(stmt, Query))
280 {
281 Query *query = (Query *) stmt;
282
283 if (query->canSetTag)
284 {
285 if (++nSetTag > 1)
286 return PORTAL_MULTI_QUERY; /* no need to look further */
287 if (query->commandType == CMD_UTILITY ||
288 query->returningList == NIL)
289 return PORTAL_MULTI_QUERY; /* no need to look further */
290 }
291 }
292 else if (IsA(stmt, PlannedStmt))
293 {
294 PlannedStmt *pstmt = (PlannedStmt *) stmt;
295
296 if (pstmt->canSetTag)
297 {
298 if (++nSetTag > 1)
299 return PORTAL_MULTI_QUERY; /* no need to look further */
300 if (pstmt->commandType == CMD_UTILITY ||
301 !pstmt->hasReturning)
302 return PORTAL_MULTI_QUERY; /* no need to look further */
303 }
304 }
305 else
306 elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
307 }
308 if (nSetTag == 1)
310
311 /* Else, it's the general case... */
312 return PORTAL_MULTI_QUERY;
313}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define stmt
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define nodeTag(nodeptr)
Definition nodes.h:139
@ CMD_UTILITY
Definition nodes.h:280
@ CMD_SELECT
Definition nodes.h:275
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
@ PORTAL_ONE_RETURNING
Definition portal.h:92
@ PORTAL_MULTI_QUERY
Definition portal.h:95
@ PORTAL_ONE_SELECT
Definition portal.h:91
@ PORTAL_ONE_MOD_WITH
Definition portal.h:93
@ PORTAL_UTIL_SELECT
Definition portal.h:94
static int fb(int x)
Definition nodes.h:135
bool hasModifyingCTE
Definition plannodes.h:81
bool canSetTag
Definition plannodes.h:84
bool hasReturning
Definition plannodes.h:78
CmdType commandType
Definition plannodes.h:66
Node * utilityStmt
Definition plannodes.h:153
List * returningList
Definition parsenodes.h:217
CmdType commandType
Definition parsenodes.h:121
Node * utilityStmt
Definition parsenodes.h:141
bool UtilityReturnsTuples(Node *parsetree)
Definition utility.c:2042

References PlannedStmt::canSetTag, CMD_SELECT, CMD_UTILITY, Query::commandType, PlannedStmt::commandType, elog, ERROR, fb(), PlannedStmt::hasModifyingCTE, PlannedStmt::hasReturning, IsA, lfirst, linitial, list_length(), NIL, nodeTag, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_UTIL_SELECT, Query::returningList, stmt, UtilityReturnsTuples(), Query::utilityStmt, and PlannedStmt::utilityStmt.

Referenced by PlanCacheComputeResultDesc(), and PortalStart().

◆ EnsurePortalSnapshotExists()

void EnsurePortalSnapshotExists ( void  )
extern

Definition at line 1761 of file pquery.c.

1762{
1763 Portal portal;
1764
1765 /*
1766 * Nothing to do if a snapshot is set. (We take it on faith that the
1767 * outermost active snapshot belongs to some Portal; or if there is no
1768 * Portal, it's somebody else's responsibility to manage things.)
1769 */
1770 if (ActiveSnapshotSet())
1771 return;
1772
1773 /* Otherwise, we'd better have an active Portal */
1774 portal = ActivePortal;
1775 if (unlikely(portal == NULL))
1776 elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1777 Assert(portal->portalSnapshot == NULL);
1778
1779 /*
1780 * Create a new snapshot, make it active, and remember it in portal.
1781 * Because the portal now references the snapshot, we must tell snapmgr.c
1782 * that the snapshot belongs to the portal's transaction level, else we
1783 * risk portalSnapshot becoming a dangling pointer.
1784 */
1786 /* PushActiveSnapshotWithLevel might have copied the snapshot */
1788}
#define Assert(condition)
Definition c.h:943
#define unlikely(x)
Definition c.h:438
Portal ActivePortal
Definition pquery.c:36
Snapshot GetTransactionSnapshot(void)
Definition snapmgr.c:272
bool ActiveSnapshotSet(void)
Definition snapmgr.c:812
void PushActiveSnapshotWithLevel(Snapshot snapshot, int snap_level)
Definition snapmgr.c:696
Snapshot GetActiveSnapshot(void)
Definition snapmgr.c:800
Snapshot portalSnapshot
Definition portal.h:169
int createLevel
Definition portal.h:133

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

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

◆ FetchPortalTargetList()

List * FetchPortalTargetList ( Portal  portal)
extern

Definition at line 323 of file pquery.c.

324{
325 /* no point in looking if we determined it doesn't return tuples */
326 if (portal->strategy == PORTAL_MULTI_QUERY)
327 return NIL;
328 /* get the primary statement and find out what it returns */
330}
PlannedStmt * PortalGetPrimaryStmt(Portal portal)
Definition portalmem.c:153
List * FetchStatementTargetList(Node *stmt)
Definition pquery.c:345
PortalStrategy strategy
Definition portal.h:146

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

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

◆ FetchStatementTargetList()

List * FetchStatementTargetList ( Node stmt)
extern

Definition at line 345 of file pquery.c.

346{
347 if (stmt == NULL)
348 return NIL;
349 if (IsA(stmt, Query))
350 {
351 Query *query = (Query *) stmt;
352
353 if (query->commandType == CMD_UTILITY)
354 {
355 /* transfer attention to utility statement */
356 stmt = query->utilityStmt;
357 }
358 else
359 {
360 if (query->commandType == CMD_SELECT)
361 return query->targetList;
362 if (query->returningList)
363 return query->returningList;
364 return NIL;
365 }
366 }
367 if (IsA(stmt, PlannedStmt))
368 {
369 PlannedStmt *pstmt = (PlannedStmt *) stmt;
370
371 if (pstmt->commandType == CMD_UTILITY)
372 {
373 /* transfer attention to utility statement */
374 stmt = pstmt->utilityStmt;
375 }
376 else
377 {
378 if (pstmt->commandType == CMD_SELECT)
379 return pstmt->planTree->targetlist;
380 if (pstmt->hasReturning)
381 return pstmt->planTree->targetlist;
382 return NIL;
383 }
384 }
385 if (IsA(stmt, FetchStmt))
386 {
389
390 Assert(!fstmt->ismove);
391 subportal = GetPortalByName(fstmt->portalname);
394 }
395 if (IsA(stmt, ExecuteStmt))
396 {
398 PreparedStatement *entry;
399
400 entry = FetchPreparedStatement(estmt->name, true);
402 }
403 return NIL;
404}
PreparedStatement * FetchPreparedStatement(const char *stmt_name, bool throwError)
Definition prepare.c:436
List * FetchPreparedStatementTargetList(PreparedStatement *stmt)
Definition prepare.c:491
#define PortalIsValid(p)
Definition portal.h:211
Portal GetPortalByName(const char *name)
Definition portalmem.c:132
List * FetchPortalTargetList(Portal portal)
Definition pquery.c:323
List * targetlist
Definition plannodes.h:235
struct Plan * planTree
Definition plannodes.h:99
List * targetList
Definition parsenodes.h:201

References Assert, CMD_SELECT, CMD_UTILITY, Query::commandType, PlannedStmt::commandType, fb(), FetchPortalTargetList(), FetchPreparedStatement(), FetchPreparedStatementTargetList(), GetPortalByName(), PlannedStmt::hasReturning, IsA, NIL, PlannedStmt::planTree, PortalIsValid, Query::returningList, stmt, Query::targetList, Plan::targetlist, Query::utilityStmt, and PlannedStmt::utilityStmt.

Referenced by CachedPlanGetTargetList(), and FetchPortalTargetList().

◆ PlannedStmtRequiresSnapshot()

bool PlannedStmtRequiresSnapshot ( PlannedStmt pstmt)
extern

Definition at line 1712 of file pquery.c.

1713{
1714 Node *utilityStmt = pstmt->utilityStmt;
1715
1716 /* If it's not a utility statement, it definitely needs a snapshot */
1717 if (utilityStmt == NULL)
1718 return true;
1719
1720 /*
1721 * Most utility statements need a snapshot, and the default presumption
1722 * about new ones should be that they do too. Hence, enumerate those that
1723 * do not need one.
1724 *
1725 * Transaction control, LOCK, and SET must *not* set a snapshot, since
1726 * they need to be executable at the start of a transaction-snapshot-mode
1727 * transaction without freezing a snapshot. By extension we allow SHOW
1728 * not to set a snapshot. The other stmts listed are just efficiency
1729 * hacks. Beware of listing anything that can modify the database --- if,
1730 * say, it has to update an index with expressions that invoke
1731 * user-defined functions, then it had better have a snapshot.
1732 */
1733 if (IsA(utilityStmt, TransactionStmt) ||
1734 IsA(utilityStmt, LockStmt) ||
1735 IsA(utilityStmt, VariableSetStmt) ||
1736 IsA(utilityStmt, VariableShowStmt) ||
1737 IsA(utilityStmt, ConstraintsSetStmt) ||
1738 /* efficiency hacks from here down */
1739 IsA(utilityStmt, FetchStmt) ||
1740 IsA(utilityStmt, ListenStmt) ||
1741 IsA(utilityStmt, NotifyStmt) ||
1742 IsA(utilityStmt, UnlistenStmt) ||
1743 IsA(utilityStmt, CheckPointStmt) ||
1744 IsA(utilityStmt, WaitStmt))
1745 return false;
1746
1747 return true;
1748}

References fb(), IsA, and PlannedStmt::utilityStmt.

Referenced by _SPI_execute_plan(), and PortalRunUtility().

◆ PortalRun()

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

Definition at line 681 of file pquery.c.

684{
685 bool result;
686 uint64 nprocessed;
693
694 Assert(PortalIsValid(portal));
695
697
698 /* Initialize empty completion data */
699 if (qc)
701
703 {
704 elog(DEBUG3, "PortalRun");
705 /* PORTAL_MULTI_QUERY logs its own stats per query */
706 ResetUsage();
707 }
708
709 /*
710 * Check for improper portal use, and mark portal active.
711 */
712 MarkPortalActive(portal);
713
714 /*
715 * Set up global portal context pointers.
716 *
717 * We have to play a special game here to support utility commands like
718 * VACUUM and CLUSTER, which internally start and commit transactions.
719 * When we are called to execute such a command, CurrentResourceOwner will
720 * be pointing to the TopTransactionResourceOwner --- which will be
721 * destroyed and replaced in the course of the internal commit and
722 * restart. So we need to be prepared to restore it as pointing to the
723 * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
724 * internally starting whole new transactions is not good.)
725 * CurrentMemoryContext has a similar problem, but the other pointers we
726 * save here will be NULL or pointing to longer-lived objects.
727 */
734 PG_TRY();
735 {
736 ActivePortal = portal;
737 if (portal->resowner)
740
742
743 switch (portal->strategy)
744 {
749
750 /*
751 * If we have not yet run the command, do so, storing its
752 * results in the portal's tuplestore. But we don't do that
753 * for the PORTAL_ONE_SELECT case.
754 */
755 if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
757
758 /*
759 * Now fetch desired portion of results.
760 */
761 nprocessed = PortalRunSelect(portal, true, count, dest);
762
763 /*
764 * If the portal result contains a command tag and the caller
765 * gave us a pointer to store it, copy it and update the
766 * rowcount.
767 */
768 if (qc && portal->qc.commandTag != CMDTAG_UNKNOWN)
769 {
770 CopyQueryCompletion(qc, &portal->qc);
771 qc->nprocessed = nprocessed;
772 }
773
774 /* Mark portal not active */
775 portal->status = PORTAL_READY;
776
777 /*
778 * Since it's a forward fetch, say DONE iff atEnd is now true.
779 */
780 result = portal->atEnd;
781 break;
782
784 PortalRunMulti(portal, isTopLevel, false,
785 dest, altdest, qc);
786
787 /* Prevent portal's commands from being re-executed */
788 MarkPortalDone(portal);
789
790 /* Always complete at end of RunMulti */
791 result = true;
792 break;
793
794 default:
795 elog(ERROR, "unrecognized portal strategy: %d",
796 (int) portal->strategy);
797 result = false; /* keep compiler quiet */
798 break;
799 }
800 }
801 PG_CATCH();
802 {
803 /* Uncaught error while executing portal: mark it dead */
804 MarkPortalFailed(portal);
805
806 /* Restore global vars and propagate error */
809 else
814 else
817
818 PG_RE_THROW();
819 }
820 PG_END_TRY();
821
824 else
829 else
832
834 ShowUsage("EXECUTOR STATISTICS");
835
837
838 return result;
839}
uint64_t uint64
Definition c.h:625
uint32 result
void InitializeQueryCompletion(QueryCompletion *qc)
Definition cmdtag.c:40
static void CopyQueryCompletion(QueryCompletion *dst, const QueryCompletion *src)
Definition cmdtag.h:45
#define PG_RE_THROW()
Definition elog.h:407
#define DEBUG3
Definition elog.h:29
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_CATCH(...)
Definition elog.h:384
bool log_executor_stats
Definition guc_tables.c:550
MemoryContext TopTransactionContext
Definition mcxt.c:171
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
MemoryContext PortalContext
Definition mcxt.c:175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
@ PORTAL_READY
Definition portal.h:107
void MarkPortalDone(Portal portal)
Definition portalmem.c:415
void MarkPortalFailed(Portal portal)
Definition portalmem.c:443
void MarkPortalActive(Portal portal)
Definition portalmem.c:396
void ShowUsage(const char *title)
Definition postgres.c:5137
void ResetUsage(void)
Definition postgres.c:5130
static void PortalRunMulti(Portal portal, bool isTopLevel, bool setHoldSnapshot, DestReceiver *dest, DestReceiver *altdest, QueryCompletion *qc)
Definition pquery.c:1182
static void FillPortalStore(Portal portal, bool isTopLevel)
Definition pquery.c:991
static uint64 PortalRunSelect(Portal portal, bool forward, long count, DestReceiver *dest)
Definition pquery.c:860
ResourceOwner TopTransactionResourceOwner
Definition resowner.c:175
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
bool atEnd
Definition portal.h:199
ResourceOwner resowner
Definition portal.h:121
QueryCompletion qc
Definition portal.h:138
MemoryContext portalContext
Definition portal.h:120
Tuplestorestate * holdStore
Definition portal.h:176
PortalStatus status
Definition portal.h:150
uint64 nprocessed
Definition cmdtag.h:32
CommandTag commandTag
Definition cmdtag.h:31

References ActivePortal, Assert, PortalData::atEnd, QueryCompletion::commandTag, CopyQueryCompletion(), CurrentMemoryContext, CurrentResourceOwner, DEBUG3, elog, ERROR, fb(), FillPortalStore(), PortalData::holdStore, InitializeQueryCompletion(), log_executor_stats, MarkPortalActive(), MarkPortalDone(), MarkPortalFailed(), MemoryContextSwitchTo(), QueryCompletion::nprocessed, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalIsValid, PortalRunMulti(), PortalRunSelect(), PortalData::qc, ResetUsage(), PortalData::resowner, result, ShowUsage(), PortalData::status, PortalData::strategy, TopTransactionContext, and TopTransactionResourceOwner.

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

◆ PortalRunFetch()

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

Definition at line 1374 of file pquery.c.

1378{
1379 uint64 result;
1384
1385 Assert(PortalIsValid(portal));
1386
1387 /*
1388 * Check for improper portal use, and mark portal active.
1389 */
1390 MarkPortalActive(portal);
1391
1392 /*
1393 * Set up global portal context pointers.
1394 */
1398 PG_TRY();
1399 {
1400 ActivePortal = portal;
1401 if (portal->resowner)
1403 PortalContext = portal->portalContext;
1404
1406
1407 switch (portal->strategy)
1408 {
1409 case PORTAL_ONE_SELECT:
1410 result = DoPortalRunFetch(portal, fdirection, count, dest);
1411 break;
1412
1415 case PORTAL_UTIL_SELECT:
1416
1417 /*
1418 * If we have not yet run the command, do so, storing its
1419 * results in the portal's tuplestore.
1420 */
1421 if (!portal->holdStore)
1422 FillPortalStore(portal, false /* isTopLevel */ );
1423
1424 /*
1425 * Now fetch desired portion of results.
1426 */
1427 result = DoPortalRunFetch(portal, fdirection, count, dest);
1428 break;
1429
1430 default:
1431 elog(ERROR, "unsupported portal strategy");
1432 result = 0; /* keep compiler quiet */
1433 break;
1434 }
1435 }
1436 PG_CATCH();
1437 {
1438 /* Uncaught error while executing portal: mark it dead */
1439 MarkPortalFailed(portal);
1440
1441 /* Restore global vars and propagate error */
1445
1446 PG_RE_THROW();
1447 }
1448 PG_END_TRY();
1449
1451
1452 /* Mark portal not active */
1453 portal->status = PORTAL_READY;
1454
1458
1459 return result;
1460}
static uint64 DoPortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition pquery.c:1472

References ActivePortal, Assert, CurrentResourceOwner, DoPortalRunFetch(), elog, ERROR, fb(), FillPortalStore(), PortalData::holdStore, MarkPortalActive(), MarkPortalFailed(), MemoryContextSwitchTo(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalIsValid, PortalData::resowner, result, PortalData::status, and PortalData::strategy.

Referenced by _SPI_cursor_operation(), and PerformPortalFetch().

◆ PortalSetResultFormat()

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

Definition at line 620 of file pquery.c.

621{
622 int natts;
623 int i;
624
625 /* Do nothing if portal won't return tuples */
626 if (portal->tupDesc == NULL)
627 return;
628 natts = portal->tupDesc->natts;
629 portal->formats = (int16 *)
631 natts * sizeof(int16));
632 if (nFormats > 1)
633 {
634 /* format specified for each column */
635 if (nFormats != natts)
638 errmsg("bind message has %d result formats but query has %d columns",
639 nFormats, natts)));
640 memcpy(portal->formats, formats, natts * sizeof(int16));
641 }
642 else if (nFormats > 0)
643 {
644 /* single format specified, use for all columns */
645 int16 format1 = formats[0];
646
647 for (i = 0; i < natts; i++)
648 portal->formats[i] = format1;
649 }
650 else
651 {
652 /* use default format for all columns */
653 for (i = 0; i < natts; i++)
654 portal->formats[i] = 0;
655 }
656}
int16_t int16
Definition c.h:619
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:152
#define ERRCODE_PROTOCOL_VIOLATION
Definition fe-connect.c:96
int i
Definition isn.c:77
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
static char * errmsg
int16 * formats
Definition portal.h:161
TupleDesc tupDesc
Definition portal.h:159

References ereport, errcode(), ERRCODE_PROTOCOL_VIOLATION, errmsg, ERROR, fb(), PortalData::formats, i, memcpy(), MemoryContextAlloc(), TupleDescData::natts, PortalData::portalContext, and PortalData::tupDesc.

Referenced by exec_bind_message(), and exec_simple_query().

◆ PortalStart()

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

Definition at line 430 of file pquery.c.

432{
437 QueryDesc *queryDesc;
438 int myeflags;
439
440 Assert(PortalIsValid(portal));
441 Assert(portal->status == PORTAL_DEFINED);
442
443 /*
444 * Set up global portal context pointers.
445 */
449 PG_TRY();
450 {
451 ActivePortal = portal;
452 if (portal->resowner)
455
457
458 /* Must remember portal param list, if any */
459 portal->portalParams = params;
460
461 /*
462 * Determine the portal execution strategy
463 */
464 portal->strategy = ChoosePortalStrategy(portal->stmts);
465
466 /*
467 * Fire her up according to the strategy
468 */
469 switch (portal->strategy)
470 {
472
473 /* Must set snapshot before starting executor. */
474 if (snapshot)
475 PushActiveSnapshot(snapshot);
476 else
478
479 /*
480 * We could remember the snapshot in portal->portalSnapshot,
481 * but presently there seems no need to, as this code path
482 * cannot be used for non-atomic execution. Hence there can't
483 * be any commit/abort that might destroy the snapshot. Since
484 * we don't do that, there's also no need to force a
485 * non-default nesting level for the snapshot.
486 */
487
488 /*
489 * Create QueryDesc in portal's context; for the moment, set
490 * the destination to DestNone.
491 */
492 queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
493 portal->sourceText,
497 params,
498 portal->queryEnv,
499 0);
500
501 /*
502 * If it's a scrollable cursor, executor needs to support
503 * REWIND and backwards scan, as well as whatever the caller
504 * might've asked for.
505 */
506 if (portal->cursorOptions & CURSOR_OPT_SCROLL)
508 else
509 myeflags = eflags;
510
511 /*
512 * Call ExecutorStart to prepare the plan for execution
513 */
514 ExecutorStart(queryDesc, myeflags);
515
516 /*
517 * This tells PortalCleanup to shut down the executor
518 */
519 portal->queryDesc = queryDesc;
520
521 /*
522 * Remember tuple descriptor (computed by ExecutorStart)
523 */
524 portal->tupDesc = queryDesc->tupDesc;
525
526 /*
527 * Reset cursor position data to "start of query"
528 */
529 portal->atStart = true;
530 portal->atEnd = false; /* allow fetches */
531 portal->portalPos = 0;
532
534 break;
535
538
539 /*
540 * We don't start the executor until we are told to run the
541 * portal. We do need to set up the result tupdesc.
542 */
543 {
544 PlannedStmt *pstmt;
545
546 pstmt = PortalGetPrimaryStmt(portal);
547 portal->tupDesc =
549 }
550
551 /*
552 * Reset cursor position data to "start of query"
553 */
554 portal->atStart = true;
555 portal->atEnd = false; /* allow fetches */
556 portal->portalPos = 0;
557 break;
558
560
561 /*
562 * We don't set snapshot here, because PortalRunUtility will
563 * take care of it if needed.
564 */
565 {
566 PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
567
568 Assert(pstmt->commandType == CMD_UTILITY);
570 }
571
572 /*
573 * Reset cursor position data to "start of query"
574 */
575 portal->atStart = true;
576 portal->atEnd = false; /* allow fetches */
577 portal->portalPos = 0;
578 break;
579
581 /* Need do nothing now */
582 portal->tupDesc = NULL;
583 break;
584 }
585 }
586 PG_CATCH();
587 {
588 /* Uncaught error while executing portal: mark it dead */
589 MarkPortalFailed(portal);
590
591 /* Restore global vars and propagate error */
595
596 PG_RE_THROW();
597 }
598 PG_END_TRY();
599
601
605
606 portal->status = PORTAL_READY;
607}
DestReceiver * None_Receiver
Definition dest.c:96
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition execMain.c:124
TupleDesc ExecCleanTypeFromTL(List *targetList)
#define EXEC_FLAG_BACKWARD
Definition executor.h:70
#define EXEC_FLAG_REWIND
Definition executor.h:69
#define CURSOR_OPT_SCROLL
#define linitial_node(type, l)
Definition pg_list.h:181
@ PORTAL_DEFINED
Definition portal.h:106
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition pquery.c:68
PortalStrategy ChoosePortalStrategy(List *stmts)
Definition pquery.c:206
void PushActiveSnapshot(Snapshot snapshot)
Definition snapmgr.c:682
void PopActiveSnapshot(void)
Definition snapmgr.c:775
#define InvalidSnapshot
Definition snapshot.h:119
uint64 portalPos
Definition portal.h:200
QueryDesc * queryDesc
Definition portal.h:156
const char * sourceText
Definition portal.h:136
bool atStart
Definition portal.h:198
List * stmts
Definition portal.h:139
QueryEnvironment * queryEnv
Definition portal.h:143
ParamListInfo portalParams
Definition portal.h:142
int cursorOptions
Definition portal.h:147
TupleDesc tupDesc
Definition execdesc.h:49
TupleDesc UtilityTupleDescriptor(Node *parsetree)
Definition utility.c:2101

References ActivePortal, Assert, PortalData::atEnd, PortalData::atStart, ChoosePortalStrategy(), CMD_UTILITY, PlannedStmt::commandType, CreateQueryDesc(), CurrentResourceOwner, CURSOR_OPT_SCROLL, PortalData::cursorOptions, EXEC_FLAG_BACKWARD, EXEC_FLAG_REWIND, ExecCleanTypeFromTL(), ExecutorStart(), fb(), GetActiveSnapshot(), GetTransactionSnapshot(), InvalidSnapshot, linitial_node, MarkPortalFailed(), MemoryContextSwitchTo(), None_Receiver, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PlannedStmt::planTree, PopActiveSnapshot(), PORTAL_DEFINED, PORTAL_MULTI_QUERY, PORTAL_ONE_MOD_WITH, PORTAL_ONE_RETURNING, PORTAL_ONE_SELECT, PORTAL_READY, PORTAL_UTIL_SELECT, PortalContext, PortalData::portalContext, PortalGetPrimaryStmt(), PortalIsValid, PortalData::portalParams, PortalData::portalPos, PushActiveSnapshot(), PortalData::queryDesc, PortalData::queryEnv, PortalData::resowner, PortalData::sourceText, PortalData::status, PortalData::stmts, PortalData::strategy, Plan::targetlist, QueryDesc::tupDesc, PortalData::tupDesc, PlannedStmt::utilityStmt, and UtilityTupleDescriptor().

Referenced by exec_bind_message(), exec_simple_query(), ExecuteQuery(), PerformCursorOpen(), and SPI_cursor_open_internal().

Variable Documentation

◆ ActivePortal