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