portal.h 7.71 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * portal.h
4
 *	  POSTGRES portal definitions.
5
 *
6
 * A portal is an abstraction which represents the execution state of
7 8 9 10 11
 * a running or runnable query.  Portals support both SQL-level CURSORs
 * and protocol-level portals.
 *
 * Scrolling (nonsequential access) and suspension of execution are allowed
 * only for portals that contain a single SELECT-type query.  We do not want
Bruce Momjian's avatar
Bruce Momjian committed
12
 * to let the client suspend an update-type query partway through!	Because
13 14 15 16 17 18 19 20
 * the query rewriter does not allow arbitrary ON SELECT rewrite rules,
 * only queries that were originally update-type could produce multiple
 * parse/plan trees; so the restriction to a single query is not a problem
 * in practice.
 *
 * For SQL cursors, we support three kinds of scroll behavior:
 *
 * (1) Neither NO SCROLL nor SCROLL was specified: to remain backward
Bruce Momjian's avatar
Bruce Momjian committed
21 22
 *	   compatible, we allow backward fetches here, unless it would
 *	   impose additional runtime overhead to do so.
23 24 25 26
 *
 * (2) NO SCROLL was specified: don't allow any backward fetches.
 *
 * (3) SCROLL was specified: allow all kinds of backward fetches, even
Bruce Momjian's avatar
Bruce Momjian committed
27
 *	   if we need to take a performance hit to do so.  (The planner sticks
28 29 30 31 32 33 34 35 36 37
 *	   a Materialize node atop the query plan if needed.)
 *
 * Case #1 is converted to #2 or #3 by looking at the query itself and
 * determining if scrollability can be supported without additional
 * overhead.
 *
 * Protocol-level portals have no nonsequential-fetch API and so the
 * distinction doesn't matter for them.  They are always initialized
 * to look like NO SCROLL cursors.
 *
38
 *
Bruce Momjian's avatar
Bruce Momjian committed
39
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
40
 * Portions Copyright (c) 1994, Regents of the University of California
41
 *
42
 * $PostgreSQL: pgsql/src/include/utils/portal.h,v 1.53 2004/09/16 16:58:43 tgl Exp $
43 44 45
 *
 *-------------------------------------------------------------------------
 */
46
#ifndef PORTAL_H
47 48
#define PORTAL_H

49 50
#include "executor/execdesc.h"
#include "nodes/memnodes.h"
51
#include "utils/resowner.h"
52
#include "utils/tuplestore.h"
53

54

55
/*
56 57 58 59 60
 * We have several execution strategies for Portals, depending on what
 * query or queries are to be executed.  (Note: in all cases, a Portal
 * executes just a single source-SQL query, and thus produces just a
 * single result from the user's viewpoint.  However, the rule rewriter
 * may expand the single source query to zero or many actual queries.)
61
 *
Bruce Momjian's avatar
Bruce Momjian committed
62 63
 * PORTAL_ONE_SELECT: the portal contains one single SELECT query.	We run
 * the Executor incrementally as results are demanded.	This strategy also
64 65
 * supports holdable cursors (the Executor results can be dumped into a
 * tuplestore for access after transaction completion).
66
 *
67 68 69 70
 * PORTAL_UTIL_SELECT: the portal contains a utility statement that returns
 * a SELECT-like result (for example, EXPLAIN or SHOW).  On first execution,
 * we run the statement and dump its results into the portal tuplestore;
 * the results are then returned to the client as demanded.
71
 *
72 73
 * PORTAL_MULTI_QUERY: all other cases.  Here, we do not support partial
 * execution: the portal's queries will be run to completion on first call.
74
 */
75 76

typedef enum PortalStrategy
77
{
78 79 80
	PORTAL_ONE_SELECT,
	PORTAL_UTIL_SELECT,
	PORTAL_MULTI_QUERY
81
} PortalStrategy;
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96
/*
 * A portal is always in one of these states.  It is possible to transit
 * from ACTIVE back to READY if the query is not run to completion;
 * otherwise we never back up in status.
 */
typedef enum PortalStatus
{
	PORTAL_NEW,					/* in process of creation */
	PORTAL_READY,				/* PortalStart complete, can run it */
	PORTAL_ACTIVE,				/* portal is running (can't delete it) */
	PORTAL_DONE,				/* portal is finished (don't re-run it) */
	PORTAL_FAILED				/* portal got error (can't re-run it) */
} PortalStatus;

97 98 99 100
/*
 * Note: typedef Portal is declared in tcop/dest.h as
 *		typedef struct PortalData *Portal;
 */
101

