Re: [HACKERS] revised sample SRF C function; proposed SRF API

Поиск
Список
Период
Сортировка
Искать

Re: [HACKERS] revised sample SRF C function; proposed SRF API

От:
Joe Conway <mail@joeconway.com>
Дата:
Tom Lane wrote:
> Definitely better.  I'd suggest also thinking about whether the
> same/similar macros can support functions that return a set of a
> scalar (non-tuple) datatype.  In my mind, the cleanest design would
> be some base macros that support functions-returning-set (of anything),
> and if you want to return a set of scalar then you just use these
> directly (handing a Datum to FUNC_RETURN_NEXT).  If you want to return
> a set of tuples then there are a couple extra steps that you need to
> do to build a tupdesc, build a tuple, and convert the tuple to Datum
> (which at the moment you do by putting it into a slot, but I think we
> ought to change that soon).  If it were really clean then the macros
> supporting these extra steps would also work without the SRF macros,
> so that you could use 'em in a function returning a single tuple.

As promised on HACKERS, here's the Composite and SRF function API patch. 
Included is a new builtin guc function, exported as show_all_vars(). In 
order to avoid creating a new bootstrapped relation, I made the pg_proc 
entry specify 0 as the function return type, and then fixed it in 
initdb.sh as follows:

$ECHO_N "setting return type for composite returning functions... "$ECHO_C

"$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <constr = NULL;
  	}
  	return desc;
+ }
+ 
+ 
+ /*
+  * RelationNameGetTupleDesc
+  *
+  * Given a (possibly qualified) relation name, build a TupleDesc.
+  */
+ TupleDesc
+ RelationNameGetTupleDesc(char *relname)
+ {
+ 	RangeVar   *relvar;
+ 	Relation	rel;
+ 	TupleDesc	tupdesc;
+ 	List	   *relname_list;
+ 
+ 	/* Open relation and get the tuple description */
+ 	relname_list = stringToQualifiedNameList(relname, "RelationNameGetTupleDesc");
+ 	relvar = makeRangeVarFromNameList(relname_list);
+ 	rel = heap_openrv(relvar, AccessShareLock);
+ 	tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
+ 	relation_close(rel, AccessShareLock);
+ 
+ 	return tupdesc;
+ }
+ 
+ /*
+  * TypeGetTupleDesc
+  *
+  * Given a type Oid, build a TupleDesc.
+  *
+  * If the type is composite, *and* a colaliases List is provided, *and*
+  * the List is of natts length, use the aliases instead of the relation
+  * attnames.
+  *
+  * If the type is a base type, a single item alias List is required.
+  */
+ TupleDesc
+ TypeGetTupleDesc(Oid typeoid, List *colaliases)
+ {
+ 	Oid			relid = typeidTypeRelid(typeoid);
+ 	TupleDesc	tupdesc;
+ 
+ 	/*
+ 	 * Build a suitable tupledesc representing the output rows
+ 	 */
+ 	if (OidIsValid(relid))
+ 	{
+ 		/* Composite data type, i.e. a table's row type */
+ 		Relation	rel;
+ 		int			natts;
+ 
+ 		rel = relation_open(relid, AccessShareLock);
+ 		tupdesc = CreateTupleDescCopy(RelationGetDescr(rel));
+ 		natts = tupdesc->natts;
+ 		relation_close(rel, AccessShareLock);
+ 
+ 		/* check to see if we've given column aliases */
+ 		if(colaliases != NIL)
+ 		{
+ 			char	   *label;
+ 			int			varattno;
+ 
+ 			/* does the List length match the number of attributes */
+ 			if (length(colaliases) != natts)
+ 				elog(ERROR, "TypeGetTupleDesc: number of aliases does not match number of attributes");
+ 
+ 			/* OK, use the aliases instead */
+ 			for (varattno = 0; varattno < natts; varattno++)
+ 			{
+ 				label = strVal(nth(varattno, colaliases));
+ 
+ 				if (label != NULL)
+ 					namestrcpy(&(tupdesc->attrs[varattno]->attname), label);
+ 				else
+ 					MemSet(NameStr(tupdesc->attrs[varattno]->attname), 0, NAMEDATALEN);
+ 			}
+ 		}
+ 	}
+ 	else
+ 	{
+ 		/* Must be a base data type, i.e. scalar */
+ 		char	   *attname;
+ 
+ 		/* the alias List is required for base types */
+ 		if (colaliases == NIL)
+ 			elog(ERROR, "TypeGetTupleDesc: no column alias was provided");
+ 
+ 		/* the alias List length must be 1 */
+ 		if (length(colaliases) != 1)
+ 			elog(ERROR, "TypeGetTupleDesc: number of aliases does not match number of attributes");
+ 
+ 		/* OK, get the column alias */
+ 		attname = strVal(lfirst(colaliases));
+ 
+ 		tupdesc = CreateTemplateTupleDesc(1);
+ 		TupleDescInitEntry(tupdesc,
+ 						   (AttrNumber) 1,
+ 						   attname,
+ 						   typeoid,
+ 						   -1,
+ 						   0,
+ 						   false);
+ 	}
+ 
+ 	return tupdesc;
  }
