From 64505ed58ba71df3221e2467dc458af2e1912895 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 29 Aug 2002 00:17:06 +0000 Subject: Code review for standalone composite types, query-specified composite types, SRFs. Not happy with memory management yet, but I'll commit these other changes. --- doc/src/sgml/ref/create_type.sgml | 37 +-- doc/src/sgml/ref/select.sgml | 99 ++++++-- doc/src/sgml/ref/select_into.sgml | 15 +- doc/src/sgml/release.sgml | 3 +- doc/src/sgml/xfunc.sgml | 469 +++++++++++++++++++++----------------- 5 files changed, 359 insertions(+), 264 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/create_type.sgml b/doc/src/sgml/ref/create_type.sgml index f100f4d87b5..b2d454f129b 100644 --- a/doc/src/sgml/ref/create_type.sgml +++ b/doc/src/sgml/ref/create_type.sgml @@ -1,5 +1,5 @@ @@ -32,11 +32,7 @@ CREATE TYPE typename ( INPUT = typename AS - ( column_definition_list ) - -where column_definition_list can be: - -( column_name data_type [, ... ] ) + ( column_name data_type [, ... ] ) @@ -216,8 +212,12 @@ CREATE TYPE type names also must not conflict with table names in the same schema.) + + Base Types + - The first form of CREATE TYPE requires the + The first form of CREATE TYPE creates a new base type + (scalar type). It requires the registration of two functions (using CREATE FUNCTION) before defining the type. The representation of a new base type is determined by input_function, which @@ -338,20 +338,27 @@ CREATE TYPE a row fit, but they will be kept in the main table preferentially over extended and external items.) + + + + Composite Types - The second form of CREATE TYPE requires a column - definition list in the form ( column_name - data_type [, ... ] ). This - creates a composite type, similar to that of a TABLE or VIEW relation. - A stand-alone composite type is useful as the return type of FUNCTION. + The second form of CREATE TYPE + creates a composite type. + The composite type is specified by a list of column names and datatypes. + This is essentially the same as the row type + of a table, but using CREATE TYPE avoids the need to + create an actual table when all that is wanted is to define a type. + A stand-alone composite type is useful as the return type of a function. + Array Types - Whenever a user-defined data type is created, + Whenever a user-defined base data type is created, PostgreSQL automatically creates an associated array type, whose name consists of the base type's name prepended with an underscore. The parser understands this @@ -436,8 +443,8 @@ CREATE TABLE big_objs (id int4, obj bigobj); This example creates a composite type and uses it in a table function definition: -CREATE TYPE compfoo AS (f1 int, f2 int); -CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS 'SELECT fooid, foorefid FROM foo' LANGUAGE SQL; +CREATE TYPE compfoo AS (f1 int, f2 text); +CREATE FUNCTION getfoo() RETURNS SETOF compfoo AS 'SELECT fooid, fooname FROM foo' LANGUAGE SQL; diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 5fdaf90f0ac..f6dd0397570 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -1,5 +1,5 @@ @@ -40,10 +40,10 @@ where from_item can be: ( select ) [ AS ] alias [ ( column_alias_list ) ] | -table_function_name ( [ argtype [, ...] ] ) +table_function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias_list | column_definition_list ) ] | -table_function_name ( [ argtype [, ...] ] ) +table_function_name ( [ argument [, ...] ] ) AS ( column_definition_list ) | from_item [ NATURAL ] join_type from_item @@ -142,10 +142,14 @@ where from_item can be: alias - A substitute name for the preceding - table_name. + A substitute name for the FROM item containing the alias. An alias is used for brevity or to eliminate ambiguity for self-joins - (where the same table is scanned multiple times). If an alias is + (where the same table is scanned multiple times). When an alias + is provided, it completely hides the actual name of the table or + table function; for example given FROM foo AS f, the + remainder of the SELECT must refer to this FROM item as f + not foo. + If an alias is written, a column alias list can also be written to provide substitute names for one or more columns of the table. @@ -172,12 +176,15 @@ where from_item can be: A table function can appear in the FROM clause. This acts as though its output were created as a temporary table for the duration of this single SELECT command. An alias may also be used. If an alias is - written, a column alias list can also be written to provide substitute names - for one or more columns of the table function. If the table function has been - defined as returning the RECORD data type, an alias, or the keyword AS, must - also be present, followed by a column definition list in the form - ( column_name data_type [, ... ] ). - The column definition list must match the actual number and types returned by the function. + written, a column alias list can also be written to provide substitute + names for one or more columns of the table function. If the table + function has been defined as returning the record data type, + an alias, or the keyword AS, must be present, followed by + a column definition list in the form ( column_name data_type [, ... ] ). + The column definition list must match the actual number and types + of columns returned by the function. @@ -395,7 +402,7 @@ where from_item can be: this was the default result, and adding sub-tables was done by appending * to the table name. This old behavior is available via the command - SET SQL_Inheritance TO OFF; + SET SQL_Inheritance TO OFF. @@ -406,16 +413,22 @@ where from_item can be: - A FROM item can be a table function (i.e. a function that returns - multiple rows and columns). When a table function is created, it may - be defined to return a named scalar or composite data type (an existing - scalar data type, or a table or view name), or it may be defined to return - a RECORD data type. When a table function is defined to return RECORD, it - must be followed in the FROM clause by an alias, or the keyword AS alone, - and then by a parenthesized list of column names and types. This provides - a query-time composite type definition. The FROM clause composite type - must match the actual composite type returned from the function or an - ERROR will be generated. + A FROM item can be a table function (typically, a function that returns + multiple rows and/or columns, though actually any function can be used). + The function is invoked with the given argument value(s), and then its + output is scanned as though it were a table. + + + + In some cases it is useful to define table functions that can return + different column sets depending on how they are invoked. To support this, + the table function can be declared as returning the pseudo-type + record. When such a function is used in FROM, it must be + followed by an alias, or the keyword AS alone, + and then by a parenthesized list of column names and types. This provides + a query-time composite type definition. The composite type definition + must match the actual composite type returned from the function, or an + error will be reported at run-time. @@ -827,6 +840,38 @@ SELECT name FROM distributors ORDER BY code; unless ORDER BY is used to constrain the order. + + + + 2002-08-28 + + + FOR UPDATE Clause + + + + FOR UPDATE [ OF tablename [, ...] ] + + + + + FOR UPDATE causes the rows retrieved by the query to be locked as though + for update. This prevents them from being modified or deleted by other + transactions until the current transaction ends. + + + + If specific tables are named in FOR UPDATE, then only rows coming from + those tables are locked. + + + + FOR UPDATE cannot be used in contexts where returned rows can't be clearly + identified with individual table rows; for example it can't be used with + aggregation. + + + @@ -1019,8 +1064,7 @@ SELECT * FROM distributors_2(111) AS (f1 int, f2 text); PostgreSQL allows one to omit the FROM clause from a query. This feature was retained from the original PostQuel query language. It has -a straightforward use to compute the results of simple constant -expressions: +a straightforward use to compute the results of simple expressions: SELECT 2+2; @@ -1062,6 +1106,11 @@ and later will warn if the implicit-FROM feature is used in a query that also contains an explicit FROM clause. + + + The table-function feature is a PostgreSQL + extension. + diff --git a/doc/src/sgml/ref/select_into.sgml b/doc/src/sgml/ref/select_into.sgml index 13e139ffbaa..8eed28791a1 100644 --- a/doc/src/sgml/ref/select_into.sgml +++ b/doc/src/sgml/ref/select_into.sgml @@ -1,5 +1,5 @@ @@ -29,20 +29,9 @@ SELECT [ ALL | DISTINCT [ ON ( expressioncondition [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ] - [ LIMIT [ start , ] { count | ALL } ] + [ LIMIT { count | ALL } ] [ OFFSET start ] [ FOR UPDATE [ OF tablename [, ...] ] ] - -where from_item can be: - -[ ONLY ] table_name [ * ] - [ [ AS ] alias [ ( column_alias_list ) ] ] -| -( select ) - [ AS ] alias [ ( column_alias_list ) ] -| -from_item [ NATURAL ] join_type from_item - [ ON join_condition | USING ( join_column_list ) ] diff --git a/doc/src/sgml/release.sgml b/doc/src/sgml/release.sgml index e457504ebef..60c78d0588f 100644 --- a/doc/src/sgml/release.sgml +++ b/doc/src/sgml/release.sgml @@ -1,5 +1,5 @@ @@ -26,6 +26,7 @@ worries about funny characters. @@ -10,23 +10,6 @@ $Header: /cvsroot/pgsql/doc/src/sgml/xfunc.sgml,v 1.56 2002/08/23 16:41:37 tgl E Introduction - - Historically, functions were perhaps considered a tool for creating - types. Today, few people build their own types but many write - their own functions. This introduction ought to be changed to - reflect this. - - - - As it turns out, part of defining a new type is the - definition of functions that describe its behavior. - Consequently, while it is possible to define a new - function without defining a new type, the reverse is - not true. We therefore describe how to add new functions - to PostgreSQL before describing - how to add new types. - - PostgreSQL provides four kinds of functions: @@ -285,8 +268,6 @@ SELECT name, double_salary(EMP) AS dream It is also possible to build a function that returns a composite type. - (However, as we'll see below, there are some - unfortunate restrictions on how the function may be used.) This is an example of a function that returns a single EMP row: @@ -330,12 +311,12 @@ ERROR: function declared to return emp returns varchar instead of text at colum - In the present release of PostgreSQL - there are some unpleasant restrictions on how functions returning - composite types can be used. Briefly, when calling a function that - returns a row, we cannot retrieve the entire row. We must either + A function that returns a row (composite type) can be used as a table + function, as described below. It can also be called in the context + of an SQL expression, but only when you extract a single attribute out of the row or pass the entire row into - another function. (Trying to display the entire row value will yield + another function that accepts the same composite type. (Trying to + display the entire row value will yield a meaningless number.) For example, @@ -357,8 +338,8 @@ ERROR: parser: parse error at or near "." - Another approach is to use - functional notation for extracting attributes. The simple way + Another option is to use + functional notation for extracting an attribute. The simple way to explain this is that we can use the notations attribute(table) and table.attribute interchangeably: @@ -412,26 +393,73 @@ SELECT getname(new_emp()); - <acronym>SQL</acronym> Table Functions (Functions Returning Sets) + <acronym>SQL</acronym> Table Functions A table function is one that may be used in the FROM - clause of a query. All SQL Language functions may be used in this manner. + clause of a query. All SQL language functions may be used in this manner, + but it is particularly useful for functions returning composite types. If the function is defined to return a base type, the table function - produces a one column result set. If the function is defined to - return SETOF sometype, the table - function returns multiple rows. To illustrate a SQL table function, - consider the following, which returns SETOF a - composite type: + produces a one-column table. If the function is defined to return + a composite type, the table function produces a column for each column + of the composite type. + + + + Here is an example: -CREATE TABLE foo (fooid int, foosubid int, fooname text, primary key(fooid,foosubid)); +CREATE TABLE foo (fooid int, foosubid int, fooname text); INSERT INTO foo VALUES(1,1,'Joe'); INSERT INTO foo VALUES(1,2,'Ed'); INSERT INTO foo VALUES(2,1,'Mary'); + +CREATE FUNCTION getfoo(int) RETURNS foo AS ' + SELECT * FROM foo WHERE fooid = $1; +' LANGUAGE SQL; + +SELECT *, upper(fooname) FROM getfoo(1) AS t1; + + + + fooid | foosubid | fooname | upper +-------+----------+---------+------- + 1 | 1 | Joe | JOE +(2 rows) + + + As the example shows, we can work with the columns of the function's + result just the same as if they were columns of a regular table. + + + + Note that we only got one row out of the function. This is because + we did not say SETOF. + + + + + + <acronym>SQL</acronym> Functions Returning Sets + + + When an SQL function is declared as returning SETOF + sometype, the function's final + SELECT query is executed to completion, and each row it + outputs is returned as an element of the set. + + + + This feature is normally used by calling the function as a table + function. In this case each row returned by the function becomes + a row of the table seen by the query. For example, assume that + table foo has the same contents as above, and we say: + + CREATE FUNCTION getfoo(int) RETURNS setof foo AS ' SELECT * FROM foo WHERE fooid = $1; ' LANGUAGE SQL; + SELECT * FROM getfoo(1) AS t1; @@ -445,14 +473,7 @@ SELECT * FROM getfoo(1) AS t1; - When an SQL function is declared as returning SETOF - sometype, the function's final - SELECT query is executed to completion, and each row it - outputs is returned as an element of the set. - - - - Functions returning sets may also currently be called in the target list + Currently, functions returning sets may also be called in the target list of a SELECT query. For each row that the SELECT generates by itself, the function returning set is invoked, and an output row is generated for each element of the function's result set. Note, @@ -1346,7 +1367,8 @@ concat_text(PG_FUNCTION_ARGS) PG_GETARG_xxx_COPY() guarantees to return a copy of the specified parameter which is safe for writing into. (The normal macros will sometimes return a - pointer to the value which must not be written to. Using the + pointer to a value that is physically stored in a table, and so + must not be written to. Using the PG_GETARG_xxx_COPY() macros guarantees a writable result.) @@ -1471,8 +1493,8 @@ LANGUAGE C; Table Function API - The Table Function API assists in the creation of a user defined - C Language table functions (). + The Table Function API assists in the creation of user-defined + C language table functions (). Table functions are functions that produce a set of rows, made up of either base (scalar) data types, or composite (multi-column) data types. The API is split into two main components: support for returning @@ -1482,105 +1504,124 @@ LANGUAGE C; The Table Function API relies on macros and functions to suppress most - of the complexity of building composite data types and return multiple - results. In addition to the version-1 conventions discussed elsewhere, - a table function always requires the following: + of the complexity of building composite data types and returning multiple + results. A table function must follow the version-1 calling convention + described above. In addition, the source file must include: #include "funcapi.h" + + Returning Tuples (Composite Types) + The Table Function API support for returning composite data types (or tuples) starts with the AttInMetadata struct. This struct holds arrays of individual attribute information needed to create a tuple from - raw C strings. It also requires a copy of the TupleDesc. The information + raw C strings. It also saves a pointer to the TupleDesc. The information carried here is derived from the TupleDesc, but it is stored here to - avoid redundant cpu cycles on each call to a Table Function. + avoid redundant CPU cycles on each call to a Table Function. In the + case of a function returning a set, the AttInMetadata struct should be + computed once during the first call and saved for re-use in later calls. -typedef struct +typedef struct AttInMetadata { - /* full TupleDesc */ - TupleDesc tupdesc; - - /* pointer to array of attribute "type"in finfo */ - FmgrInfo *attinfuncs; + /* full TupleDesc */ + TupleDesc tupdesc; - /* pointer to array of attribute type typelem */ - Oid *attelems; + /* array of attribute type input function finfo */ + FmgrInfo *attinfuncs; - /* pointer to array of attribute type typtypmod */ - int4 *atttypmods; + /* array of attribute type typelem */ + Oid *attelems; + /* array of attribute typmod */ + int32 *atttypmods; } AttInMetadata; To assist you in populating this struct, several functions and a macro are available. Use -TupleDesc RelationNameGetTupleDesc(char *relname) +TupleDesc RelationNameGetTupleDesc(const char *relname) - to get a TupleDesc based on the function's return type relation, or + to get a TupleDesc based on a specified relation, or TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases) - to get a TupleDesc based on the function's type oid. This can be used to - get a TupleDesc for a base (scalar), or composite (relation) type. Then + to get a TupleDesc based on a type OID. This can be used to + get a TupleDesc for a base (scalar) or composite (relation) type. Then AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc) will return a pointer to an AttInMetadata struct, initialized based on - the function's TupleDesc. AttInMetadata is be used in conjunction with + the given TupleDesc. AttInMetadata can be used in conjunction with C strings to produce a properly formed tuple. The metadata is stored here - for use across calls to avoid redundant work. + to avoid redundant work across multiple calls. - In order to return a tuple you must create a tuple slot based on the + To return a tuple you must create a tuple slot based on the TupleDesc. You can use TupleTableSlot *TupleDescGetSlot(TupleDesc tupdesc) to initialize this tuple slot, or obtain one through other (user provided) means. The tuple slot is needed to create a Datum for return by the - function. + function. The same slot can (and should) be re-used on each call. - If desired, + After constructing an AttInMetadata structure, HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values) can be used to 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. The C strings should be in the form expected by the "in" function - of the attribute data type. For more information on this requirement, - see the individual data type "in" functions in the source code - (e.g. textin() for data type TEXT). In order to return a NULL value for + tuple. Each C string should be in the form expected by the input function + of the attribute data type. In order to return a NULL value for one of the attributes, the corresponding pointer in the "values" array - should be set to NULL. + should be set to NULL. This function will need to be called again + for each tuple you return. - In order to get an attribute "in" function and typelem value given the - typeid, use - -void get_type_metadata(Oid typeid, Oid *attinfuncid, Oid *attelem) - + Building a tuple via TupleDescGetAttInMetadata and BuildTupleFromCStrings + is only convenient if your function naturally computes the values to + be returned as text strings. If your code naturally computes the + values as a set of Datums, you should instead use the underlying + heap_formtuple routine to convert the Datums directly into a tuple. + You will still need the TupleDesc and a TupleTableSlot, but not + AttInMetadata. - Finally, in order to return a tuple using the SRF portion of the API - (described below), the tuple must be converted into a Datum. Use + Once you have built a tuple to return from your function, the tuple must + be converted into a Datum. Use TupleGetDatum(TupleTableSlot *slot, HeapTuple tuple) - to get a Datum given a tuple and a slot. + to get a Datum given a tuple and a slot. This Datum can be returned + directly if you intend to return just a single row, or it can be used + as the current return value in a set-returning function. + + + + An example appears below. + + + + Returning Sets + - The Table Function API support for set returning functions starts with - the FuncCallContext struct. This struct holds function context for - SRFs using fcinfo->flinfo->fn_extra to hold a pointer to it across calls. + A set-returning function (SRF) is normally called once for each item it + returns. The SRF must therefore save enough state to remember what it + was doing and return the next item on each call. The Table Function API + provides the FuncCallContext struct to help control this process. + fcinfo->flinfo->fn_extra is used to + hold a pointer to FuncCallContext across calls. typedef struct { @@ -1639,12 +1680,13 @@ typedef struct } FuncCallContext; - To assist you in populating this struct, several functions and macros - are available. Use + An SRF uses several functions and macros that automatically manipulate + the FuncCallContext struct (and expect to find it via + fn_extra). Use SRF_IS_FIRSTCALL() - to determine if your function has been called for the first or a + to determine if your function is being called for the first or a subsequent time. On the first call (only) use SRF_FIRSTCALL_INIT() @@ -1663,8 +1705,9 @@ SRF_PERCALL_SETUP() SRF_RETURN_NEXT(funcctx, result) - to send it and prepare for the next call. Finally, when your function - is finished returning data, use + to return it to the caller. (The result + must be a Datum, either a single value or a tuple prepared as described + earlier.) Finally, when your function is finished returning data, use SRF_RETURN_DONE(funcctx) @@ -1677,136 +1720,139 @@ SRF_RETURN_DONE(funcctx) Datum my_Set_Returning_Function(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - Datum result; - - [user defined declarations] - - if(SRF_IS_FIRSTCALL()) - { - [user defined code] - funcctx = SRF_FIRSTCALL_INIT(); - [if returning composite] - [obtain slot] - funcctx->slot = slot; - [endif returning composite] - [user defined code] - } - [user defined code] - funcctx = SRF_PERCALL_SETUP(); - [user defined code] - - if (funcctx->call_cntr < funcctx->max_calls) - { - [user defined code] - [obtain result Datum] - SRF_RETURN_NEXT(funcctx, result); - } - else - { - SRF_RETURN_DONE(funcctx); - } + FuncCallContext *funcctx; + Datum result; + [user defined declarations] + + if (SRF_IS_FIRSTCALL()) + { + /* one-time setup code appears here: */ + [user defined code] + funcctx = SRF_FIRSTCALL_INIT(); + [if returning composite] + [build TupleDesc, and perhaps AttInMetadata] + [obtain slot] + funcctx->slot = slot; + [endif returning composite] + [user defined code] + } + + /* each-time setup code appears here: */ + [user defined code] + funcctx = SRF_PERCALL_SETUP(); + [user defined code] + + /* this is just one way we might test whether we are done: */ + if (funcctx->call_cntr < funcctx->max_calls) + { + /* here we want to return another item: */ + [user defined code] + [obtain result Datum] + SRF_RETURN_NEXT(funcctx, result); + } + else + { + /* here we are done returning items, and just need to clean up: */ + [user defined code] + SRF_RETURN_DONE(funcctx); + } } - An example of a simple composite returning SRF looks like: + A complete example of a simple SRF returning a composite type looks like: PG_FUNCTION_INFO_V1(testpassbyval); Datum testpassbyval(PG_FUNCTION_ARGS) { - FuncCallContext *funcctx; - int call_cntr; - int max_calls; - TupleDesc tupdesc; - TupleTableSlot *slot; - AttInMetadata *attinmeta; + FuncCallContext *funcctx; + int call_cntr; + int max_calls; + TupleDesc tupdesc; + TupleTableSlot *slot; + AttInMetadata *attinmeta; + + /* stuff done only on the first call of the function */ + if (SRF_IS_FIRSTCALL()) + { + /* create a function context for cross-call persistence */ + funcctx = SRF_FIRSTCALL_INIT(); + + /* total number of tuples to be returned */ + funcctx->max_calls = PG_GETARG_UINT32(0); - /* stuff done only on the first call of the function */ - if(SRF_IS_FIRSTCALL()) - { - /* create a function context for cross-call persistence */ - funcctx = SRF_FIRSTCALL_INIT(); - - /* total number of tuples to be returned */ - funcctx->max_calls = PG_GETARG_UINT32(0); - - /* - * Build a tuple description for a __testpassbyval tuple - */ - tupdesc = RelationNameGetTupleDesc("__testpassbyval"); + /* + * Build a tuple description for a __testpassbyval tuple + */ + tupdesc = RelationNameGetTupleDesc("__testpassbyval"); - /* allocate a slot for a tuple with this tupdesc */ - slot = TupleDescGetSlot(tupdesc); + /* allocate a slot for a tuple with this tupdesc */ + slot = TupleDescGetSlot(tupdesc); - /* assign slot to function context */ - funcctx->slot = slot; + /* assign slot to function context */ + funcctx->slot = slot; - /* - * Generate attribute metadata needed later to produce tuples from raw - * C strings - */ - attinmeta = TupleDescGetAttInMetadata(tupdesc); - funcctx->attinmeta = attinmeta; + /* + * Generate attribute metadata needed later to produce tuples from raw + * C strings + */ + attinmeta = TupleDescGetAttInMetadata(tupdesc); + funcctx->attinmeta = attinmeta; } - /* stuff done on every call of the function */ - funcctx = SRF_PERCALL_SETUP(); + /* stuff done on every call of the function */ + funcctx = SRF_PERCALL_SETUP(); - call_cntr = funcctx->call_cntr; - max_calls = funcctx->max_calls; - slot = funcctx->slot; - attinmeta = funcctx->attinmeta; + call_cntr = funcctx->call_cntr; + max_calls = funcctx->max_calls; + slot = funcctx->slot; + attinmeta = funcctx->attinmeta; - if (call_cntr < max_calls) /* do when there is more left to send */ - { - char **values; - HeapTuple tuple; - Datum result; - - /* - * 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(3 * sizeof(char *)); - values[0] = (char *) palloc(16 * sizeof(char)); - values[1] = (char *) palloc(16 * sizeof(char)); - values[2] = (char *) palloc(16 * sizeof(char)); - - snprintf(values[0], 16, "%d", 1 * PG_GETARG_INT32(1)); - snprintf(values[1], 16, "%d", 2 * PG_GETARG_INT32(1)); - snprintf(values[2], 16, "%d", 3 * PG_GETARG_INT32(1)); - - /* build a tuple */ - tuple = BuildTupleFromCStrings(attinmeta, values); - - /* make the tuple into a datum */ - result = TupleGetDatum(slot, tuple); - - /* Clean up */ - pfree(values[0]); - pfree(values[1]); - pfree(values[2]); - pfree(values); - - SRF_RETURN_NEXT(funcctx, result); - } - else /* do when there is no more left */ - { - SRF_RETURN_DONE(funcctx); - } + if (call_cntr < max_calls) /* do when there is more left to send */ + { + char **values; + HeapTuple tuple; + Datum result; + + /* + * 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(3 * sizeof(char *)); + values[0] = (char *) palloc(16 * sizeof(char)); + values[1] = (char *) palloc(16 * sizeof(char)); + values[2] = (char *) palloc(16 * sizeof(char)); + + snprintf(values[0], 16, "%d", 1 * PG_GETARG_INT32(1)); + snprintf(values[1], 16, "%d", 2 * PG_GETARG_INT32(1)); + snprintf(values[2], 16, "%d", 3 * PG_GETARG_INT32(1)); + + /* build a tuple */ + tuple = BuildTupleFromCStrings(attinmeta, values); + + /* make the tuple into a datum */ + result = TupleGetDatum(slot, tuple); + + /* Clean up */ + pfree(values[0]); + pfree(values[1]); + pfree(values[2]); + pfree(values); + + SRF_RETURN_NEXT(funcctx, result); + } + else /* do when there is no more left */ + { + SRF_RETURN_DONE(funcctx); + } } with supporting SQL code of -CREATE VIEW __testpassbyval AS - SELECT - 0::INT4 AS f1, - 0::INT4 AS f2, - 0::INT4 AS f3; +CREATE TYPE __testpassbyval AS (f1 int4, f2 int4, f3 int4); CREATE OR REPLACE FUNCTION testpassbyval(int4, int4) RETURNS setof __testpassbyval AS 'MODULE_PATHNAME','testpassbyval' LANGUAGE 'c' IMMUTABLE STRICT; @@ -1816,6 +1862,9 @@ CREATE OR REPLACE FUNCTION testpassbyval(int4, int4) RETURNS setof __testpassbyv See contrib/tablefunc for more examples of Table Functions. + + + @@ -2031,23 +2080,23 @@ CREATE FUNCTION test(int, int) RETURNS int Table functions work wherever tables do in SELECT statements. For example -CREATE TABLE foo (fooid int, foosubid int, fooname text, primary key(fooid,foosubid)); -CREATE FUNCTION getfoo(int) RETURNS foo AS 'SELECT * FROM foo WHERE fooid = $1;' LANGUAGE SQL; +CREATE TABLE foo (fooid int, foosubid int, fooname text); + +CREATE FUNCTION getfoo(int) RETURNS setof foo AS ' + SELECT * FROM foo WHERE fooid = $1; +' LANGUAGE SQL; + SELECT * FROM getfoo(1) AS t1; -SELECT * FROM foo where foosubid in (select foosubid from getfoo(foo.fooid) z where z.fooid = foo.fooid); + +SELECT * FROM foo +WHERE foosubid in (select foosubid from getfoo(foo.fooid) z + where z.fooid = foo.fooid); + CREATE VIEW vw_getfoo AS SELECT * FROM getfoo(1); SELECT * FROM vw_getfoo; are all valid statements. - - - Currently, table functions are supported as SQL language functions - () and C language functions - (). See these individual sections for more - details. - - -- cgit v1.2.3