Portal portal;
int k;
- /* Ensure that the plan contains only one regular SELECT query */
+ /* Ensure that the plan contains only one query */
if (list_length(ptlist) != 1 || list_length(qtlist) != 1)
ereport(ERROR,
(errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
queryTree = (Query *) linitial((List *) linitial(qtlist));
planTree = (Plan *) linitial(ptlist);
- if (queryTree->commandType != CMD_SELECT)
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
- errmsg("cannot open non-SELECT query as cursor")));
- if (queryTree->into != NULL)
+ /* Must be a query that returns tuples */
+ switch (queryTree->commandType)
+ {
+ case CMD_SELECT:
+ if (queryTree->into != NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
+ errmsg("cannot open SELECT INTO query as cursor")));
+ break;
+ case CMD_UTILITY:
+ if (!UtilityReturnsTuples(queryTree->utilityStmt))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
+ errmsg("cannot open non-SELECT query as cursor")));
+ break;
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
+ errmsg("cannot open non-SELECT query as cursor")));
+ break;
+ }
+
+ /*
+ * If told to be read-only, we'd better check for read-only queries.
+ */
+ if (read_only && !QueryIsReadOnly(queryTree))
ereport(ERROR,
- (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
- errmsg("cannot open SELECT INTO query as cursor")));
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ /* translator: %s is a SQL statement name */
+ errmsg("%s is not allowed in a non-volatile function",
+ CreateQueryTag(queryTree))));
/* Reset SPI result */
SPI_processed = 0;
*/
PortalDefineQuery(portal,
NULL, /* unfortunately don't have sourceText */
- "SELECT", /* cursor's query is always a SELECT */
+ "SELECT", /* nor the raw parse tree... */
list_make1(queryTree),
list_make1(planTree),
PortalGetHeapMemory(portal));
* Set up options for portal.
*/
portal->cursorOptions &= ~(CURSOR_OPT_SCROLL | CURSOR_OPT_NO_SCROLL);
- if (ExecSupportsBackwardScan(plan))
+ if (planTree == NULL || ExecSupportsBackwardScan(planTree))
portal->cursorOptions |= CURSOR_OPT_SCROLL;
else
portal->cursorOptions |= CURSOR_OPT_NO_SCROLL;
*/
PortalStart(portal, paramLI, snapshot);
- Assert(portal->strategy == PORTAL_ONE_SELECT);
+ Assert(portal->strategy == PORTAL_ONE_SELECT ||
+ portal->strategy == PORTAL_UTIL_SELECT);
/* Return the created portal */
return portal;