Index: src/backend/executor/execTuples.c
===================================================================
RCS file: /opt/src/cvs/pgsql/src/backend/executor/execTuples.c,v
retrieving revision 1.51
diff -c -r1.51 execTuples.c
*** src/backend/executor/execTuples.c	21 Mar 2002 06:21:04 -0000	1.51
--- src/backend/executor/execTuples.c	7 Jun 2002 16:43:51 -0000
***************
*** 107,117 ****
   */
  #include "postgres.h"
  
  #include "access/heapam.h"
  #include "catalog/pg_type.h"
  #include "executor/executor.h"
  
- 
  /* ----------------------------------------------------------------
   *				  tuple table create/delete functions
   * ----------------------------------------------------------------
--- 107,117 ----
   */
  #include "postgres.h"
  
+ #include "funcapi.h"
  #include "access/heapam.h"
  #include "catalog/pg_type.h"
  #include "executor/executor.h"
  
  /* ----------------------------------------------------------------
   *				  tuple table create/delete functions
   * ----------------------------------------------------------------
***************
*** 673,675 ****
--- 673,795 ----
  
  	return typeInfo;
  }
+ 
+ /*
+  * TupleDescGetSlot - Initialize a slot based on the supplied
+  * tupledesc
+  */
+ TupleTableSlot *
+ TupleDescGetSlot(TupleDesc tupdesc)
+ {
+ 	TupleTableSlot	   *slot;
+ 
+ 	/* Make a standalone slot */
+ 	slot = MakeTupleTableSlot();
+ 
+ 	/* Bind the tuple description to the slot */
+ 	ExecSetSlotDescriptor(slot, tupdesc, true);
+ 
+ 	/* Return the slot */
+ 	return slot;
+ }
+ 
+ /*
+  * TupleDescGetAttInMetadata - Get a pointer to AttInMetadata based on the
+  * supplied TupleDesc. AttInMetadata can be used in conjunction with C strings
+  * to produce a properly formed tuple.
+  */
+ AttInMetadata *
+ TupleDescGetAttInMetadata(TupleDesc tupdesc)
+ {
+ 	int				natts;
+ 	int				i;
+ 	Oid				atttypeid;
+ 	Oid				attinfuncid;
+ 	Oid				attelem;
+ 	FmgrInfo	   *attinfuncinfo;
+ 	Oid			   *attelems;
+ 	int4		   *atttypmods;
+ 	AttInMetadata  *attinmeta;
+ 
+ 	attinmeta = (AttInMetadata *) palloc(sizeof(AttInMetadata));
+ 	natts = tupdesc->natts;
+ 
+ 	/*
+ 	 * Gather info needed later to call the "in" function for each attribute
+ 	 */
+ 	attinfuncinfo = (FmgrInfo *) palloc(natts * sizeof(FmgrInfo));
+ 	attelems = (Oid *) palloc(natts * sizeof(Oid));
+ 	atttypmods = (int4 *) palloc(natts * sizeof(int4));
+ 
+ 	for (i = 0; i < natts; i++)
+ 	{
+ 		atttypeid = tupdesc->attrs[i]->atttypid;
+ 		get_type_metadata(atttypeid, &attinfuncid, &attelem);
+ 
+ 		fmgr_info(attinfuncid, &attinfuncinfo[i]);
+ 		attelems[i] = attelem;
+ 		atttypmods[i] = tupdesc->attrs[i]->atttypmod;
+ 	}
+ 	attinmeta->tupdesc = tupdesc;
+ 	attinmeta->attinfuncs = attinfuncinfo;
+ 	attinmeta->attelems = attelems;
+ 	attinmeta->atttypmods = atttypmods;
+ 
+ 	return attinmeta;
+ }
+ 
+ /*
+  * BuildTupleFromCStrings - build a HeapTuple given user data in C string form.
+  * values is an array of C strings, one for each attribute of the return tuple.
+  */
+ HeapTuple
+ BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
+ {
+ 	TupleDesc			tupdesc;
+ 	int					natts;
+ 	HeapTuple			tuple;
+ 	char			   *nulls;
+ 	int					i;
+ 	Datum			   *dvalues;
+ 	FmgrInfo			attinfuncinfo;
+ 	Oid					attelem;
+ 	int4				atttypmod;
+ 
+ 	tupdesc = attinmeta->tupdesc;
+ 	natts = tupdesc->natts;
+ 
+ 	dvalues = (Datum *) palloc(natts * sizeof(Datum));
+ 
+ 	/* Call the "in" function for each attribute */
+ 	for (i = 0; i < natts; i++)
+ 	{
+ 		if (values[i] != NULL)
+ 		{
+ 			attinfuncinfo = attinmeta->attinfuncs[i];
+ 			attelem = attinmeta->attelems[i];
+ 			atttypmod = attinmeta->atttypmods[i];
+ 
+ 			dvalues[i] = FunctionCall3(&attinfuncinfo, CStringGetDatum(values[i]),
+ 										ObjectIdGetDatum(attelem),
+ 										Int32GetDatum(atttypmod));
+ 		}
+ 		else
+ 			dvalues[i] = PointerGetDatum(NULL);
+ 	}
+ 
+ 	/*
+ 	 * Form a tuple
+ 	 */
+ 	nulls = (char *) palloc(natts * sizeof(char));
+ 	for (i = 0; i < natts; i++)
+ 	{
+ 		if (DatumGetPointer(dvalues[i]) != NULL)
+ 			nulls[i] = ' ';
+ 		else
+ 			nulls[i] = 'n';
+ 	}
+ 	tuple = heap_formtuple(tupdesc, dvalues, nulls);
+ 
+ 	return tuple;
+ }
+ 
Index: src/backend/utils/adt/regproc.c
===================================================================
RCS file: /opt/src/cvs/pgsql/src/backend/utils/adt/regproc.c,v
retrieving revision 1.68
diff -c -r1.68 regproc.c
*** src/backend/utils/adt/regproc.c	11 May 2002 00:24:16 -0000	1.68
--- src/backend/utils/adt/regproc.c	7 Jun 2002 16:43:51 -0000
***************
*** 37,44 ****
  #include "utils/lsyscache.h"
  #include "utils/syscache.h"
  
