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-2009, 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
444 * SPI, 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
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
492 * sequence 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
511 * rowid, 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
519 * last 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
537 * attribute number three.
539 * Be careful to assign the value to the array index based
540 * on 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
547 * will do that for us
549 if (i
< (num_categories
- 1))
556 * We'll fill in NULLs for the missing values, but we need
557 * to decrement the counter since this sql result row
558 * doesn't belong to the current output tuple.
570 /* build the tuple */
571 tuple
= BuildTupleFromCStrings(attinmeta
, values
);
573 /* switch to appropriate context while storing the tuple */
574 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
575 tuplestore_puttuple(tupstore
, tuple
);
576 MemoryContextSwitchTo(oldcontext
);
578 heap_freetuple(tuple
);
581 /* Remember current rowid */
583 xpstrdup(lastrowid
, values
[0]);
587 for (i
= 0; i
< num_categories
+ 1; i
++)
588 if (values
[i
] != NULL
)
593 /* let the caller know we're sending back a tuplestore */
594 rsinfo
->returnMode
= SFRM_Materialize
;
595 rsinfo
->setResult
= tupstore
;
596 rsinfo
->setDesc
= tupdesc
;
598 /* release SPI related resources (and return to caller's context) */
605 * crosstab_hash - reimplement crosstab as materialized function and
606 * properly deal with missing values (i.e. don't pack remaining
607 * values to the left)
609 * crosstab - create a crosstab of rowids and values columns from a
610 * SQL statement returning one rowid column, one category column,
611 * and one value column.
613 * e.g. given sql which produces:
616 * ------+-------+-------
626 * <===== values columns =====>
627 * rowid cat1 cat2 cat3 cat4
628 * ------+-------+-------+-------+-------
629 * row1 val1 val2 null val4
630 * row2 val5 val6 val7 val8
633 * 1. SQL result must be ordered by 1.
634 * 2. The number of values columns depends on the tuple description
635 * of the function's declared return type.
636 * 3. Missing values (i.e. missing category) are filled in with nulls.
637 * 4. Extra values (i.e. not in category results) are skipped.
639 PG_FUNCTION_INFO_V1(crosstab_hash
);
641 crosstab_hash(PG_FUNCTION_ARGS
)
643 char *sql
= text_to_cstring(PG_GETARG_TEXT_PP(0));
644 char *cats_sql
= text_to_cstring(PG_GETARG_TEXT_PP(1));
645 ReturnSetInfo
*rsinfo
= (ReturnSetInfo
*) fcinfo
->resultinfo
;
647 MemoryContext per_query_ctx
;
648 MemoryContext oldcontext
;
651 /* check to see if caller supports us returning a tuplestore */
652 if (rsinfo
== NULL
|| !IsA(rsinfo
, ReturnSetInfo
))
654 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
655 errmsg("set-valued function called in context that cannot accept a set")));
656 if (!(rsinfo
->allowedModes
& SFRM_Materialize
) ||
657 rsinfo
->expectedDesc
== NULL
)
659 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
660 errmsg("materialize mode required, but it is not " \
661 "allowed in this context")));
663 per_query_ctx
= rsinfo
->econtext
->ecxt_per_query_memory
;
664 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
666 /* get the requested return tuple description */
667 tupdesc
= CreateTupleDescCopy(rsinfo
->expectedDesc
);
670 * Check to make sure we have a reasonable tuple descriptor
672 * Note we will attempt to coerce the values into whatever the return
673 * attribute type is and depend on the "in" function to complain if
676 if (tupdesc
->natts
< 2)
678 (errcode(ERRCODE_SYNTAX_ERROR
),
679 errmsg("query-specified return tuple and " \
680 "crosstab function are not compatible")));
682 /* load up the categories hash table */
683 crosstab_hash
= load_categories_hash(cats_sql
, per_query_ctx
);
685 /* let the caller know we're sending back a tuplestore */
686 rsinfo
->returnMode
= SFRM_Materialize
;
688 /* now go build it */
689 rsinfo
->setResult
= get_crosstab_tuplestore(sql
,
693 rsinfo
->allowedModes
& SFRM_Materialize_Random
);
696 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
697 * tuples are in our tuplestore and passed back through rsinfo->setResult.
698 * rsinfo->setDesc is set to the tuple description that we actually used
699 * to build our tuples with, so the caller can verify we did what it was
702 rsinfo
->setDesc
= tupdesc
;
703 MemoryContextSwitchTo(oldcontext
);
709 * load up the categories hash table
712 load_categories_hash(char *cats_sql
, MemoryContext per_query_ctx
)
718 MemoryContext SPIcontext
;
720 /* initialize the category hash table */
721 MemSet(&ctl
, 0, sizeof(ctl
));
722 ctl
.keysize
= MAX_CATNAME_LEN
;
723 ctl
.entrysize
= sizeof(crosstab_HashEnt
);
724 ctl
.hcxt
= per_query_ctx
;
727 * use INIT_CATS, defined above as a guess of how many hash table entries
728 * to create, initially
730 crosstab_hash
= hash_create("crosstab hash",
733 HASH_ELEM
| HASH_CONTEXT
);
735 /* Connect to SPI manager */
736 if ((ret
= SPI_connect()) < 0)
738 elog(ERROR
, "load_categories_hash: SPI_connect returned %d", ret
);
740 /* Retrieve the category name rows */
741 ret
= SPI_execute(cats_sql
, true, 0);
742 proc
= SPI_processed
;
744 /* Check for qualifying tuples */
745 if ((ret
== SPI_OK_SELECT
) && (proc
> 0))
747 SPITupleTable
*spi_tuptable
= SPI_tuptable
;
748 TupleDesc spi_tupdesc
= spi_tuptable
->tupdesc
;
752 * The provided categories SQL query must always return one column:
753 * category - the label or identifier for each column
755 if (spi_tupdesc
->natts
!= 1)
757 (errcode(ERRCODE_SYNTAX_ERROR
),
758 errmsg("provided \"categories\" SQL must " \
759 "return 1 column of at least one row")));
761 for (i
= 0; i
< proc
; i
++)
763 crosstab_cat_desc
*catdesc
;
767 /* get the next sql result tuple */
768 spi_tuple
= spi_tuptable
->vals
[i
];
770 /* get the category from the current sql result tuple */
771 catname
= SPI_getvalue(spi_tuple
, spi_tupdesc
, 1);
773 SPIcontext
= MemoryContextSwitchTo(per_query_ctx
);
775 catdesc
= (crosstab_cat_desc
*) palloc(sizeof(crosstab_cat_desc
));
776 catdesc
->catname
= catname
;
779 /* Add the proc description block to the hashtable */
780 crosstab_HashTableInsert(crosstab_hash
, catdesc
);
782 MemoryContextSwitchTo(SPIcontext
);
786 if (SPI_finish() != SPI_OK_FINISH
)
788 elog(ERROR
, "load_categories_hash: SPI_finish() failed");
790 return crosstab_hash
;
794 * create and populate the crosstab tuplestore using the provided source query
796 static Tuplestorestate
*
797 get_crosstab_tuplestore(char *sql
,
800 MemoryContext per_query_ctx
,
803 Tuplestorestate
*tupstore
;
804 int num_categories
= hash_get_num_entries(crosstab_hash
);
805 AttInMetadata
*attinmeta
= TupleDescGetAttInMetadata(tupdesc
);
810 MemoryContext SPIcontext
;
812 /* initialize our tuplestore (while still in query context!) */
813 tupstore
= tuplestore_begin_heap(randomAccess
, false, work_mem
);
815 /* Connect to SPI manager */
816 if ((ret
= SPI_connect()) < 0)
818 elog(ERROR
, "get_crosstab_tuplestore: SPI_connect returned %d", ret
);
820 /* Now retrieve the crosstab source rows */
821 ret
= SPI_execute(sql
, true, 0);
822 proc
= SPI_processed
;
824 /* Check for qualifying tuples */
825 if ((ret
== SPI_OK_SELECT
) && (proc
> 0))
827 SPITupleTable
*spi_tuptable
= SPI_tuptable
;
828 TupleDesc spi_tupdesc
= spi_tuptable
->tupdesc
;
829 int ncols
= spi_tupdesc
->natts
;
831 char *lastrowid
= NULL
;
832 bool firstpass
= true;
837 if (num_categories
== 0)
839 /* no qualifying category tuples */
841 (errcode(ERRCODE_SYNTAX_ERROR
),
842 errmsg("provided \"categories\" SQL must " \
843 "return 1 column of at least one row")));
847 * The provided SQL query must always return at least three columns:
849 * 1. rowname the label for each row - column 1 in the final result
850 * 2. category the label for each value-column in the final result 3.
851 * value the values used to populate the value-columns
853 * If there are more than three columns, the last two are taken as
854 * "category" and "values". The first column is taken as "rowname".
855 * Additional columns (2 thru N-2) are assumed the same for the same
856 * "rowname", and are copied into the result tuple from the first time
857 * we encounter a particular rowname.
861 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
862 errmsg("invalid source data SQL statement"),
863 errdetail("The provided SQL must return 3 " \
864 " columns; rowid, category, and values.")));
866 result_ncols
= (ncols
- 2) + num_categories
;
868 /* Recheck to make sure we tuple descriptor still looks reasonable */
869 if (tupdesc
->natts
!= result_ncols
)
871 (errcode(ERRCODE_SYNTAX_ERROR
),
872 errmsg("invalid return type"),
873 errdetail("Query-specified return " \
874 "tuple has %d columns but crosstab " \
875 "returns %d.", tupdesc
->natts
, result_ncols
)));
878 values
= (char **) palloc(result_ncols
* sizeof(char *));
880 /* and make sure it's clear */
881 memset(values
, '\0', result_ncols
* sizeof(char *));
883 for (i
= 0; i
< proc
; i
++)
886 crosstab_cat_desc
*catdesc
;
889 /* get the next sql result tuple */
890 spi_tuple
= spi_tuptable
->vals
[i
];
892 /* get the rowid from the current sql result tuple */
893 rowid
= SPI_getvalue(spi_tuple
, spi_tupdesc
, 1);
896 * if we're on a new output row, grab the column values up to
899 if (firstpass
|| !xstreq(lastrowid
, rowid
))
902 * a new row means we need to flush the old one first, unless
903 * we're on the very first row
907 /* rowid changed, flush the previous output row */
908 tuple
= BuildTupleFromCStrings(attinmeta
, values
);
910 /* switch to appropriate context while storing the tuple */
911 SPIcontext
= MemoryContextSwitchTo(per_query_ctx
);
912 tuplestore_puttuple(tupstore
, tuple
);
913 MemoryContextSwitchTo(SPIcontext
);
915 for (j
= 0; j
< result_ncols
; j
++)
920 for (j
= 1; j
< ncols
- 2; j
++)
921 values
[j
] = SPI_getvalue(spi_tuple
, spi_tupdesc
, j
+ 1);
923 /* we're no longer on the first pass */
927 /* look up the category and fill in the appropriate column */
928 catname
= SPI_getvalue(spi_tuple
, spi_tupdesc
, ncols
- 1);
932 crosstab_HashTableLookup(crosstab_hash
, catname
, catdesc
);
935 values
[catdesc
->attidx
+ ncols
- 2] =
936 SPI_getvalue(spi_tuple
, spi_tupdesc
, ncols
);
940 xpstrdup(lastrowid
, rowid
);
943 /* flush the last output row */
944 tuple
= BuildTupleFromCStrings(attinmeta
, values
);
946 /* switch to appropriate context while storing the tuple */
947 SPIcontext
= MemoryContextSwitchTo(per_query_ctx
);
948 tuplestore_puttuple(tupstore
, tuple
);
949 MemoryContextSwitchTo(SPIcontext
);
952 if (SPI_finish() != SPI_OK_FINISH
)
954 elog(ERROR
, "get_crosstab_tuplestore: SPI_finish() failed");
956 tuplestore_donestoring(tupstore
);
962 * connectby_text - produce a result set from a hierarchical (parent/child)
965 * e.g. given table foo:
967 * keyid parent_keyid pos
968 * ------+------------+--
980 * connectby(text relname, text keyid_fld, text parent_keyid_fld
981 * [, text orderby_fld], text start_with, int max_depth
982 * [, text branch_delim])
983 * connectby('foo', 'keyid', 'parent_keyid', 'pos', 'row2', 0, '~') returns:
985 * keyid parent_id level branch serial
986 * ------+-----------+--------+-----------------------
988 * row5 row2 1 row2~row5 2
989 * row9 row5 2 row2~row5~row9 3
990 * row4 row2 1 row2~row4 4
991 * row6 row4 2 row2~row4~row6 5
992 * row8 row6 3 row2~row4~row6~row8 6
995 PG_FUNCTION_INFO_V1(connectby_text
);
997 #define CONNECTBY_NCOLS 4
998 #define CONNECTBY_NCOLS_NOBRANCH 3
1001 connectby_text(PG_FUNCTION_ARGS
)
1003 char *relname
= text_to_cstring(PG_GETARG_TEXT_PP(0));
1004 char *key_fld
= text_to_cstring(PG_GETARG_TEXT_PP(1));
1005 char *parent_key_fld
= text_to_cstring(PG_GETARG_TEXT_PP(2));
1006 char *start_with
= text_to_cstring(PG_GETARG_TEXT_PP(3));
1007 int max_depth
= PG_GETARG_INT32(4);
1008 char *branch_delim
= NULL
;
1009 bool show_branch
= false;
1010 bool show_serial
= false;
1011 ReturnSetInfo
*rsinfo
= (ReturnSetInfo
*) fcinfo
->resultinfo
;
1013 AttInMetadata
*attinmeta
;
1014 MemoryContext per_query_ctx
;
1015 MemoryContext oldcontext
;
1017 /* check to see if caller supports us returning a tuplestore */
1018 if (rsinfo
== NULL
|| !IsA(rsinfo
, ReturnSetInfo
))
1020 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1021 errmsg("set-valued function called in context that cannot accept a set")));
1022 if (!(rsinfo
->allowedModes
& SFRM_Materialize
) ||
1023 rsinfo
->expectedDesc
== NULL
)
1025 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1026 errmsg("materialize mode required, but it is not " \
1027 "allowed in this context")));
1029 if (fcinfo
->nargs
== 6)
1031 branch_delim
= text_to_cstring(PG_GETARG_TEXT_PP(5));
1035 /* default is no show, tilde for the delimiter */
1036 branch_delim
= pstrdup("~");
1038 per_query_ctx
= rsinfo
->econtext
->ecxt_per_query_memory
;
1039 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
1041 /* get the requested return tuple description */
1042 tupdesc
= CreateTupleDescCopy(rsinfo
->expectedDesc
);
1044 /* does it meet our needs */
1045 validateConnectbyTupleDesc(tupdesc
, show_branch
, show_serial
);
1047 /* OK, use it then */
1048 attinmeta
= TupleDescGetAttInMetadata(tupdesc
);
1050 /* OK, go to work */
1051 rsinfo
->returnMode
= SFRM_Materialize
;
1052 rsinfo
->setResult
= connectby(relname
,
1062 rsinfo
->allowedModes
& SFRM_Materialize_Random
,
1064 rsinfo
->setDesc
= tupdesc
;
1066 MemoryContextSwitchTo(oldcontext
);
1069 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1070 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1071 * rsinfo->setDesc is set to the tuple description that we actually used
1072 * to build our tuples with, so the caller can verify we did what it was
1078 PG_FUNCTION_INFO_V1(connectby_text_serial
);
1080 connectby_text_serial(PG_FUNCTION_ARGS
)
1082 char *relname
= text_to_cstring(PG_GETARG_TEXT_PP(0));
1083 char *key_fld
= text_to_cstring(PG_GETARG_TEXT_PP(1));
1084 char *parent_key_fld
= text_to_cstring(PG_GETARG_TEXT_PP(2));
1085 char *orderby_fld
= text_to_cstring(PG_GETARG_TEXT_PP(3));
1086 char *start_with
= text_to_cstring(PG_GETARG_TEXT_PP(4));
1087 int max_depth
= PG_GETARG_INT32(5);
1088 char *branch_delim
= NULL
;
1089 bool show_branch
= false;
1090 bool show_serial
= true;
1091 ReturnSetInfo
*rsinfo
= (ReturnSetInfo
*) fcinfo
->resultinfo
;
1093 AttInMetadata
*attinmeta
;
1094 MemoryContext per_query_ctx
;
1095 MemoryContext oldcontext
;
1097 /* check to see if caller supports us returning a tuplestore */
1098 if (rsinfo
== NULL
|| !IsA(rsinfo
, ReturnSetInfo
))
1100 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1101 errmsg("set-valued function called in context that cannot accept a set")));
1102 if (!(rsinfo
->allowedModes
& SFRM_Materialize
) ||
1103 rsinfo
->expectedDesc
== NULL
)
1105 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED
),
1106 errmsg("materialize mode required, but it is not " \
1107 "allowed in this context")));
1109 if (fcinfo
->nargs
== 7)
1111 branch_delim
= text_to_cstring(PG_GETARG_TEXT_PP(6));
1115 /* default is no show, tilde for the delimiter */
1116 branch_delim
= pstrdup("~");
1118 per_query_ctx
= rsinfo
->econtext
->ecxt_per_query_memory
;
1119 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
1121 /* get the requested return tuple description */
1122 tupdesc
= CreateTupleDescCopy(rsinfo
->expectedDesc
);
1124 /* does it meet our needs */
1125 validateConnectbyTupleDesc(tupdesc
, show_branch
, show_serial
);
1127 /* OK, use it then */
1128 attinmeta
= TupleDescGetAttInMetadata(tupdesc
);
1130 /* OK, go to work */
1131 rsinfo
->returnMode
= SFRM_Materialize
;
1132 rsinfo
->setResult
= connectby(relname
,
1142 rsinfo
->allowedModes
& SFRM_Materialize_Random
,
1144 rsinfo
->setDesc
= tupdesc
;
1146 MemoryContextSwitchTo(oldcontext
);
1149 * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1150 * tuples are in our tuplestore and passed back through rsinfo->setResult.
1151 * rsinfo->setDesc is set to the tuple description that we actually used
1152 * to build our tuples with, so the caller can verify we did what it was
1160 * connectby - does the real work for connectby_text()
1162 static Tuplestorestate
*
1163 connectby(char *relname
,
1165 char *parent_key_fld
,
1172 MemoryContext per_query_ctx
,
1174 AttInMetadata
*attinmeta
)
1176 Tuplestorestate
*tupstore
= NULL
;
1178 MemoryContext oldcontext
;
1182 /* Connect to SPI manager */
1183 if ((ret
= SPI_connect()) < 0)
1184 /* internal error */
1185 elog(ERROR
, "connectby: SPI_connect returned %d", ret
);
1187 /* switch to longer term context to create the tuple store */
1188 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
1190 /* initialize our tuplestore */
1191 tupstore
= tuplestore_begin_heap(randomAccess
, false, work_mem
);
1193 MemoryContextSwitchTo(oldcontext
);
1195 /* now go get the whole tree */
1196 tupstore
= build_tuplestore_recursively(key_fld
,
1202 start_with
, /* current_branch */
1203 0, /* initial level is 0 */
1204 &serial
, /* initial serial is 1 */
1217 static Tuplestorestate
*
1218 build_tuplestore_recursively(char *key_fld
,
1219 char *parent_key_fld
,
1230 MemoryContext per_query_ctx
,
1231 AttInMetadata
*attinmeta
,
1232 Tuplestorestate
*tupstore
)
1234 TupleDesc tupdesc
= attinmeta
->tupdesc
;
1235 MemoryContext oldcontext
;
1242 char *current_key_parent
;
1243 char current_level
[INT32_STRLEN
];
1244 char serial_str
[INT32_STRLEN
];
1245 char *current_branch
;
1248 if (max_depth
> 0 && level
> max_depth
)
1251 initStringInfo(&sql
);
1253 /* Build initial sql statement */
1256 appendStringInfo(&sql
, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1261 quote_literal_cstr(start_with
),
1262 key_fld
, key_fld
, parent_key_fld
);
1267 appendStringInfo(&sql
, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1272 quote_literal_cstr(start_with
),
1273 key_fld
, key_fld
, parent_key_fld
,
1279 values
= (char **) palloc((CONNECTBY_NCOLS
+ serial_column
) * sizeof(char *));
1281 values
= (char **) palloc((CONNECTBY_NCOLS_NOBRANCH
+ serial_column
) * sizeof(char *));
1283 /* First time through, do a little setup */
1286 /* root value is the one we initially start with */
1287 values
[0] = start_with
;
1289 /* root value has no parent */
1292 /* root level is 0 */
1293 sprintf(current_level
, "%d", level
);
1294 values
[2] = current_level
;
1296 /* root branch is just starting root value */
1298 values
[3] = start_with
;
1300 /* root starts the serial with 1 */
1303 sprintf(serial_str
, "%d", (*serial
)++);
1305 values
[4] = serial_str
;
1307 values
[3] = serial_str
;
1310 /* construct the tuple */
1311 tuple
= BuildTupleFromCStrings(attinmeta
, values
);
1313 /* switch to long lived context while storing the tuple */
1314 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
1317 tuplestore_puttuple(tupstore
, tuple
);
1319 /* now reset the context */
1320 MemoryContextSwitchTo(oldcontext
);
1322 /* increment level */
1326 /* Retrieve the desired rows */
1327 ret
= SPI_execute(sql
.data
, true, 0);
1328 proc
= SPI_processed
;
1330 /* Check for qualifying tuples */
1331 if ((ret
== SPI_OK_SELECT
) && (proc
> 0))
1333 HeapTuple spi_tuple
;
1334 SPITupleTable
*tuptable
= SPI_tuptable
;
1335 TupleDesc spi_tupdesc
= tuptable
->tupdesc
;
1337 StringInfoData branchstr
;
1338 StringInfoData chk_branchstr
;
1339 StringInfoData chk_current_key
;
1341 /* First time through, do a little more setup */
1345 * Check that return tupdesc is compatible with the one we got
1346 * from the query, but only at level 0 -- no need to check more
1350 if (!compatConnectbyTupleDescs(tupdesc
, spi_tupdesc
))
1352 (errcode(ERRCODE_SYNTAX_ERROR
),
1353 errmsg("invalid return type"),
1354 errdetail("Return and SQL tuple descriptions are " \
1358 initStringInfo(&branchstr
);
1359 initStringInfo(&chk_branchstr
);
1360 initStringInfo(&chk_current_key
);
1362 for (i
= 0; i
< proc
; i
++)
1364 /* initialize branch for this pass */
1365 appendStringInfo(&branchstr
, "%s", branch
);
1366 appendStringInfo(&chk_branchstr
, "%s%s%s", branch_delim
, branch
, branch_delim
);
1368 /* get the next sql result tuple */
1369 spi_tuple
= tuptable
->vals
[i
];
1371 /* get the current key and parent */
1372 current_key
= SPI_getvalue(spi_tuple
, spi_tupdesc
, 1);
1373 appendStringInfo(&chk_current_key
, "%s%s%s", branch_delim
, current_key
, branch_delim
);
1374 current_key_parent
= pstrdup(SPI_getvalue(spi_tuple
, spi_tupdesc
, 2));
1376 /* get the current level */
1377 sprintf(current_level
, "%d", level
);
1379 /* check to see if this key is also an ancestor */
1380 if (strstr(chk_branchstr
.data
, chk_current_key
.data
))
1381 elog(ERROR
, "infinite recursion detected");
1383 /* OK, extend the branch */
1384 appendStringInfo(&branchstr
, "%s%s", branch_delim
, current_key
);
1385 current_branch
= branchstr
.data
;
1388 values
[0] = pstrdup(current_key
);
1389 values
[1] = current_key_parent
;
1390 values
[2] = current_level
;
1392 values
[3] = current_branch
;
1395 sprintf(serial_str
, "%d", (*serial
)++);
1397 values
[4] = serial_str
;
1399 values
[3] = serial_str
;
1402 tuple
= BuildTupleFromCStrings(attinmeta
, values
);
1404 xpfree(current_key
);
1405 xpfree(current_key_parent
);
1407 /* switch to long lived context while storing the tuple */
1408 oldcontext
= MemoryContextSwitchTo(per_query_ctx
);
1410 /* store the tuple for later use */
1411 tuplestore_puttuple(tupstore
, tuple
);
1413 /* now reset the context */
1414 MemoryContextSwitchTo(oldcontext
);
1416 heap_freetuple(tuple
);
1418 /* recurse using current_key_parent as the new start_with */
1419 tupstore
= build_tuplestore_recursively(key_fld
,
1435 /* reset branch for next pass */
1436 resetStringInfo(&branchstr
);
1437 resetStringInfo(&chk_branchstr
);
1438 resetStringInfo(&chk_current_key
);
1441 xpfree(branchstr
.data
);
1442 xpfree(chk_branchstr
.data
);
1443 xpfree(chk_current_key
.data
);
1450 * Check expected (query runtime) tupdesc suitable for Connectby
1453 validateConnectbyTupleDesc(TupleDesc tupdesc
, bool show_branch
, bool show_serial
)
1455 int serial_column
= 0;
1460 /* are there the correct number of columns */
1463 if (tupdesc
->natts
!= (CONNECTBY_NCOLS
+ serial_column
))
1465 (errcode(ERRCODE_SYNTAX_ERROR
),
1466 errmsg("invalid return type"),
1467 errdetail("Query-specified return tuple has " \
1468 "wrong number of columns.")));
1472 if (tupdesc
->natts
!= CONNECTBY_NCOLS_NOBRANCH
+ serial_column
)
1474 (errcode(ERRCODE_SYNTAX_ERROR
),
1475 errmsg("invalid return type"),
1476 errdetail("Query-specified return tuple has " \
1477 "wrong number of columns.")));
1480 /* check that the types of the first two columns match */
1481 if (tupdesc
->attrs
[0]->atttypid
!= tupdesc
->attrs
[1]->atttypid
)
1483 (errcode(ERRCODE_SYNTAX_ERROR
),
1484 errmsg("invalid return type"),
1485 errdetail("First two columns must be the same type.")));
1487 /* check that the type of the third column is INT4 */
1488 if (tupdesc
->attrs
[2]->atttypid
!= INT4OID
)
1490 (errcode(ERRCODE_SYNTAX_ERROR
),
1491 errmsg("invalid return type"),
1492 errdetail("Third column must be type %s.",
1493 format_type_be(INT4OID
))));
1495 /* check that the type of the fourth column is TEXT if applicable */
1496 if (show_branch
&& tupdesc
->attrs
[3]->atttypid
!= TEXTOID
)
1498 (errcode(ERRCODE_SYNTAX_ERROR
),
1499 errmsg("invalid return type"),
1500 errdetail("Fourth column must be type %s.",
1501 format_type_be(TEXTOID
))));
1503 /* check that the type of the fifth column is INT4 */
1504 if (show_branch
&& show_serial
&& tupdesc
->attrs
[4]->atttypid
!= INT4OID
)
1505 elog(ERROR
, "query-specified return tuple not valid for Connectby: "
1506 "fifth column must be type %s", format_type_be(INT4OID
));
1508 /* check that the type of the fifth column is INT4 */
1509 if (!show_branch
&& show_serial
&& tupdesc
->attrs
[3]->atttypid
!= INT4OID
)
1510 elog(ERROR
, "query-specified return tuple not valid for Connectby: "
1511 "fourth column must be type %s", format_type_be(INT4OID
));
1513 /* OK, the tupdesc is valid for our purposes */
1517 * Check if spi sql tupdesc and return tupdesc are compatible
1520 compatConnectbyTupleDescs(TupleDesc ret_tupdesc
, TupleDesc sql_tupdesc
)
1525 /* check the key_fld types match */
1526 ret_atttypid
= ret_tupdesc
->attrs
[0]->atttypid
;
1527 sql_atttypid
= sql_tupdesc
->attrs
[0]->atttypid
;
1528 if (ret_atttypid
!= sql_atttypid
)
1530 (errcode(ERRCODE_SYNTAX_ERROR
),
1531 errmsg("invalid return type"),
1532 errdetail("SQL key field datatype does " \
1533 "not match return key field datatype.")));
1535 /* check the parent_key_fld types match */
1536 ret_atttypid
= ret_tupdesc
->attrs
[1]->atttypid
;
1537 sql_atttypid
= sql_tupdesc
->attrs
[1]->atttypid
;
1538 if (ret_atttypid
!= sql_atttypid
)
1540 (errcode(ERRCODE_SYNTAX_ERROR
),
1541 errmsg("invalid return type"),
1542 errdetail("SQL parent key field datatype does " \
1543 "not match return parent key field datatype.")));
1545 /* OK, the two tupdescs are compatible for our purposes */
1550 * Check if two tupdescs match in type of attributes
1553 compatCrosstabTupleDescs(TupleDesc ret_tupdesc
, TupleDesc sql_tupdesc
)
1556 Form_pg_attribute ret_attr
;
1558 Form_pg_attribute sql_attr
;
1561 if (ret_tupdesc
->natts
< 2 ||
1562 sql_tupdesc
->natts
< 3)
1565 /* check the rowid types match */
1566 ret_atttypid
= ret_tupdesc
->attrs
[0]->atttypid
;
1567 sql_atttypid
= sql_tupdesc
->attrs
[0]->atttypid
;
1568 if (ret_atttypid
!= sql_atttypid
)
1570 (errcode(ERRCODE_SYNTAX_ERROR
),
1571 errmsg("invalid return type"),
1572 errdetail("SQL rowid datatype does not match " \
1573 "return rowid datatype.")));
1576 * - attribute [1] of the sql tuple is the category; no need to check it -
1577 * attribute [2] of the sql tuple should match attributes [1] to [natts]
1578 * of the return tuple
1580 sql_attr
= sql_tupdesc
->attrs
[2];
1581 for (i
= 1; i
< ret_tupdesc
->natts
; i
++)
1583 ret_attr
= ret_tupdesc
->attrs
[i
];
1585 if (ret_attr
->atttypid
!= sql_attr
->atttypid
)
1589 /* OK, the two tupdescs are compatible for our purposes */
1594 * Return a properly quoted literal value.
1595 * Uses quote_literal in quote.c
1598 quote_literal_cstr(char *rawstr
)
1604 rawstr_text
= cstring_to_text(rawstr
);
1605 result_text
= DatumGetTextP(DirectFunctionCall1(quote_literal
,
1606 PointerGetDatum(rawstr_text
)));
1607 result
= text_to_cstring(result_text
);