102
typedef struct PortalData
103
{
104 105 106
	/* Bookkeeping data */
	const char *name;			/* portal's name */
	MemoryContext heap;			/* subsidiary memory for portal */
107
	ResourceOwner resowner;		/* resources owned by portal */
Bruce Momjian's avatar
Bruce Momjian committed
108
	void		(*cleanup) (Portal portal);		/* cleanup hook */
109 110 111 112 113
	SubTransactionId createSubid;	/* the ID of the creating subxact */
	/*
	 * if createSubid is InvalidSubTransactionId, the portal is held over
	 * from a previous transaction
	 */
114 115 116 117 118 119

	/* The query or queries the portal will execute */
	const char *sourceText;		/* text of query, if known (may be NULL) */
	const char *commandTag;		/* command tag for original query */
	List	   *parseTrees;		/* parse tree(s) */
	List	   *planTrees;		/* plan tree(s) */
Bruce Momjian's avatar
Bruce Momjian committed
120 121
	MemoryContext queryContext; /* where the above trees live */

122 123 124
	/*
	 * Note: queryContext effectively identifies which prepared statement
	 * the portal depends on, if any.  The queryContext is *not* owned by
Bruce Momjian's avatar
Bruce Momjian committed
125 126 127
	 * the portal and is not to be deleted by portal destruction.  (But
	 * for a cursor it is the same as "heap", and that context is deleted
	 * by portal destruction.)
128
	 */
Bruce Momjian's avatar
Bruce Momjian committed
129
	ParamListInfo portalParams; /* params to pass to query */
130 131 132 133 134 135

	/* Features/options */
	PortalStrategy strategy;	/* see above */
	int			cursorOptions;	/* DECLARE CURSOR option bits */

	/* Status data */
136
	PortalStatus status;		/* see above */
Bruce Momjian's avatar
Bruce Momjian committed
137
	bool		portalUtilReady;	/* PortalRunUtility complete? */
138 139 140 141 142 143

	/* If not NULL, Executor is active; call ExecutorEnd eventually: */
	QueryDesc  *queryDesc;		/* info needed for executor invocation */

	/* If portal returns tuples, this is their tupdesc: */
	TupleDesc	tupDesc;		/* descriptor for result tuples */
144 145
	/* and these are the format codes to use for the columns: */
	int16	   *formats;		/* a format code for each column */
146 147

	/*
Bruce Momjian's avatar
Bruce Momjian committed
148 149 150
	 * Where we store tuples for a held cursor or a PORTAL_UTIL_SELECT
	 * query. (A cursor held past the end of its transaction no longer has
	 * any active executor state.)
151
	 */
Bruce Momjian's avatar
Bruce Momjian committed
152 153
	Tuplestorestate *holdStore; /* store for holdable cursors */
	MemoryContext holdContext;	/* memory containing holdStore */
154

155 156
	/*
	 * atStart, atEnd and portalPos indicate the current cursor position.
Bruce Momjian's avatar
Bruce Momjian committed
157 158 159 160 161 162
	 * portalPos is zero before the first row, N after fetching N'th row
	 * of query.  After we run off the end, portalPos = # of rows in
	 * query, and atEnd is true.  If portalPos overflows, set posOverflow
	 * (this causes us to stop relying on its value for navigation).  Note
	 * that atStart implies portalPos == 0, but not the reverse (portalPos
	 * could have overflowed).
163 164 165 166 167
	 */
	bool		atStart;
	bool		atEnd;
	bool		posOverflow;
	long		portalPos;
168
} PortalData;
169 170

/*
Bruce Momjian's avatar
Bruce Momjian committed
171
 * PortalIsValid
172
 *		True iff portal is valid.
173
 */
174
#define PortalIsValid(p) PointerIsValid(p)
175

176 177 178
/*
 * Access macros for Portal ... use these in preference to field access.
 */
179
#define PortalGetQueryDesc(portal)	((portal)->queryDesc)
Bruce Momjian's avatar
Bruce Momjian committed
180
#define PortalGetHeapMemory(portal) ((portal)->heap)
181 182


183
/* Prototypes for functions in utils/mmgr/portalmem.c */
184
extern void EnablePortalManager(void);
185 186 187
extern void AtCommit_Portals(void);
extern void AtAbort_Portals(void);
extern void AtCleanup_Portals(void);
188 189 190 191 192 193 194
extern void AtSubCommit_Portals(SubTransactionId mySubid,
								SubTransactionId parentSubid,
								ResourceOwner parentXactOwner);
extern void AtSubAbort_Portals(SubTransactionId mySubid,
							   SubTransactionId parentSubid,
							   ResourceOwner parentXactOwner);
extern void AtSubCleanup_Portals(SubTransactionId mySubid);
195 196
extern Portal CreatePortal(const char *name, bool allowDup, bool dupSilent);
extern Portal CreateNewPortal(void);
197
extern void PortalDrop(Portal portal, bool isTopCommit);
198
extern void DropDependentPortals(MemoryContext queryContext);
199
extern Portal GetPortalByName(const char *name);
200
extern void PortalDefineQuery(Portal portal,
Bruce Momjian's avatar
Bruce Momjian committed
201 202 203 204 205
				  const char *sourceText,
				  const char *commandTag,
				  List *parseTrees,
				  List *planTrees,
				  MemoryContext queryContext);
206
extern void PortalCreateHoldStore(Portal portal);
207

208
#endif   /* PORTAL_H */