Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pquery.c
4 : : * POSTGRES process query command code
5 : : *
6 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/tcop/pquery.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : :
16 : : #include "postgres.h"
17 : :
18 : : #include <limits.h>
19 : :
20 : : #include "access/xact.h"
21 : : #include "commands/prepare.h"
22 : : #include "executor/executor.h"
23 : : #include "executor/tstoreReceiver.h"
24 : : #include "miscadmin.h"
25 : : #include "pg_trace.h"
26 : : #include "tcop/pquery.h"
27 : : #include "tcop/utility.h"
28 : : #include "utils/memutils.h"
29 : : #include "utils/snapmgr.h"
30 : :
31 : :
32 : : /*
33 : : * ActivePortal is the currently executing Portal (the most closely nested,
34 : : * if there are several).
35 : : */
36 : : Portal ActivePortal = NULL;
37 : :
38 : :
39 : : static void ProcessQuery(PlannedStmt *plan,
40 : : const char *sourceText,
41 : : ParamListInfo params,
42 : : QueryEnvironment *queryEnv,
43 : : DestReceiver *dest,
44 : : QueryCompletion *qc);
45 : : static void FillPortalStore(Portal portal, bool isTopLevel);
46 : : static uint64 RunFromStore(Portal portal, ScanDirection direction, uint64 count,
47 : : DestReceiver *dest);
48 : : static uint64 PortalRunSelect(Portal portal, bool forward, long count,
49 : : DestReceiver *dest);
50 : : static void PortalRunUtility(Portal portal, PlannedStmt *pstmt,
51 : : bool isTopLevel, bool setHoldSnapshot,
52 : : DestReceiver *dest, QueryCompletion *qc);
53 : : static void PortalRunMulti(Portal portal,
54 : : bool isTopLevel, bool setHoldSnapshot,
55 : : DestReceiver *dest, DestReceiver *altdest,
56 : : QueryCompletion *qc);
57 : : static uint64 DoPortalRunFetch(Portal portal,
58 : : FetchDirection fdirection,
59 : : long count,
60 : : DestReceiver *dest);
61 : : static void DoPortalRewind(Portal portal);
62 : :
63 : :
64 : : /*
65 : : * CreateQueryDesc
66 : : */
67 : : QueryDesc *
6505 bruce@momjian.us 68 :CBC 285365 : CreateQueryDesc(PlannedStmt *plannedstmt,
69 : : const char *sourceText,
70 : : Snapshot snapshot,
71 : : Snapshot crosscheck_snapshot,
72 : : DestReceiver *dest,
73 : : ParamListInfo params,
74 : : QueryEnvironment *queryEnv,
75 : : int instrument_options)
76 : : {
10225 77 : 285365 : QueryDesc *qd = (QueryDesc *) palloc(sizeof(QueryDesc));
78 : :
6773 tgl@sss.pgh.pa.us 79 : 285365 : qd->operation = plannedstmt->commandType; /* operation */
2999 80 : 285365 : qd->plannedstmt = plannedstmt; /* plan */
5931 bruce@momjian.us 81 : 285365 : qd->sourceText = sourceText; /* query text */
6326 alvherre@alvh.no-ip. 82 : 285365 : qd->snapshot = RegisterSnapshot(snapshot); /* snapshot */
83 : : /* RI check snapshot */
84 : 285365 : qd->crosscheck_snapshot = RegisterSnapshot(crosscheck_snapshot);
10226 bruce@momjian.us 85 : 285365 : qd->dest = dest; /* output dest */
8311 tgl@sss.pgh.pa.us 86 : 285365 : qd->params = params; /* parameter values passed into query */
3081 kgrittn@postgresql.o 87 : 285365 : qd->queryEnv = queryEnv;
2999 tgl@sss.pgh.pa.us 88 : 285365 : qd->instrument_options = instrument_options; /* instrumentation wanted? */
89 : :
90 : : /* null these fields until set by ExecutorStart */
8311 91 : 285365 : qd->tupDesc = NULL;
92 : 285365 : qd->estate = NULL;
93 : 285365 : qd->planstate = NULL;
6135 94 : 285365 : qd->totaltime = NULL;
95 : :
96 : : /* not yet executed */
3089 rhaas@postgresql.org 97 : 285365 : qd->already_executed = false;
98 : :
8311 tgl@sss.pgh.pa.us 99 : 285365 : return qd;
100 : : }
101 : :
102 : : /*
103 : : * FreeQueryDesc
104 : : */
105 : : void
8301 106 : 270979 : FreeQueryDesc(QueryDesc *qdesc)
107 : : {
108 : : /* Can't be a live query */
109 [ - + ]: 270979 : Assert(qdesc->estate == NULL);
110 : :
111 : : /* forget our snapshots */
6326 alvherre@alvh.no-ip. 112 : 270979 : UnregisterSnapshot(qdesc->snapshot);
113 : 270979 : UnregisterSnapshot(qdesc->crosscheck_snapshot);
114 : :
115 : : /* Only the QueryDesc itself need be freed */
8301 tgl@sss.pgh.pa.us 116 : 270979 : pfree(qdesc);
117 : 270979 : }
118 : :
119 : :
120 : : /*
121 : : * ProcessQuery
122 : : * Execute a single plannable query within a PORTAL_MULTI_QUERY,
123 : : * PORTAL_ONE_RETURNING, or PORTAL_ONE_MOD_WITH portal
124 : : *
125 : : * plan: the plan tree for the query
126 : : * sourceText: the source text of the query
127 : : * params: any parameters needed
128 : : * dest: where to send results
129 : : * qc: where to store the command completion status data.
130 : : *
131 : : * qc may be NULL if caller doesn't want a status string.
132 : : *
133 : : * Must be called in a memory context that will be reset or deleted on
134 : : * error; otherwise the executor's memory usage will be leaked.
135 : : */
136 : : static void
6505 bruce@momjian.us 137 : 44271 : ProcessQuery(PlannedStmt *plan,
138 : : const char *sourceText,
139 : : ParamListInfo params,
140 : : QueryEnvironment *queryEnv,
141 : : DestReceiver *dest,
142 : : QueryCompletion *qc)
143 : : {
144 : : QueryDesc *queryDesc;
145 : :
146 : : /*
147 : : * Create the QueryDesc object
148 : : */
107 amitlan@postgresql.o 149 : 44271 : queryDesc = CreateQueryDesc(plan, sourceText,
150 : : GetActiveSnapshot(), InvalidSnapshot,
151 : : dest, params, queryEnv, 0);
152 : :
153 : : /*
154 : : * Call ExecutorStart to prepare the plan for execution
155 : : */
156 : 44271 : ExecutorStart(queryDesc, 0);
157 : :
158 : : /*
159 : : * Run the plan to completion.
160 : : */
271 tgl@sss.pgh.pa.us 161 : 43761 : ExecutorRun(queryDesc, ForwardScanDirection, 0);
162 : :
163 : : /*
164 : : * Build command completion status data, if caller wants one.
165 : : */
2014 alvherre@alvh.no-ip. 166 [ + + ]: 42154 : if (qc)
167 : : {
6773 tgl@sss.pgh.pa.us 168 [ + + + + : 41842 : switch (queryDesc->operation)
+ - ]
169 : : {
8593 170 : 60 : case CMD_SELECT:
2014 alvherre@alvh.no-ip. 171 : 60 : SetQueryCompletion(qc, CMDTAG_SELECT, queryDesc->estate->es_processed);
8593 tgl@sss.pgh.pa.us 172 : 60 : break;
173 : 33950 : case CMD_INSERT:
2014 alvherre@alvh.no-ip. 174 : 33950 : SetQueryCompletion(qc, CMDTAG_INSERT, queryDesc->estate->es_processed);
8593 tgl@sss.pgh.pa.us 175 : 33950 : break;
176 : 5551 : case CMD_UPDATE:
2014 alvherre@alvh.no-ip. 177 : 5551 : SetQueryCompletion(qc, CMDTAG_UPDATE, queryDesc->estate->es_processed);
8593 tgl@sss.pgh.pa.us 178 : 5551 : break;
179 : 1739 : case CMD_DELETE:
2014 alvherre@alvh.no-ip. 180 : 1739 : SetQueryCompletion(qc, CMDTAG_DELETE, queryDesc->estate->es_processed);
8593 tgl@sss.pgh.pa.us 181 : 1739 : break;
1258 alvherre@alvh.no-ip. 182 : 542 : case CMD_MERGE:
183 : 542 : SetQueryCompletion(qc, CMDTAG_MERGE, queryDesc->estate->es_processed);
184 : 542 : break;
8593 tgl@sss.pgh.pa.us 185 :UBC 0 : default:
2014 alvherre@alvh.no-ip. 186 : 0 : SetQueryCompletion(qc, CMDTAG_UNKNOWN, queryDesc->estate->es_processed);
8593 tgl@sss.pgh.pa.us 187 : 0 : break;
188 : : }
189 : : }
190 : :
191 : : /*
192 : : * Now, we close down all the scans and free allocated resources.
193 : : */
5305 tgl@sss.pgh.pa.us 194 :CBC 42154 : ExecutorFinish(queryDesc);
8311 195 : 41615 : ExecutorEnd(queryDesc);
196 : :
8301 197 : 41615 : FreeQueryDesc(queryDesc);
10651 scrappy@hub.org 198 : 41615 : }
199 : :
200 : : /*
201 : : * ChoosePortalStrategy
202 : : * Select portal execution strategy given the intended statement list.
203 : : *
204 : : * The list elements can be Querys or PlannedStmts.
205 : : * That's more general than portals need, but plancache.c uses this too.
206 : : *
207 : : * See the comments in portal.h.
208 : : */
209 : : PortalStrategy
6773 tgl@sss.pgh.pa.us 210 : 407393 : ChoosePortalStrategy(List *stmts)
211 : : {
212 : : int nSetTag;
213 : : ListCell *lc;
214 : :
215 : : /*
216 : : * PORTAL_ONE_SELECT and PORTAL_UTIL_SELECT need only consider the
217 : : * single-statement case, since there are no rewrite rules that can add
218 : : * auxiliary queries to a SELECT or a utility command. PORTAL_ONE_MOD_WITH
219 : : * likewise allows only one top-level statement.
220 : : */
221 [ + + ]: 407393 : if (list_length(stmts) == 1)
222 : : {
223 : 407225 : Node *stmt = (Node *) linitial(stmts);
224 : :
225 [ + + ]: 407225 : if (IsA(stmt, Query))
226 : : {
227 : 39161 : Query *query = (Query *) stmt;
228 : :
229 [ + - ]: 39161 : if (query->canSetTag)
230 : : {
3157 231 [ + + ]: 39161 : if (query->commandType == CMD_SELECT)
232 : : {
5307 233 [ - + ]: 28154 : if (query->hasModifyingCTE)
5307 tgl@sss.pgh.pa.us 234 :UBC 0 : return PORTAL_ONE_MOD_WITH;
235 : : else
5307 tgl@sss.pgh.pa.us 236 :CBC 28154 : return PORTAL_ONE_SELECT;
237 : : }
3157 238 [ + + ]: 11007 : if (query->commandType == CMD_UTILITY)
239 : : {
6773 240 [ + + ]: 8203 : if (UtilityReturnsTuples(query->utilityStmt))
241 : 5028 : return PORTAL_UTIL_SELECT;
242 : : /* it can't be ONE_RETURNING, so give up */
243 : 3175 : return PORTAL_MULTI_QUERY;
244 : : }
245 : : }
246 : : }
247 [ + - ]: 368064 : else if (IsA(stmt, PlannedStmt))
248 : : {
249 : 368064 : PlannedStmt *pstmt = (PlannedStmt *) stmt;
250 : :
251 [ + + ]: 368064 : if (pstmt->canSetTag)
252 : : {
3157 253 [ + + ]: 368046 : if (pstmt->commandType == CMD_SELECT)
254 : : {
5307 255 [ + + ]: 145122 : if (pstmt->hasModifyingCTE)
256 : 66 : return PORTAL_ONE_MOD_WITH;
257 : : else
258 : 145056 : return PORTAL_ONE_SELECT;
259 : : }
3157 260 [ + + ]: 222924 : if (pstmt->commandType == CMD_UTILITY)
261 : : {
262 [ + + ]: 179193 : if (UtilityReturnsTuples(pstmt->utilityStmt))
263 : 23253 : return PORTAL_UTIL_SELECT;
264 : : /* it can't be ONE_RETURNING, so give up */
265 : 155940 : return PORTAL_MULTI_QUERY;
266 : : }
267 : : }
268 : : }
269 : : else
3157 tgl@sss.pgh.pa.us 270 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
271 : : }
272 : :
273 : : /*
274 : : * PORTAL_ONE_RETURNING has to allow auxiliary queries added by rewrite.
275 : : * Choose PORTAL_ONE_RETURNING if there is exactly one canSetTag query and
276 : : * it has a RETURNING list.
277 : : */
6963 tgl@sss.pgh.pa.us 278 :CBC 46721 : nSetTag = 0;
6773 279 [ + + + + : 48473 : foreach(lc, stmts)
+ + ]
280 : : {
281 : 46805 : Node *stmt = (Node *) lfirst(lc);
282 : :
283 [ + + ]: 46805 : if (IsA(stmt, Query))
284 : : {
285 : 2810 : Query *query = (Query *) stmt;
286 : :
287 [ + + ]: 2810 : if (query->canSetTag)
288 : : {
289 [ - + ]: 2807 : if (++nSetTag > 1)
290 : 45053 : return PORTAL_MULTI_QUERY; /* no need to look further */
3157 291 [ + - ]: 2807 : if (query->commandType == CMD_UTILITY ||
292 [ + + ]: 2807 : query->returningList == NIL)
6773 293 : 2656 : return PORTAL_MULTI_QUERY; /* no need to look further */
294 : : }
295 : : }
296 [ + - ]: 43995 : else if (IsA(stmt, PlannedStmt))
297 : : {
298 : 43995 : PlannedStmt *pstmt = (PlannedStmt *) stmt;
299 : :
300 [ + + ]: 43995 : if (pstmt->canSetTag)
301 : : {
302 [ - + ]: 43890 : if (++nSetTag > 1)
6773 tgl@sss.pgh.pa.us 303 :UBC 0 : return PORTAL_MULTI_QUERY; /* no need to look further */
3157 tgl@sss.pgh.pa.us 304 [ + - ]:CBC 43890 : if (pstmt->commandType == CMD_UTILITY ||
305 [ + + ]: 43890 : !pstmt->hasReturning)
6773 306 : 42397 : return PORTAL_MULTI_QUERY; /* no need to look further */
307 : : }
308 : : }
309 : : else
3157 tgl@sss.pgh.pa.us 310 [ # # ]:UBC 0 : elog(ERROR, "unrecognized node type: %d", (int) nodeTag(stmt));
311 : : }
6963 tgl@sss.pgh.pa.us 312 [ + + ]:CBC 1668 : if (nSetTag == 1)
313 : 1644 : return PORTAL_ONE_RETURNING;
314 : :
315 : : /* Else, it's the general case... */
316 : 24 : return PORTAL_MULTI_QUERY;
317 : : }
318 : :
319 : : /*
320 : : * FetchPortalTargetList
321 : : * Given a portal that returns tuples, extract the query targetlist.
322 : : * Returns NIL if the portal doesn't have a determinable targetlist.
323 : : *
324 : : * Note: do not modify the result.
325 : : */
326 : : List *
7381 327 : 155727 : FetchPortalTargetList(Portal portal)
328 : : {
329 : : /* no point in looking if we determined it doesn't return tuples */
6773 330 [ + + ]: 155727 : if (portal->strategy == PORTAL_MULTI_QUERY)
331 : 15 : return NIL;
332 : : /* get the primary statement and find out what it returns */
3157 333 : 155712 : return FetchStatementTargetList((Node *) PortalGetPrimaryStmt(portal));
334 : : }
335 : :
336 : : /*
337 : : * FetchStatementTargetList
338 : : * Given a statement that returns tuples, extract the query targetlist.
339 : : * Returns NIL if the statement doesn't have a determinable targetlist.
340 : : *
341 : : * This can be applied to a Query or a PlannedStmt.
342 : : * That's more general than portals need, but plancache.c uses this too.
343 : : *
344 : : * Note: do not modify the result.
345 : : *
346 : : * XXX be careful to keep this in sync with UtilityReturnsTuples.
347 : : */
348 : : List *
6773 349 : 163708 : FetchStatementTargetList(Node *stmt)
350 : : {
351 [ - + ]: 163708 : if (stmt == NULL)
6773 tgl@sss.pgh.pa.us 352 :UBC 0 : return NIL;
6773 tgl@sss.pgh.pa.us 353 [ + + ]:CBC 163708 : if (IsA(stmt, Query))
354 : : {
355 : 7996 : Query *query = (Query *) stmt;
356 : :
3157 357 [ + + ]: 7996 : if (query->commandType == CMD_UTILITY)
358 : : {
359 : : /* transfer attention to utility statement */
6773 360 : 6 : stmt = query->utilityStmt;
361 : : }
362 : : else
363 : : {
3157 364 [ + + ]: 7990 : if (query->commandType == CMD_SELECT)
6773 365 : 7984 : return query->targetList;
366 [ + - ]: 6 : if (query->returningList)
367 : 6 : return query->returningList;
6773 tgl@sss.pgh.pa.us 368 :UBC 0 : return NIL;
369 : : }
370 : : }
6773 tgl@sss.pgh.pa.us 371 [ + + ]:CBC 155718 : if (IsA(stmt, PlannedStmt))
372 : : {
373 : 155712 : PlannedStmt *pstmt = (PlannedStmt *) stmt;
374 : :
3157 375 [ + + ]: 155712 : if (pstmt->commandType == CMD_UTILITY)
376 : : {
377 : : /* transfer attention to utility statement */
378 : 19068 : stmt = pstmt->utilityStmt;
379 : : }
380 : : else
381 : : {
382 [ + + ]: 136644 : if (pstmt->commandType == CMD_SELECT)
383 : 135226 : return pstmt->planTree->targetlist;
384 [ + - ]: 1418 : if (pstmt->hasReturning)
385 : 1418 : return pstmt->planTree->targetlist;
3157 tgl@sss.pgh.pa.us 386 :UBC 0 : return NIL;
387 : : }
388 : : }
6773 tgl@sss.pgh.pa.us 389 [ + + ]:CBC 19074 : if (IsA(stmt, FetchStmt))
390 : : {
391 : 2783 : FetchStmt *fstmt = (FetchStmt *) stmt;
392 : : Portal subportal;
393 : :
394 [ - + ]: 2783 : Assert(!fstmt->ismove);
395 : 2783 : subportal = GetPortalByName(fstmt->portalname);
396 [ - + ]: 2783 : Assert(PortalIsValid(subportal));
397 : 2783 : return FetchPortalTargetList(subportal);
398 : : }
399 [ + + ]: 16291 : if (IsA(stmt, ExecuteStmt))
400 : : {
401 : 7953 : ExecuteStmt *estmt = (ExecuteStmt *) stmt;
402 : : PreparedStatement *entry;
403 : :
404 : 7953 : entry = FetchPreparedStatement(estmt->name, true);
405 : 7953 : return FetchPreparedStatementTargetList(entry);
406 : : }
7381 407 : 8338 : return NIL;
408 : : }
409 : :
410 : : /*
411 : : * PortalStart
412 : : * Prepare a portal for execution.
413 : : *
414 : : * Caller must already have created the portal, done PortalDefineQuery(),
415 : : * and adjusted portal options if needed.
416 : : *
417 : : * If parameters are needed by the query, they must be passed in "params"
418 : : * (caller is responsible for giving them appropriate lifetime).
419 : : *
420 : : * The caller can also provide an initial set of "eflags" to be passed to
421 : : * ExecutorStart (but note these can be modified internally, and they are
422 : : * currently only honored for PORTAL_ONE_SELECT portals). Most callers
423 : : * should simply pass zero.
424 : : *
425 : : * The caller can optionally pass a snapshot to be used; pass InvalidSnapshot
426 : : * for the normal behavior of setting a new snapshot. This parameter is
427 : : * presently ignored for non-PORTAL_ONE_SELECT portals (it's only intended
428 : : * to be used for cursors).
429 : : *
430 : : * On return, portal is ready to accept PortalRun() calls, and the result
431 : : * tupdesc (if any) is known.
432 : : */
433 : : void
4919 434 : 368226 : PortalStart(Portal portal, ParamListInfo params,
435 : : int eflags, Snapshot snapshot)
436 : : {
437 : : Portal saveActivePortal;
438 : : ResourceOwner saveResourceOwner;
439 : : MemoryContext savePortalContext;
440 : : MemoryContext oldContext;
441 : : QueryDesc *queryDesc;
442 : : int myeflags;
443 : :
1044 peter@eisentraut.org 444 [ - + ]: 368226 : Assert(PortalIsValid(portal));
445 [ - + ]: 368226 : Assert(portal->status == PORTAL_DEFINED);
446 : :
447 : : /*
448 : : * Set up global portal context pointers.
449 : : */
7721 tgl@sss.pgh.pa.us 450 : 368226 : saveActivePortal = ActivePortal;
451 : 368226 : saveResourceOwner = CurrentResourceOwner;
452 : 368226 : savePortalContext = PortalContext;
7707 453 [ + + ]: 368226 : PG_TRY();
454 : : {
455 : 368226 : ActivePortal = portal;
4468 456 [ + - ]: 368226 : if (portal->resowner)
457 : 368226 : CurrentResourceOwner = portal->resowner;
2821 peter_e@gmx.net 458 : 368226 : PortalContext = portal->portalContext;
459 : :
460 : 368226 : oldContext = MemoryContextSwitchTo(PortalContext);
461 : :
462 : : /* Must remember portal param list, if any */
7707 tgl@sss.pgh.pa.us 463 : 368226 : portal->portalParams = params;
464 : :
465 : : /*
466 : : * Determine the portal execution strategy
467 : : */
6773 468 : 368226 : portal->strategy = ChoosePortalStrategy(portal->stmts);
469 : :
470 : : /*
471 : : * Fire her up according to the strategy
472 : : */
7707 473 [ + + + + : 368226 : switch (portal->strategy)
- ]
474 : : {
475 : 145056 : case PORTAL_ONE_SELECT:
476 : :
477 : : /* Must set snapshot before starting executor. */
4667 478 [ + + ]: 145056 : if (snapshot)
479 : 12255 : PushActiveSnapshot(snapshot);
480 : : else
6326 alvherre@alvh.no-ip. 481 : 132801 : PushActiveSnapshot(GetTransactionSnapshot());
482 : :
483 : : /*
484 : : * We could remember the snapshot in portal->portalSnapshot,
485 : : * but presently there seems no need to, as this code path
486 : : * cannot be used for non-atomic execution. Hence there can't
487 : : * be any commit/abort that might destroy the snapshot. Since
488 : : * we don't do that, there's also no need to force a
489 : : * non-default nesting level for the snapshot.
490 : : */
491 : :
492 : : /*
493 : : * Create QueryDesc in portal's context; for the moment, set
494 : : * the destination to DestNone.
495 : : */
3071 tgl@sss.pgh.pa.us 496 : 145056 : queryDesc = CreateQueryDesc(linitial_node(PlannedStmt, portal->stmts),
497 : : portal->sourceText,
498 : : GetActiveSnapshot(),
499 : : InvalidSnapshot,
500 : : None_Receiver,
501 : : params,
502 : : portal->queryEnv,
503 : : 0);
504 : :
505 : : /*
506 : : * If it's a scrollable cursor, executor needs to support
507 : : * REWIND and backwards scan, as well as whatever the caller
508 : : * might've asked for.
509 : : */
7130 510 [ + + ]: 145056 : if (portal->cursorOptions & CURSOR_OPT_SCROLL)
4919 511 : 1946 : myeflags = eflags | EXEC_FLAG_REWIND | EXEC_FLAG_BACKWARD;
512 : : else
513 : 143110 : myeflags = eflags;
514 : :
515 : : /*
516 : : * Call ExecutorStart to prepare the plan for execution
517 : : */
107 amitlan@postgresql.o 518 : 145056 : ExecutorStart(queryDesc, myeflags);
519 : :
520 : : /*
521 : : * This tells PortalCleanup to shut down the executor
522 : : */
7707 tgl@sss.pgh.pa.us 523 : 144743 : portal->queryDesc = queryDesc;
524 : :
525 : : /*
526 : : * Remember tuple descriptor (computed by ExecutorStart)
527 : : */
528 : 144743 : portal->tupDesc = queryDesc->tupDesc;
529 : :
530 : : /*
531 : : * Reset cursor position data to "start of query"
532 : : */
533 : 144743 : portal->atStart = true;
7678 bruce@momjian.us 534 : 144743 : portal->atEnd = false; /* allow fetches */
7707 tgl@sss.pgh.pa.us 535 : 144743 : portal->portalPos = 0;
536 : :
6326 alvherre@alvh.no-ip. 537 : 144743 : PopActiveSnapshot();
7707 tgl@sss.pgh.pa.us 538 : 144743 : break;
539 : :
6965 540 : 1559 : case PORTAL_ONE_RETURNING:
541 : : case PORTAL_ONE_MOD_WITH:
542 : :
543 : : /*
544 : : * We don't start the executor until we are told to run the
545 : : * portal. We do need to set up the result tupdesc.
546 : : */
547 : : {
548 : : PlannedStmt *pstmt;
549 : :
3157 550 : 1559 : pstmt = PortalGetPrimaryStmt(portal);
6773 551 : 1559 : portal->tupDesc =
2482 andres@anarazel.de 552 : 1559 : ExecCleanTypeFromTL(pstmt->planTree->targetlist);
553 : : }
554 : :
555 : : /*
556 : : * Reset cursor position data to "start of query"
557 : : */
6965 tgl@sss.pgh.pa.us 558 : 1559 : portal->atStart = true;
559 : 1559 : portal->atEnd = false; /* allow fetches */
560 : 1559 : portal->portalPos = 0;
561 : 1559 : break;
562 : :
7707 563 : 23253 : case PORTAL_UTIL_SELECT:
564 : :
565 : : /*
566 : : * We don't set snapshot here, because PortalRunUtility will
567 : : * take care of it if needed.
568 : : */
569 : : {
3157 570 : 23253 : PlannedStmt *pstmt = PortalGetPrimaryStmt(portal);
571 : :
572 [ - + ]: 23253 : Assert(pstmt->commandType == CMD_UTILITY);
573 : 23253 : portal->tupDesc = UtilityTupleDescriptor(pstmt->utilityStmt);
574 : : }
575 : :
576 : : /*
577 : : * Reset cursor position data to "start of query"
578 : : */
7707 579 : 23239 : portal->atStart = true;
7678 bruce@momjian.us 580 : 23239 : portal->atEnd = false; /* allow fetches */
7707 tgl@sss.pgh.pa.us 581 : 23239 : portal->portalPos = 0;
582 : 23239 : break;
583 : :
584 : 198358 : case PORTAL_MULTI_QUERY:
585 : : /* Need do nothing now */
586 : 198358 : portal->tupDesc = NULL;
587 : 198358 : break;
588 : : }
589 : : }
590 : 327 : PG_CATCH();
591 : : {
592 : : /* Uncaught error while executing portal: mark it dead */
4952 593 : 327 : MarkPortalFailed(portal);
594 : :
595 : : /* Restore global vars and propagate error */
7707 596 : 327 : ActivePortal = saveActivePortal;
597 : 327 : CurrentResourceOwner = saveResourceOwner;
598 : 327 : PortalContext = savePortalContext;
599 : :
600 : 327 : PG_RE_THROW();
601 : : }
602 [ - + ]: 367899 : PG_END_TRY();
603 : :
8163 604 : 367899 : MemoryContextSwitchTo(oldContext);
605 : :
7721 606 : 367899 : ActivePortal = saveActivePortal;
607 : 367899 : CurrentResourceOwner = saveResourceOwner;
608 : 367899 : PortalContext = savePortalContext;
609 : :
610 : 367899 : portal->status = PORTAL_READY;
8163 611 : 367899 : }
612 : :
613 : : /*
614 : : * PortalSetResultFormat
615 : : * Select the format codes for a portal's output.
616 : : *
617 : : * This must be run after PortalStart for a portal that will be read by
618 : : * a DestRemote or DestRemoteExecute destination. It is not presently needed
619 : : * for other destination types.
620 : : *
621 : : * formats[] is the client format request, as per Bind message conventions.
622 : : */
623 : : void
8157 624 : 351542 : PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
625 : : {
626 : : int natts;
627 : : int i;
628 : :
629 : : /* Do nothing if portal won't return tuples */
630 [ + + ]: 351542 : if (portal->tupDesc == NULL)
631 : 198307 : return;
632 : 153235 : natts = portal->tupDesc->natts;
633 : 153235 : portal->formats = (int16 *)
2821 peter_e@gmx.net 634 : 153235 : MemoryContextAlloc(portal->portalContext,
635 : : natts * sizeof(int16));
8157 tgl@sss.pgh.pa.us 636 [ - + ]: 153235 : if (nFormats > 1)
637 : : {
638 : : /* format specified for each column */
8157 tgl@sss.pgh.pa.us 639 [ # # ]:UBC 0 : if (nFormats != natts)
8082 640 [ # # ]: 0 : ereport(ERROR,
641 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
642 : : errmsg("bind message has %d result formats but query has %d columns",
643 : : nFormats, natts)));
8157 644 : 0 : memcpy(portal->formats, formats, natts * sizeof(int16));
645 : : }
8069 bruce@momjian.us 646 [ + - ]:CBC 153235 : else if (nFormats > 0)
647 : : {
648 : : /* single format specified, use for all columns */
8157 tgl@sss.pgh.pa.us 649 : 153235 : int16 format1 = formats[0];
650 : :
651 [ + + ]: 657541 : for (i = 0; i < natts; i++)
652 : 504306 : portal->formats[i] = format1;
653 : : }
654 : : else
655 : : {
656 : : /* use default format for all columns */
8157 tgl@sss.pgh.pa.us 657 [ # # ]:UBC 0 : for (i = 0; i < natts; i++)
658 : 0 : portal->formats[i] = 0;
659 : : }
660 : : }
661 : :
662 : : /*
663 : : * PortalRun
664 : : * Run a portal's query or queries.
665 : : *
666 : : * count <= 0 is interpreted as a no-op: the destination gets started up
667 : : * and shut down, but nothing else happens. Also, count == FETCH_ALL is
668 : : * interpreted as "all rows". Note that count is ignored in multi-query
669 : : * situations, where we always run the portal to completion.
670 : : *
671 : : * isTopLevel: true if query is being executed at backend "top level"
672 : : * (that is, directly from a client command message)
673 : : *
674 : : * dest: where to send output of primary (canSetTag) query
675 : : *
676 : : * altdest: where to send output of non-primary queries
677 : : *
678 : : * qc: where to store command completion status data.
679 : : * May be NULL if caller doesn't want status data.
680 : : *
681 : : * Returns true if the portal's execution is complete, false if it was
682 : : * suspended due to exhaustion of the count parameter.
683 : : */
684 : : bool
271 tgl@sss.pgh.pa.us 685 :CBC 359570 : PortalRun(Portal portal, long count, bool isTopLevel,
686 : : DestReceiver *dest, DestReceiver *altdest,
687 : : QueryCompletion *qc)
688 : : {
689 : : bool result;
690 : : uint64 nprocessed;
691 : : ResourceOwner saveTopTransactionResourceOwner;
692 : : MemoryContext saveTopTransactionContext;
693 : : Portal saveActivePortal;
694 : : ResourceOwner saveResourceOwner;
695 : : MemoryContext savePortalContext;
696 : : MemoryContext saveMemoryContext;
697 : :
1044 peter@eisentraut.org 698 [ - + ]: 359570 : Assert(PortalIsValid(portal));
699 : :
700 : : TRACE_POSTGRESQL_QUERY_EXECUTE_START();
701 : :
702 : : /* Initialize empty completion data */
2014 alvherre@alvh.no-ip. 703 [ + - ]: 359570 : if (qc)
704 : 359570 : InitializeQueryCompletion(qc);
705 : :
7842 bruce@momjian.us 706 [ - + - - ]: 359570 : if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
707 : : {
6752 tgl@sss.pgh.pa.us 708 [ # # ]:UBC 0 : elog(DEBUG3, "PortalRun");
709 : : /* PORTAL_MULTI_QUERY logs its own stats per query */
7842 bruce@momjian.us 710 : 0 : ResetUsage();
711 : : }
712 : :
713 : : /*
714 : : * Check for improper portal use, and mark portal active.
715 : : */
3655 tgl@sss.pgh.pa.us 716 :CBC 359570 : MarkPortalActive(portal);
717 : :
718 : : /*
719 : : * Set up global portal context pointers.
720 : : *
721 : : * We have to play a special game here to support utility commands like
722 : : * VACUUM and CLUSTER, which internally start and commit transactions.
723 : : * When we are called to execute such a command, CurrentResourceOwner will
724 : : * be pointing to the TopTransactionResourceOwner --- which will be
725 : : * destroyed and replaced in the course of the internal commit and
726 : : * restart. So we need to be prepared to restore it as pointing to the
727 : : * exit-time TopTransactionResourceOwner. (Ain't that ugly? This idea of
728 : : * internally starting whole new transactions is not good.)
729 : : * CurrentMemoryContext has a similar problem, but the other pointers we
730 : : * save here will be NULL or pointing to longer-lived objects.
731 : : */
7642 732 : 359570 : saveTopTransactionResourceOwner = TopTransactionResourceOwner;
733 : 359570 : saveTopTransactionContext = TopTransactionContext;
7839 734 : 359570 : saveActivePortal = ActivePortal;
7721 735 : 359570 : saveResourceOwner = CurrentResourceOwner;
8163 736 : 359570 : savePortalContext = PortalContext;
7642 737 : 359570 : saveMemoryContext = CurrentMemoryContext;
7707 738 [ + + ]: 359570 : PG_TRY();
739 : : {
740 : 359570 : ActivePortal = portal;
4468 741 [ + - ]: 359570 : if (portal->resowner)
742 : 359570 : CurrentResourceOwner = portal->resowner;
2821 peter_e@gmx.net 743 : 359570 : PortalContext = portal->portalContext;
744 : :
7642 tgl@sss.pgh.pa.us 745 : 359570 : MemoryContextSwitchTo(PortalContext);
746 : :
7707 747 [ + + - ]: 359570 : switch (portal->strategy)
748 : : {
749 : 161212 : case PORTAL_ONE_SELECT:
750 : : case PORTAL_ONE_RETURNING:
751 : : case PORTAL_ONE_MOD_WITH:
752 : : case PORTAL_UTIL_SELECT:
753 : :
754 : : /*
755 : : * If we have not yet run the command, do so, storing its
756 : : * results in the portal's tuplestore. But we don't do that
757 : : * for the PORTAL_ONE_SELECT case.
758 : : */
5681 bruce@momjian.us 759 [ + + + - ]: 161212 : if (portal->strategy != PORTAL_ONE_SELECT && !portal->holdStore)
6752 tgl@sss.pgh.pa.us 760 : 20756 : FillPortalStore(portal, isTopLevel);
761 : :
762 : : /*
763 : : * Now fetch desired portion of results.
764 : : */
5681 bruce@momjian.us 765 : 161006 : nprocessed = PortalRunSelect(portal, true, count, dest);
766 : :
767 : : /*
768 : : * If the portal result contains a command tag and the caller
769 : : * gave us a pointer to store it, copy it and update the
770 : : * rowcount.
771 : : */
2014 alvherre@alvh.no-ip. 772 [ + - + - ]: 157418 : if (qc && portal->qc.commandTag != CMDTAG_UNKNOWN)
773 : : {
774 : 157418 : CopyQueryCompletion(qc, &portal->qc);
775 : 157418 : qc->nprocessed = nprocessed;
776 : : }
777 : :
778 : : /* Mark portal not active */
7707 tgl@sss.pgh.pa.us 779 : 157418 : portal->status = PORTAL_READY;
780 : :
781 : : /*
782 : : * Since it's a forward fetch, say DONE iff atEnd is now true.
783 : : */
784 : 157418 : result = portal->atEnd;
785 : 157418 : break;
786 : :
787 : 198358 : case PORTAL_MULTI_QUERY:
3317 788 : 198358 : PortalRunMulti(portal, isTopLevel, false,
789 : : dest, altdest, qc);
790 : :
791 : : /* Prevent portal's commands from being re-executed */
5301 792 : 188070 : MarkPortalDone(portal);
793 : :
794 : : /* Always complete at end of RunMulti */
7707 795 : 188070 : result = true;
796 : 188070 : break;
797 : :
7707 tgl@sss.pgh.pa.us 798 :UBC 0 : default:
799 [ # # ]: 0 : elog(ERROR, "unrecognized portal strategy: %d",
800 : : (int) portal->strategy);
801 : : result = false; /* keep compiler quiet */
802 : : break;
803 : : }
804 : : }
7707 tgl@sss.pgh.pa.us 805 :CBC 14078 : PG_CATCH();
806 : : {
807 : : /* Uncaught error while executing portal: mark it dead */
4952 808 : 14078 : MarkPortalFailed(portal);
809 : :
810 : : /* Restore global vars and propagate error */
7642 811 [ + + ]: 14078 : if (saveMemoryContext == saveTopTransactionContext)
812 : 13893 : MemoryContextSwitchTo(TopTransactionContext);
813 : : else
814 : 185 : MemoryContextSwitchTo(saveMemoryContext);
7707 815 : 14078 : ActivePortal = saveActivePortal;
7642 816 [ + + ]: 14078 : if (saveResourceOwner == saveTopTransactionResourceOwner)
817 : 13941 : CurrentResourceOwner = TopTransactionResourceOwner;
818 : : else
819 : 137 : CurrentResourceOwner = saveResourceOwner;
7707 820 : 14078 : PortalContext = savePortalContext;
821 : :
822 : 14078 : PG_RE_THROW();
823 : : }
824 [ - + ]: 345488 : PG_END_TRY();
825 : :
7642 826 [ + + ]: 345488 : if (saveMemoryContext == saveTopTransactionContext)
827 : 324739 : MemoryContextSwitchTo(TopTransactionContext);
828 : : else
829 : 20749 : MemoryContextSwitchTo(saveMemoryContext);
7839 830 : 345488 : ActivePortal = saveActivePortal;
7642 831 [ + + ]: 345488 : if (saveResourceOwner == saveTopTransactionResourceOwner)
832 : 334377 : CurrentResourceOwner = TopTransactionResourceOwner;
833 : : else
834 : 11111 : CurrentResourceOwner = saveResourceOwner;
8163 835 : 345488 : PortalContext = savePortalContext;
836 : :
7855 bruce@momjian.us 837 [ - + - - ]: 345488 : if (log_executor_stats && portal->strategy != PORTAL_MULTI_QUERY)
7855 bruce@momjian.us 838 :UBC 0 : ShowUsage("EXECUTOR STATISTICS");
839 : :
840 : : TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
841 : :
8163 tgl@sss.pgh.pa.us 842 :CBC 345488 : return result;
843 : : }
844 : :
845 : : /*
846 : : * PortalRunSelect
847 : : * Execute a portal's query in PORTAL_ONE_SELECT mode, and also
848 : : * when fetching from a completed holdStore in PORTAL_ONE_RETURNING,
849 : : * PORTAL_ONE_MOD_WITH, and PORTAL_UTIL_SELECT cases.
850 : : *
851 : : * This handles simple N-rows-forward-or-backward cases. For more complex
852 : : * nonsequential access to a portal, see PortalRunFetch.
853 : : *
854 : : * count <= 0 is interpreted as a no-op: the destination gets started up
855 : : * and shut down, but nothing else happens. Also, count == FETCH_ALL is
856 : : * interpreted as "all rows". (cf FetchStmt.howMany)
857 : : *
858 : : * Caller must already have validated the Portal and done appropriate
859 : : * setup (cf. PortalRun).
860 : : *
861 : : * Returns number of rows processed (suitable for use in result tag)
862 : : */
863 : : static uint64
864 : 186928 : PortalRunSelect(Portal portal,
865 : : bool forward,
866 : : long count,
867 : : DestReceiver *dest)
868 : : {
869 : : QueryDesc *queryDesc;
870 : : ScanDirection direction;
871 : : uint64 nprocessed;
872 : :
873 : : /*
874 : : * NB: queryDesc will be NULL if we are fetching from a held cursor or a
875 : : * completed utility query; can't use it in that path.
876 : : */
2821 peter_e@gmx.net 877 : 186928 : queryDesc = portal->queryDesc;
878 : :
879 : : /* Caller messed up if we have neither a ready query nor held data. */
8163 tgl@sss.pgh.pa.us 880 [ + + - + ]: 186928 : Assert(queryDesc || portal->holdStore);
881 : :
882 : : /*
883 : : * Force the queryDesc destination to the right thing. This supports
884 : : * MOVE, for example, which will pass in dest = DestNone. This is okay to
885 : : * change as long as we do it on every fetch. (The Executor must not
886 : : * assume that dest never changes.)
887 : : */
888 [ + + ]: 186928 : if (queryDesc)
889 : 150341 : queryDesc->dest = dest;
890 : :
891 : : /*
892 : : * Determine which direction to go in, and check to see if we're already
893 : : * at the end of the available tuples in that direction. If so, set the
894 : : * direction to NoMovement to avoid trying to fetch any tuples. (This
895 : : * check exists because not all plan node types are robust about being
896 : : * called again if they've already returned NULL once.) Then call the
897 : : * executor (we must not skip this, because the destination needs to see a
898 : : * setup and shutdown even if no tuples are available). Finally, update
899 : : * the portal position state depending on the number of tuples that were
900 : : * retrieved.
901 : : */
902 [ + + ]: 186928 : if (forward)
903 : : {
904 [ + + + + ]: 186617 : if (portal->atEnd || count <= 0)
905 : : {
906 : 1848 : direction = NoMovementScanDirection;
3465 907 : 1848 : count = 0; /* don't pass negative count to executor */
908 : : }
909 : : else
8163 910 : 184769 : direction = ForwardScanDirection;
911 : :
912 : : /* In the executor, zero count processes all rows */
913 [ + + ]: 186617 : if (count == FETCH_ALL)
914 : 161170 : count = 0;
915 : :
916 [ + + ]: 186617 : if (portal->holdStore)
3465 917 : 36578 : nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
918 : : else
919 : : {
6326 alvherre@alvh.no-ip. 920 : 150039 : PushActiveSnapshot(queryDesc->snapshot);
271 tgl@sss.pgh.pa.us 921 : 150039 : ExecutorRun(queryDesc, direction, (uint64) count);
8163 922 : 146436 : nprocessed = queryDesc->estate->es_processed;
6326 alvherre@alvh.no-ip. 923 : 146436 : PopActiveSnapshot();
924 : : }
925 : :
7137 neilc@samurai.com 926 [ + + ]: 183014 : if (!ScanDirectionIsNoMovement(direction))
927 : : {
8163 tgl@sss.pgh.pa.us 928 [ + + ]: 181166 : if (nprocessed > 0)
2999 929 : 153039 : portal->atStart = false; /* OK to go backward now */
3465 930 [ + + + + ]: 181166 : if (count == 0 || nprocessed < (uint64) count)
8069 bruce@momjian.us 931 : 164621 : portal->atEnd = true; /* we retrieved 'em all */
8163 tgl@sss.pgh.pa.us 932 : 181166 : portal->portalPos += nprocessed;
933 : : }
934 : : }
935 : : else
936 : : {
937 [ + + ]: 311 : if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
8082 938 [ + - ]: 12 : ereport(ERROR,
939 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
940 : : errmsg("cursor can only scan forward"),
941 : : errhint("Declare it with SCROLL option to enable backward scan.")));
942 : :
8163 943 [ + + - + ]: 299 : if (portal->atStart || count <= 0)
944 : : {
945 : 36 : direction = NoMovementScanDirection;
3465 946 : 36 : count = 0; /* don't pass negative count to executor */
947 : : }
948 : : else
8163 949 : 263 : direction = BackwardScanDirection;
950 : :
951 : : /* In the executor, zero count processes all rows */
952 [ + + ]: 299 : if (count == FETCH_ALL)
953 : 31 : count = 0;
954 : :
955 [ + + ]: 299 : if (portal->holdStore)
3465 956 : 6 : nprocessed = RunFromStore(portal, direction, (uint64) count, dest);
957 : : else
958 : : {
6326 alvherre@alvh.no-ip. 959 : 293 : PushActiveSnapshot(queryDesc->snapshot);
271 tgl@sss.pgh.pa.us 960 : 293 : ExecutorRun(queryDesc, direction, (uint64) count);
8163 961 : 293 : nprocessed = queryDesc->estate->es_processed;
6326 alvherre@alvh.no-ip. 962 : 293 : PopActiveSnapshot();
963 : : }
964 : :
7137 neilc@samurai.com 965 [ + + ]: 299 : if (!ScanDirectionIsNoMovement(direction))
966 : : {
8163 tgl@sss.pgh.pa.us 967 [ + + + + ]: 263 : if (nprocessed > 0 && portal->atEnd)
968 : : {
8069 bruce@momjian.us 969 : 80 : portal->atEnd = false; /* OK to go forward now */
970 : 80 : portal->portalPos++; /* adjust for endpoint case */
971 : : }
3465 tgl@sss.pgh.pa.us 972 [ + + + + ]: 263 : if (count == 0 || nprocessed < (uint64) count)
973 : : {
8069 bruce@momjian.us 974 : 94 : portal->atStart = true; /* we retrieved 'em all */
8163 tgl@sss.pgh.pa.us 975 : 94 : portal->portalPos = 0;
976 : : }
977 : : else
978 : : {
979 : 169 : portal->portalPos -= nprocessed;
980 : : }
981 : : }
982 : : }
983 : :
984 : 183313 : return nprocessed;
985 : : }
986 : :
987 : : /*
988 : : * FillPortalStore
989 : : * Run the query and load result tuples into the portal's tuple store.
990 : : *
991 : : * This is used for PORTAL_ONE_RETURNING, PORTAL_ONE_MOD_WITH, and
992 : : * PORTAL_UTIL_SELECT cases only.
993 : : */
994 : : static void
6752 995 : 24798 : FillPortalStore(Portal portal, bool isTopLevel)
996 : : {
997 : : DestReceiver *treceiver;
998 : : QueryCompletion qc;
999 : :
2014 alvherre@alvh.no-ip. 1000 : 24798 : InitializeQueryCompletion(&qc);
6965 tgl@sss.pgh.pa.us 1001 : 24798 : PortalCreateHoldStore(portal);
6124 1002 : 24798 : treceiver = CreateDestReceiver(DestTuplestore);
1003 : 24798 : SetTuplestoreDestReceiverParams(treceiver,
1004 : : portal->holdStore,
1005 : : portal->holdContext,
1006 : : false,
1007 : : NULL,
1008 : : NULL);
1009 : :
6965 1010 [ + + - ]: 24798 : switch (portal->strategy)
1011 : : {
1012 : 1559 : case PORTAL_ONE_RETURNING:
1013 : : case PORTAL_ONE_MOD_WITH:
1014 : :
1015 : : /*
1016 : : * Run the portal to completion just as for the default
1017 : : * PORTAL_MULTI_QUERY case, but send the primary query's output to
1018 : : * the tuplestore. Auxiliary query outputs are discarded. Set the
1019 : : * portal's holdSnapshot to the snapshot used (or a copy of it).
1020 : : */
3317 1021 : 1559 : PortalRunMulti(portal, isTopLevel, true,
1022 : : treceiver, None_Receiver, &qc);
6965 1023 : 1483 : break;
1024 : :
1025 : 23239 : case PORTAL_UTIL_SELECT:
3071 1026 : 23239 : PortalRunUtility(portal, linitial_node(PlannedStmt, portal->stmts),
1027 : : isTopLevel, true, treceiver, &qc);
6965 1028 : 23106 : break;
1029 : :
6965 tgl@sss.pgh.pa.us 1030 :UBC 0 : default:
1031 [ # # ]: 0 : elog(ERROR, "unsupported portal strategy: %d",
1032 : : (int) portal->strategy);
1033 : : break;
1034 : : }
1035 : :
1036 : : /* Override portal completion data with actual command results */
2014 alvherre@alvh.no-ip. 1037 [ + + ]:CBC 24589 : if (qc.commandTag != CMDTAG_UNKNOWN)
1038 : 12216 : CopyQueryCompletion(&portal->qc, &qc);
1039 : :
2921 peter_e@gmx.net 1040 : 24589 : treceiver->rDestroy(treceiver);
6965 tgl@sss.pgh.pa.us 1041 : 24589 : }
1042 : :
1043 : : /*
1044 : : * RunFromStore
1045 : : * Fetch tuples from the portal's tuple store.
1046 : : *
1047 : : * Calling conventions are similar to ExecutorRun, except that we
1048 : : * do not depend on having a queryDesc or estate. Therefore we return the
1049 : : * number of tuples processed as the result, not in estate->es_processed.
1050 : : *
1051 : : * One difference from ExecutorRun is that the destination receiver functions
1052 : : * are run in the caller's memory context (since we have no estate). Watch
1053 : : * out for memory leaks.
1054 : : */
1055 : : static uint64
3465 1056 : 36584 : RunFromStore(Portal portal, ScanDirection direction, uint64 count,
1057 : : DestReceiver *dest)
1058 : : {
1059 : 36584 : uint64 current_tuple_count = 0;
1060 : : TupleTableSlot *slot;
1061 : :
2487 andres@anarazel.de 1062 : 36584 : slot = MakeSingleTupleTableSlot(portal->tupDesc, &TTSOpsMinimalTuple);
1063 : :
2921 peter_e@gmx.net 1064 : 36584 : dest->rStartup(dest, CMD_SELECT, portal->tupDesc);
1065 : :
7137 neilc@samurai.com 1066 [ + + ]: 36584 : if (ScanDirectionIsNoMovement(direction))
1067 : : {
1068 : : /* do nothing except start/stop the destination */
1069 : : }
1070 : : else
1071 : : {
1072 : 35300 : bool forward = ScanDirectionIsForward(direction);
1073 : :
1074 : : for (;;)
8163 tgl@sss.pgh.pa.us 1075 : 215598 : {
1076 : : MemoryContext oldcontext;
1077 : : bool ok;
1078 : :
1079 : 250898 : oldcontext = MemoryContextSwitchTo(portal->holdContext);
1080 : :
6007 1081 : 250898 : ok = tuplestore_gettupleslot(portal->holdStore, forward, false,
1082 : : slot);
1083 : :
8163 1084 : 250898 : MemoryContextSwitchTo(oldcontext);
1085 : :
7011 1086 [ + + ]: 250898 : if (!ok)
8163 1087 : 24616 : break;
1088 : :
1089 : : /*
1090 : : * If we are not able to send the tuple, we assume the destination
1091 : : * has closed and no more tuples can be sent. If that's the case,
1092 : : * end the loop.
1093 : : */
2921 peter_e@gmx.net 1094 [ - + ]: 226282 : if (!dest->receiveSlot(slot, dest))
3379 rhaas@postgresql.org 1095 :UBC 0 : break;
1096 : :
7479 tgl@sss.pgh.pa.us 1097 :CBC 226282 : ExecClearTuple(slot);
1098 : :
1099 : : /*
1100 : : * check our tuple count.. if we've processed the proper number
1101 : : * then quit, else loop again and process more tuples. Zero count
1102 : : * means no limit.
1103 : : */
8163 1104 : 226282 : current_tuple_count++;
1105 [ + + + + ]: 226282 : if (count && count == current_tuple_count)
1106 : 10684 : break;
1107 : : }
1108 : : }
1109 : :
2921 peter_e@gmx.net 1110 : 36584 : dest->rShutdown(dest);
1111 : :
7479 tgl@sss.pgh.pa.us 1112 : 36584 : ExecDropSingleTupleTableSlot(slot);
1113 : :
3465 1114 : 36584 : return current_tuple_count;
1115 : : }
1116 : :
1117 : : /*
1118 : : * PortalRunUtility
1119 : : * Execute a utility statement inside a portal.
1120 : : */
1121 : : static void
3157 1122 : 179179 : PortalRunUtility(Portal portal, PlannedStmt *pstmt,
1123 : : bool isTopLevel, bool setHoldSnapshot,
1124 : : DestReceiver *dest, QueryCompletion *qc)
1125 : : {
1126 : : /*
1127 : : * Set snapshot if utility stmt needs one.
1128 : : */
1569 1129 [ + + ]: 179179 : if (PlannedStmtRequiresSnapshot(pstmt))
1130 : : {
1131 : 143738 : Snapshot snapshot = GetTransactionSnapshot();
1132 : :
1133 : : /* If told to, register the snapshot we're using and save in portal */
3317 1134 [ + + ]: 143738 : if (setHoldSnapshot)
1135 : : {
1136 : 20014 : snapshot = RegisterSnapshot(snapshot);
1137 : 20014 : portal->holdSnapshot = snapshot;
1138 : : }
1139 : :
1140 : : /*
1141 : : * In any case, make the snapshot active and remember it in portal.
1142 : : * Because the portal now references the snapshot, we must tell
1143 : : * snapmgr.c that the snapshot belongs to the portal's transaction
1144 : : * level, else we risk portalSnapshot becoming a dangling pointer.
1145 : : */
1436 1146 : 143738 : PushActiveSnapshotWithLevel(snapshot, portal->createLevel);
1147 : : /* PushActiveSnapshotWithLevel might have copied the snapshot */
1569 1148 : 143738 : portal->portalSnapshot = GetActiveSnapshot();
1149 : : }
1150 : : else
1151 : 35441 : portal->portalSnapshot = NULL;
1152 : :
3157 1153 : 179179 : ProcessUtility(pstmt,
1154 : : portal->sourceText,
1541 1155 : 179179 : (portal->cplan != NULL), /* protect tree if in plancache */
2999 1156 : 179179 : isTopLevel ? PROCESS_UTILITY_TOPLEVEL : PROCESS_UTILITY_QUERY,
1157 : : portal->portalParams,
1158 : : portal->queryEnv,
1159 : : dest,
1160 : : qc);
1161 : :
1162 : : /* Some utility statements may change context on us */
2821 peter_e@gmx.net 1163 : 171338 : MemoryContextSwitchTo(portal->portalContext);
1164 : :
1165 : : /*
1166 : : * Some utility commands (e.g., VACUUM) pop the ActiveSnapshot stack from
1167 : : * under us, so don't complain if it's now empty. Otherwise, our snapshot
1168 : : * should be the top one; pop it. Note that this could be a different
1169 : : * snapshot from the one we made above; see EnsurePortalSnapshotExists.
1170 : : */
1569 tgl@sss.pgh.pa.us 1171 [ + + + - ]: 171338 : if (portal->portalSnapshot != NULL && ActiveSnapshotSet())
1172 : : {
1173 [ - + ]: 130902 : Assert(portal->portalSnapshot == GetActiveSnapshot());
6326 alvherre@alvh.no-ip. 1174 : 130902 : PopActiveSnapshot();
1175 : : }
1569 tgl@sss.pgh.pa.us 1176 : 171338 : portal->portalSnapshot = NULL;
8163 1177 : 171338 : }
1178 : :
1179 : : /*
1180 : : * PortalRunMulti
1181 : : * Execute a portal's queries in the general case (multi queries
1182 : : * or non-SELECT-like queries)
1183 : : */
1184 : : static void
3317 1185 : 199917 : PortalRunMulti(Portal portal,
1186 : : bool isTopLevel, bool setHoldSnapshot,
1187 : : DestReceiver *dest, DestReceiver *altdest,
1188 : : QueryCompletion *qc)
1189 : : {
5304 1190 : 199917 : bool active_snapshot_set = false;
1191 : : ListCell *stmtlist_item;
1192 : :
1193 : : /*
1194 : : * If the destination is DestRemoteExecute, change to DestNone. The
1195 : : * reason is that the client won't be expecting any tuples, and indeed has
1196 : : * no way to know what they are, since there is no provision for Describe
1197 : : * to send a RowDescription message when this portal execution strategy is
1198 : : * in effect. This presently will only affect SELECT commands added to
1199 : : * non-SELECT queries by rewrite rules: such commands will be executed,
1200 : : * but the results will be discarded unless you use "simple Query"
1201 : : * protocol.
1202 : : */
7247 alvherre@alvh.no-ip. 1203 [ + + ]: 199917 : if (dest->mydest == DestRemoteExecute)
8159 tgl@sss.pgh.pa.us 1204 : 5034 : dest = None_Receiver;
7247 alvherre@alvh.no-ip. 1205 [ + + ]: 199917 : if (altdest->mydest == DestRemoteExecute)
8159 tgl@sss.pgh.pa.us 1206 : 5034 : altdest = None_Receiver;
1207 : :
1208 : : /*
1209 : : * Loop to handle the individual queries generated from a single parsetree
1210 : : * by analysis and rewrite.
1211 : : */
6773 1212 [ + - + + : 389764 : foreach(stmtlist_item, portal->stmts)
+ + ]
1213 : : {
3071 1214 : 200211 : PlannedStmt *pstmt = lfirst_node(PlannedStmt, stmtlist_item);
1215 : :
1216 : : /*
1217 : : * If we got a cancel signal in prior command, quit
1218 : : */
8163 1219 [ - + ]: 200211 : CHECK_FOR_INTERRUPTS();
1220 : :
3157 1221 [ + + ]: 200211 : if (pstmt->utilityStmt == NULL)
1222 : : {
1223 : : /*
1224 : : * process a plannable query.
1225 : : */
1226 : : TRACE_POSTGRESQL_QUERY_EXECUTE_START();
1227 : :
8163 1228 [ - + ]: 44271 : if (log_executor_stats)
8163 tgl@sss.pgh.pa.us 1229 :UBC 0 : ResetUsage();
1230 : :
1231 : : /*
1232 : : * Must always have a snapshot for plannable queries. First time
1233 : : * through, take a new snapshot; for subsequent queries in the
1234 : : * same portal, just update the snapshot's copy of the command
1235 : : * counter.
1236 : : */
5304 tgl@sss.pgh.pa.us 1237 [ + + ]:CBC 44271 : if (!active_snapshot_set)
1238 : : {
3317 1239 : 43977 : Snapshot snapshot = GetTransactionSnapshot();
1240 : :
1241 : : /* If told to, register the snapshot and save in portal */
1242 [ + + ]: 43977 : if (setHoldSnapshot)
1243 : : {
1244 : 1559 : snapshot = RegisterSnapshot(snapshot);
1245 : 1559 : portal->holdSnapshot = snapshot;
1246 : : }
1247 : :
1248 : : /*
1249 : : * We can't have the holdSnapshot also be the active one,
1250 : : * because UpdateActiveSnapshotCommandId would complain. So
1251 : : * force an extra snapshot copy. Plain PushActiveSnapshot
1252 : : * would have copied the transaction snapshot anyway, so this
1253 : : * only adds a copy step when setHoldSnapshot is true. (It's
1254 : : * okay for the command ID of the active snapshot to diverge
1255 : : * from what holdSnapshot has.)
1256 : : */
1257 : 43977 : PushCopiedSnapshot(snapshot);
1258 : :
1259 : : /*
1260 : : * As for PORTAL_ONE_SELECT portals, it does not seem
1261 : : * necessary to maintain portal->portalSnapshot here.
1262 : : */
1263 : :
5304 1264 : 43977 : active_snapshot_set = true;
1265 : : }
1266 : : else
1267 : 294 : UpdateActiveSnapshotCommandId();
1268 : :
6773 1269 [ + + ]: 44271 : if (pstmt->canSetTag)
1270 : : {
1271 : : /* statement can set tag string */
1272 : 43956 : ProcessQuery(pstmt,
1273 : : portal->sourceText,
1274 : : portal->portalParams,
1275 : : portal->queryEnv,
1276 : : dest, qc);
1277 : : }
1278 : : else
1279 : : {
1280 : : /* stmt added by rewrite cannot set tag */
1281 : 315 : ProcessQuery(pstmt,
1282 : : portal->sourceText,
1283 : : portal->portalParams,
1284 : : portal->queryEnv,
1285 : : altdest, NULL);
1286 : : }
1287 : :
8163 1288 [ - + ]: 41615 : if (log_executor_stats)
8163 tgl@sss.pgh.pa.us 1289 :UBC 0 : ShowUsage("EXECUTOR STATISTICS");
1290 : :
1291 : : TRACE_POSTGRESQL_QUERY_EXECUTE_DONE();
1292 : : }
1293 : : else
1294 : : {
1295 : : /*
1296 : : * process utility functions (create, destroy, etc..)
1297 : : *
1298 : : * We must not set a snapshot here for utility commands (if one is
1299 : : * needed, PortalRunUtility will do it). If a utility command is
1300 : : * alone in a portal then everything's fine. The only case where
1301 : : * a utility command can be part of a longer list is that rules
1302 : : * are allowed to include NotifyStmt. NotifyStmt doesn't care
1303 : : * whether it has a snapshot or not, so we just leave the current
1304 : : * snapshot alone if we have one.
1305 : : */
3157 tgl@sss.pgh.pa.us 1306 [ + - ]:CBC 155940 : if (pstmt->canSetTag)
1307 : : {
5304 1308 [ - + ]: 155940 : Assert(!active_snapshot_set);
1309 : : /* statement can set tag string */
3157 1310 : 155940 : PortalRunUtility(portal, pstmt, isTopLevel, false,
1311 : : dest, qc);
1312 : : }
1313 : : else
1314 : : {
3157 tgl@sss.pgh.pa.us 1315 [ # # ]:UBC 0 : Assert(IsA(pstmt->utilityStmt, NotifyStmt));
1316 : : /* stmt added by rewrite cannot set tag */
1317 : 0 : PortalRunUtility(portal, pstmt, isTopLevel, false,
1318 : : altdest, NULL);
1319 : : }
1320 : : }
1321 : :
1322 : : /*
1323 : : * Clear subsidiary contexts to recover temporary memory.
1324 : : */
2821 peter_e@gmx.net 1325 [ - + ]:CBC 189847 : Assert(portal->portalContext == CurrentMemoryContext);
1326 : :
1327 : 189847 : MemoryContextDeleteChildren(portal->portalContext);
1328 : :
1329 : : /*
1330 : : * Avoid crashing if portal->stmts has been reset. This can only
1331 : : * occur if a CALL or DO utility statement executed an internal
1332 : : * COMMIT/ROLLBACK (cf PortalReleaseCachedPlan). The CALL or DO must
1333 : : * have been the only statement in the portal, so there's nothing left
1334 : : * for us to do; but we don't want to dereference a now-dangling list
1335 : : * pointer.
1336 : : */
1676 tgl@sss.pgh.pa.us 1337 [ - + ]: 189847 : if (portal->stmts == NIL)
1676 tgl@sss.pgh.pa.us 1338 :UBC 0 : break;
1339 : :
1340 : : /*
1341 : : * Increment command counter between queries, but not after the last
1342 : : * one.
1343 : : */
1676 tgl@sss.pgh.pa.us 1344 [ + + ]:CBC 189847 : if (lnext(portal->stmts, stmtlist_item) != NULL)
1345 : 294 : CommandCounterIncrement();
1346 : : }
1347 : :
1348 : : /* Pop the snapshot if we pushed one. */
5304 1349 [ + + ]: 189553 : if (active_snapshot_set)
1350 : 41321 : PopActiveSnapshot();
1351 : :
1352 : : /*
1353 : : * If a command tag was requested and we did not fill in a run-time-
1354 : : * determined tag above, copy the parse-time tag from the Portal. (There
1355 : : * might not be any tag there either, in edge cases such as empty prepared
1356 : : * statements. That's OK.)
1357 : : */
51 alvherre@kurilemu.de 1358 [ + - ]: 189553 : if (qc &&
1359 [ + + ]: 189553 : qc->commandTag == CMDTAG_UNKNOWN &&
1360 [ + - ]: 141534 : portal->qc.commandTag != CMDTAG_UNKNOWN)
1361 : 141534 : CopyQueryCompletion(qc, &portal->qc);
8163 tgl@sss.pgh.pa.us 1362 : 189553 : }
1363 : :
1364 : : /*
1365 : : * PortalRunFetch
1366 : : * Variant form of PortalRun that supports SQL FETCH directions.
1367 : : *
1368 : : * Note: we presently assume that no callers of this want isTopLevel = true.
1369 : : *
1370 : : * count <= 0 is interpreted as a no-op: the destination gets started up
1371 : : * and shut down, but nothing else happens. Also, count == FETCH_ALL is
1372 : : * interpreted as "all rows". (cf FetchStmt.howMany)
1373 : : *
1374 : : * Returns number of rows processed (suitable for use in result tag)
1375 : : */
1376 : : uint64
1377 : 25883 : PortalRunFetch(Portal portal,
1378 : : FetchDirection fdirection,
1379 : : long count,
1380 : : DestReceiver *dest)
1381 : : {
1382 : : uint64 result;
1383 : : Portal saveActivePortal;
1384 : : ResourceOwner saveResourceOwner;
1385 : : MemoryContext savePortalContext;
1386 : : MemoryContext oldContext;
1387 : :
1044 peter@eisentraut.org 1388 [ - + ]: 25883 : Assert(PortalIsValid(portal));
1389 : :
1390 : : /*
1391 : : * Check for improper portal use, and mark portal active.
1392 : : */
3655 tgl@sss.pgh.pa.us 1393 : 25883 : MarkPortalActive(portal);
1394 : :
1395 : : /*
1396 : : * Set up global portal context pointers.
1397 : : */
7839 1398 : 25874 : saveActivePortal = ActivePortal;
7721 1399 : 25874 : saveResourceOwner = CurrentResourceOwner;
8163 1400 : 25874 : savePortalContext = PortalContext;
7707 1401 [ + + ]: 25874 : PG_TRY();
1402 : : {
1403 : 25874 : ActivePortal = portal;
4468 1404 [ + + ]: 25874 : if (portal->resowner)
1405 : 25778 : CurrentResourceOwner = portal->resowner;
2821 peter_e@gmx.net 1406 : 25874 : PortalContext = portal->portalContext;
1407 : :
7707 tgl@sss.pgh.pa.us 1408 : 25874 : oldContext = MemoryContextSwitchTo(PortalContext);
1409 : :
1410 [ + + - ]: 25874 : switch (portal->strategy)
1411 : : {
1412 : 9930 : case PORTAL_ONE_SELECT:
1413 : 9930 : result = DoPortalRunFetch(portal, fdirection, count, dest);
1414 : 9900 : break;
1415 : :
6965 1416 : 15944 : case PORTAL_ONE_RETURNING:
1417 : : case PORTAL_ONE_MOD_WITH:
1418 : : case PORTAL_UTIL_SELECT:
1419 : :
1420 : : /*
1421 : : * If we have not yet run the command, do so, storing its
1422 : : * results in the portal's tuplestore.
1423 : : */
1424 [ + + ]: 15944 : if (!portal->holdStore)
6505 bruce@momjian.us 1425 : 4042 : FillPortalStore(portal, false /* isTopLevel */ );
1426 : :
1427 : : /*
1428 : : * Now fetch desired portion of results.
1429 : : */
7513 tgl@sss.pgh.pa.us 1430 : 15941 : result = DoPortalRunFetch(portal, fdirection, count, dest);
1431 : 15941 : break;
1432 : :
7707 tgl@sss.pgh.pa.us 1433 :UBC 0 : default:
1434 [ # # ]: 0 : elog(ERROR, "unsupported portal strategy");
1435 : : result = 0; /* keep compiler quiet */
1436 : : break;
1437 : : }
1438 : : }
7707 tgl@sss.pgh.pa.us 1439 :CBC 33 : PG_CATCH();
1440 : : {
1441 : : /* Uncaught error while executing portal: mark it dead */
4952 1442 : 33 : MarkPortalFailed(portal);
1443 : :
1444 : : /* Restore global vars and propagate error */
7707 1445 : 33 : ActivePortal = saveActivePortal;
1446 : 33 : CurrentResourceOwner = saveResourceOwner;
1447 : 33 : PortalContext = savePortalContext;
1448 : :
1449 : 33 : PG_RE_THROW();
1450 : : }
1451 [ - + ]: 25841 : PG_END_TRY();
1452 : :
8163 1453 : 25841 : MemoryContextSwitchTo(oldContext);
1454 : :
1455 : : /* Mark portal not active */
7721 1456 : 25841 : portal->status = PORTAL_READY;
1457 : :
7839 1458 : 25841 : ActivePortal = saveActivePortal;
7721 1459 : 25841 : CurrentResourceOwner = saveResourceOwner;
8163 1460 : 25841 : PortalContext = savePortalContext;
1461 : :
1462 : 25841 : return result;
1463 : : }
1464 : :
1465 : : /*
1466 : : * DoPortalRunFetch
1467 : : * Guts of PortalRunFetch --- the portal context is already set up
1468 : : *
1469 : : * Here, count < 0 typically reverses the direction. Also, count == FETCH_ALL
1470 : : * is interpreted as "all rows". (cf FetchStmt.howMany)
1471 : : *
1472 : : * Returns number of rows processed (suitable for use in result tag)
1473 : : */
1474 : : static uint64
1475 : 25871 : DoPortalRunFetch(Portal portal,
1476 : : FetchDirection fdirection,
1477 : : long count,
1478 : : DestReceiver *dest)
1479 : : {
1480 : : bool forward;
1481 : :
7513 1482 [ + + + + : 25871 : Assert(portal->strategy == PORTAL_ONE_SELECT ||
+ - - + ]
1483 : : portal->strategy == PORTAL_ONE_RETURNING ||
1484 : : portal->strategy == PORTAL_ONE_MOD_WITH ||
1485 : : portal->strategy == PORTAL_UTIL_SELECT);
1486 : :
1487 : : /*
1488 : : * Note: we disallow backwards fetch (including re-fetch of current row)
1489 : : * for NO SCROLL cursors, but we interpret that very loosely: you can use
1490 : : * any of the FetchDirection options, so long as the end result is to move
1491 : : * forwards by at least one row. Currently it's sufficient to check for
1492 : : * NO SCROLL in DoPortalRewind() and in the forward == false path in
1493 : : * PortalRunSelect(); but someday we might prefer to account for that
1494 : : * restriction explicitly here.
1495 : : */
8163 1496 [ + + + + : 25871 : switch (fdirection)
- ]
1497 : : {
1498 : 25492 : case FETCH_FORWARD:
1499 [ + + ]: 25492 : if (count < 0)
1500 : : {
8163 tgl@sss.pgh.pa.us 1501 :GBC 2 : fdirection = FETCH_BACKWARD;
1502 : 2 : count = -count;
1503 : : }
1504 : : /* fall out of switch to share code with FETCH_BACKWARD */
8163 tgl@sss.pgh.pa.us 1505 :CBC 25492 : break;
1506 : 253 : case FETCH_BACKWARD:
1507 [ + + ]: 253 : if (count < 0)
1508 : : {
8163 tgl@sss.pgh.pa.us 1509 :GBC 1 : fdirection = FETCH_FORWARD;
1510 : 1 : count = -count;
1511 : : }
1512 : : /* fall out of switch to share code with FETCH_FORWARD */
8163 tgl@sss.pgh.pa.us 1513 :CBC 253 : break;
1514 : 81 : case FETCH_ABSOLUTE:
1515 [ + + ]: 81 : if (count > 0)
1516 : : {
1517 : : /*
1518 : : * Definition: Rewind to start, advance count-1 rows, return
1519 : : * next row (if any).
1520 : : *
1521 : : * In practice, if the goal is less than halfway back to the
1522 : : * start, it's better to scan from where we are.
1523 : : *
1524 : : * Also, if current portalPos is outside the range of "long",
1525 : : * do it the hard way to avoid possible overflow of the count
1526 : : * argument to PortalRunSelect. We must exclude exactly
1527 : : * LONG_MAX, as well, lest the count look like FETCH_ALL.
1528 : : *
1529 : : * In any case, we arrange to fetch the target row going
1530 : : * forwards.
1531 : : */
3465 1532 [ + + ]: 48 : if ((uint64) (count - 1) <= portal->portalPos / 2 ||
1533 [ - + ]: 19 : portal->portalPos >= (uint64) LONG_MAX)
1534 : : {
8163 1535 : 29 : DoPortalRewind(portal);
1536 [ - + ]: 26 : if (count > 1)
8069 bruce@momjian.us 1537 :UBC 0 : PortalRunSelect(portal, true, count - 1,
1538 : : None_Receiver);
1539 : : }
1540 : : else
1541 : : {
3465 tgl@sss.pgh.pa.us 1542 :CBC 19 : long pos = (long) portal->portalPos;
1543 : :
8163 1544 [ - + ]: 19 : if (portal->atEnd)
8163 tgl@sss.pgh.pa.us 1545 :UBC 0 : pos++; /* need one extra fetch if off end */
8163 tgl@sss.pgh.pa.us 1546 [ + + ]:CBC 19 : if (count <= pos)
8069 bruce@momjian.us 1547 : 6 : PortalRunSelect(portal, false, pos - count + 1,
1548 : : None_Receiver);
1549 [ + + ]: 13 : else if (count > pos + 1)
1550 : 6 : PortalRunSelect(portal, true, count - pos - 1,
1551 : : None_Receiver);
1552 : : }
8163 tgl@sss.pgh.pa.us 1553 : 42 : return PortalRunSelect(portal, true, 1L, dest);
1554 : : }
1555 [ + + ]: 33 : else if (count < 0)
1556 : : {
1557 : : /*
1558 : : * Definition: Advance to end, back up abs(count)-1 rows,
1559 : : * return prior row (if any). We could optimize this if we
1560 : : * knew in advance where the end was, but typically we won't.
1561 : : * (Is it worth considering case where count > half of size of
1562 : : * query? We could rewind once we know the size ...)
1563 : : */
8159 1564 : 29 : PortalRunSelect(portal, true, FETCH_ALL, None_Receiver);
8163 1565 [ - + ]: 29 : if (count < -1)
8069 bruce@momjian.us 1566 :UBC 0 : PortalRunSelect(portal, false, -count - 1, None_Receiver);
8163 tgl@sss.pgh.pa.us 1567 :CBC 29 : return PortalRunSelect(portal, false, 1L, dest);
1568 : : }
1569 : : else
1570 : : {
1571 : : /* count == 0 */
1572 : : /* Rewind to start, return zero rows */
1573 : 4 : DoPortalRewind(portal);
1574 : 4 : return PortalRunSelect(portal, true, 0L, dest);
1575 : : }
1576 : : break;
1577 : 45 : case FETCH_RELATIVE:
1578 [ + + ]: 45 : if (count > 0)
1579 : : {
1580 : : /*
1581 : : * Definition: advance count-1 rows, return next row (if any).
1582 : : */
1583 [ + + ]: 20 : if (count > 1)
8069 bruce@momjian.us 1584 : 13 : PortalRunSelect(portal, true, count - 1, None_Receiver);
8163 tgl@sss.pgh.pa.us 1585 : 20 : return PortalRunSelect(portal, true, 1L, dest);
1586 : : }
1587 [ + + ]: 25 : else if (count < 0)
1588 : : {
1589 : : /*
1590 : : * Definition: back up abs(count)-1 rows, return prior row (if
1591 : : * any).
1592 : : */
1593 [ + + ]: 16 : if (count < -1)
8069 bruce@momjian.us 1594 : 9 : PortalRunSelect(portal, false, -count - 1, None_Receiver);
8163 tgl@sss.pgh.pa.us 1595 : 16 : return PortalRunSelect(portal, false, 1L, dest);
1596 : : }
1597 : : else
1598 : : {
1599 : : /* count == 0 */
1600 : : /* Same as FETCH FORWARD 0, so fall out of switch */
1601 : 9 : fdirection = FETCH_FORWARD;
1602 : : }
1603 : 9 : break;
8163 tgl@sss.pgh.pa.us 1604 :UBC 0 : default:
8082 1605 [ # # ]: 0 : elog(ERROR, "bogus direction");
1606 : : break;
1607 : : }
1608 : :
1609 : : /*
1610 : : * Get here with fdirection == FETCH_FORWARD or FETCH_BACKWARD, and count
1611 : : * >= 0.
1612 : : */
8163 tgl@sss.pgh.pa.us 1613 :CBC 25754 : forward = (fdirection == FETCH_FORWARD);
1614 : :
1615 : : /*
1616 : : * Zero count means to re-fetch the current row, if any (per SQL)
1617 : : */
1618 [ + + ]: 25754 : if (count == 0)
1619 : : {
1620 : : bool on_row;
1621 : :
1622 : : /* Are we sitting on a row? */
1623 [ + - + - ]: 9 : on_row = (!portal->atStart && !portal->atEnd);
1624 : :
7247 alvherre@alvh.no-ip. 1625 [ - + ]: 9 : if (dest->mydest == DestNone)
1626 : : {
1627 : : /* MOVE 0 returns 0/1 based on if FETCH 0 would return a row */
3465 tgl@sss.pgh.pa.us 1628 :UBC 0 : return on_row ? 1 : 0;
1629 : : }
1630 : : else
1631 : : {
1632 : : /*
1633 : : * If we are sitting on a row, back up one so we can re-fetch it.
1634 : : * If we are not sitting on a row, we still have to start up and
1635 : : * shut down the executor so that the destination is initialized
1636 : : * and shut down correctly; so keep going. To PortalRunSelect,
1637 : : * count == 0 means we will retrieve no row.
1638 : : */
8163 tgl@sss.pgh.pa.us 1639 [ + - ]:CBC 9 : if (on_row)
1640 : : {
8159 1641 : 9 : PortalRunSelect(portal, false, 1L, None_Receiver);
1642 : : /* Set up to fetch one row forward */
8163 1643 : 6 : count = 1;
1644 : 6 : forward = true;
1645 : : }
1646 : : }
1647 : : }
1648 : :
1649 : : /*
1650 : : * Optimize MOVE BACKWARD ALL into a Rewind.
1651 : : */
7247 alvherre@alvh.no-ip. 1652 [ + + + + : 25751 : if (!forward && count == FETCH_ALL && dest->mydest == DestNone)
+ + ]
1653 : : {
3465 tgl@sss.pgh.pa.us 1654 : 12 : uint64 result = portal->portalPos;
1655 : :
8163 1656 [ + + - + ]: 12 : if (result > 0 && !portal->atEnd)
8163 tgl@sss.pgh.pa.us 1657 :UBC 0 : result--;
8163 tgl@sss.pgh.pa.us 1658 :CBC 12 : DoPortalRewind(portal);
1659 : 12 : return result;
1660 : : }
1661 : :
1662 : 25739 : return PortalRunSelect(portal, forward, count, dest);
1663 : : }
1664 : :
1665 : : /*
1666 : : * DoPortalRewind - rewind a Portal to starting point
1667 : : */
1668 : : static void
1669 : 45 : DoPortalRewind(Portal portal)
1670 : : {
1671 : : QueryDesc *queryDesc;
1672 : :
1673 : : /*
1674 : : * No work is needed if we've not advanced nor attempted to advance the
1675 : : * cursor (and we don't want to throw a NO SCROLL error in this case).
1676 : : */
1457 1677 [ + + + - ]: 45 : if (portal->atStart && !portal->atEnd)
1678 : 9 : return;
1679 : :
1680 : : /* Otherwise, cursor must allow scrolling */
1681 [ + + ]: 36 : if (portal->cursorOptions & CURSOR_OPT_NO_SCROLL)
1682 [ + - ]: 3 : ereport(ERROR,
1683 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1684 : : errmsg("cursor can only scan forward"),
1685 : : errhint("Declare it with SCROLL option to enable backward scan.")));
1686 : :
1687 : : /* Rewind holdStore, if we have one */
8163 1688 [ + + ]: 33 : if (portal->holdStore)
1689 : : {
1690 : : MemoryContext oldcontext;
1691 : :
1692 : 3 : oldcontext = MemoryContextSwitchTo(portal->holdContext);
1693 : 3 : tuplestore_rescan(portal->holdStore);
1694 : 3 : MemoryContextSwitchTo(oldcontext);
1695 : : }
1696 : :
1697 : : /* Rewind executor, if active */
2821 peter_e@gmx.net 1698 : 33 : queryDesc = portal->queryDesc;
4140 tgl@sss.pgh.pa.us 1699 [ + + ]: 33 : if (queryDesc)
1700 : : {
1701 : 30 : PushActiveSnapshot(queryDesc->snapshot);
1702 : 30 : ExecutorRewind(queryDesc);
1703 : 30 : PopActiveSnapshot();
1704 : : }
1705 : :
8163 1706 : 33 : portal->atStart = true;
1707 : 33 : portal->atEnd = false;
1708 : 33 : portal->portalPos = 0;
1709 : : }
1710 : :
1711 : : /*
1712 : : * PlannedStmtRequiresSnapshot - what it says on the tin
1713 : : */
1714 : : bool
1569 1715 : 230657 : PlannedStmtRequiresSnapshot(PlannedStmt *pstmt)
1716 : : {
1717 : 230657 : Node *utilityStmt = pstmt->utilityStmt;
1718 : :
1719 : : /* If it's not a utility statement, it definitely needs a snapshot */
1720 [ + + ]: 230657 : if (utilityStmt == NULL)
1721 : 38854 : return true;
1722 : :
1723 : : /*
1724 : : * Most utility statements need a snapshot, and the default presumption
1725 : : * about new ones should be that they do too. Hence, enumerate those that
1726 : : * do not need one.
1727 : : *
1728 : : * Transaction control, LOCK, and SET must *not* set a snapshot, since
1729 : : * they need to be executable at the start of a transaction-snapshot-mode
1730 : : * transaction without freezing a snapshot. By extension we allow SHOW
1731 : : * not to set a snapshot. The other stmts listed are just efficiency
1732 : : * hacks. Beware of listing anything that can modify the database --- if,
1733 : : * say, it has to update an index with expressions that invoke
1734 : : * user-defined functions, then it had better have a snapshot.
1735 : : */
1736 [ + + ]: 191803 : if (IsA(utilityStmt, TransactionStmt) ||
1737 [ + + ]: 174115 : IsA(utilityStmt, LockStmt) ||
1738 [ + + ]: 173480 : IsA(utilityStmt, VariableSetStmt) ||
1739 [ + + ]: 154518 : IsA(utilityStmt, VariableShowStmt) ||
1740 [ + + ]: 154105 : IsA(utilityStmt, ConstraintsSetStmt) ||
1741 : : /* efficiency hacks from here down */
1742 [ + + ]: 154054 : IsA(utilityStmt, FetchStmt) ||
1743 [ + + ]: 150329 : IsA(utilityStmt, ListenStmt) ||
1744 [ + + ]: 150292 : IsA(utilityStmt, NotifyStmt) ||
1745 [ + + ]: 150248 : IsA(utilityStmt, UnlistenStmt) ||
1746 [ + + ]: 150229 : IsA(utilityStmt, CheckPointStmt))
1747 : 42007 : return false;
1748 : :
1749 : 149796 : return true;
1750 : : }
1751 : :
1752 : : /*
1753 : : * EnsurePortalSnapshotExists - recreate Portal-level snapshot, if needed
1754 : : *
1755 : : * Generally, we will have an active snapshot whenever we are executing
1756 : : * inside a Portal, unless the Portal's query is one of the utility
1757 : : * statements exempted from that rule (see PlannedStmtRequiresSnapshot).
1758 : : * However, procedures and DO blocks can commit or abort the transaction,
1759 : : * and thereby destroy all snapshots. This function can be called to
1760 : : * re-establish the Portal-level snapshot when none exists.
1761 : : */
1762 : : void
1763 : 188974 : EnsurePortalSnapshotExists(void)
1764 : : {
1765 : : Portal portal;
1766 : :
1767 : : /*
1768 : : * Nothing to do if a snapshot is set. (We take it on faith that the
1769 : : * outermost active snapshot belongs to some Portal; or if there is no
1770 : : * Portal, it's somebody else's responsibility to manage things.)
1771 : : */
1772 [ + + ]: 188974 : if (ActiveSnapshotSet())
1773 : 186755 : return;
1774 : :
1775 : : /* Otherwise, we'd better have an active Portal */
1776 : 2219 : portal = ActivePortal;
1498 1777 [ - + ]: 2219 : if (unlikely(portal == NULL))
1498 tgl@sss.pgh.pa.us 1778 [ # # ]:UBC 0 : elog(ERROR, "cannot execute SQL without an outer snapshot or portal");
1569 tgl@sss.pgh.pa.us 1779 [ - + ]:CBC 2219 : Assert(portal->portalSnapshot == NULL);
1780 : :
1781 : : /*
1782 : : * Create a new snapshot, make it active, and remember it in portal.
1783 : : * Because the portal now references the snapshot, we must tell snapmgr.c
1784 : : * that the snapshot belongs to the portal's transaction level, else we
1785 : : * risk portalSnapshot becoming a dangling pointer.
1786 : : */
1436 1787 : 2219 : PushActiveSnapshotWithLevel(GetTransactionSnapshot(), portal->createLevel);
1788 : : /* PushActiveSnapshotWithLevel might have copied the snapshot */
1569 1789 : 2219 : portal->portalSnapshot = GetActiveSnapshot();
1790 : : }
|