- 
- static List *stringToQualifiedNameList(const char *string, const char *caller);
  static void parseNameAndArgTypes(const char *string, const char *caller,
  								 const char *type0_spelling,
  								 List **names, int *nargs, Oid *argtypes);
--- 37,42 ----
***************
*** 960,973 ****
  }
  
  
- /*****************************************************************************
-  *	 SUPPORT ROUTINES														 *
-  *****************************************************************************/
- 
  /*
   * Given a C string, parse it into a qualified-name list.
   */
! static List *
  stringToQualifiedNameList(const char *string, const char *caller)
  {
  	char	   *rawname;
--- 958,967 ----
  }
  
  
  /*
   * Given a C string, parse it into a qualified-name list.
   */
! List *
  stringToQualifiedNameList(const char *string, const char *caller)
  {
  	char	   *rawname;
***************
*** 996,1001 ****
--- 990,999 ----
  
  	return result;
  }
+ 
+ /*****************************************************************************
+  *	 SUPPORT ROUTINES														 *
+  *****************************************************************************/
  
  /*
   * Given a C string, parse it into a qualified function or operator name
Index: src/backend/utils/fmgr/Makefile
===================================================================
RCS file: /opt/src/cvs/pgsql/src/backend/utils/fmgr/Makefile,v
retrieving revision 1.12
diff -c -r1.12 Makefile
*** src/backend/utils/fmgr/Makefile	16 Sep 2001 16:11:11 -0000	1.12
--- src/backend/utils/fmgr/Makefile	7 Jun 2002 16:43:51 -0000
***************
*** 12,18 ****
  top_builddir = ../../../..
  include $(top_builddir)/src/Makefile.global
  
! OBJS = dfmgr.o fmgr.o
  
  override CPPFLAGS += -DPKGLIBDIR=\"$(pkglibdir)\" -DDLSUFFIX=\"$(DLSUFFIX)\"
  
--- 12,18 ----
  top_builddir = ../../../..
  include $(top_builddir)/src/Makefile.global
  
! OBJS = dfmgr.o fmgr.o funcapi.o
  
  override CPPFLAGS += -DPKGLIBDIR=\"$(pkglibdir)\" -DDLSUFFIX=\"$(DLSUFFIX)\"
  
Index: src/backend/utils/fmgr/funcapi.c
===================================================================
RCS file: src/backend/utils/fmgr/funcapi.c
diff -N src/backend/utils/fmgr/funcapi.c
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- src/backend/utils/fmgr/funcapi.c	7 Jun 2002 16:43:51 -0000
***************
*** 0 ****
--- 1,122 ----
+ /*-------------------------------------------------------------------------
+  *
+  * funcapi.c
+  *	  Utility and convenience functions for fmgr functions that return
+  *	  sets and/or composite types.
+  *
+  * Copyright (c) 2002, PostgreSQL Global Development Group
+  *
+  *-------------------------------------------------------------------------
+  */
+ 
+ #include "funcapi.h"
+ #include "catalog/pg_type.h"
+ #include "utils/syscache.h"
+ 
+ /*
+  * init_MultiFuncCall
+  * Create an empty FuncCallContext data structure
+  * and do some other basic Multi-function call setup
+  * and error checking
+  */
+ FuncCallContext *
+ init_MultiFuncCall(PG_FUNCTION_ARGS, TupleTableSlot *slot)
+ {
+ 	FuncCallContext *retval;
+ 
+ 	/*
+ 	 * Bail if we're called in the wrong context
+ 	 */
+ 	if (fcinfo->resultinfo == NULL || !IsA(fcinfo->resultinfo, ReturnSetInfo))
+ 		elog(ERROR, "function called in context that does not accept a set result");
+ 
+ 	if (fcinfo->flinfo->fn_extra == NULL)
+ 	{
+ 		/*
+ 		 * First call
+ 		 */
+ 		MemoryContext oldcontext;
+ 
+ 		/* switch to the appropriate memory context */
+ 		oldcontext = MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt);
+ 
+ 		/*
+ 		 * allocate space and zero it
+ 		 */
+ 		retval = (FuncCallContext *) palloc(sizeof(FuncCallContext));
+ 		MemSet(retval, 0, sizeof(FuncCallContext));
+ 
+ 		/*
+ 		 * initialize the elements
+ 		 */
+ 		retval->call_cntr = 0;
+ 		retval->max_calls = 0;
+ 		retval->slot = slot;
+ 		retval->fctx = NULL;
+ 		retval->attinmeta = NULL;
+ 		retval->fmctx = fcinfo->flinfo->fn_mcxt;
+ 
+ 		/*
+ 		 * save the pointer for cross-call use
+ 		 */
+ 		fcinfo->flinfo->fn_extra = retval;
+ 
+ 		/* back to the original memory context */
+ 		MemoryContextSwitchTo(oldcontext);
+ 	}
+ 	else	/* second and subsequent calls */
+ 	{
+ 		elog(ERROR, "init_MultiFuncCall may not be called more than once");
+ 
+ 		/* never reached, but keep compiler happy */
+ 		retval = NULL;
+ 	}
+ 
+ 	return retval;
+ }
+ 
+ /*
+  * end_MultiFuncCall
+  * Clean up after init_MultiFuncCall
+  */
+ void
+ end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx)
+ {
+ 	MemoryContext oldcontext;
+ 
+ 	/* unbind from fcinfo */
+ 	fcinfo->flinfo->fn_extra = NULL;
+ 
+ 	/*
+ 	 * Caller is responsible to free up memory for individual
+ 	 * struct elements other than att_in_funcinfo and elements.
+ 	 */
+ 	oldcontext = MemoryContextSwitchTo(funcctx->fmctx);
+ 
+ 	if (funcctx->attinmeta != NULL)
+ 		pfree(funcctx->attinmeta);
+ 
+ 	pfree(funcctx);
+ 
+ 	MemoryContextSwitchTo(oldcontext);
+ }
+ 
+ void
+ get_type_metadata(Oid typeid, Oid *attinfuncid, Oid *attelem)
+ {
+ 	HeapTuple		typeTuple;
+ 	Form_pg_type	typtup;
+ 
+ 	typeTuple = SearchSysCache(TYPEOID,
+ 							   ObjectIdGetDatum(typeid),
+ 							   0, 0, 0);
+ 	if (!HeapTupleIsValid(typeTuple))
+ 		elog(ERROR, "get_type_metadata: Cache lookup of type %u failed", typeid);
+ 
+ 	typtup = (Form_pg_type) GETSTRUCT(typeTuple);
+ 
+ 	*attinfuncid = typtup->typinput;
+ 	*attelem = typtup->typelem;
+ 
+ 	ReleaseSysCache(typeTuple);
+ }
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /opt/src/cvs/pgsql/src/backend/utils/misc/guc.c,v
retrieving revision 1.69
diff -c -r1.69 guc.c
*** src/backend/utils/misc/guc.c	17 May 2002 20:32:29 -0000	1.69
--- src/backend/utils/misc/guc.c	7 Jun 2002 16:43:51 -0000
***************
*** 21,28 ****
--- 21,30 ----
  
  #include "utils/guc.h"
  
