Commit d27f363e authored by Jan Wieck's avatar Jan Wieck

Enhancement of SPI to get access to portals

- New functions to create a portal using a prepared/saved
  SPI plan or lookup an existing portal by name.
- Functions to fetch/move from/in portals. Results are placed
  in the usual SPI_processed and SPI_tuptable, so the entire
  set of utility functions can be used to gain attribute access.
- Prepared/saved SPI plans now use their own memory context
  and SPI_freeplan(plan) can remove them.
- Tuple result sets (SPI_tuptable) now uses it's own memory
  context and can be free'd by SPI_freetuptable(tuptab).

Enhancement of PL/pgSQL

- Uses generic named portals internally in FOR ... SELECT
  loops to avoid running out of memory on huge result sets.
- Support for CURSOR and REFCURSOR syntax using the new SPI
  functionality. Cursors used internally only need no explicit
  transaction block. Refcursor variables can be used inside
  of explicit transaction block to pass cursors between main
  application and functions.


Jan
parent be03eb25
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.127 2001/05/09 21:10:38 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.128 2001/05/21 14:22:11 wieck Exp $
*
* NOTES
* The PerformAddAttribute() code, like most of the relation
......@@ -108,6 +108,7 @@ PerformPortalFetch(char *name,
QueryDesc *queryDesc;
EState *estate;
MemoryContext oldcontext;
bool faked_desc = false;
/*
* sanity checks
......@@ -143,13 +144,14 @@ PerformPortalFetch(char *name,
queryDesc = PortalGetQueryDesc(portal);
estate = PortalGetState(portal);
if (dest == None) /* MOVE */
if (dest != queryDesc->dest) /* MOVE */
{
QueryDesc *qdesc = (QueryDesc *) palloc(sizeof(QueryDesc));
memcpy(qdesc, queryDesc, sizeof(QueryDesc));
qdesc->dest = dest;
queryDesc = qdesc;
faked_desc = true;
}
BeginCommand(name,
......@@ -197,7 +199,7 @@ PerformPortalFetch(char *name,
/*
* Clean up and switch back to old context.
*/
if (dest == None) /* MOVE */
if (faked_desc) /* MOVE */
pfree(queryDesc);
MemoryContextSwitchTo(oldcontext);
......
This diff is collapsed.
......@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: catversion.h,v 1.79 2001/05/20 20:28:19 tgl Exp $
* $Id: catversion.h,v 1.80 2001/05/21 14:22:17 wieck Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 200105191
#define CATALOG_VERSION_NO 200105211
#endif
......@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_type.h,v 1.105 2001/05/14 20:30:21 momjian Exp $
* $Id: pg_type.h,v 1.106 2001/05/21 14:22:18 wieck Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
......@@ -413,6 +413,11 @@ DATA(insert OID = 1700 ( numeric PGUID -1 -1 f b t \054 0 0 numeric_in nume
DESCR("numeric(precision, decimal), arbitrary precision number");
#define NUMERICOID 1700
/* OID 1790 */
DATA(insert OID = 1790 ( refcursor PGUID -1 -1 f b t \054 0 0 textin textout textin textout i x _null_ ));
DESCR("reference cursor (portal name)");
#define REFCURSOROID 1790
/*
* prototypes for functions in pg_type.c
......
......@@ -41,6 +41,7 @@
typedef struct
{
MemoryContext tuptabcxt; /* memory context of result table */
uint32 alloced; /* # of alloced vals */
uint32 free; /* # of free vals */
TupleDesc tupdesc; /* tuple descriptor */
......@@ -83,6 +84,7 @@ extern int SPI_exec(char *src, int tcount);
extern int SPI_execp(void *plan, Datum *values, char *Nulls, int tcount);
extern void *SPI_prepare(char *src, int nargs, Oid *argtypes);
extern void *SPI_saveplan(void *plan);
extern int SPI_freeplan(void *plan);
extern HeapTuple SPI_copytuple(HeapTuple tuple);
extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
......@@ -98,6 +100,14 @@ extern void *SPI_palloc(Size size);
extern void *SPI_repalloc(void *pointer, Size size);
extern void SPI_pfree(void *pointer);
extern void SPI_freetuple(HeapTuple pointer);
extern void SPI_freetuptable(SPITupleTable *tuptable);
extern Portal SPI_cursor_open(char *name, void *plan,
Datum *Values, char *Nulls);
extern Portal SPI_cursor_find(char *name);
extern void SPI_cursor_fetch(Portal portal, bool forward, int count);
extern void SPI_cursor_move(Portal portal, bool forward, int count);
extern void SPI_cursor_close(Portal portal);
extern void AtEOXact_SPI(void);
......
......@@ -3,7 +3,7 @@
* spi.c
* Server Programming Interface private declarations
*
* $Header: /cvsroot/pgsql/src/include/executor/spi_priv.h,v 1.7 2000/06/28 03:33:05 tgl Exp $
* $Header: /cvsroot/pgsql/src/include/executor/spi_priv.h,v 1.8 2001/05/21 14:22:18 wieck Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -25,6 +25,7 @@ typedef struct
typedef struct
{
MemoryContext plancxt;
List *qtlist;
List *ptlist;
int nargs;
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: portal.h,v 1.27 2001/03/22 04:01:14 momjian Exp $
* $Id: portal.h,v 1.28 2001/05/21 14:22:18 wieck Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -55,7 +55,7 @@ typedef struct PortalData
* estimate of the maximum number of open portals a user would have,
* used in initially sizing the PortalHashTable in EnablePortalManager()
*/
#define PORTALS_PER_USER 10
#define PORTALS_PER_USER 64
extern void EnablePortalManager(void);
......
This diff is collapsed.
......@@ -3,7 +3,7 @@
* procedural language
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.30 2001/04/18 20:42:56 tgl Exp $
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.31 2001/05/21 14:22:18 wieck Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
......@@ -282,6 +282,7 @@ plpgsql_compile(Oid fn_oid, int functype)
perm_fmgr_info(typeStruct->typinput, &(var->datatype->typinput));
var->datatype->typelem = typeStruct->typelem;
var->datatype->typbyval = typeStruct->typbyval;
var->datatype->typlen = typeStruct->typlen;
var->datatype->atttypmod = -1;
var->isconst = true;
var->notnull = false;
......@@ -313,6 +314,9 @@ plpgsql_compile(Oid fn_oid, int functype)
memset(rec, 0, sizeof(PLpgSQL_rec));
rec->dtype = PLPGSQL_DTYPE_REC;
rec->refname = strdup("new");
rec->tup = NULL;
rec->tupdesc = NULL;
rec->freetup = false;
plpgsql_adddatum((PLpgSQL_datum *) rec);
plpgsql_ns_additem(PLPGSQL_NSTYPE_REC, rec->recno, rec->refname);
function->new_varno = rec->recno;
......@@ -324,6 +328,9 @@ plpgsql_compile(Oid fn_oid, int functype)
memset(rec, 0, sizeof(PLpgSQL_rec));
rec->dtype = PLPGSQL_DTYPE_REC;
rec->refname = strdup("old");
rec->tup = NULL;
rec->tupdesc = NULL;
rec->freetup = false;
plpgsql_adddatum((PLpgSQL_datum *) rec);
plpgsql_ns_additem(PLPGSQL_NSTYPE_REC, rec->recno, rec->refname);
function->old_varno = rec->recno;
......@@ -632,6 +639,7 @@ plpgsql_parse_word(char *word)
perm_fmgr_info(typeStruct->typinput, &(typ->typinput));
typ->typelem = typeStruct->typelem;
typ->typbyval = typeStruct->typbyval;
typ->typlen = typeStruct->typlen;
typ->atttypmod = -1;
plpgsql_yylval.dtype = typ;
......@@ -948,6 +956,7 @@ plpgsql_parse_wordtype(char *word)
perm_fmgr_info(typeStruct->typinput, &(typ->typinput));
typ->typelem = typeStruct->typelem;
typ->typbyval = typeStruct->typbyval;
typ->typlen = typeStruct->typlen;
typ->atttypmod = -1;
plpgsql_yylval.dtype = typ;
......@@ -1091,6 +1100,7 @@ plpgsql_parse_dblwordtype(char *string)
perm_fmgr_info(typeStruct->typinput, &(typ->typinput));
typ->typelem = typeStruct->typelem;
typ->typbyval = typeStruct->typbyval;
typ->typlen = typeStruct->typlen;
typ->atttypmod = attrStruct->atttypmod;
plpgsql_yylval.dtype = typ;
......@@ -1230,13 +1240,14 @@ plpgsql_parse_wordrowtype(char *string)
perm_fmgr_info(typeStruct->typinput, &(var->datatype->typinput));
var->datatype->typelem = typeStruct->typelem;
var->datatype->typbyval = typeStruct->typbyval;
var->datatype->typlen = typeStruct->typlen;
var->datatype->atttypmod = attrStruct->atttypmod;
var->isconst = false;
var->notnull = false;
var->default_val = NULL;
var->value = (Datum) 0;
var->isnull = true;
var->shouldfree = false;
var->freeval = false;
ReleaseSysCache(typetup);
ReleaseSysCache(attrtup);
......
This diff is collapsed.
......@@ -3,7 +3,7 @@
* procedural language
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.12 2001/03/22 06:16:21 momjian Exp $
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_funcs.c,v 1.13 2001/05/21 14:22:19 wieck Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
......@@ -387,6 +387,9 @@ static void dump_execsql(PLpgSQL_stmt_execsql * stmt);
static void dump_dynexecute(PLpgSQL_stmt_dynexecute * stmt);
static void dump_dynfors(PLpgSQL_stmt_dynfors * stmt);
static void dump_getdiag(PLpgSQL_stmt_getdiag * stmt);
static void dump_open(PLpgSQL_stmt_open * stmt);
static void dump_fetch(PLpgSQL_stmt_fetch * stmt);
static void dump_close(PLpgSQL_stmt_close * stmt);
static void dump_expr(PLpgSQL_expr * expr);
......@@ -450,6 +453,15 @@ dump_stmt(PLpgSQL_stmt * stmt)
case PLPGSQL_STMT_GETDIAG:
dump_getdiag((PLpgSQL_stmt_getdiag *) stmt);
break;
case PLPGSQL_STMT_OPEN:
dump_open((PLpgSQL_stmt_open *) stmt);
break;
case PLPGSQL_STMT_FETCH:
dump_fetch((PLpgSQL_stmt_fetch *) stmt);
break;
case PLPGSQL_STMT_CLOSE:
dump_close((PLpgSQL_stmt_close *) stmt);
break;
default:
elog(ERROR, "plpgsql_dump: unknown cmd_type %d\n", stmt->cmd_type);
break;
......@@ -619,6 +631,66 @@ dump_select(PLpgSQL_stmt_select * stmt)
}
static void
dump_open(PLpgSQL_stmt_open * stmt)
{
dump_ind();
printf("OPEN curvar=%d\n", stmt->curvar);
dump_indent += 2;
if (stmt->argquery != NULL)
{
dump_ind();
printf(" arguments = '");
dump_expr(stmt->argquery);
printf("'\n");
}
if (stmt->query != NULL)
{
dump_ind();
printf(" query = '");
dump_expr(stmt->query);
printf("'\n");
}
if (stmt->dynquery != NULL)
{
dump_ind();
printf(" execute = '");
dump_expr(stmt->dynquery);
printf("'\n");
}
dump_indent -= 2;
}
static void
dump_fetch(PLpgSQL_stmt_fetch * stmt)
{
dump_ind();
printf("FETCH curvar=%d\n", stmt->curvar);
dump_indent += 2;
if (stmt->rec != NULL)
{
dump_ind();
printf(" target = %d %s\n", stmt->rec->recno, stmt->rec->refname);
}
if (stmt->row != NULL)
{
dump_ind();
printf(" target = %d %s\n", stmt->row->rowno, stmt->row->refname);
}
dump_indent -= 2;
}
static void
dump_close(PLpgSQL_stmt_close * stmt)
{
dump_ind();
printf("CLOSE curvar=%d\n", stmt->curvar);
}
static void
dump_exit(PLpgSQL_stmt_exit * stmt)
{
......@@ -777,6 +849,25 @@ plpgsql_dumptree(PLpgSQL_function * func)
var->refname, var->datatype->typname,
var->datatype->typoid,
var->datatype->atttypmod);
if (var->isconst)
printf(" CONSTANT\n");
if (var->notnull)
printf(" NOT NULL\n");
if (var->default_val != NULL)
{
printf(" DEFAULT ");
dump_expr(var->default_val);
printf("\n");
}
if (var->cursor_explicit_expr != NULL)
{
if (var->cursor_explicit_argrow >= 0)
printf(" CURSOR argument row %d\n", var->cursor_explicit_argrow);
printf(" CURSOR IS ");
dump_expr(var->cursor_explicit_expr);
printf("\n");
}
}
break;
case PLPGSQL_DTYPE_ROW:
......
......@@ -3,7 +3,7 @@
* procedural language
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.13 2001/03/22 04:01:42 momjian Exp $
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.14 2001/05/21 14:22:19 wieck Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
......@@ -94,7 +94,10 @@ enum
PLPGSQL_STMT_EXECSQL,
PLPGSQL_STMT_DYNEXECUTE,
PLPGSQL_STMT_DYNFORS,
PLPGSQL_STMT_GETDIAG
PLPGSQL_STMT_GETDIAG,
PLPGSQL_STMT_OPEN,
PLPGSQL_STMT_FETCH,
PLPGSQL_STMT_CLOSE
};
......@@ -139,6 +142,7 @@ typedef struct
Oid typoid;
FmgrInfo typinput;
Oid typelem;
int16 typlen;
bool typbyval;
int32 atttypmod;
} PLpgSQL_type;
......@@ -176,10 +180,12 @@ typedef struct
int isconst;
int notnull;
PLpgSQL_expr *default_val;
PLpgSQL_expr *cursor_explicit_expr;
int cursor_explicit_argrow;
Datum value;
bool isnull;
int shouldfree;
bool freeval;
} PLpgSQL_var;
......@@ -206,6 +212,8 @@ typedef struct
HeapTuple tup;
TupleDesc tupdesc;
bool freetup;
bool freetupdesc;
} PLpgSQL_rec;
......@@ -369,6 +377,36 @@ typedef struct
} PLpgSQL_stmt_select;
typedef struct
{ /* OPEN a curvar */
int cmd_type;
int lineno;
int curvar;
PLpgSQL_row *returntype;
PLpgSQL_expr *argquery;
PLpgSQL_expr *query;
PLpgSQL_expr *dynquery;
} PLpgSQL_stmt_open;
typedef struct
{ /* FETCH curvar INTO statement */
int cmd_type;
int lineno;
PLpgSQL_rec *rec;
PLpgSQL_row *row;
int curvar;
} PLpgSQL_stmt_fetch;
typedef struct
{ /* CLOSE curvar */
int cmd_type;
int lineno;
int curvar;
} PLpgSQL_stmt_close;
typedef struct
{ /* EXIT statement */
int cmd_type;
......
......@@ -4,7 +4,7 @@
* procedural language
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/Attic/scan.l,v 1.11 2001/05/18 21:16:59 wieck Exp $
* $Header: /cvsroot/pgsql/src/pl/plpgsql/src/Attic/scan.l,v 1.12 2001/05/21 14:22:19 wieck Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
......@@ -93,7 +93,9 @@ alias { return K_ALIAS; }
begin { return K_BEGIN; }
bpchar { return T_BPCHAR; }
char { return T_CHAR; }
close { return K_CLOSE; }
constant { return K_CONSTANT; }
cursor { return K_CURSOR; }
debug { return K_DEBUG; }
declare { return K_DECLARE; }
default { return K_DEFAULT; }
......@@ -104,16 +106,19 @@ end { return K_END; }
exception { return K_EXCEPTION; }
execute { return K_EXECUTE; }
exit { return K_EXIT; }
fetch { return K_FETCH; }
for { return K_FOR; }
from { return K_FROM; }
get { return K_GET; }
if { return K_IF; }
in { return K_IN; }
into { return K_INTO; }
is { return K_IS; }
loop { return K_LOOP; }
not { return K_NOT; }
notice { return K_NOTICE; }
null { return K_NULL; }
open { return K_OPEN; }
perform { return K_PERFORM; }
raise { return K_RAISE; }
record { return K_RECORD; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment