00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "postgres.h"
00023
00024 #include <limits.h>
00025
00026 #include "access/xact.h"
00027 #include "commands/portalcmds.h"
00028 #include "executor/executor.h"
00029 #include "executor/tstoreReceiver.h"
00030 #include "tcop/pquery.h"
00031 #include "utils/memutils.h"
00032 #include "utils/snapmgr.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 void
00044 PerformCursorOpen(PlannedStmt *stmt, ParamListInfo params,
00045 const char *queryString, bool isTopLevel)
00046 {
00047 DeclareCursorStmt *cstmt = (DeclareCursorStmt *) stmt->utilityStmt;
00048 Portal portal;
00049 MemoryContext oldContext;
00050
00051 if (cstmt == NULL || !IsA(cstmt, DeclareCursorStmt))
00052 elog(ERROR, "PerformCursorOpen called for non-cursor query");
00053
00054
00055
00056
00057
00058 if (!cstmt->portalname || cstmt->portalname[0] == '\0')
00059 ereport(ERROR,
00060 (errcode(ERRCODE_INVALID_CURSOR_NAME),
00061 errmsg("invalid cursor name: must not be empty")));
00062
00063
00064
00065
00066
00067
00068 if (!(cstmt->options & CURSOR_OPT_HOLD))
00069 RequireTransactionChain(isTopLevel, "DECLARE CURSOR");
00070
00071
00072
00073
00074 portal = CreatePortal(cstmt->portalname, false, false);
00075
00076 oldContext = MemoryContextSwitchTo(PortalGetHeapMemory(portal));
00077
00078 stmt = copyObject(stmt);
00079 stmt->utilityStmt = NULL;
00080
00081 queryString = pstrdup(queryString);
00082
00083 PortalDefineQuery(portal,
00084 NULL,
00085 queryString,
00086 "SELECT",
00087 list_make1(stmt),
00088 NULL);
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 params = copyParamList(params);
00101
00102 MemoryContextSwitchTo(oldContext);
00103
00104
00105
00106
00107
00108
00109
00110
00111 portal->cursorOptions = cstmt->options;
00112 if (!(portal->cursorOptions & (CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL)))
00113 {
00114 if (stmt->rowMarks == NIL &&
00115 ExecSupportsBackwardScan(stmt->planTree))
00116 portal->cursorOptions |= CURSOR_OPT_SCROLL;
00117 else
00118 portal->cursorOptions |= CURSOR_OPT_NO_SCROLL;
00119 }
00120
00121
00122
00123
00124 PortalStart(portal, params, 0, GetActiveSnapshot());
00125
00126 Assert(portal->strategy == PORTAL_ONE_SELECT);
00127
00128
00129
00130
00131
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 void
00146 PerformPortalFetch(FetchStmt *stmt,
00147 DestReceiver *dest,
00148 char *completionTag)
00149 {
00150 Portal portal;
00151 long nprocessed;
00152
00153
00154
00155
00156
00157 if (!stmt->portalname || stmt->portalname[0] == '\0')
00158 ereport(ERROR,
00159 (errcode(ERRCODE_INVALID_CURSOR_NAME),
00160 errmsg("invalid cursor name: must not be empty")));
00161
00162
00163 portal = GetPortalByName(stmt->portalname);
00164 if (!PortalIsValid(portal))
00165 {
00166 ereport(ERROR,
00167 (errcode(ERRCODE_UNDEFINED_CURSOR),
00168 errmsg("cursor \"%s\" does not exist", stmt->portalname)));
00169 return;
00170 }
00171
00172
00173 if (stmt->ismove)
00174 dest = None_Receiver;
00175
00176
00177 nprocessed = PortalRunFetch(portal,
00178 stmt->direction,
00179 stmt->howMany,
00180 dest);
00181
00182
00183 if (completionTag)
00184 snprintf(completionTag, COMPLETION_TAG_BUFSIZE, "%s %ld",
00185 stmt->ismove ? "MOVE" : "FETCH",
00186 nprocessed);
00187 }
00188
00189
00190
00191
00192
00193 void
00194 PerformPortalClose(const char *name)
00195 {
00196 Portal portal;
00197
00198
00199 if (name == NULL)
00200 {
00201 PortalHashTableDeleteAll();
00202 return;
00203 }
00204
00205
00206
00207
00208
00209 if (name[0] == '\0')
00210 ereport(ERROR,
00211 (errcode(ERRCODE_INVALID_CURSOR_NAME),
00212 errmsg("invalid cursor name: must not be empty")));
00213
00214
00215
00216
00217 portal = GetPortalByName(name);
00218 if (!PortalIsValid(portal))
00219 {
00220 ereport(ERROR,
00221 (errcode(ERRCODE_UNDEFINED_CURSOR),
00222 errmsg("cursor \"%s\" does not exist", name)));
00223 return;
00224 }
00225
00226
00227
00228
00229 PortalDrop(portal, false);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 void
00243 PortalCleanup(Portal portal)
00244 {
00245 QueryDesc *queryDesc;
00246
00247
00248
00249
00250 AssertArg(PortalIsValid(portal));
00251 AssertArg(portal->cleanup == PortalCleanup);
00252
00253
00254
00255
00256
00257
00258 queryDesc = PortalGetQueryDesc(portal);
00259 if (queryDesc)
00260 {
00261
00262
00263
00264
00265
00266
00267 portal->queryDesc = NULL;
00268
00269 if (portal->status != PORTAL_FAILED)
00270 {
00271 ResourceOwner saveResourceOwner;
00272
00273
00274 saveResourceOwner = CurrentResourceOwner;
00275 PG_TRY();
00276 {
00277 CurrentResourceOwner = portal->resowner;
00278 ExecutorFinish(queryDesc);
00279 ExecutorEnd(queryDesc);
00280 FreeQueryDesc(queryDesc);
00281 }
00282 PG_CATCH();
00283 {
00284
00285 CurrentResourceOwner = saveResourceOwner;
00286 PG_RE_THROW();
00287 }
00288 PG_END_TRY();
00289 CurrentResourceOwner = saveResourceOwner;
00290 }
00291 }
00292 }
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302 void
00303 PersistHoldablePortal(Portal portal)
00304 {
00305 QueryDesc *queryDesc = PortalGetQueryDesc(portal);
00306 Portal saveActivePortal;
00307 ResourceOwner saveResourceOwner;
00308 MemoryContext savePortalContext;
00309 MemoryContext oldcxt;
00310
00311
00312
00313
00314
00315 Assert(portal->createSubid != InvalidSubTransactionId);
00316 Assert(queryDesc != NULL);
00317
00318
00319
00320
00321 Assert(portal->holdContext != NULL);
00322 Assert(portal->holdStore != NULL);
00323
00324
00325
00326
00327
00328 oldcxt = MemoryContextSwitchTo(portal->holdContext);
00329
00330 portal->tupDesc = CreateTupleDescCopy(portal->tupDesc);
00331
00332 MemoryContextSwitchTo(oldcxt);
00333
00334
00335
00336
00337 if (portal->status != PORTAL_READY)
00338 ereport(ERROR,
00339 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
00340 errmsg("portal \"%s\" cannot be run", portal->name)));
00341 portal->status = PORTAL_ACTIVE;
00342
00343
00344
00345
00346 saveActivePortal = ActivePortal;
00347 saveResourceOwner = CurrentResourceOwner;
00348 savePortalContext = PortalContext;
00349 PG_TRY();
00350 {
00351 ActivePortal = portal;
00352 CurrentResourceOwner = portal->resowner;
00353 PortalContext = PortalGetHeapMemory(portal);
00354
00355 MemoryContextSwitchTo(PortalContext);
00356
00357 PushActiveSnapshot(queryDesc->snapshot);
00358
00359
00360
00361
00362
00363 ExecutorRewind(queryDesc);
00364
00365
00366
00367
00368
00369 queryDesc->dest = CreateDestReceiver(DestTuplestore);
00370 SetTuplestoreDestReceiverParams(queryDesc->dest,
00371 portal->holdStore,
00372 portal->holdContext,
00373 true);
00374
00375
00376 ExecutorRun(queryDesc, ForwardScanDirection, 0L);
00377
00378 (*queryDesc->dest->rDestroy) (queryDesc->dest);
00379 queryDesc->dest = NULL;
00380
00381
00382
00383
00384 portal->queryDesc = NULL;
00385 ExecutorFinish(queryDesc);
00386 ExecutorEnd(queryDesc);
00387 FreeQueryDesc(queryDesc);
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 MemoryContextSwitchTo(portal->holdContext);
00399
00400 if (portal->atEnd)
00401 {
00402
00403 while (tuplestore_advance(portal->holdStore, true))
00404 ;
00405 }
00406 else
00407 {
00408 long store_pos;
00409
00410 if (portal->posOverflow)
00411 ereport(ERROR,
00412 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
00413 errmsg("could not reposition held cursor")));
00414
00415 tuplestore_rescan(portal->holdStore);
00416
00417 for (store_pos = 0; store_pos < portal->portalPos; store_pos++)
00418 {
00419 if (!tuplestore_advance(portal->holdStore, true))
00420 elog(ERROR, "unexpected end of tuple stream");
00421 }
00422 }
00423 }
00424 PG_CATCH();
00425 {
00426
00427 MarkPortalFailed(portal);
00428
00429
00430 ActivePortal = saveActivePortal;
00431 CurrentResourceOwner = saveResourceOwner;
00432 PortalContext = savePortalContext;
00433
00434 PG_RE_THROW();
00435 }
00436 PG_END_TRY();
00437
00438 MemoryContextSwitchTo(oldcxt);
00439
00440
00441 portal->status = PORTAL_READY;
00442
00443 ActivePortal = saveActivePortal;
00444 CurrentResourceOwner = saveResourceOwner;
00445 PortalContext = savePortalContext;
00446
00447 PopActiveSnapshot();
00448
00449
00450
00451
00452
00453
00454
00455 MemoryContextDeleteChildren(PortalGetHeapMemory(portal));
00456 }