+ #include "funcapi.h"
  #include "access/xlog.h"
  #include "catalog/namespace.h"
+ #include "catalog/pg_type.h"
  #include "commands/async.h"
  #include "commands/variable.h"
  #include "fmgr.h"
***************
*** 824,830 ****
  
  
  static int guc_var_compare(const void *a, const void *b);
! static void _ShowOption(struct config_generic *record);
  
  
  /*
--- 826,833 ----
  
  
  static int guc_var_compare(const void *a, const void *b);
! static char *_ShowOption(struct config_generic *record);
! static char *GetNextGUCConfig(int varnum, char **varname);
  
  
  /*
***************
*** 2204,2215 ****
  ShowGUCConfigOption(const char *name)
  {
  	struct config_generic *record;
  
  	record = find_option(name);
  	if (record == NULL)
  		elog(ERROR, "Option '%s' is not recognized", name);
  
! 	_ShowOption(record);
  }
  
  /*
--- 2207,2224 ----
  ShowGUCConfigOption(const char *name)
  {
  	struct config_generic *record;
+ 	char *val;
  
  	record = find_option(name);
  	if (record == NULL)
  		elog(ERROR, "Option '%s' is not recognized", name);
  
! 	val = _ShowOption(record);
! 	if(val != NULL)
! 	{
! 		elog(INFO, "%s is %s", record->name, val);
! 		pfree(val);
! 	}
  }
  
  /*
***************
*** 2219,2239 ****
  ShowAllGUCConfig(void)
  {
  	int			i;
  
  	for (i = 0; i < num_guc_variables; i++)
  	{
  		struct config_generic *conf = guc_variables[i];
  
  		if ((conf->flags & GUC_NO_SHOW_ALL) == 0)
! 			_ShowOption(conf);
  	}
  }
  
! static void
  _ShowOption(struct config_generic *record)
  {
  	char		buffer[256];
  	const char *val;
  
  	switch (record->vartype)
  	{
--- 2228,2274 ----
  ShowAllGUCConfig(void)
  {
  	int			i;
+ 	char	   *val;
  
  	for (i = 0; i < num_guc_variables; i++)
  	{
  		struct config_generic *conf = guc_variables[i];
  
  		if ((conf->flags & GUC_NO_SHOW_ALL) == 0)
! 		{
! 			val = _ShowOption(conf);
! 			if(val != NULL)
! 			{
! 				elog(INFO, "%s is %s", conf->name, val);
! 				pfree(val);
! 			}
! 		}
  	}
  }
  
! /*
!  * SHOW X or SHOW ALL command
!  */
! static char *
! GetNextGUCConfig(int varnum, char **varname)
! {
! 	struct config_generic *conf = guc_variables[varnum];
! 
! 	*varname = pstrdup(conf->name);
! 
! 	if ((conf->flags & GUC_NO_SHOW_ALL) == 0)
! 		return _ShowOption(conf);
! 	else
! 		return NULL;
! 
! }
! 
! static char *
  _ShowOption(struct config_generic *record)
  {
  	char		buffer[256];
  	const char *val;
+ 	char	   *retval;
  
  	switch (record->vartype)
  	{
***************
*** 2297,2303 ****
  			break;
  	}
  
! 	elog(INFO, "%s is %s", record->name, val);
  }
  
  
