4 * Sample to demonstrate C functions which return setof scalar
6 * Joe Conway <mail@joeconway.com>
8 * Nabil Sayegh <postgresql@e-trolley.de>
10 * Copyright (c) 2002-2006, PostgreSQL Global Development Group
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose, without fee, and without a written agreement
14 * is hereby granted, provided that the above copyright notice and this
15 * paragraph and the following two paragraphs appear in all copies.
17 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
18 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
19 * LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
20 * DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
23 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
26 * ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
27 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
36 #include "executor/spi.h"
37 #include "lib/stringinfo.h"
38 #include "miscadmin.h"
39 #include "utils/builtins.h"
40 #include "utils/guc.h"
41 #include "utils/lsyscache.h"
43 #include "tablefunc.h"
47 static int load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
48 static Tuplestorestate *get_crosstab_tuplestore(char *sql,
51 MemoryContext per_query_ctx);
52 static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial);
53 static bool compatCrosstabTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
54 static bool compatConnectbyTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2);
55 static void get_normal_pair(float8 *x1, float8 *x2);
56 static Tuplestorestate *connectby(char *relname,
65 MemoryContext per_query_ctx,
66 AttInMetadata *attinmeta);
67 static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
79 MemoryContext per_query_ctx,
80 AttInMetadata *attinmeta,
81 Tuplestorestate *tupstore);
82 static char *quote_literal_cstr(char *rawstr);
86 float8 mean; /* mean of the distribution */
87 float8 stddev; /* stddev of the distribution */
88 float8 carry_val; /* hold second generated value */
89 bool use_carry; /* use second generated value */
94 SPITupleTable *spi_tuptable; /* sql results from user query */
95 char *lastrowid; /* rowid of the last tuple sent */
98 #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
99 #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
100 #define xpfree(var_) \
109 /* sign, 10 digits, '\0' */
110 #define INT32_STRLEN 12
112 /* hash table support */
113 static HTAB *crosstab_HashTable;
115 /* The information we cache about loaded procedures */
116 typedef struct crosstab_cat_desc
119 int attidx; /* zero based */
122 #define MAX_CATNAME_LEN NAMEDATALEN
125 #define crosstab_HashTableLookup(CATNAME, CATDESC) \
127 crosstab_HashEnt *hentry; char key[MAX_CATNAME_LEN]; \
129 MemSet(key, 0, MAX_CATNAME_LEN); \
130 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATNAME); \
131 hentry = (crosstab_HashEnt*) hash_search(crosstab_HashTable, \
132 key, HASH_FIND, NULL); \
134 CATDESC = hentry->catdesc; \
139 #define crosstab_HashTableInsert(CATDESC) \
141 crosstab_HashEnt *hentry; bool found; char key[MAX_CATNAME_LEN]; \
143 MemSet(key, 0, MAX_CATNAME_LEN); \
144 snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATDESC->catname); \
145 hentry = (crosstab_HashEnt*) hash_search(crosstab_HashTable, \
146 key, HASH_ENTER, &found); \
149 (errcode(ERRCODE_DUPLICATE_OBJECT), \
150 errmsg("duplicate category name"))); \
151 hentry->catdesc = CATDESC; \
155 typedef struct crosstab_hashent
157 char internal_catname[MAX_CATNAME_LEN];
158 crosstab_cat_desc *catdesc;
162 * normal_rand - return requested number of random values
163 * with a Gaussian (Normal) distribution.
165 * inputs are int numvals, float8 mean, and float8 stddev
166 * returns setof float8
168 PG_FUNCTION_INFO_V1(normal_rand);
170 normal_rand(PG_FUNCTION_ARGS)
172 FuncCallContext *funcctx;
175 normal_rand_fctx *fctx;
180 MemoryContext oldcontext;
182 /* stuff done only on the first call of the function */
183 if (SRF_IS_FIRSTCALL())
185 /* create a function context for cross-call persistence */
186 funcctx = SRF_FIRSTCALL_INIT();
189 * switch to memory context appropriate for multiple function calls
191 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
193 /* total number of tuples to be returned */
194 funcctx->max_calls = PG_GETARG_UINT32(0);
196 /* allocate memory for user context */
197 fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
200 * Use fctx to keep track of upper and lower bounds from call to call.
201 * It will also be used to carry over the spare value we get from the
202 * Box-Muller algorithm so that we only actually calculate a new value
205 fctx->mean = PG_GETARG_FLOAT8(1);
206 fctx->stddev = PG_GETARG_FLOAT8(2);
208 fctx->use_carry = false;
210 funcctx->user_fctx = fctx;
212 MemoryContextSwitchTo(oldcontext);
215 /* stuff done on every call of the function */
216 funcctx = SRF_PERCALL_SETUP();
218 call_cntr = funcctx->call_cntr;
219 max_calls = funcctx->max_calls;
220 fctx = funcctx->user_fctx;
222 stddev = fctx->stddev;
223 carry_val = fctx->carry_val;
224 use_carry = fctx->use_carry;
226 if (call_cntr < max_calls) /* do when there is more left to send */
233 * reset use_carry and use second value obtained on last pass
235 fctx->use_carry = false;
243 /* Get the next two normal values */
244 get_normal_pair(&normval_1, &normval_2);
247 result = mean + (stddev * normval_1);
249 /* and save the second */
250 fctx->carry_val = mean + (stddev * normval_2);
251 fctx->use_carry = true;
254 /* send the result */
255 SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
258 /* do when there is no more left */
259 SRF_RETURN_DONE(funcctx);
264 * Assigns normally distributed (Gaussian) values to a pair of provided
265 * parameters, with mean 0, standard deviation 1.
267 * This routine implements Algorithm P (Polar method for normal deviates)
268 * from Knuth's _The_Art_of_Computer_Programming_, Volume 2, 3rd ed., pages
269 * 122-126. Knuth cites his source as "The polar method", G. E. P. Box, M. E.
270 * Muller, and G. Marsaglia, _Annals_Math,_Stat._ 29 (1958), 610-611.
274 get_normal_pair(float8 *x1, float8 *x2)
284 u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
285 u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
287 v1 = (2.0 * u1) - 1.0;
288 v2 = (2.0 * u2) - 1.0;
290 s = v1 * v1 + v2 * v2;
300 s = sqrt((-2.0 * log(s)) / s);
307 * crosstab - create a crosstab of rowids and values columns from a
308 * SQL statement returning one rowid column, one category column,
309 * and one value column.
311 * e.g. given sql which produces:
314 * ------+-------+-------
325 * <===== values columns =====>
326 * rowid cat1 cat2 cat3 cat4
327 * ------+-------+-------+-------+-------
328 * row1 val1 val2 val3 val4
329 * row2 val5 val6 val7 val8
332 * 1. SQL result must be ordered by 1,2.
333 * 2. The number of values columns depends on the tuple description
334 * of the function's declared return type. The return type's columns
335 * must match the datatypes of the SQL query's result. The datatype
336 * of the category column can be anything, however.
337 * 3. Missing values (i.e. not enough adjacent rows of same rowid to
338 * fill the number of result values columns) are filled in with nulls.
339 * 4. Extra values (i.e. too many adjacent rows of same rowid to fill
340 * the number of result values columns) are skipped.
341 * 5. Rows with all nulls in the values columns are skipped.
343 PG_FUNCTION_INFO_V1(crosstab);
345 crosstab(PG_FUNCTION_ARGS)
347 FuncCallContext *funcctx;
348 TupleDesc ret_tupdesc;
351 AttInMetadata *attinmeta;
352 SPITupleTable *spi_tuptable = NULL;
353 TupleDesc spi_tupdesc;
354 char *lastrowid = NULL;
358 MemoryContext oldcontext;
360 /* stuff done only on the first call of the function */
361 if (SRF_IS_FIRSTCALL())
363 char *sql = GET_STR(PG_GETARG_TEXT_P(0));
368 /* create a function context for cross-call persistence */
369 funcctx = SRF_FIRSTCALL_INIT();
372 * switch to memory context appropriate for multiple function calls
374 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
376 /* Connect to SPI manager */
377 if ((ret = SPI_connect()) < 0)
379 elog(ERROR, "crosstab: SPI_connect returned %d", ret);
381 /* Retrieve the desired rows */
382 ret = SPI_execute(sql, true, 0);
383 proc = SPI_processed;
385 /* Check for qualifying tuples */
386 if ((ret == SPI_OK_SELECT) && (proc > 0))
388 spi_tuptable = SPI_tuptable;
389 spi_tupdesc = spi_tuptable->tupdesc;
392 * The provided SQL query must always return three columns.
395 * the label or identifier for each row in the final result
397 * the label or identifier for each column in the final result
399 * the value for each column in the final result
402 if (spi_tupdesc->natts != 3)
404 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
405 errmsg("invalid source data SQL statement"),
406 errdetail("The provided SQL must return 3 "
407 "columns: rowid, category, and values.")));
411 /* no qualifying tuples */
413 SRF_RETURN_DONE(funcctx);
416 /* SPI switches context on us, so reset it */
417 MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
419 /* get a tuple descriptor for our result type */
420 switch (get_call_result_type(fcinfo, NULL, &tupdesc))
422 case TYPEFUNC_COMPOSITE:
425 case TYPEFUNC_RECORD:
426 /* failed to determine actual type of RECORD */
428 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
429 errmsg("function returning record called in context "
430 "that cannot accept type record")));
433 /* result type isn't composite */
434 elog(ERROR, "return type must be a row type");
438 /* make sure we have a persistent copy of the tupdesc */
439 tupdesc = CreateTupleDescCopy(tupdesc);
442 * Check that return tupdesc is compatible with the data we got from
443 * SPI, at least based on number and type of attributes
445 if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
447 (errcode(ERRCODE_SYNTAX_ERROR),
448 errmsg("return and sql tuple descriptions are " \
452 * Generate attribute metadata needed later to produce tuples from raw
455 attinmeta = TupleDescGetAttInMetadata(tupdesc);
456 funcctx->attinmeta = attinmeta;
458 /* allocate memory for user context */
459 fctx = (crosstab_fctx *) palloc(sizeof(crosstab_fctx));
462 * Save spi data for use across calls
464 fctx->spi_tuptable = spi_tuptable;
465 fctx->lastrowid = NULL;
466 funcctx->user_fctx = fctx;
468 /* total number of tuples to be returned */
469 funcctx->max_calls = proc;
471 MemoryContextSwitchTo(oldcontext);
474 /* stuff done on every call of the function */
475 funcctx = SRF_PERCALL_SETUP();
478 * initialize per-call variables
480 call_cntr = funcctx->call_cntr;
481 max_calls = funcctx->max_calls;
483 /* user context info */
484 fctx = (crosstab_fctx *) funcctx->user_fctx;
485 lastrowid = fctx->lastrowid;
486 spi_tuptable = fctx->spi_tuptable;
489 spi_tupdesc = spi_tuptable->tupdesc;
491 /* attribute return type and return tuple description */
492 attinmeta = funcctx->attinmeta;
493 ret_tupdesc = attinmeta->tupdesc;
495 /* the return tuple always must have 1 rowid + num_categories columns */
496 num_categories = ret_tupdesc->natts - 1;
498 if (call_cntr < max_calls) /* do when there is more left to send */
503 bool allnulls = true;
508 values = (char **) palloc((1 + num_categories) * sizeof(char *));
510 /* and make sure it's clear */
511 memset(values, '\0', (1 + num_categories) * sizeof(char *));
514 * now loop through the sql results and assign each value in
515 * sequence to the next category
517 for (i = 0; i < num_categories; i++)
522 /* see if we've gone too far already */
523 if (call_cntr >= max_calls)
526 /* get the next sql result tuple */
527 spi_tuple = spi_tuptable->vals[call_cntr];
529 /* get the rowid from the current sql result tuple */
530 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
533 * If this is the first pass through the values for this rowid
534 * set it, otherwise make sure it hasn't changed on us. Also
535 * check to see if the rowid is the same as that of the last
536 * tuple sent -- if so, skip this tuple entirely
539 values[0] = pstrdup(rowid);
541 if ((rowid != NULL) && (strcmp(rowid, values[0]) == 0))
543 if ((lastrowid != NULL) && (strcmp(rowid, lastrowid) == 0))
545 else if (allnulls == true)
549 * Get the next category item value, which is alway
550 * attribute number three.
552 * Be careful to sssign the value to the array index based
553 * on which category we are presently processing.
555 values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
558 * increment the counter since we consume a row for each
559 * category, but not for last pass because the API will do
562 if (i < (num_categories - 1))
563 call_cntr = ++funcctx->call_cntr;
568 * We'll fill in NULLs for the missing values, but we need
569 * to decrement the counter since this sql result row
570 * doesn't belong to the current output tuple.
572 call_cntr = --funcctx->call_cntr;
580 xpfree(fctx->lastrowid);
582 if (values[0] != NULL)
585 * switch to memory context appropriate for multiple function
588 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
590 lastrowid = fctx->lastrowid = pstrdup(values[0]);
591 MemoryContextSwitchTo(oldcontext);
596 /* build the tuple */
597 tuple = BuildTupleFromCStrings(attinmeta, values);
599 /* make the tuple into a datum */
600 result = HeapTupleGetDatum(tuple);
603 for (i = 0; i < num_categories + 1; i++)
604 if (values[i] != NULL)
608 SRF_RETURN_NEXT(funcctx, result);
613 * Skipping this tuple entirely, but we need to advance the
614 * counter like the API would if we had returned one.
616 call_cntr = ++funcctx->call_cntr;
618 /* we'll start over at the top */
621 /* see if we've gone too far already */
622 if (call_cntr >= max_calls)
624 /* release SPI related resources */
626 SRF_RETURN_DONE(funcctx);
632 /* do when there is no more left */
634 /* release SPI related resources */
636 SRF_RETURN_DONE(funcctx);
641 * crosstab_hash - reimplement crosstab as materialized function and
642 * properly deal with missing values (i.e. don't pack remaining
643 * values to the left)
645 * crosstab - create a crosstab of rowids and values columns from a
646 * SQL statement returning one rowid column, one category column,
647 * and one value column.
649 * e.g. given sql which produces:
652 * ------+-------+-------
662 * <===== values columns =====>
663 * rowid cat1 cat2 cat3 cat4
664 * ------+-------+-------+-------+-------
665 * row1 val1 val2 null val4
666 * row2 val5 val6 val7 val8
669 * 1. SQL result must be ordered by 1.
670 * 2. The number of values columns depends on the tuple description
671 * of the function's declared return type.
672 * 3. Missing values (i.e. missing category) are filled in with nulls.
673 * 4. Extra values (i.e. not in category results) are skipped.
675 PG_FUNCTION_INFO_V1(crosstab_hash);
677 crosstab_hash(PG_FUNCTION_ARGS)
679 char *sql = GET_STR(PG_GETARG_TEXT_P(0));
680 char *cats_sql = GET_STR(PG_GETARG_TEXT_P(1));
681 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
683 MemoryContext per_query_ctx;
684 MemoryContext oldcontext;
687 /* check to see if caller supports us returning a tuplestore */
688 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
690 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
691 errmsg("set-valued function called in context that cannot accept a set")));
692 if (!(rsinfo->allowedModes & SFRM_Materialize))
694 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
695 errmsg("materialize mode required, but it is not " \
696 "allowed in this context")));
698 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
699 oldcontext = MemoryContextSwitchTo(per_query_ctx);
701 /* get the requested return tuple description */
702 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
705 * Check to make sure we have a reasonable tuple descriptor
707 * Note we will attempt to coerce the values into whatever the return
708 * attribute type is and depend on the "in" function to complain if
711 if (tupdesc->natts < 2)
713 (errcode(ERRCODE_SYNTAX_ERROR),
714 errmsg("query-specified return tuple and " \
715 "crosstab function are not compatible")));
717 /* load up the categories hash table */
718 num_categories = load_categories_hash(cats_sql, per_query_ctx);
720 /* let the caller know we're sending back a tuplestore */
721 rsinfo->returnMode = SFRM_Materialize;
723 /* now go build it */
724 rsinfo->setResult = get_crosstab_tuplestore(sql,
730 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
731 * tuples are in our tuplestore and passed back through rsinfo->setResult.
732 * rsinfo->setDesc is set to the tuple description that we actually used
733 * to build our tuples with, so the caller can verify we did what it was
736 rsinfo->setDesc = tupdesc;
737 MemoryContextSwitchTo(oldcontext);
743 * load up the categories hash table
746 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
751 MemoryContext SPIcontext;
752 int num_categories = 0;
754 /* initialize the category hash table */
755 ctl.keysize = MAX_CATNAME_LEN;
756 ctl.entrysize = sizeof(crosstab_HashEnt);
759 * use INIT_CATS, defined above as a guess of how many hash table entries
760 * to create, initially
762 crosstab_HashTable = hash_create("crosstab hash", INIT_CATS, &ctl, HASH_ELEM);
764 /* Connect to SPI manager */
765 if ((ret = SPI_connect()) < 0)
767 elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
769 /* Retrieve the category name rows */
770 ret = SPI_execute(cats_sql, true, 0);
771 num_categories = proc = SPI_processed;
773 /* Check for qualifying tuples */
774 if ((ret == SPI_OK_SELECT) && (proc > 0))
776 SPITupleTable *spi_tuptable = SPI_tuptable;
777 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
781 * The provided categories SQL query must always return one column:
782 * category - the label or identifier for each column
784 if (spi_tupdesc->natts != 1)
786 (errcode(ERRCODE_SYNTAX_ERROR),
787 errmsg("provided \"categories\" SQL must " \
788 "return 1 column of at least one row")));
790 for (i = 0; i < proc; i++)
792 crosstab_cat_desc *catdesc;
796 /* get the next sql result tuple */
797 spi_tuple = spi_tuptable->vals[i];
799 /* get the category from the current sql result tuple */
800 catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
802 SPIcontext = MemoryContextSwitchTo(per_query_ctx);
804 catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
805 catdesc->catname = catname;
808 /* Add the proc description block to the hashtable */
809 crosstab_HashTableInsert(catdesc);
811 MemoryContextSwitchTo(SPIcontext);
815 if (SPI_finish() != SPI_OK_FINISH)
817 elog(ERROR, "load_categories_hash: SPI_finish() failed");
819 return num_categories;
823 * create and populate the crosstab tuplestore using the provided source query
825 static Tuplestorestate *
826 get_crosstab_tuplestore(char *sql,
829 MemoryContext per_query_ctx)
831 Tuplestorestate *tupstore;
832 AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
837 MemoryContext SPIcontext;
839 /* initialize our tuplestore */
840 tupstore = tuplestore_begin_heap(true, false, work_mem);
842 /* Connect to SPI manager */
843 if ((ret = SPI_connect()) < 0)
845 elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
847 /* Now retrieve the crosstab source rows */
848 ret = SPI_execute(sql, true, 0);
849 proc = SPI_processed;
851 /* Check for qualifying tuples */
852 if ((ret == SPI_OK_SELECT) && (proc > 0))
854 SPITupleTable *spi_tuptable = SPI_tuptable;
855 TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
856 int ncols = spi_tupdesc->natts;
858 char *lastrowid = NULL;
863 if (num_categories == 0)
865 /* no qualifying category tuples */
867 (errcode(ERRCODE_SYNTAX_ERROR),
868 errmsg("provided \"categories\" SQL must " \
869 "return 1 column of at least one row")));
873 * The provided SQL query must always return at least three columns:
875 * 1. rowname the label for each row - column 1 in the final result
876 * 2. category the label for each value-column in the final result 3.
877 * value the values used to populate the value-columns
879 * If there are more than three columns, the last two are taken as
880 * "category" and "values". The first column is taken as "rowname".
881 * Additional columns (2 thru N-2) are assumed the same for the same
882 * "rowname", and are copied into the result tuple from the first time
883 * we encounter a particular rowname.
887 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
888 errmsg("invalid source data SQL statement"),
889 errdetail("The provided SQL must return 3 " \
890 " columns; rowid, category, and values.")));
892 result_ncols = (ncols - 2) + num_categories;
894 /* Recheck to make sure we tuple descriptor still looks reasonable */
895 if (tupdesc->natts != result_ncols)
897 (errcode(ERRCODE_SYNTAX_ERROR),
898 errmsg("invalid return type"),
899 errdetail("Query-specified return " \
900 "tuple has %d columns but crosstab " \
901 "returns %d.", tupdesc->natts, result_ncols)));
904 values = (char **) palloc(result_ncols * sizeof(char *));
906 /* and make sure it's clear */
907 memset(values, '\0', result_ncols * sizeof(char *));
909 for (i = 0; i < proc; i++)
912 crosstab_cat_desc *catdesc;
915 /* get the next sql result tuple */
916 spi_tuple = spi_tuptable->vals[i];
918 /* get the rowid from the current sql result tuple */
919 rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
921 /* if rowid is null, skip this tuple entirely */
926 * if we're on a new output row, grab the column values up to
929 if ((lastrowid == NULL) || (strcmp(rowid, lastrowid) != 0))
932 * a new row means we need to flush the old one first, unless
933 * we're on the very first row
935 if (lastrowid != NULL)
937 /* rowid changed, flush the previous output row */
938 tuple = BuildTupleFromCStrings(attinmeta, values);
940 /* switch to appropriate context while storing the tuple */
941 SPIcontext = MemoryContextSwitchTo(per_query_ctx);
942 tuplestore_puttuple(tupstore, tuple);
943 MemoryContextSwitchTo(SPIcontext);
945 for (j = 0; j < result_ncols; j++)
950 for (j = 1; j < ncols - 2; j++)
951 values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
954 /* look up the category and fill in the appropriate column */
955 catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
959 crosstab_HashTableLookup(catname, catdesc);
962 values[catdesc->attidx + ncols - 2] =
963 SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
967 lastrowid = pstrdup(rowid);
970 /* flush the last output row */
971 tuple = BuildTupleFromCStrings(attinmeta, values);
973 /* switch to appropriate context while storing the tuple */
974 SPIcontext = MemoryContextSwitchTo(per_query_ctx);
975 tuplestore_puttuple(tupstore, tuple);
976 MemoryContextSwitchTo(SPIcontext);
979 if (SPI_finish() != SPI_OK_FINISH)
981 elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
983 tuplestore_donestoring(tupstore);
989 * connectby_text - produce a result set from a hierarchical (parent/child)
992 * e.g. given table foo:
994 * keyid parent_keyid pos
995 * ------+------------+--
1007 * connectby(text relname, text keyid_fld, text parent_keyid_fld
1008 * [, text orderby_fld], text start_with, int max_depth
1009 * [, text branch_delim])
1010 * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
1012 * keyid parent_id level branch serial
1013 * ------+-----------+--------+-----------------------
1014 * row2 NULL 0 row2 1
1015 * row5 row2 1 row2~row5 2
1016 * row9 row5 2 row2~row5~row9 3
1017 * row4 row2 1 row2~row4 4
1018 * row6 row4 2 row2~row4~row6 5
1019 * row8 row6 3 row2~row4~row6~row8 6
1022 PG_FUNCTION_INFO_V1(connectby_text);
1024 #define CONNECTBY_NCOLS 4
1025 #define CONNECTBY_NCOLS_NOBRANCH 3
1028 connectby_text(PG_FUNCTION_ARGS)
1030 char *relname = GET_STR(PG_GETARG_TEXT_P(0));
1031 char *key_fld = GET_STR(PG_GETARG_TEXT_P(1));
1032 char *parent_key_fld = GET_STR(PG_GETARG_TEXT_P(2));
1033 char *start_with = GET_STR(PG_GETARG_TEXT_P(3));
1034 int max_depth = PG_GETARG_INT32(4);
1035 char *branch_delim = NULL;
1036 bool show_branch = false;
1037 bool show_serial = false;
1038 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1040 AttInMetadata *attinmeta;
1041 MemoryContext per_query_ctx;
1042 MemoryContext oldcontext;
1044 /* check to see if caller supports us returning a tuplestore */
1045 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1047 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1048 errmsg("set-valued function called in context that cannot accept a set")));
1049 if (!(rsinfo->allowedModes & SFRM_Materialize))
1051 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1052 errmsg("materialize mode required, but it is not " \
1053 "allowed in this context")));
1055 if (fcinfo->nargs == 6)
1057 branch_delim = GET_STR(PG_GETARG_TEXT_P(5));
1061 /* default is no show, tilde for the delimiter */
1062 branch_delim = pstrdup("~");
1064 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1065 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1067 /* get the requested return tuple description */
1068 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1070 /* does it meet our needs */
1071 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1073 /* OK, use it then */
1074 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1076 /* OK, go to work */
1077 rsinfo->returnMode = SFRM_Materialize;
1078 rsinfo->setResult = connectby(relname,
1089 rsinfo->setDesc = tupdesc;
1091 MemoryContextSwitchTo(oldcontext);
1094 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1095 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1096 * rsinfo->setDesc is set to the tuple description that we actually used
1097 * to build our tuples with, so the caller can verify we did what it was
1103 PG_FUNCTION_INFO_V1(connectby_text_serial);
1105 connectby_text_serial(PG_FUNCTION_ARGS)
1107 char *relname = GET_STR(PG_GETARG_TEXT_P(0));
1108 char *key_fld = GET_STR(PG_GETARG_TEXT_P(1));
1109 char *parent_key_fld = GET_STR(PG_GETARG_TEXT_P(2));
1110 char *orderby_fld = GET_STR(PG_GETARG_TEXT_P(3));
1111 char *start_with = GET_STR(PG_GETARG_TEXT_P(4));
1112 int max_depth = PG_GETARG_INT32(5);
1113 char *branch_delim = NULL;
1114 bool show_branch = false;
1115 bool show_serial = true;
1117 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1119 AttInMetadata *attinmeta;
1120 MemoryContext per_query_ctx;
1121 MemoryContext oldcontext;
1123 /* check to see if caller supports us returning a tuplestore */
1124 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1126 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1127 errmsg("set-valued function called in context that cannot accept a set")));
1128 if (!(rsinfo->allowedModes & SFRM_Materialize))
1130 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1131 errmsg("materialize mode required, but it is not " \
1132 "allowed in this context")));
1134 if (fcinfo->nargs == 7)
1136 branch_delim = GET_STR(PG_GETARG_TEXT_P(6));
1140 /* default is no show, tilde for the delimiter */
1141 branch_delim = pstrdup("~");
1143 per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1144 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1146 /* get the requested return tuple description */
1147 tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1149 /* does it meet our needs */
1150 validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1152 /* OK, use it then */
1153 attinmeta = TupleDescGetAttInMetadata(tupdesc);
1155 /* OK, go to work */
1156 rsinfo->returnMode = SFRM_Materialize;
1157 rsinfo->setResult = connectby(relname,
1168 rsinfo->setDesc = tupdesc;
1170 MemoryContextSwitchTo(oldcontext);
1173 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1174 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1175 * rsinfo->setDesc is set to the tuple description that we actually used
1176 * to build our tuples with, so the caller can verify we did what it was
1184 * connectby - does the real work for connectby_text()
1186 static Tuplestorestate *
1187 connectby(char *relname,
1189 char *parent_key_fld,
1196 MemoryContext per_query_ctx,
1197 AttInMetadata *attinmeta)
1199 Tuplestorestate *tupstore = NULL;
1201 MemoryContext oldcontext;
1205 /* Connect to SPI manager */
1206 if ((ret = SPI_connect()) < 0)
1207 /* internal error */
1208 elog(ERROR, "connectby: SPI_connect returned %d", ret);
1210 /* switch to longer term context to create the tuple store */
1211 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1213 /* initialize our tuplestore */
1214 tupstore = tuplestore_begin_heap(true, false, work_mem);
1216 MemoryContextSwitchTo(oldcontext);
1218 /* now go get the whole tree */
1219 tupstore = build_tuplestore_recursively(key_fld,
1225 start_with, /* current_branch */
1226 0, /* initial level is 0 */
1227 &serial, /* initial serial is 1 */
1240 static Tuplestorestate *
1241 build_tuplestore_recursively(char *key_fld,
1242 char *parent_key_fld,
1253 MemoryContext per_query_ctx,
1254 AttInMetadata *attinmeta,
1255 Tuplestorestate *tupstore)
1257 TupleDesc tupdesc = attinmeta->tupdesc;
1258 MemoryContext oldcontext;
1265 char *current_key_parent;
1266 char current_level[INT32_STRLEN];
1267 char serial_str[INT32_STRLEN];
1268 char *current_branch;
1271 if (max_depth > 0 && level > max_depth)
1274 initStringInfo(&sql);
1276 /* Build initial sql statement */
1279 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1284 quote_literal_cstr(start_with),
1285 key_fld, key_fld, parent_key_fld);
1290 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1295 quote_literal_cstr(start_with),
1296 key_fld, key_fld, parent_key_fld,
1302 values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1304 values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1306 /* First time through, do a little setup */
1309 /* root value is the one we initially start with */
1310 values[0] = start_with;
1312 /* root value has no parent */
1315 /* root level is 0 */
1316 sprintf(current_level, "%d", level);
1317 values[2] = current_level;
1319 /* root branch is just starting root value */
1321 values[3] = start_with;
1323 /* root starts the serial with 1 */
1326 sprintf(serial_str, "%d", (*serial)++);
1328 values[4] = serial_str;
1330 values[3] = serial_str;
1333 /* construct the tuple */
1334 tuple = BuildTupleFromCStrings(attinmeta, values);
1336 /* switch to long lived context while storing the tuple */
1337 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1340 tuplestore_puttuple(tupstore, tuple);
1342 /* now reset the context */
1343 MemoryContextSwitchTo(oldcontext);
1345 /* increment level */
1349 /* Retrieve the desired rows */
1350 ret = SPI_execute(sql.data, true, 0);
1351 proc = SPI_processed;
1353 /* Check for qualifying tuples */
1354 if ((ret == SPI_OK_SELECT) && (proc > 0))
1356 HeapTuple spi_tuple;
1357 SPITupleTable *tuptable = SPI_tuptable;
1358 TupleDesc spi_tupdesc = tuptable->tupdesc;
1360 StringInfoData branchstr;
1361 StringInfoData chk_branchstr;
1362 StringInfoData chk_current_key;
1364 /* First time through, do a little more setup */
1368 * Check that return tupdesc is compatible with the one we got
1369 * from the query, but only at level 0 -- no need to check more
1373 if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
1375 (errcode(ERRCODE_SYNTAX_ERROR),
1376 errmsg("invalid return type"),
1377 errdetail("Return and SQL tuple descriptions are " \
1381 for (i = 0; i < proc; i++)
1383 /* start a new branch */
1384 initStringInfo(&branchstr);
1386 /* need these to check for recursion */
1387 initStringInfo(&chk_branchstr);
1388 initStringInfo(&chk_current_key);
1390 /* initialize branch for this pass */
1391 appendStringInfo(&branchstr, "%s", branch);
1392 appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1394 /* get the next sql result tuple */
1395 spi_tuple = tuptable->vals[i];
1397 /* get the current key and parent */
1398 current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1399 appendStringInfo(&chk_current_key, "%s%s%s", branch_delim, current_key, branch_delim);
1400 current_key_parent = pstrdup(SPI_getvalue(spi_tuple, spi_tupdesc, 2));
1402 /* get the current level */
1403 sprintf(current_level, "%d", level);
1405 /* check to see if this key is also an ancestor */
1406 if (strstr(chk_branchstr.data, chk_current_key.data))
1407 elog(ERROR, "infinite recursion detected");
1409 /* OK, extend the branch */
1410 appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1411 current_branch = branchstr.data;
1414 values[0] = pstrdup(current_key);
1415 values[1] = current_key_parent;
1416 values[2] = current_level;
1418 values[3] = current_branch;
1421 sprintf(serial_str, "%d", (*serial)++);
1423 values[4] = serial_str;
1425 values[3] = serial_str;
1428 tuple = BuildTupleFromCStrings(attinmeta, values);
1430 xpfree(current_key);
1431 xpfree(current_key_parent);
1433 /* switch to long lived context while storing the tuple */
1434 oldcontext = MemoryContextSwitchTo(per_query_ctx);
1436 /* store the tuple for later use */
1437 tuplestore_puttuple(tupstore, tuple);
1439 /* now reset the context */
1440 MemoryContextSwitchTo(oldcontext);
1442 heap_freetuple(tuple);
1444 /* recurse using current_key_parent as the new start_with */
1445 tupstore = build_tuplestore_recursively(key_fld,
1461 /* reset branch for next pass */
1462 xpfree(branchstr.data);
1463 xpfree(chk_branchstr.data);
1464 xpfree(chk_current_key.data);
1472 * Check expected (query runtime) tupdesc suitable for Connectby
1475 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1477 int serial_column = 0;
1482 /* are there the correct number of columns */
1485 if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1487 (errcode(ERRCODE_SYNTAX_ERROR),
1488 errmsg("invalid return type"),
1489 errdetail("Query-specified return tuple has " \
1490 "wrong number of columns.")));
1494 if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1496 (errcode(ERRCODE_SYNTAX_ERROR),
1497 errmsg("invalid return type"),
1498 errdetail("Query-specified return tuple has " \
1499 "wrong number of columns.")));
1502 /* check that the types of the first two columns match */
1503 if (tupdesc->attrs[0]->atttypid != tupdesc->attrs[1]->atttypid)
1505 (errcode(ERRCODE_SYNTAX_ERROR),
1506 errmsg("invalid return type"),
1507 errdetail("First two columns must be the same type.")));
1509 /* check that the type of the third column is INT4 */
1510 if (tupdesc->attrs[2]->atttypid != INT4OID)
1512 (errcode(ERRCODE_SYNTAX_ERROR),
1513 errmsg("invalid return type"),
1514 errdetail("Third column must be type %s.",
1515 format_type_be(INT4OID))));
1517 /* check that the type of the fourth column is TEXT if applicable */
1518 if (show_branch && tupdesc->attrs[3]->atttypid != TEXTOID)
1520 (errcode(ERRCODE_SYNTAX_ERROR),
1521 errmsg("invalid return type"),
1522 errdetail("Fourth column must be type %s.",
1523 format_type_be(TEXTOID))));
1525 /* check that the type of the fifth column is INT4 */
1526 if (show_branch && show_serial && tupdesc->attrs[4]->atttypid != INT4OID)
1527 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1528 "fifth column must be type %s", format_type_be(INT4OID));
1530 /* check that the type of the fifth column is INT4 */
1531 if (!show_branch && show_serial && tupdesc->attrs[3]->atttypid != INT4OID)
1532 elog(ERROR, "query-specified return tuple not valid for Connectby: "
1533 "fourth column must be type %s", format_type_be(INT4OID));
1535 /* OK, the tupdesc is valid for our purposes */
1539 * Check if spi sql tupdesc and return tupdesc are compatible
1542 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1547 /* check the key_fld types match */
1548 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1549 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1550 if (ret_atttypid != sql_atttypid)
1552 (errcode(ERRCODE_SYNTAX_ERROR),
1553 errmsg("invalid return type"),
1554 errdetail("SQL key field datatype does " \
1555 "not match return key field datatype.")));
1557 /* check the parent_key_fld types match */
1558 ret_atttypid = ret_tupdesc->attrs[1]->atttypid;
1559 sql_atttypid = sql_tupdesc->attrs[1]->atttypid;
1560 if (ret_atttypid != sql_atttypid)
1562 (errcode(ERRCODE_SYNTAX_ERROR),
1563 errmsg("invalid return type"),
1564 errdetail("SQL parent key field datatype does " \
1565 "not match return parent key field datatype.")));
1567 /* OK, the two tupdescs are compatible for our purposes */
1572 * Check if two tupdescs match in type of attributes
1575 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1578 Form_pg_attribute ret_attr;
1580 Form_pg_attribute sql_attr;
1583 /* check the rowid types match */
1584 ret_atttypid = ret_tupdesc->attrs[0]->atttypid;
1585 sql_atttypid = sql_tupdesc->attrs[0]->atttypid;
1586 if (ret_atttypid != sql_atttypid)
1588 (errcode(ERRCODE_SYNTAX_ERROR),
1589 errmsg("invalid return type"),
1590 errdetail("SQL rowid datatype does not match " \
1591 "return rowid datatype.")));
1594 * - attribute [1] of the sql tuple is the category; no need to check it -
1595 * attribute [2] of the sql tuple should match attributes [1] to [natts]
1596 * of the return tuple
1598 sql_attr = sql_tupdesc->attrs[2];
1599 for (i = 1; i < ret_tupdesc->natts; i++)
1601 ret_attr = ret_tupdesc->attrs[i];
1603 if (ret_attr->atttypid != sql_attr->atttypid)
1607 /* OK, the two tupdescs are compatible for our purposes */
1612 * Return a properly quoted literal value.
1613 * Uses quote_literal in quote.c
1616 quote_literal_cstr(char *rawstr)
1622 rawstr_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(rawstr)));
1623 result_text = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(rawstr_text)));
1624 result = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(result_text)));