2 * $PostgreSQL: pgsql/contrib/tablefunc/tablefunc.c,v 1.62 2010/01/02 16:57:32 momjian Exp $
7 * Sample to demonstrate C functions which return setof scalar
9 * Joe Conway <mail@joeconway.com>
11 * Nabil Sayegh <postgresql@e-trolley.de>
13 * Copyright (c) 2002-2010, PostgreSQL Global Development Group
15 * Permission to use, copy, modify, and distribute this software and its
16 * documentation for any purpose, without fee, and without a written agreement
17 * is hereby granted, provided that the above copyright notice and this
18 * paragraph and the following two paragraphs appear in all copies.
20 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
21 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
22 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
23 * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
26 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
27 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
29 * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
30 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
37 #include "catalog/pg_type.h"
40 #include "executor/spi.h"
41 #include "lib/stringinfo.h"
42 #include "miscadmin.h"
43 #include "utils/builtins.h"
44 #include "utils/guc.h"
45 #include "utils/lsyscache.h"
47 #include "tablefunc.h"
51 static HTAB *load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
52 static Tuplestorestate *get_crosstab_tuplestore(char *sql,
55 MemoryContext per_query_ctx,
57 static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial);
58 static bool compatCrosstabTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
59 static bool compatConnectbyTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
60 static void get_normal_pair(float8 *x1, float8 *x2);
61 static Tuplestorestate *connectby(char *relname,
70 MemoryContext per_query_ctx,
72 AttInMetadata *attinmeta);
73 static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
85 MemoryContext per_query_ctx,
86 AttInMetadata *attinmeta,
87 Tuplestorestate *tupstore);
88 static char *quote_literal_cstr(char *rawstr);
92 float8 mean; /* mean of the distribution */
93 float8 stddev; /* stddev of the distribution */
94 float8 carry_val; /* hold second generated value */
95 bool use_carry; /* use second generated value */
98 #define xpfree(var_) \
107 #define xpstrdup(tgtvar_, srcvar_) \
110 tgtvar_ = pstrdup(srcvar_); \
115 #define xstreq(tgtvar_, srcvar_) \
116 (((tgtvar_ == NULL) && (srcvar_ == NULL)) || \
117 ((tgtvar_ != NULL) && (srcvar_ != NULL) && (strcmp(tgtvar_, srcvar_) == 0)))
119 /* sign, 10 digits, '\0' */
120 #define INT32_STRLEN 12
122 /* stored info for a crosstab category */
123 typedef struct crosstab_cat_desc
125 char *catname; /* full category name */
126 int attidx; /* zero based */
129 #define MAX_CATNAME_LEN NAMEDATALEN
132 #define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC) \
134 crosstab_HashEnt *hentry; char key[MAX_CATNAME_LEN]; \
136 MemSet(key, 0, MAX_CATNAME_LEN); \
137 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATNAME); \
138 hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
139 key, HASH_FIND, NULL); \
141 CATDESC = hentry->catdesc; \
146 #define crosstab_HashTableInsert(HASHTAB, CATDESC) \
148 crosstab_HashEnt *hentry; bool found; char key[MAX_CATNAME_LEN]; \
150 MemSet(key, 0, MAX_CATNAME_LEN); \
151 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATDESC->catname); \
152 hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
153 key, HASH_ENTER, &found); \
156 (errcode(ERRCODE_DUPLICATE_OBJECT), \
157 errmsg("duplicate category name"))); \
158 hentry->catdesc = CATDESC; \
162 typedef struct crosstab_hashent
164 char internal_catname[MAX_CATNAME_LEN];
165 crosstab_cat_desc *catdesc;
169 * normal_rand - return requested number of random values
170 * with a Gaussian (Normal) distribution.
172 * inputs are int numvals, float8 mean, and float8 stddev
173 * returns setof float8
175 PG_FUNCTION_INFO_V1(normal_rand);
177 normal_rand(PG_FUNCTION_ARGS)
179 FuncCallContext *funcctx;
182 normal_rand_fctx *fctx;
187 MemoryContext oldcontext;
189 /* stuff done only on the first call of the function */
190 if (SRF_IS_FIRSTCALL())
192 /* create a function context for cross-call persistence */
193 funcctx = SRF_FIRSTCALL_INIT();
196 * switch to memory context appropriate for multiple function calls
198 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
200 /* total number of tuples to be returned */
201 funcctx->max_calls = PG_GETARG_UINT32(0);
203 /* allocate memory for user context */
204 fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
207 * Use fctx to keep track of upper and lower bounds from call to call.
208 * It will also be used to carry over the spare value we get from the
209 * Box-Muller algorithm so that we only actually calculate a new value
212 fctx->mean = PG_GETARG_FLOAT8(1);
213 fctx->stddev = PG_GETARG_FLOAT8(2);
215 fctx->use_carry = false;
217 funcctx->user_fctx = fctx;
219 MemoryContextSwitchTo(oldcontext);
222 /* stuff done on every call of the function */
223 funcctx = SRF_PERCALL_SETUP();
225 call_cntr = funcctx->call_cntr;
226 max_calls = funcctx->max_calls;
227 fctx = funcctx->user_fctx;
229 stddev = fctx->stddev;
230 carry_val = fctx->carry_val;
231 use_carry = fctx->use_carry;
233 if (call_cntr < max_calls) /* do when there is more left to send */
240 * reset use_carry and use second value obtained on last pass
242 fctx->use_carry = false;
250 /* Get the next two normal values */
251 get_normal_pair(&normval_1, &normval_2);
254 result = mean + (stddev * normval_1);
256 /* and save the second */
257 fctx->carry_val = mean + (stddev * normval_2);
258 fctx->use_carry = true;
261 /* send the result */
262 SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
265 /* do when there is no more left */
266 SRF_RETURN_DONE(funcctx);
271 * Assigns normally distributed (Gaussian) values to a pair of provided
272 * parameters, with mean 0, standard deviation 1.
274 * This routine implements Algorithm P (Polar method for normal deviates)
275 * from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages
276 * 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E.
277 * Muller, and G. Marsaglia, _Annals_Math,_Stat._ 29 (1958), 610-611.
281 get_normal_pair(float8 *x1, float8 *x2)
291 u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
292 u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
294 v1 = (2.0 * u1) - 1.0;
295 v2 = (2.0 * u2) - 1.0;
297 s = v1 * v1 + v2 * v2;
307 s = sqrt((-2.0 * log(s)) / s);
314 * crosstab - create a crosstab of rowids and values columns from a
315 * SQL statement returning one rowid column, one category column,
316 * and one value column.
318 * e.g. given sql which produces:
321 * ------+-------+-------
332 * <===== values columns =====>
333 * rowid cat1 cat2 cat3 cat4
334 * ------+-------+-------+-------+-------
335 * row1 val1 val2 val3 val4
336 * row2 val5 val6 val7 val8
339 * 1. SQL result must be ordered by 1,2.
340 * 2. The number of values columns depends on the tuple description
341 * of the function's declared return type. The return type's columns
342 * must match the datatypes of the SQL query's result. The datatype
343 * of the category column can be anything, however.
344 * 3. Missing values (i.e. not enough adjacent rows of same rowid to
345 * fill the number of result values columns) are filled in with nulls.
346 * 4. Extra values (i.e. too many adjacent rows of same rowid to fill
347 * the number of result values columns) are skipped.
348 * 5. Rows with all nulls in the values columns are skipped.
350 PG_FUNCTION_INFO_V1(crosstab);
352 crosstab(PG_FUNCTION_ARGS)
354 char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
355 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
356 Tuplestorestate *tupstore;
360 AttInMetadata *attinmeta;
361 SPITupleTable *spi_tuptable;
362 TupleDesc spi_tupdesc;
367 MemoryContext per_query_ctx;
368 MemoryContext oldcontext;
372 /* check to see if caller supports us returning a tuplestore */
373 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
375 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
376 errmsg("set-valued function called in context that cannot accept a set")));
377 if (!(rsinfo->allowedModes & SFRM_Materialize))
379 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
380 errmsg("materialize mode required, but it is not " \
381 "allowed in this context")));
383 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
385 /* Connect to SPI manager */
386 if ((ret = SPI_connect()) < 0)
388 elog(ERROR, "crosstab: SPI_connect returned %d", ret);
390 /* Retrieve the desired rows */
391 ret = SPI_execute(sql, true, 0);
392 proc = SPI_processed;
394 /* If no qualifying tuples, fall out early */
395 if (ret != SPI_OK_SELECT || proc <= 0)
398 rsinfo->isDone = ExprEndResult;
402 spi_tuptable = SPI_tuptable;
403 spi_tupdesc = spi_tuptable->tupdesc;
406 * The provided SQL query must always return three columns.
409 * the label or identifier for each row in the final result
411 * the label or identifier for each column in the final result
413 * the value for each column in the final result
416 if (spi_tupdesc->natts != 3)
418 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
419 errmsg("invalid source data SQL statement"),
420 errdetail("The provided SQL must return 3 "
421 "columns: rowid, category, and values.")));
423 /* get a tuple descriptor for our result type */
424 switch (get_call_result_type(fcinfo, NULL, &tupdesc))
426 case TYPEFUNC_COMPOSITE:
429 case TYPEFUNC_RECORD:
430 /* failed to determine actual type of RECORD */
432 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
433 errmsg("function returning record called in context "
434 "that cannot accept type record")));
437 /* result type isn't composite */
438 elog(ERROR, "return type must be a row type");
443 * Check that return tupdesc is compatible with the data we got from SPI,
444 * at least based on number and type of attributes
446 if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
448 (errcode(ERRCODE_SYNTAX_ERROR),
449 errmsg("return and sql tuple descriptions are " \
453 * switch to long-lived memory context
455 oldcontext = MemoryContextSwitchTo(per_query_ctx);
457 /* make sure we have a persistent copy of the result tupdesc */
458 tupdesc = CreateTupleDescCopy(tupdesc);
460 /* initialize our tuplestore in long-lived context */
462 tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
465 MemoryContextSwitchTo(oldcontext);
468 * Generate attribute metadata needed later to produce tuples from raw C
471 attinmeta = TupleDescGetAttInMetadata(tupdesc);
473 /* total number of tuples to be examined */
476 /* the return tuple always must have 1 rowid + num_categories columns */
477 num_categories = tupdesc->natts - 1;
482 for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
484 bool skip_tuple = false;
487 /* allocate and zero space */
488 values = (char **) palloc0((1 + num_categories) * sizeof(char *));
491 * now loop through the sql results and assign each value in sequence
492 * to the next category
494 for (i = 0; i < num_categories; i++)
499 /* see if we've gone too far already */
500 if (call_cntr >= max_calls)
503 /* get the next sql result tuple */
504 spi_tuple = spi_tuptable->vals[call_cntr];
506 /* get the rowid from the current sql result tuple */
507 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
510 * If this is the first pass through the values for this rowid,
511 * set the first column to rowid
515 xpstrdup(values[0], rowid);
518 * Check to see if the rowid is the same as that of the last
519 * tuple sent -- if so, skip this tuple entirely
521 if (!firstpass && xstreq(lastrowid, rowid))
530 * If rowid hasn't changed on us, continue building the output
533 if (xstreq(rowid, values[0]))
536 * Get the next category item value, which is always attribute
539 * Be careful to assign the value to the array index based on
540 * which category we are presently processing.
542 values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
545 * increment the counter since we consume a row for each
546 * category, but not for last pass because the outer loop will
549 if (i < (num_categories - 1))
556 * We'll fill in NULLs for the missing values, but we need to
557 * decrement the counter since this sql result row doesn't
558 * belong to the current output tuple.
570 /* build the tuple and store it */
571 tuple = BuildTupleFromCStrings(attinmeta, values);
572 tuplestore_puttuple(tupstore, tuple);
573 heap_freetuple(tuple);
576 /* Remember current rowid */
578 xpstrdup(lastrowid, values[0]);
582 for (i = 0; i < num_categories + 1; i++)
583 if (values[i] != NULL)
588 /* let the caller know we're sending back a tuplestore */
589 rsinfo->returnMode = SFRM_Materialize;
590 rsinfo->setResult = tupstore;
591 rsinfo->setDesc = tupdesc;
593 /* release SPI related resources (and return to caller's context) */
600 * crosstab_hash - reimplement crosstab as materialized function and
601 * properly deal with missing values (i.e. don't pack remaining
602 * values to the left)
604 * crosstab - create a crosstab of rowids and values columns from a
605 * SQL statement returning one rowid column, one category column,
606 * and one value column.
608 * e.g. given sql which produces:
611 * ------+-------+-------
621 * <===== values columns =====>
622 * rowid cat1 cat2 cat3 cat4
623 * ------+-------+-------+-------+-------
624 * row1 val1 val2 null val4
625 * row2 val5 val6 val7 val8
628 * 1. SQL result must be ordered by 1.
629 * 2. The number of values columns depends on the tuple description
630 * of the function's declared return type.
631 * 3. Missing values (i.e. missing category) are filled in with nulls.
632 * 4. Extra values (i.e. not in category results) are skipped.
634 PG_FUNCTION_INFO_V1(crosstab_hash);
636 crosstab_hash(PG_FUNCTION_ARGS)
638 char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
639 char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
640 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
642 MemoryContext per_query_ctx;
643 MemoryContext oldcontext;
646 /* check to see if caller supports us returning a tuplestore */
647 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
649 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
650 errmsg("set-valued function called in context that cannot accept a set")));
651 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
652 rsinfo->expectedDesc == NULL)
654 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
655 errmsg("materialize mode required, but it is not " \
656 "allowed in this context")));
658 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
659 oldcontext = MemoryContextSwitchTo(per_query_ctx);
661 /* get the requested return tuple description */
662 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
665 * Check to make sure we have a reasonable tuple descriptor
667 * Note we will attempt to coerce the values into whatever the return
668 * attribute type is and depend on the "in" function to complain if
671 if (tupdesc->natts < 2)
673 (errcode(ERRCODE_SYNTAX_ERROR),
674 errmsg("query-specified return tuple and " \
675 "crosstab function are not compatible")));
677 /* load up the categories hash table */
678 crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
680 /* let the caller know we're sending back a tuplestore */
681 rsinfo->returnMode = SFRM_Materialize;
683 /* now go build it */
684 rsinfo->setResult = get_crosstab_tuplestore(sql,
688 rsinfo->allowedModes & SFRM_Materialize_Random);
691 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
692 * tuples are in our tuplestore and passed back through rsinfo->setResult.
693 * rsinfo->setDesc is set to the tuple description that we actually used
694 * to build our tuples with, so the caller can verify we did what it was
697 rsinfo->setDesc = tupdesc;
698 MemoryContextSwitchTo(oldcontext);
704 * load up the categories hash table
707 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
713 MemoryContext SPIcontext;
715 /* initialize the category hash table */
716 MemSet(&ctl, 0, sizeof(ctl));
717 ctl.keysize = MAX_CATNAME_LEN;
718 ctl.entrysize = sizeof(crosstab_HashEnt);
719 ctl.hcxt = per_query_ctx;
722 * use INIT_CATS, defined above as a guess of how many hash table entries
723 * to create, initially
725 crosstab_hash = hash_create("crosstab hash",
728 HASH_ELEM | HASH_CONTEXT);
730 /* Connect to SPI manager */
731 if ((ret = SPI_connect()) < 0)
733 elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
735 /* Retrieve the category name rows */
736 ret = SPI_execute(cats_sql, true, 0);
737 proc = SPI_processed;
739 /* Check for qualifying tuples */
740 if ((ret == SPI_OK_SELECT) && (proc > 0))
742 SPITupleTable *spi_tuptable = SPI_tuptable;
743 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
747 * The provided categories SQL query must always return one column:
748 * category - the label or identifier for each column
750 if (spi_tupdesc->natts != 1)
752 (errcode(ERRCODE_SYNTAX_ERROR),
753 errmsg("provided \"categories\" SQL must " \
754 "return 1 column of at least one row")));
756 for (i = 0; i < proc; i++)
758 crosstab_cat_desc *catdesc;
762 /* get the next sql result tuple */
763 spi_tuple = spi_tuptable->vals[i];
765 /* get the category from the current sql result tuple */
766 catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
768 SPIcontext = MemoryContextSwitchTo(per_query_ctx);
770 catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
771 catdesc->catname = catname;
774 /* Add the proc description block to the hashtable */
775 crosstab_HashTableInsert(crosstab_hash, catdesc);
777 MemoryContextSwitchTo(SPIcontext);
781 if (SPI_finish() != SPI_OK_FINISH)
783 elog(ERROR, "load_categories_hash: SPI_finish() failed");
785 return crosstab_hash;
789 * create and populate the crosstab tuplestore using the provided source query
791 static Tuplestorestate *
792 get_crosstab_tuplestore(char *sql,
795 MemoryContext per_query_ctx,
798 Tuplestorestate *tupstore;
799 int num_categories = hash_get_num_entries(crosstab_hash);
800 AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
806 /* initialize our tuplestore (while still in query context!) */
807 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
809 /* Connect to SPI manager */
810 if ((ret = SPI_connect()) < 0)
812 elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
814 /* Now retrieve the crosstab source rows */
815 ret = SPI_execute(sql, true, 0);
816 proc = SPI_processed;
818 /* Check for qualifying tuples */
819 if ((ret == SPI_OK_SELECT) && (proc > 0))
821 SPITupleTable *spi_tuptable = SPI_tuptable;
822 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
823 int ncols = spi_tupdesc->natts;
825 char *lastrowid = NULL;
826 bool firstpass = true;
831 if (num_categories == 0)
833 /* no qualifying category tuples */
835 (errcode(ERRCODE_SYNTAX_ERROR),
836 errmsg("provided \"categories\" SQL must " \
837 "return 1 column of at least one row")));
841 * The provided SQL query must always return at least three columns:
843 * 1. rowname the label for each row - column 1 in the final result
844 * 2. category the label for each value-column in the final result 3.
845 * value the values used to populate the value-columns
847 * If there are more than three columns, the last two are taken as
848 * "category" and "values". The first column is taken as "rowname".
849 * Additional columns (2 thru N-2) are assumed the same for the same
850 * "rowname", and are copied into the result tuple from the first time
851 * we encounter a particular rowname.
855 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
856 errmsg("invalid source data SQL statement"),
857 errdetail("The provided SQL must return 3 " \
858 " columns; rowid, category, and values.")));
860 result_ncols = (ncols - 2) + num_categories;
862 /* Recheck to make sure we tuple descriptor still looks reasonable */
863 if (tupdesc->natts != result_ncols)
865 (errcode(ERRCODE_SYNTAX_ERROR),
866 errmsg("invalid return type"),
867 errdetail("Query-specified return " \
868 "tuple has %d columns but crosstab " \
869 "returns %d.", tupdesc->natts, result_ncols)));
872 values = (char **) palloc(result_ncols * sizeof(char *));
874 /* and make sure it's clear */
875 memset(values, '\0', result_ncols * sizeof(char *));
877 for (i = 0; i < proc; i++)
880 crosstab_cat_desc *catdesc;
883 /* get the next sql result tuple */
884 spi_tuple = spi_tuptable->vals[i];
886 /* get the rowid from the current sql result tuple */
887 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
890 * if we're on a new output row, grab the column values up to
893 if (firstpass || !xstreq(lastrowid, rowid))
896 * a new row means we need to flush the old one first, unless
897 * we're on the very first row
901 /* rowid changed, flush the previous output row */
902 tuple = BuildTupleFromCStrings(attinmeta, values);
904 tuplestore_puttuple(tupstore, tuple);
906 for (j = 0; j < result_ncols; j++)
911 for (j = 1; j < ncols - 2; j++)
912 values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
914 /* we're no longer on the first pass */
918 /* look up the category and fill in the appropriate column */
919 catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
923 crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
926 values[catdesc->attidx + ncols - 2] =
927 SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
931 xpstrdup(lastrowid, rowid);
934 /* flush the last output row */
935 tuple = BuildTupleFromCStrings(attinmeta, values);
937 tuplestore_puttuple(tupstore, tuple);
940 if (SPI_finish() != SPI_OK_FINISH)
942 elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
944 tuplestore_donestoring(tupstore);
950 * connectby_text - produce a result set from a hierarchical (parent/child)
953 * e.g. given table foo:
955 * keyid parent_keyid pos
956 * ------+------------+--
968 * connectby(text relname, text keyid_fld, text parent_keyid_fld
969 * [, text orderby_fld], text start_with, int max_depth
970 * [, text branch_delim])
971 * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
973 * keyid parent_id level branch serial
974 * ------+-----------+--------+-----------------------
976 * row5 row2 1 row2~row5 2
977 * row9 row5 2 row2~row5~row9 3
978 * row4 row2 1 row2~row4 4
979 * row6 row4 2 row2~row4~row6 5
980 * row8 row6 3 row2~row4~row6~row8 6
983 PG_FUNCTION_INFO_V1(connectby_text);
985 #define CONNECTBY_NCOLS 4
986 #define CONNECTBY_NCOLS_NOBRANCH 3
989 connectby_text(PG_FUNCTION_ARGS)
991 char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
992 char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
993 char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
994 char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
995 int max_depth = PG_GETARG_INT32(4);
996 char *branch_delim = NULL;
997 bool show_branch = false;
998 bool show_serial = false;
999 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1001 AttInMetadata *attinmeta;
1002 MemoryContext per_query_ctx;
1003 MemoryContext oldcontext;
1005 /* check to see if caller supports us returning a tuplestore */
1006 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1008 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1009 errmsg("set-valued function called in context that cannot accept a set")));
1010 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1011 rsinfo->expectedDesc == NULL)
1013 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1014 errmsg("materialize mode required, but it is not " \
1015 "allowed in this context")));
1017 if (fcinfo->nargs == 6)
1019 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1023 /* default is no show, tilde for the delimiter */
1024 branch_delim = pstrdup("~");
1026 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1027 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1029 /* get the requested return tuple description */
1030 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1032 /* does it meet our needs */
1033 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1035 /* OK, use it then */
1036 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1038 /* OK, go to work */
1039 rsinfo->returnMode = SFRM_Materialize;
1040 rsinfo->setResult = connectby(relname,
1050 rsinfo->allowedModes & SFRM_Materialize_Random,
1052 rsinfo->setDesc = tupdesc;
1054 MemoryContextSwitchTo(oldcontext);
1057 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1058 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1059 * rsinfo->setDesc is set to the tuple description that we actually used
1060 * to build our tuples with, so the caller can verify we did what it was
1066 PG_FUNCTION_INFO_V1(connectby_text_serial);
1068 connectby_text_serial(PG_FUNCTION_ARGS)
1070 char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
1071 char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1072 char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1073 char *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1074 char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1075 int max_depth = PG_GETARG_INT32(5);
1076 char *branch_delim = NULL;
1077 bool show_branch = false;
1078 bool show_serial = true;
1079 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1081 AttInMetadata *attinmeta;
1082 MemoryContext per_query_ctx;
1083 MemoryContext oldcontext;
1085 /* check to see if caller supports us returning a tuplestore */
1086 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1088 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1089 errmsg("set-valued function called in context that cannot accept a set")));
1090 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1091 rsinfo->expectedDesc == NULL)
1093 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1094 errmsg("materialize mode required, but it is not " \
1095 "allowed in this context")));
1097 if (fcinfo->nargs == 7)
1099 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1103 /* default is no show, tilde for the delimiter */
1104 branch_delim = pstrdup("~");
1106 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1107 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1109 /* get the requested return tuple description */
1110 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1112 /* does it meet our needs */
1113 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1115 /* OK, use it then */
1116 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1118 /* OK, go to work */
1119 rsinfo->returnMode = SFRM_Materialize;
1120 rsinfo->setResult = connectby(relname,
1130 rsinfo->allowedModes & SFRM_Materialize_Random,
1132 rsinfo->setDesc = tupdesc;
1134 MemoryContextSwitchTo(oldcontext);
1137 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1138 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1139 * rsinfo->setDesc is set to the tuple description that we actually used
1140 * to build our tuples with, so the caller can verify we did what it was
1148 * connectby - does the real work for connectby_text()
1150 static Tuplestorestate *
1151 connectby(char *relname,
1153 char *parent_key_fld,
1160 MemoryContext per_query_ctx,
1162 AttInMetadata *attinmeta)
1164 Tuplestorestate *tupstore = NULL;
1166 MemoryContext oldcontext;
1170 /* Connect to SPI manager */
1171 if ((ret = SPI_connect()) < 0)
1172 /* internal error */
1173 elog(ERROR, "connectby: SPI_connect returned %d", ret);
1175 /* switch to longer term context to create the tuple store */
1176 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1178 /* initialize our tuplestore */
1179 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1181 MemoryContextSwitchTo(oldcontext);
1183 /* now go get the whole tree */
1184 tupstore = build_tuplestore_recursively(key_fld,
1190 start_with, /* current_branch */
1191 0, /* initial level is 0 */
1192 &serial, /* initial serial is 1 */
1205 static Tuplestorestate *
1206 build_tuplestore_recursively(char *key_fld,
1207 char *parent_key_fld,
1218 MemoryContext per_query_ctx,
1219 AttInMetadata *attinmeta,
1220 Tuplestorestate *tupstore)
1222 TupleDesc tupdesc = attinmeta->tupdesc;
1229 char *current_key_parent;
1230 char current_level[INT32_STRLEN];
1231 char serial_str[INT32_STRLEN];
1232 char *current_branch;
1235 if (max_depth > 0 && level > max_depth)
1238 initStringInfo(&sql);
1240 /* Build initial sql statement */
1243 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1248 quote_literal_cstr(start_with),
1249 key_fld, key_fld, parent_key_fld);
1254 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1259 quote_literal_cstr(start_with),
1260 key_fld, key_fld, parent_key_fld,
1266 values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1268 values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1270 /* First time through, do a little setup */
1273 /* root value is the one we initially start with */
1274 values[0] = start_with;
1276 /* root value has no parent */
1279 /* root level is 0 */
1280 sprintf(current_level, "%d", level);
1281 values[2] = current_level;
1283 /* root branch is just starting root value */
1285 values[3] = start_with;
1287 /* root starts the serial with 1 */
1290 sprintf(serial_str, "%d", (*serial)++);
1292 values[4] = serial_str;
1294 values[3] = serial_str;
1297 /* construct the tuple */
1298 tuple = BuildTupleFromCStrings(attinmeta, values);
1301 tuplestore_puttuple(tupstore, tuple);
1303 /* increment level */
1307 /* Retrieve the desired rows */
1308 ret = SPI_execute(sql.data, true, 0);
1309 proc = SPI_processed;
1311 /* Check for qualifying tuples */
1312 if ((ret == SPI_OK_SELECT) && (proc > 0))
1314 HeapTuple spi_tuple;
1315 SPITupleTable *tuptable = SPI_tuptable;
1316 TupleDesc spi_tupdesc = tuptable->tupdesc;
1318 StringInfoData branchstr;
1319 StringInfoData chk_branchstr;
1320 StringInfoData chk_current_key;
1322 /* First time through, do a little more setup */
1326 * Check that return tupdesc is compatible with the one we got
1327 * from the query, but only at level 0 -- no need to check more
1331 if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
1333 (errcode(ERRCODE_SYNTAX_ERROR),
1334 errmsg("invalid return type"),
1335 errdetail("Return and SQL tuple descriptions are " \
1339 initStringInfo(&branchstr);
1340 initStringInfo(&chk_branchstr);
1341 initStringInfo(&chk_current_key);
1343 for (i = 0; i < proc; i++)
1345 /* initialize branch for this pass */
1346 appendStringInfo(&branchstr, "%s", branch);
1347 appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1349 /* get the next sql result tuple */
1350 spi_tuple = tuptable->vals[i];
1352 /* get the current key and parent */
1353 current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1354 appendStringInfo(&chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
1355 current_key_parent = pstrdup(SPI_getvalue(spi_tuple, spi_tupdesc, 2));
1357 /* get the current level */
1358 sprintf(current_level, "%d", level);
1360 /* check to see if this key is also an ancestor */
1361 if (strstr(chk_branchstr.data, chk_current_key.data))
1362 elog(ERROR, "infinite recursion detected");
1364 /* OK, extend the branch */
1365 appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1366 current_branch = branchstr.data;
1369 values[0] = pstrdup(current_key);
1370 values[1] = current_key_parent;
1371 values[2] = current_level;
1373 values[3] = current_branch;
1376 sprintf(serial_str, "%d", (*serial)++);
1378 values[4] = serial_str;
1380 values[3] = serial_str;
1383 tuple = BuildTupleFromCStrings(attinmeta, values);
1385 xpfree(current_key);
1386 xpfree(current_key_parent);
1388 /* store the tuple for later use */
1389 tuplestore_puttuple(tupstore, tuple);
1391 heap_freetuple(tuple);
1393 /* recurse using current_key_parent as the new start_with */
1394 tupstore = build_tuplestore_recursively(key_fld,
1410 /* reset branch for next pass */
1411 resetStringInfo(&branchstr);
1412 resetStringInfo(&chk_branchstr);
1413 resetStringInfo(&chk_current_key);
1416 xpfree(branchstr.data);
1417 xpfree(chk_branchstr.data);
1418 xpfree(chk_current_key.data);
1425 * Check expected (query runtime) tupdesc suitable for Connectby
1428 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1430 int serial_column = 0;
1435 /* are there the correct number of columns */
1438 if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1440 (errcode(ERRCODE_SYNTAX_ERROR),
1441 errmsg("invalid return type"),
1442 errdetail("Query-specified return tuple has " \
1443 "wrong number of columns.")));
1447 if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1449 (errcode(ERRCODE_SYNTAX_ERROR),
1450 errmsg("invalid return type"),
1451 errdetail("Query-specified return tuple has " \
1452 "wrong number of columns.")));
1455 /* check that the types of the first two columns match */
1456 if (tupdesc->attrs[0]->atttypid != tupdesc->attrs[1]->atttypid)
1458 (errcode(ERRCODE_SYNTAX_ERROR),
1459 errmsg("invalid return type"),
1460 errdetail("First two columns must be the same type.")));
1462 /* check that the type of the third column is INT4 */
1463 if (tupdesc->attrs[2]->atttypid != INT4OID)
1465 (errcode(ERRCODE_SYNTAX_ERROR),
1466 errmsg("invalid return type"),
1467 errdetail("Third column must be type %s.",
1468 format_type_be(INT4OID))));
1470 /* check that the type of the fourth column is TEXT if applicable */
1471 if (show_branch && tupdesc->attrs[3]->atttypid != TEXTOID)
1473 (errcode(ERRCODE_SYNTAX_ERROR),
1474 errmsg("invalid return type"),
1475 errdetail("Fourth column must be type %s.",
1476 format_type_be(TEXTOID))));
1478 /* check that the type of the fifth column is INT4 */
1479 if (show_branch && show_serial && tupdesc->attrs[4]->atttypid != INT4OID)
1480 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1481 "fifth column must be type %s", format_type_be(INT4OID));
1483 /* check that the type of the fifth column is INT4 */
1484 if (!show_branch && show_serial && tupdesc->attrs[3]->atttypid != INT4OID)
1485 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1486 "fourth column must be type %s", format_type_be(INT4OID));
1488 /* OK, the tupdesc is valid for our purposes */
1492 * Check if spi sql tupdesc and return tupdesc are compatible
1495 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1500 /* check the key_fld types match */
1501 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1502 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1503 if (ret_atttypid != sql_atttypid)
1505 (errcode(ERRCODE_SYNTAX_ERROR),
1506 errmsg("invalid return type"),
1507 errdetail("SQL key field datatype does " \
1508 "not match return key field datatype.")));
1510 /* check the parent_key_fld types match */
1511 ret_atttypid = ret_tupdesc->attrs[1]->atttypid;
1512 sql_atttypid = sql_tupdesc->attrs[1]->atttypid;
1513 if (ret_atttypid != sql_atttypid)
1515 (errcode(ERRCODE_SYNTAX_ERROR),
1516 errmsg("invalid return type"),
1517 errdetail("SQL parent key field datatype does " \
1518 "not match return parent key field datatype.")));
1520 /* OK, the two tupdescs are compatible for our purposes */
1525 * Check if two tupdescs match in type of attributes
1528 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1531 Form_pg_attribute ret_attr;
1533 Form_pg_attribute sql_attr;
1536 if (ret_tupdesc->natts < 2 ||
1537 sql_tupdesc->natts < 3)
1540 /* check the rowid types match */
1541 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1542 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1543 if (ret_atttypid != sql_atttypid)
1545 (errcode(ERRCODE_SYNTAX_ERROR),
1546 errmsg("invalid return type"),
1547 errdetail("SQL rowid datatype does not match " \
1548 "return rowid datatype.")));
1551 * - attribute [1] of the sql tuple is the category; no need to check it -
1552 * attribute [2] of the sql tuple should match attributes [1] to [natts]
1553 * of the return tuple
1555 sql_attr = sql_tupdesc->attrs[2];
1556 for (i = 1; i < ret_tupdesc->natts; i++)
1558 ret_attr = ret_tupdesc->attrs[i];
1560 if (ret_attr->atttypid != sql_attr->atttypid)
1564 /* OK, the two tupdescs are compatible for our purposes */
1569 * Return a properly quoted literal value.
1570 * Uses quote_literal in quote.c
1573 quote_literal_cstr(char *rawstr)
1579 rawstr_text = cstring_to_text(rawstr);
1580 result_text = DatumGetTextP(DirectFunctionCall1(quote_literal,
1581 PointerGetDatum(rawstr_text)));
1582 result = text_to_cstring(result_text);