--- 2332,2340 ----
  			break;
  	}
  
! 	retval = pstrdup(val);
! 
! 	return retval;
  }
  
  
***************
*** 2539,2541 ****
--- 2576,2672 ----
  
  	return newarray;
  }
+ 
+ 
+ /*
+  * showguc_all - equiv to SHOW ALL command but implemented as
+  * an SRF.
+  */
+ Datum
+ showguc_all(PG_FUNCTION_ARGS)
+ {
+ 	FuncCallContext	   *funcctx;
+ 	TupleDesc			tupdesc;
+ 	int					call_cntr;
+ 	int					max_calls;
+ 	TupleTableSlot	   *slot;
+ 	AttInMetadata	   *attinmeta;
+ 	struct config_generic **fctx;
+ 
+ 	/* stuff done only on the first call of the function */
+  	if(SRF_IS_FIRSTPASS())
+  	{
+ 		/*
+ 		 * Build a tuple description for a pg__guc tuple
+ 		 */
+ 		tupdesc = RelationNameGetTupleDesc("pg__guc");
+ 
+ 		/* allocate a slot for a tuple with this tupdesc */
+ 		slot = TupleDescGetSlot(tupdesc);
+ 
+ 		/* create a function context for cross-call persistence */
+  		funcctx = SRF_FIRSTCALL_INIT(slot);
+ 
+ 		/*
+ 		 * Generate attribute metadata needed later to produce tuples from raw
+ 		 * C strings
+ 		 */
+ 		attinmeta = TupleDescGetAttInMetadata(tupdesc);
+ 		funcctx->attinmeta = attinmeta;
+ 
+ 		/* total number of tuples to be returned */
+ 		funcctx->max_calls = num_guc_variables;
+ 
+ 		/* pointer to structure containing user defined context */
+ 		funcctx->fctx = (void *) guc_variables;
+     }
+ 
+ 	/* stuff done on every call of the function */
+  	funcctx = SRF_PERCALL_SETUP(funcctx);
+ 
+ 	call_cntr = funcctx->call_cntr;
+ 	max_calls = funcctx->max_calls;
+ 	slot = funcctx->slot;
+ 	fctx = (struct config_generic **) funcctx->fctx;
+ 	attinmeta = funcctx->attinmeta;
+  
+  	if (call_cntr < max_calls)	/* do when there is more left to send */
+  	{
+ 		char	   *varname;
+ 		char	   *varval;
+ 		char	   **values;
+ 		HeapTuple	tuple;
+ 		Datum		result;
+ 
+ 		/*
+ 		 * Get the next GUC variable name and value
+ 		 */
+ 		varval = GetNextGUCConfig(call_cntr, &varname);
+ 
+ 		/*
+ 		 * Prepare a values array for storage in our slot.
+ 		 * This should be an array of C strings which will
+ 		 * be processed later by the appropriate "in" functions.
+ 		 */
+ 		values = (char **) palloc(2 * sizeof(char *));
+ 		values[0] = varname;
+ 		values[1] = varval;
+ 
+ 		/* build a tuple */
+ 		tuple = BuildTupleFromCStrings(attinmeta, values);
+ 
+ 		/* make the tuple into a datum */
+ 		result = TupleGetDatum(slot, tuple);
+ 
+ 		/* Clean up */
+ 		pfree(varname);
+ 		pfree(values);
+ 
+  		SRF_RETURN_NEXT(funcctx, result);
+  	}
+  	else	/* do when there is no more left */
+  	{
+  		SRF_RETURN_DONE(funcctx);
+  	}
+ }
+ 
Index: src/bin/initdb/initdb.sh
===================================================================
RCS file: /opt/src/cvs/pgsql/src/bin/initdb/initdb.sh,v
retrieving revision 1.155
diff -c -r1.155 initdb.sh
*** src/bin/initdb/initdb.sh	21 May 2002 19:06:00 -0000	1.155
--- src/bin/initdb/initdb.sh	7 Jun 2002 21:26:20 -0000
***************
*** 967,972 ****
--- 967,993 ----
  	| "$PGPATH"/postgres $PGSQL_OPT template1 > /dev/null || exit_nicely
  echo "ok"
  
