2 * contrib/tablefunc/tablefunc.c
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-2015, 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 "access/htup_details.h"
38 #include "catalog/pg_type.h"
39 #include "executor/spi.h"
41 #include "lib/stringinfo.h"
42 #include "miscadmin.h"
43 #include "utils/builtins.h"
45 #include "tablefunc.h"
49 static HTAB *load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
50 static Tuplestorestate *get_crosstab_tuplestore(char *sql,
53 MemoryContext per_query_ctx,
55 static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial);
56 static bool compatCrosstabTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
57 static bool compatConnectbyTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
58 static void get_normal_pair(float8 *x1, float8 *x2);
59 static Tuplestorestate *connectby(char *relname,
68 MemoryContext per_query_ctx,
70 AttInMetadata *attinmeta);
71 static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
83 MemoryContext per_query_ctx,
84 AttInMetadata *attinmeta,
85 Tuplestorestate *tupstore);
89 float8 mean; /* mean of the distribution */
90 float8 stddev; /* stddev of the distribution */
91 float8 carry_val; /* hold second generated value */
92 bool use_carry; /* use second generated value */
95 #define xpfree(var_) \
104 #define xpstrdup(tgtvar_, srcvar_) \
107 tgtvar_ = pstrdup(srcvar_); \
112 #define xstreq(tgtvar_, srcvar_) \
113 (((tgtvar_ == NULL) && (srcvar_ == NULL)) || \
114 ((tgtvar_ != NULL) && (srcvar_ != NULL) && (strcmp(tgtvar_, srcvar_) == 0)))
116 /* sign, 10 digits, '\0' */
117 #define INT32_STRLEN 12
119 /* stored info for a crosstab category */
120 typedef struct crosstab_cat_desc
122 char *catname; /* full category name */
123 int attidx; /* zero based */
126 #define MAX_CATNAME_LEN NAMEDATALEN
129 #define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC) \
131 crosstab_HashEnt *hentry; char key[MAX_CATNAME_LEN]; \
133 MemSet(key, 0, MAX_CATNAME_LEN); \
134 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATNAME); \
135 hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
136 key, HASH_FIND, NULL); \
138 CATDESC = hentry->catdesc; \
143 #define crosstab_HashTableInsert(HASHTAB, CATDESC) \
145 crosstab_HashEnt *hentry; bool found; char key[MAX_CATNAME_LEN]; \
147 MemSet(key, 0, MAX_CATNAME_LEN); \
148 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATDESC->catname); \
149 hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
150 key, HASH_ENTER, &found); \
153 (errcode(ERRCODE_DUPLICATE_OBJECT), \
154 errmsg("duplicate category name"))); \
155 hentry->catdesc = CATDESC; \
159 typedef struct crosstab_hashent
161 char internal_catname[MAX_CATNAME_LEN];
162 crosstab_cat_desc *catdesc;
166 * normal_rand - return requested number of random values
167 * with a Gaussian (Normal) distribution.
169 * inputs are int numvals, float8 mean, and float8 stddev
170 * returns setof float8
172 PG_FUNCTION_INFO_V1(normal_rand);
174 normal_rand(PG_FUNCTION_ARGS)
176 FuncCallContext *funcctx;
179 normal_rand_fctx *fctx;
184 MemoryContext oldcontext;
186 /* stuff done only on the first call of the function */
187 if (SRF_IS_FIRSTCALL())
189 /* create a function context for cross-call persistence */
190 funcctx = SRF_FIRSTCALL_INIT();
193 * switch to memory context appropriate for multiple function calls
195 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
197 /* total number of tuples to be returned */
198 funcctx->max_calls = PG_GETARG_UINT32(0);
200 /* allocate memory for user context */
201 fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
204 * Use fctx to keep track of upper and lower bounds from call to call.
205 * It will also be used to carry over the spare value we get from the
206 * Box-Muller algorithm so that we only actually calculate a new value
209 fctx->mean = PG_GETARG_FLOAT8(1);
210 fctx->stddev = PG_GETARG_FLOAT8(2);
212 fctx->use_carry = false;
214 funcctx->user_fctx = fctx;
216 MemoryContextSwitchTo(oldcontext);
219 /* stuff done on every call of the function */
220 funcctx = SRF_PERCALL_SETUP();
222 call_cntr = funcctx->call_cntr;
223 max_calls = funcctx->max_calls;
224 fctx = funcctx->user_fctx;
226 stddev = fctx->stddev;
227 carry_val = fctx->carry_val;
228 use_carry = fctx->use_carry;
230 if (call_cntr < max_calls) /* do when there is more left to send */
237 * reset use_carry and use second value obtained on last pass
239 fctx->use_carry = false;
247 /* Get the next two normal values */
248 get_normal_pair(&normval_1, &normval_2);
251 result = mean + (stddev * normval_1);
253 /* and save the second */
254 fctx->carry_val = mean + (stddev * normval_2);
255 fctx->use_carry = true;
258 /* send the result */
259 SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
262 /* do when there is no more left */
263 SRF_RETURN_DONE(funcctx);
268 * Assigns normally distributed (Gaussian) values to a pair of provided
269 * parameters, with mean 0, standard deviation 1.
271 * This routine implements Algorithm P (Polar method for normal deviates)
272 * from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages
273 * 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E.
274 * Muller, and G. Marsaglia, _Annals_Math,_Stat._ 29 (1958), 610-611.
278 get_normal_pair(float8 *x1, float8 *x2)
288 u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
289 u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
291 v1 = (2.0 * u1) - 1.0;
292 v2 = (2.0 * u2) - 1.0;
294 s = v1 * v1 + v2 * v2;
304 s = sqrt((-2.0 * log(s)) / s);
311 * crosstab - create a crosstab of rowids and values columns from a
312 * SQL statement returning one rowid column, one category column,
313 * and one value column.
315 * e.g. given sql which produces:
318 * ------+-------+-------
329 * <===== values columns =====>
330 * rowid cat1 cat2 cat3 cat4
331 * ------+-------+-------+-------+-------
332 * row1 val1 val2 val3 val4
333 * row2 val5 val6 val7 val8
336 * 1. SQL result must be ordered by 1,2.
337 * 2. The number of values columns depends on the tuple description
338 * of the function's declared return type. The return type's columns
339 * must match the datatypes of the SQL query's result. The datatype
340 * of the category column can be anything, however.
341 * 3. Missing values (i.e. not enough adjacent rows of same rowid to
342 * fill the number of result values columns) are filled in with nulls.
343 * 4. Extra values (i.e. too many adjacent rows of same rowid to fill
344 * the number of result values columns) are skipped.
345 * 5. Rows with all nulls in the values columns are skipped.
347 PG_FUNCTION_INFO_V1(crosstab);
349 crosstab(PG_FUNCTION_ARGS)
351 char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
352 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
353 Tuplestorestate *tupstore;
357 AttInMetadata *attinmeta;
358 SPITupleTable *spi_tuptable;
359 TupleDesc spi_tupdesc;
364 MemoryContext per_query_ctx;
365 MemoryContext oldcontext;
369 /* check to see if caller supports us returning a tuplestore */
370 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
372 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
373 errmsg("set-valued function called in context that cannot accept a set")));
374 if (!(rsinfo->allowedModes & SFRM_Materialize))
376 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
377 errmsg("materialize mode required, but it is not " \
378 "allowed in this context")));
380 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
382 /* Connect to SPI manager */
383 if ((ret = SPI_connect()) < 0)
385 elog(ERROR, "crosstab: SPI_connect returned %d", ret);
387 /* Retrieve the desired rows */
388 ret = SPI_execute(sql, true, 0);
389 proc = SPI_processed;
391 /* If no qualifying tuples, fall out early */
392 if (ret != SPI_OK_SELECT || proc <= 0)
395 rsinfo->isDone = ExprEndResult;
399 spi_tuptable = SPI_tuptable;
400 spi_tupdesc = spi_tuptable->tupdesc;
403 * The provided SQL query must always return three columns.
406 * the label or identifier for each row in the final result
408 * the label or identifier for each column in the final result
410 * the value for each column in the final result
413 if (spi_tupdesc->natts != 3)
415 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
416 errmsg("invalid source data SQL statement"),
417 errdetail("The provided SQL must return 3 "
418 "columns: rowid, category, and values.")));
420 /* get a tuple descriptor for our result type */
421 switch (get_call_result_type(fcinfo, NULL, &tupdesc))
423 case TYPEFUNC_COMPOSITE:
426 case TYPEFUNC_RECORD:
427 /* failed to determine actual type of RECORD */
429 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
430 errmsg("function returning record called in context "
431 "that cannot accept type record")));
434 /* result type isn't composite */
435 elog(ERROR, "return type must be a row type");
440 * Check that return tupdesc is compatible with the data we got from SPI,
441 * at least based on number and type of attributes
443 if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
445 (errcode(ERRCODE_SYNTAX_ERROR),
446 errmsg("return and sql tuple descriptions are " \
450 * switch to long-lived memory context
452 oldcontext = MemoryContextSwitchTo(per_query_ctx);
454 /* make sure we have a persistent copy of the result tupdesc */
455 tupdesc = CreateTupleDescCopy(tupdesc);
457 /* initialize our tuplestore in long-lived context */
459 tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
462 MemoryContextSwitchTo(oldcontext);
465 * Generate attribute metadata needed later to produce tuples from raw C
468 attinmeta = TupleDescGetAttInMetadata(tupdesc);
470 /* total number of tuples to be examined */
473 /* the return tuple always must have 1 rowid + num_categories columns */
474 num_categories = tupdesc->natts - 1;
479 for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
481 bool skip_tuple = false;
484 /* allocate and zero space */
485 values = (char **) palloc0((1 + num_categories) * sizeof(char *));
488 * now loop through the sql results and assign each value in sequence
489 * to the next category
491 for (i = 0; i < num_categories; i++)
496 /* see if we've gone too far already */
497 if (call_cntr >= max_calls)
500 /* get the next sql result tuple */
501 spi_tuple = spi_tuptable->vals[call_cntr];
503 /* get the rowid from the current sql result tuple */
504 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
507 * If this is the first pass through the values for this rowid,
508 * set the first column to rowid
512 xpstrdup(values[0], rowid);
515 * Check to see if the rowid is the same as that of the last
516 * tuple sent -- if so, skip this tuple entirely
518 if (!firstpass && xstreq(lastrowid, rowid))
527 * If rowid hasn't changed on us, continue building the output
530 if (xstreq(rowid, values[0]))
533 * Get the next category item value, which is always attribute
536 * Be careful to assign the value to the array index based on
537 * which category we are presently processing.
539 values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
542 * increment the counter since we consume a row for each
543 * category, but not for last pass because the outer loop will
546 if (i < (num_categories - 1))
553 * We'll fill in NULLs for the missing values, but we need to
554 * decrement the counter since this sql result row doesn't
555 * belong to the current output tuple.
567 /* build the tuple and store it */
568 tuple = BuildTupleFromCStrings(attinmeta, values);
569 tuplestore_puttuple(tupstore, tuple);
570 heap_freetuple(tuple);
573 /* Remember current rowid */
575 xpstrdup(lastrowid, values[0]);
579 for (i = 0; i < num_categories + 1; i++)
580 if (values[i] != NULL)
585 /* let the caller know we're sending back a tuplestore */
586 rsinfo->returnMode = SFRM_Materialize;
587 rsinfo->setResult = tupstore;
588 rsinfo->setDesc = tupdesc;
590 /* release SPI related resources (and return to caller's context) */
597 * crosstab_hash - reimplement crosstab as materialized function and
598 * properly deal with missing values (i.e. don't pack remaining
599 * values to the left)
601 * crosstab - create a crosstab of rowids and values columns from a
602 * SQL statement returning one rowid column, one category column,
603 * and one value column.
605 * e.g. given sql which produces:
608 * ------+-------+-------
618 * <===== values columns =====>
619 * rowid cat1 cat2 cat3 cat4
620 * ------+-------+-------+-------+-------
621 * row1 val1 val2 null val4
622 * row2 val5 val6 val7 val8
625 * 1. SQL result must be ordered by 1.
626 * 2. The number of values columns depends on the tuple description
627 * of the function's declared return type.
628 * 3. Missing values (i.e. missing category) are filled in with nulls.
629 * 4. Extra values (i.e. not in category results) are skipped.
631 PG_FUNCTION_INFO_V1(crosstab_hash);
633 crosstab_hash(PG_FUNCTION_ARGS)
635 char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
636 char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
637 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
639 MemoryContext per_query_ctx;
640 MemoryContext oldcontext;
643 /* check to see if caller supports us returning a tuplestore */
644 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
646 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
647 errmsg("set-valued function called in context that cannot accept a set")));
648 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
649 rsinfo->expectedDesc == NULL)
651 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
652 errmsg("materialize mode required, but it is not " \
653 "allowed in this context")));
655 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
656 oldcontext = MemoryContextSwitchTo(per_query_ctx);
658 /* get the requested return tuple description */
659 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
662 * Check to make sure we have a reasonable tuple descriptor
664 * Note we will attempt to coerce the values into whatever the return
665 * attribute type is and depend on the "in" function to complain if
668 if (tupdesc->natts < 2)
670 (errcode(ERRCODE_SYNTAX_ERROR),
671 errmsg("query-specified return tuple and " \
672 "crosstab function are not compatible")));
674 /* load up the categories hash table */
675 crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
677 /* let the caller know we're sending back a tuplestore */
678 rsinfo->returnMode = SFRM_Materialize;
680 /* now go build it */
681 rsinfo->setResult = get_crosstab_tuplestore(sql,
685 rsinfo->allowedModes & SFRM_Materialize_Random);
688 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
689 * tuples are in our tuplestore and passed back through rsinfo->setResult.
690 * rsinfo->setDesc is set to the tuple description that we actually used
691 * to build our tuples with, so the caller can verify we did what it was
694 rsinfo->setDesc = tupdesc;
695 MemoryContextSwitchTo(oldcontext);
701 * load up the categories hash table
704 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
710 MemoryContext SPIcontext;
712 /* initialize the category hash table */
713 MemSet(&ctl, 0, sizeof(ctl));
714 ctl.keysize = MAX_CATNAME_LEN;
715 ctl.entrysize = sizeof(crosstab_HashEnt);
716 ctl.hcxt = per_query_ctx;
719 * use INIT_CATS, defined above as a guess of how many hash table entries
720 * to create, initially
722 crosstab_hash = hash_create("crosstab hash",
725 HASH_ELEM | HASH_CONTEXT);
727 /* Connect to SPI manager */
728 if ((ret = SPI_connect()) < 0)
730 elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
732 /* Retrieve the category name rows */
733 ret = SPI_execute(cats_sql, true, 0);
734 proc = SPI_processed;
736 /* Check for qualifying tuples */
737 if ((ret == SPI_OK_SELECT) && (proc > 0))
739 SPITupleTable *spi_tuptable = SPI_tuptable;
740 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
744 * The provided categories SQL query must always return one column:
745 * category - the label or identifier for each column
747 if (spi_tupdesc->natts != 1)
749 (errcode(ERRCODE_SYNTAX_ERROR),
750 errmsg("provided \"categories\" SQL must " \
751 "return 1 column of at least one row")));
753 for (i = 0; i < proc; i++)
755 crosstab_cat_desc *catdesc;
759 /* get the next sql result tuple */
760 spi_tuple = spi_tuptable->vals[i];
762 /* get the category from the current sql result tuple */
763 catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
765 SPIcontext = MemoryContextSwitchTo(per_query_ctx);
767 catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
768 catdesc->catname = catname;
771 /* Add the proc description block to the hashtable */
772 crosstab_HashTableInsert(crosstab_hash, catdesc);
774 MemoryContextSwitchTo(SPIcontext);
778 if (SPI_finish() != SPI_OK_FINISH)
780 elog(ERROR, "load_categories_hash: SPI_finish() failed");
782 return crosstab_hash;
786 * create and populate the crosstab tuplestore using the provided source query
788 static Tuplestorestate *
789 get_crosstab_tuplestore(char *sql,
792 MemoryContext per_query_ctx,
795 Tuplestorestate *tupstore;
796 int num_categories = hash_get_num_entries(crosstab_hash);
797 AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
803 /* initialize our tuplestore (while still in query context!) */
804 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
806 /* Connect to SPI manager */
807 if ((ret = SPI_connect()) < 0)
809 elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
811 /* Now retrieve the crosstab source rows */
812 ret = SPI_execute(sql, true, 0);
813 proc = SPI_processed;
815 /* Check for qualifying tuples */
816 if ((ret == SPI_OK_SELECT) && (proc > 0))
818 SPITupleTable *spi_tuptable = SPI_tuptable;
819 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
820 int ncols = spi_tupdesc->natts;
822 char *lastrowid = NULL;
823 bool firstpass = true;
828 if (num_categories == 0)
830 /* no qualifying category tuples */
832 (errcode(ERRCODE_SYNTAX_ERROR),
833 errmsg("provided \"categories\" SQL must " \
834 "return 1 column of at least one row")));
838 * The provided SQL query must always return at least three columns:
840 * 1. rowname the label for each row - column 1 in the final result
841 * 2. category the label for each value-column in the final result 3.
842 * value the values used to populate the value-columns
844 * If there are more than three columns, the last two are taken as
845 * "category" and "values". The first column is taken as "rowname".
846 * Additional columns (2 thru N-2) are assumed the same for the same
847 * "rowname", and are copied into the result tuple from the first time
848 * we encounter a particular rowname.
852 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
853 errmsg("invalid source data SQL statement"),
854 errdetail("The provided SQL must return 3 " \
855 " columns; rowid, category, and values.")));
857 result_ncols = (ncols - 2) + num_categories;
859 /* Recheck to make sure we tuple descriptor still looks reasonable */
860 if (tupdesc->natts != result_ncols)
862 (errcode(ERRCODE_SYNTAX_ERROR),
863 errmsg("invalid return type"),
864 errdetail("Query-specified return " \
865 "tuple has %d columns but crosstab " \
866 "returns %d.", tupdesc->natts, result_ncols)));
869 values = (char **) palloc(result_ncols * sizeof(char *));
871 /* and make sure it's clear */
872 memset(values, '\0', result_ncols * sizeof(char *));
874 for (i = 0; i < proc; i++)
877 crosstab_cat_desc *catdesc;
880 /* get the next sql result tuple */
881 spi_tuple = spi_tuptable->vals[i];
883 /* get the rowid from the current sql result tuple */
884 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
887 * if we're on a new output row, grab the column values up to
890 if (firstpass || !xstreq(lastrowid, rowid))
893 * a new row means we need to flush the old one first, unless
894 * we're on the very first row
898 /* rowid changed, flush the previous output row */
899 tuple = BuildTupleFromCStrings(attinmeta, values);
901 tuplestore_puttuple(tupstore, tuple);
903 for (j = 0; j < result_ncols; j++)
908 for (j = 1; j < ncols - 2; j++)
909 values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
911 /* we're no longer on the first pass */
915 /* look up the category and fill in the appropriate column */
916 catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
920 crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
923 values[catdesc->attidx + ncols - 2] =
924 SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
928 xpstrdup(lastrowid, rowid);
931 /* flush the last output row */
932 tuple = BuildTupleFromCStrings(attinmeta, values);
934 tuplestore_puttuple(tupstore, tuple);
937 if (SPI_finish() != SPI_OK_FINISH)
939 elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
941 tuplestore_donestoring(tupstore);
947 * connectby_text - produce a result set from a hierarchical (parent/child)
950 * e.g. given table foo:
952 * keyid parent_keyid pos
953 * ------+------------+--
965 * connectby(text relname, text keyid_fld, text parent_keyid_fld
966 * [, text orderby_fld], text start_with, int max_depth
967 * [, text branch_delim])
968 * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
970 * keyid parent_id level branch serial
971 * ------+-----------+--------+-----------------------
973 * row5 row2 1 row2~row5 2
974 * row9 row5 2 row2~row5~row9 3
975 * row4 row2 1 row2~row4 4
976 * row6 row4 2 row2~row4~row6 5
977 * row8 row6 3 row2~row4~row6~row8 6
980 PG_FUNCTION_INFO_V1(connectby_text);
982 #define CONNECTBY_NCOLS 4
983 #define CONNECTBY_NCOLS_NOBRANCH 3
986 connectby_text(PG_FUNCTION_ARGS)
988 char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
989 char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
990 char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
991 char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
992 int max_depth = PG_GETARG_INT32(4);
993 char *branch_delim = NULL;
994 bool show_branch = false;
995 bool show_serial = false;
996 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
998 AttInMetadata *attinmeta;
999 MemoryContext per_query_ctx;
1000 MemoryContext oldcontext;
1002 /* check to see if caller supports us returning a tuplestore */
1003 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1005 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1006 errmsg("set-valued function called in context that cannot accept a set")));
1007 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1008 rsinfo->expectedDesc == NULL)
1010 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1011 errmsg("materialize mode required, but it is not " \
1012 "allowed in this context")));
1014 if (fcinfo->nargs == 6)
1016 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1020 /* default is no show, tilde for the delimiter */
1021 branch_delim = pstrdup("~");
1023 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1024 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1026 /* get the requested return tuple description */
1027 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1029 /* does it meet our needs */
1030 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1032 /* OK, use it then */
1033 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1035 /* OK, go to work */
1036 rsinfo->returnMode = SFRM_Materialize;
1037 rsinfo->setResult = connectby(relname,
1047 rsinfo->allowedModes & SFRM_Materialize_Random,
1049 rsinfo->setDesc = tupdesc;
1051 MemoryContextSwitchTo(oldcontext);
1054 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1055 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1056 * rsinfo->setDesc is set to the tuple description that we actually used
1057 * to build our tuples with, so the caller can verify we did what it was
1063 PG_FUNCTION_INFO_V1(connectby_text_serial);
1065 connectby_text_serial(PG_FUNCTION_ARGS)
1067 char *relname = text_to_cstring(PG_GETARG_TEXT_PP(0));
1068 char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1069 char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1070 char *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1071 char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1072 int max_depth = PG_GETARG_INT32(5);
1073 char *branch_delim = NULL;
1074 bool show_branch = false;
1075 bool show_serial = true;
1076 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1078 AttInMetadata *attinmeta;
1079 MemoryContext per_query_ctx;
1080 MemoryContext oldcontext;
1082 /* check to see if caller supports us returning a tuplestore */
1083 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1085 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1086 errmsg("set-valued function called in context that cannot accept a set")));
1087 if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1088 rsinfo->expectedDesc == NULL)
1090 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1091 errmsg("materialize mode required, but it is not " \
1092 "allowed in this context")));
1094 if (fcinfo->nargs == 7)
1096 branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1100 /* default is no show, tilde for the delimiter */
1101 branch_delim = pstrdup("~");
1103 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1104 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1106 /* get the requested return tuple description */
1107 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1109 /* does it meet our needs */
1110 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1112 /* OK, use it then */
1113 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1115 /* OK, go to work */
1116 rsinfo->returnMode = SFRM_Materialize;
1117 rsinfo->setResult = connectby(relname,
1127 rsinfo->allowedModes & SFRM_Materialize_Random,
1129 rsinfo->setDesc = tupdesc;
1131 MemoryContextSwitchTo(oldcontext);
1134 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1135 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1136 * rsinfo->setDesc is set to the tuple description that we actually used
1137 * to build our tuples with, so the caller can verify we did what it was
1145 * connectby - does the real work for connectby_text()
1147 static Tuplestorestate *
1148 connectby(char *relname,
1150 char *parent_key_fld,
1157 MemoryContext per_query_ctx,
1159 AttInMetadata *attinmeta)
1161 Tuplestorestate *tupstore = NULL;
1163 MemoryContext oldcontext;
1167 /* Connect to SPI manager */
1168 if ((ret = SPI_connect()) < 0)
1169 /* internal error */
1170 elog(ERROR, "connectby: SPI_connect returned %d", ret);
1172 /* switch to longer term context to create the tuple store */
1173 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1175 /* initialize our tuplestore */
1176 tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1178 MemoryContextSwitchTo(oldcontext);
1180 /* now go get the whole tree */
1181 tupstore = build_tuplestore_recursively(key_fld,
1187 start_with, /* current_branch */
1188 0, /* initial level is 0 */
1189 &serial, /* initial serial is 1 */
1202 static Tuplestorestate *
1203 build_tuplestore_recursively(char *key_fld,
1204 char *parent_key_fld,
1215 MemoryContext per_query_ctx,
1216 AttInMetadata *attinmeta,
1217 Tuplestorestate *tupstore)
1219 TupleDesc tupdesc = attinmeta->tupdesc;
1226 char *current_key_parent;
1227 char current_level[INT32_STRLEN];
1228 char serial_str[INT32_STRLEN];
1229 char *current_branch;
1232 if (max_depth > 0 && level > max_depth)
1235 initStringInfo(&sql);
1237 /* Build initial sql statement */
1240 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1245 quote_literal_cstr(start_with),
1246 key_fld, key_fld, parent_key_fld);
1251 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1256 quote_literal_cstr(start_with),
1257 key_fld, key_fld, parent_key_fld,
1263 values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1265 values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1267 /* First time through, do a little setup */
1270 /* root value is the one we initially start with */
1271 values[0] = start_with;
1273 /* root value has no parent */
1276 /* root level is 0 */
1277 sprintf(current_level, "%d", level);
1278 values[2] = current_level;
1280 /* root branch is just starting root value */
1282 values[3] = start_with;
1284 /* root starts the serial with 1 */
1287 sprintf(serial_str, "%d", (*serial)++);
1289 values[4] = serial_str;
1291 values[3] = serial_str;
1294 /* construct the tuple */
1295 tuple = BuildTupleFromCStrings(attinmeta, values);
1298 tuplestore_puttuple(tupstore, tuple);
1300 /* increment level */
1304 /* Retrieve the desired rows */
1305 ret = SPI_execute(sql.data, true, 0);
1306 proc = SPI_processed;
1308 /* Check for qualifying tuples */
1309 if ((ret == SPI_OK_SELECT) && (proc > 0))
1311 HeapTuple spi_tuple;
1312 SPITupleTable *tuptable = SPI_tuptable;
1313 TupleDesc spi_tupdesc = tuptable->tupdesc;
1315 StringInfoData branchstr;
1316 StringInfoData chk_branchstr;
1317 StringInfoData chk_current_key;
1319 /* First time through, do a little more setup */
1323 * Check that return tupdesc is compatible with the one we got
1324 * from the query, but only at level 0 -- no need to check more
1328 if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
1330 (errcode(ERRCODE_SYNTAX_ERROR),
1331 errmsg("invalid return type"),
1332 errdetail("Return and SQL tuple descriptions are " \
1336 initStringInfo(&branchstr);
1337 initStringInfo(&chk_branchstr);
1338 initStringInfo(&chk_current_key);
1340 for (i = 0; i < proc; i++)
1342 /* initialize branch for this pass */
1343 appendStringInfoString(&branchstr, branch);
1344 appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1346 /* get the next sql result tuple */
1347 spi_tuple = tuptable->vals[i];
1349 /* get the current key and parent */
1350 current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1351 appendStringInfo(&chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
1352 current_key_parent = pstrdup(SPI_getvalue(spi_tuple, spi_tupdesc, 2));
1354 /* get the current level */
1355 sprintf(current_level, "%d", level);
1357 /* check to see if this key is also an ancestor */
1358 if (strstr(chk_branchstr.data, chk_current_key.data))
1359 elog(ERROR, "infinite recursion detected");
1361 /* OK, extend the branch */
1362 appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1363 current_branch = branchstr.data;
1366 values[0] = pstrdup(current_key);
1367 values[1] = current_key_parent;
1368 values[2] = current_level;
1370 values[3] = current_branch;
1373 sprintf(serial_str, "%d", (*serial)++);
1375 values[4] = serial_str;
1377 values[3] = serial_str;
1380 tuple = BuildTupleFromCStrings(attinmeta, values);
1382 xpfree(current_key);
1383 xpfree(current_key_parent);
1385 /* store the tuple for later use */
1386 tuplestore_puttuple(tupstore, tuple);
1388 heap_freetuple(tuple);
1390 /* recurse using current_key_parent as the new start_with */
1391 tupstore = build_tuplestore_recursively(key_fld,
1407 /* reset branch for next pass */
1408 resetStringInfo(&branchstr);
1409 resetStringInfo(&chk_branchstr);
1410 resetStringInfo(&chk_current_key);
1413 xpfree(branchstr.data);
1414 xpfree(chk_branchstr.data);
1415 xpfree(chk_current_key.data);
1422 * Check expected (query runtime) tupdesc suitable for Connectby
1425 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1427 int serial_column = 0;
1432 /* are there the correct number of columns */
1435 if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1437 (errcode(ERRCODE_SYNTAX_ERROR),
1438 errmsg("invalid return type"),
1439 errdetail("Query-specified return tuple has " \
1440 "wrong number of columns.")));
1444 if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1446 (errcode(ERRCODE_SYNTAX_ERROR),
1447 errmsg("invalid return type"),
1448 errdetail("Query-specified return tuple has " \
1449 "wrong number of columns.")));
1452 /* check that the types of the first two columns match */
1453 if (tupdesc->attrs[0]->atttypid != tupdesc->attrs[1]->atttypid)
1455 (errcode(ERRCODE_SYNTAX_ERROR),
1456 errmsg("invalid return type"),
1457 errdetail("First two columns must be the same type.")));
1459 /* check that the type of the third column is INT4 */
1460 if (tupdesc->attrs[2]->atttypid != INT4OID)
1462 (errcode(ERRCODE_SYNTAX_ERROR),
1463 errmsg("invalid return type"),
1464 errdetail("Third column must be type %s.",
1465 format_type_be(INT4OID))));
1467 /* check that the type of the fourth column is TEXT if applicable */
1468 if (show_branch && tupdesc->attrs[3]->atttypid != TEXTOID)
1470 (errcode(ERRCODE_SYNTAX_ERROR),
1471 errmsg("invalid return type"),
1472 errdetail("Fourth column must be type %s.",
1473 format_type_be(TEXTOID))));
1475 /* check that the type of the fifth column is INT4 */
1476 if (show_branch && show_serial && tupdesc->attrs[4]->atttypid != INT4OID)
1477 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1478 "fifth column must be type %s", format_type_be(INT4OID));
1480 /* check that the type of the fifth column is INT4 */
1481 if (!show_branch && show_serial && tupdesc->attrs[3]->atttypid != INT4OID)
1482 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1483 "fourth column must be type %s", format_type_be(INT4OID));
1485 /* OK, the tupdesc is valid for our purposes */
1489 * Check if spi sql tupdesc and return tupdesc are compatible
1492 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1497 /* check the key_fld types match */
1498 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1499 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1500 if (ret_atttypid != sql_atttypid)
1502 (errcode(ERRCODE_SYNTAX_ERROR),
1503 errmsg("invalid return type"),
1504 errdetail("SQL key field datatype does " \
1505 "not match return key field datatype.")));
1507 /* check the parent_key_fld types match */
1508 ret_atttypid = ret_tupdesc->attrs[1]->atttypid;
1509 sql_atttypid = sql_tupdesc->attrs[1]->atttypid;
1510 if (ret_atttypid != sql_atttypid)
1512 (errcode(ERRCODE_SYNTAX_ERROR),
1513 errmsg("invalid return type"),
1514 errdetail("SQL parent key field datatype does " \
1515 "not match return parent key field datatype.")));
1517 /* OK, the two tupdescs are compatible for our purposes */
1522 * Check if two tupdescs match in type of attributes
1525 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1528 Form_pg_attribute ret_attr;
1530 Form_pg_attribute sql_attr;
1533 if (ret_tupdesc->natts < 2 ||
1534 sql_tupdesc->natts < 3)
1537 /* check the rowid types match */
1538 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1539 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1540 if (ret_atttypid != sql_atttypid)
1542 (errcode(ERRCODE_SYNTAX_ERROR),
1543 errmsg("invalid return type"),
1544 errdetail("SQL rowid datatype does not match " \
1545 "return rowid datatype.")));
1548 * - attribute [1] of the sql tuple is the category; no need to check it -
1549 * attribute [2] of the sql tuple should match attributes [1] to [natts]
1550 * of the return tuple
1552 sql_attr = sql_tupdesc->attrs[2];
1553 for (i = 1; i < ret_tupdesc->natts; i++)
1555 ret_attr = ret_tupdesc->attrs[i];
1557 if (ret_attr->atttypid != sql_attr->atttypid)
1561 /* OK, the two tupdescs are compatible for our purposes */