+ 
+ $ECHO_N "setting return type for composite returning functions... "$ECHO_C
+ 
+ "$PGPATH"/postgres $PGSQL_OPT template1 >/dev/null <
+  * 
+  * 	if(SRF_IS_FIRSTPASS())
+  * 	{
+  * 		
+  * 		
+  * 		funcctx = SRF_FIRSTCALL_INIT(slot);
+  * 		
+  *    }
+  * 	
+  * 	funcctx = SRF_PERCALL_SETUP(funcctx);
+  * 	
+  * 
+  * 	if (funcctx->call_cntr < funcctx->max_calls)
+  * 	{
+  * 		
+  * 		
+  * 		SRF_RETURN_NEXT(funcctx, result);
+  * 	}
+  * 	else
+  * 	{
+  * 		SRF_RETURN_DONE(funcctx);
+  * 	}
+  * }
+  *
+  */
+ 
+ /* from funcapi.c */
+ extern FuncCallContext *init_MultiFuncCall(PG_FUNCTION_ARGS, TupleTableSlot *slot);
+ extern void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx);
+ 
+ #define SRF_IS_FIRSTPASS() (fcinfo->flinfo->fn_extra == NULL)
+ #define SRF_FIRSTCALL_INIT(_slot) init_MultiFuncCall(fcinfo,_slot)
+ #define SRF_PERCALL_SETUP(_funcctx) fcinfo->flinfo->fn_extra;ExecClearTuple(_funcctx->slot)
+ #define SRF_RETURN_NEXT(_funcctx, _result) \
+ 	do { \
+ 		ReturnSetInfo	   *rsi; \
+ 		_funcctx->call_cntr++; \
+ 		rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
+ 		rsi->isDone = ExprMultipleResult; \
+ 		PG_RETURN_DATUM(_result); \
+ 	} while (0)
+ 
+ #define  SRF_RETURN_DONE(_funcctx) \
+ 	do { \
+ 		ReturnSetInfo	   *rsi; \
+ 		end_MultiFuncCall(fcinfo, _funcctx); \
+ 		rsi = (ReturnSetInfo *) fcinfo->resultinfo; \
+ 		rsi->isDone = ExprEndResult; \
+ 		_funcctx->slot = NULL; \
+ 		PG_RETURN_NULL(); \
+ 	} while (0)
+ 
+ #endif   /* FUNCAPI_H */
Index: src/include/catalog/catversion.h
===================================================================
RCS file: /opt/src/cvs/pgsql/src/include/catalog/catversion.h,v
retrieving revision 1.133
diff -c -r1.133 catversion.h
*** src/include/catalog/catversion.h	22 May 2002 17:21:01 -0000	1.133
--- src/include/catalog/catversion.h	7 Jun 2002 16:43:51 -0000
***************
*** 53,58 ****
   */
  
  /*							yyyymmddN */
! #define CATALOG_VERSION_NO	200205221
  
  #endif
--- 53,58 ----
   */
  
  /*							yyyymmddN */
! #define CATALOG_VERSION_NO	200206061
  
  #endif
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /opt/src/cvs/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.240
diff -c -r1.240 pg_proc.h
*** src/include/catalog/pg_proc.h	24 May 2002 18:57:56 -0000	1.240
--- src/include/catalog/pg_proc.h	7 Jun 2002 17:48:25 -0000
***************
*** 2871,2876 ****
--- 2871,2879 ----
  DATA(insert OID = 2072 (  date_mi_interval	PGNSP PGUID 14 f f f t f i 2 1114 "1082 1186" 100 0 0 100  "select cast($1 as timestamp without time zone) - $2;" - _null_ ));
  DESCR("subtract");
  
+ DATA(insert OID = 2073 (  show_all_vars	    PGNSP PGUID 12 f f f t t s 0 0 "0" 100 0 0 100	showguc_all - _null_ ));
+ DESCR("show all GUC variable values");
+ 
  /* Aggregates (moved here from pg_aggregate for 7.3) */
  
  DATA(insert OID = 2100 (  avg				PGNSP PGUID 12 t f f f f i 1 1700 "20" 100 0 0 100  aggregate_dummy - _null_ ));
Index: src/include/utils/builtins.h
===================================================================
RCS file: /opt/src/cvs/pgsql/src/include/utils/builtins.h,v
retrieving revision 1.182
diff -c -r1.182 builtins.h
*** src/include/utils/builtins.h	18 May 2002 21:38:41 -0000	1.182
--- src/include/utils/builtins.h	7 Jun 2002 16:43:52 -0000
***************
*** 341,346 ****
--- 341,347 ----
  extern Datum regclassout(PG_FUNCTION_ARGS);
  extern Datum regtypein(PG_FUNCTION_ARGS);
  extern Datum regtypeout(PG_FUNCTION_ARGS);
+ extern List *stringToQualifiedNameList(const char *string, const char *caller);
  
  /* ruleutils.c */
  extern Datum pg_get_ruledef(PG_FUNCTION_ARGS);
***************
*** 630,634 ****
--- 631,638 ----
  /* quote.c */
  extern Datum quote_ident(PG_FUNCTION_ARGS);
  extern Datum quote_literal(PG_FUNCTION_ARGS);
+ 
+ /* guc.c */
+ extern Datum showguc_all(PG_FUNCTION_ARGS);
  
  #endif   /* BUILTINS_H */
Index: src/test/regress/expected/rules.out
===================================================================
RCS file: /opt/src/cvs/pgsql/src/test/regress/expected/rules.out,v
retrieving revision 1.53
diff -c -r1.53 rules.out
*** src/test/regress/expected/rules.out	3 May 2002 00:32:19 -0000	1.53
--- src/test/regress/expected/rules.out	7 Jun 2002 21:30:58 -0000
***************
*** 1267,1272 ****
--- 1267,1273 ----
           viewname         |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  --------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   iexit                    | SELECT ih.name, ih.thepath, interpt_pp(ih.thepath, r.thepath) AS exit FROM ihighway ih, ramp r WHERE (ih.thepath ## r.thepath);
+  pg__guc                  | SELECT ''::text AS varname, ''::text AS varval;
   pg_indexes               | SELECT c.relname AS tablename, i.relname AS indexname, pg_get_indexdef(x.indexrelid) AS indexdef FROM pg_index x, pg_class c, pg_class i WHERE ((((c.relkind = 'r'::"char") AND (i.relkind = 'i'::"char")) AND (c.oid = x.indrelid)) AND (i.oid = x.indexrelid));
   pg_rules                 | SELECT n.nspname AS schemaname, c.relname AS tablename, r.rulename, pg_get_ruledef(r.oid) AS definition FROM ((pg_rewrite r JOIN pg_class c ON ((c.oid = r.ev_class))) LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace))) WHERE (r.rulename <> '_RETURN'::name);
   pg_stat_activity         | SELECT d.oid AS datid, d.datname, pg_stat_get_backend_pid(s.backendid) AS procpid, pg_stat_get_backend_userid(s.backendid) AS usesysid, u.usename, pg_stat_get_backend_activity(s.backendid) AS current_query FROM pg_database d, (SELECT pg_stat_get_backend_idset() AS backendid) s, pg_shadow u WHERE ((pg_stat_get_backend_dbid(s.backendid) = d.oid) AND (pg_stat_get_backend_userid(s.backendid) = u.usesysid));
***************
*** 1304,1310 ****
   shoelace_obsolete        | SELECT shoelace.sl_name, shoelace.sl_avail, shoelace.sl_color, shoelace.sl_len, shoelace.sl_unit, shoelace.sl_len_cm FROM shoelace WHERE (NOT (EXISTS (SELECT shoe.shoename FROM shoe WHERE (shoe.slcolor = shoelace.sl_color))));
   street                   | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
   toyemp                   | SELECT emp.name, emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
! (38 rows)
  
  SELECT tablename, rulename, definition FROM pg_rules 
  	ORDER BY tablename, rulename;
--- 1305,1311 ----
   shoelace_obsolete        | SELECT shoelace.sl_name, shoelace.sl_avail, shoelace.sl_color, shoelace.sl_len, shoelace.sl_unit, shoelace.sl_len_cm FROM shoelace WHERE (NOT (EXISTS (SELECT shoe.shoename FROM shoe WHERE (shoe.slcolor = shoelace.sl_color))));
   street                   | SELECT r.name, r.thepath, c.cname FROM ONLY road r, real_city c WHERE (c.outline ## r.thepath);
   toyemp                   | SELECT emp.name, emp.age, emp."location", (12 * emp.salary) AS annualsal FROM emp;
! (39 rows)
  
  SELECT tablename, rulename, definition FROM pg_rules 
  	ORDER BY tablename, rulename;
FAQ