Start background writer during archive recovery. Background writer now performs
[PostgreSQL.git] / contrib / tablefunc / tablefunc.c
blob3e5e90fe8183e25c163c31c88bfd32daf9ffe13c
1 /*
2 * $PostgreSQL$
5 * tablefunc
7 * Sample to demonstrate C functions which return setof scalar
8 * and setof composite.
9 * Joe Conway <mail@joeconway.com>
10 * And contributors:
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.
33 #include "postgres.h"
35 #include <math.h>
37 #include "catalog/pg_type.h"
38 #include "fmgr.h"
39 #include "funcapi.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"
49 PG_MODULE_MAGIC;
51 static HTAB *load_categories_hash(char *cats_sql, MemoryContext per_query_ctx);
52 static Tuplestorestate *get_crosstab_tuplestore(char *sql,
53 HTAB *crosstab_hash,
54 TupleDesc tupdesc,
55 MemoryContext per_query_ctx,
56 bool randomAccess);
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,
62 char *key_fld,
63 char *parent_key_fld,
64 char *orderby_fld,
65 char *branch_delim,
66 char *start_with,
67 int max_depth,
68 bool show_branch,
69 bool show_serial,
70 MemoryContext per_query_ctx,
71 bool randomAccess,
72 AttInMetadata *attinmeta);
73 static Tuplestorestate *build_tuplestore_recursively(char *key_fld,
74 char *parent_key_fld,
75 char *relname,
76 char *orderby_fld,
77 char *branch_delim,
78 char *start_with,
79 char *branch,
80 int level,
81 int *serial,
82 int max_depth,
83 bool show_branch,
84 bool show_serial,
85 MemoryContext per_query_ctx,
86 AttInMetadata *attinmeta,
87 Tuplestorestate *tupstore);
88 static char *quote_literal_cstr(char *rawstr);
90 typedef struct
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 */
96 } normal_rand_fctx;
98 #define xpfree(var_) \
99 do { \
100 if (var_ != NULL) \
102 pfree(var_); \
103 var_ = NULL; \
105 } while (0)
107 #define xpstrdup(tgtvar_, srcvar_) \
108 do { \
109 if (srcvar_) \
110 tgtvar_ = pstrdup(srcvar_); \
111 else \
112 tgtvar_ = NULL; \
113 } while (0)
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 */
127 } crosstab_cat_desc;
129 #define MAX_CATNAME_LEN NAMEDATALEN
130 #define INIT_CATS 64
132 #define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC) \
133 do { \
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); \
140 if (hentry) \
141 CATDESC = hentry->catdesc; \
142 else \
143 CATDESC = NULL; \
144 } while(0)
146 #define crosstab_HashTableInsert(HASHTAB, CATDESC) \
147 do { \
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); \
154 if (found) \
155 ereport(ERROR, \
156 (errcode(ERRCODE_DUPLICATE_OBJECT), \
157 errmsg("duplicate category name"))); \
158 hentry->catdesc = CATDESC; \
159 } while(0)
161 /* hash table */
162 typedef struct crosstab_hashent
164 char internal_catname[MAX_CATNAME_LEN];
165 crosstab_cat_desc *catdesc;
166 } crosstab_HashEnt;
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);
176 Datum
177 normal_rand(PG_FUNCTION_ARGS)
179 FuncCallContext *funcctx;
180 int call_cntr;
181 int max_calls;
182 normal_rand_fctx *fctx;
183 float8 mean;
184 float8 stddev;
185 float8 carry_val;
186 bool use_carry;
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
210 * every other call.
212 fctx->mean = PG_GETARG_FLOAT8(1);
213 fctx->stddev = PG_GETARG_FLOAT8(2);
214 fctx->carry_val = 0;
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;
228 mean = fctx->mean;
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 */
235 float8 result;
237 if (use_carry)
240 * reset use_carry and use second value obtained on last pass
242 fctx->use_carry = false;
243 result = carry_val;
245 else
247 float8 normval_1;
248 float8 normval_2;
250 /* Get the next two normal values */
251 get_normal_pair(&normval_1, &normval_2);
253 /* use the first */
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));
264 else
265 /* do when there is no more left */
266 SRF_RETURN_DONE(funcctx);
270 * get_normal_pair()
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.
280 static void
281 get_normal_pair(float8 *x1, float8 *x2)
283 float8 u1,
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;
298 } while (s >= 1.0);
300 if (s == 0)
302 *x1 = 0;
303 *x2 = 0;
305 else
307 s = sqrt((-2.0 * log(s)) / s);
308 *x1 = v1 * s;
309 *x2 = v2 * 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:
320 * rowid cat value
321 * ------+-------+-------
322 * row1 cat1 val1
323 * row1 cat2 val2
324 * row1 cat3 val3
325 * row1 cat4 val4
326 * row2 cat1 val5
327 * row2 cat2 val6
328 * row2 cat3 val7
329 * row2 cat4 val8
331 * crosstab returns:
332 * <===== values columns =====>
333 * rowid cat1 cat2 cat3 cat4
334 * ------+-------+-------+-------+-------
335 * row1 val1 val2 val3 val4
336 * row2 val5 val6 val7 val8
338 * NOTES:
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);
351 Datum
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;
357 TupleDesc tupdesc;
358 int call_cntr;
359 int max_calls;
360 AttInMetadata *attinmeta;
361 SPITupleTable *spi_tuptable;
362 TupleDesc spi_tupdesc;
363 bool firstpass;
364 char *lastrowid;
365 int i;
366 int num_categories;
367 MemoryContext per_query_ctx;
368 MemoryContext oldcontext;
369 int ret;
370 int proc;
372 /* check to see if caller supports us returning a tuplestore */
373 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
374 ereport(ERROR,
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))
378 ereport(ERROR,
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)
387 /* internal error */
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)
397 SPI_finish();
398 rsinfo->isDone = ExprEndResult;
399 PG_RETURN_NULL();
402 spi_tuptable = SPI_tuptable;
403 spi_tupdesc = spi_tuptable->tupdesc;
405 /*----------
406 * The provided SQL query must always return three columns.
408 * 1. rowname
409 * the label or identifier for each row in the final result
410 * 2. category
411 * the label or identifier for each column in the final result
412 * 3. values
413 * the value for each column in the final result
414 *----------
416 if (spi_tupdesc->natts != 3)
417 ereport(ERROR,
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:
427 /* success */
428 break;
429 case TYPEFUNC_RECORD:
430 /* failed to determine actual type of RECORD */
431 ereport(ERROR,
432 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
433 errmsg("function returning record called in context "
434 "that cannot accept type record")));
435 break;
436 default:
437 /* result type isn't composite */
438 elog(ERROR, "return type must be a row type");
439 break;
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))
447 ereport(ERROR,
448 (errcode(ERRCODE_SYNTAX_ERROR),
449 errmsg("return and sql tuple descriptions are " \
450 "incompatible")));
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 */
461 tupstore =
462 tuplestore_begin_heap(rsinfo->allowedModes & SFRM_Materialize_Random,
463 false, work_mem);
465 MemoryContextSwitchTo(oldcontext);
468 * Generate attribute metadata needed later to produce tuples from raw
469 * C strings
471 attinmeta = TupleDescGetAttInMetadata(tupdesc);
473 /* total number of tuples to be examined */
474 max_calls = proc;
476 /* the return tuple always must have 1 rowid + num_categories columns */
477 num_categories = tupdesc->natts - 1;
479 firstpass = true;
480 lastrowid = NULL;
482 for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
484 bool skip_tuple = false;
485 char **values;
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++)
496 HeapTuple spi_tuple;
497 char *rowid;
499 /* see if we've gone too far already */
500 if (call_cntr >= max_calls)
501 break;
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
513 if (i == 0)
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))
523 xpfree(rowid);
524 skip_tuple = true;
525 break;
530 * If rowid hasn't changed on us, continue building the output
531 * tuple.
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))
550 call_cntr++;
551 xpfree(rowid);
553 else
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.
560 call_cntr--;
561 xpfree(rowid);
562 break;
566 if (!skip_tuple)
568 HeapTuple 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 */
582 xpfree(lastrowid);
583 xpstrdup(lastrowid, values[0]);
584 firstpass = false;
586 /* Clean up */
587 for (i = 0; i < num_categories + 1; i++)
588 if (values[i] != NULL)
589 pfree(values[i]);
590 pfree(values);
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) */
599 SPI_finish();
601 return (Datum) 0;
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:
615 * rowid cat value
616 * ------+-------+-------
617 * row1 cat1 val1
618 * row1 cat2 val2
619 * row1 cat4 val4
620 * row2 cat1 val5
621 * row2 cat2 val6
622 * row2 cat3 val7
623 * row2 cat4 val8
625 * crosstab returns:
626 * <===== values columns =====>
627 * rowid cat1 cat2 cat3 cat4
628 * ------+-------+-------+-------+-------
629 * row1 val1 val2 null val4
630 * row2 val5 val6 val7 val8
632 * NOTES:
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);
640 Datum
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;
646 TupleDesc tupdesc;
647 MemoryContext per_query_ctx;
648 MemoryContext oldcontext;
649 HTAB *crosstab_hash;
651 /* check to see if caller supports us returning a tuplestore */
652 if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
653 ereport(ERROR,
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)
658 ereport(ERROR,
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
674 * needed.
676 if (tupdesc->natts < 2)
677 ereport(ERROR,
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,
690 crosstab_hash,
691 tupdesc,
692 per_query_ctx,
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
700 * expecting.
702 rsinfo->setDesc = tupdesc;
703 MemoryContextSwitchTo(oldcontext);
705 return (Datum) 0;
709 * load up the categories hash table
711 static HTAB *
712 load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
714 HTAB *crosstab_hash;
715 HASHCTL ctl;
716 int ret;
717 int proc;
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",
731 INIT_CATS,
732 &ctl,
733 HASH_ELEM | HASH_CONTEXT);
735 /* Connect to SPI manager */
736 if ((ret = SPI_connect()) < 0)
737 /* internal error */
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;
749 int i;
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)
756 ereport(ERROR,
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;
764 char *catname;
765 HeapTuple spi_tuple;
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;
777 catdesc->attidx = i;
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)
787 /* internal error */
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,
798 HTAB *crosstab_hash,
799 TupleDesc tupdesc,
800 MemoryContext per_query_ctx,
801 bool randomAccess)
803 Tuplestorestate *tupstore;
804 int num_categories = hash_get_num_entries(crosstab_hash);
805 AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
806 char **values;
807 HeapTuple tuple;
808 int ret;
809 int proc;
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)
817 /* internal error */
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;
830 char *rowid;
831 char *lastrowid = NULL;
832 bool firstpass = true;
833 int i,
835 int result_ncols;
837 if (num_categories == 0)
839 /* no qualifying category tuples */
840 ereport(ERROR,
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.
859 if (ncols < 3)
860 ereport(ERROR,
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)
870 ereport(ERROR,
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)));
877 /* allocate space */
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++)
885 HeapTuple spi_tuple;
886 crosstab_cat_desc *catdesc;
887 char *catname;
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
897 * column N-2 now
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
905 if (!firstpass)
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++)
916 xpfree(values[j]);
919 values[0] = rowid;
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 */
924 firstpass = false;
927 /* look up the category and fill in the appropriate column */
928 catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
930 if (catname != NULL)
932 crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
934 if (catdesc)
935 values[catdesc->attidx + ncols - 2] =
936 SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
939 xpfree(lastrowid);
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)
953 /* internal error */
954 elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
956 tuplestore_donestoring(tupstore);
958 return tupstore;
962 * connectby_text - produce a result set from a hierarchical (parent/child)
963 * table.
965 * e.g. given table foo:
967 * keyid parent_keyid pos
968 * ------+------------+--
969 * row1 NULL 0
970 * row2 row1 0
971 * row3 row1 0
972 * row4 row2 1
973 * row5 row2 0
974 * row6 row4 0
975 * row7 row3 0
976 * row8 row6 0
977 * row9 row5 0
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 * ------+-----------+--------+-----------------------
987 * row2 NULL 0 row2 1
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
1000 Datum
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;
1012 TupleDesc tupdesc;
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))
1019 ereport(ERROR,
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)
1024 ereport(ERROR,
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));
1032 show_branch = true;
1034 else
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,
1053 key_fld,
1054 parent_key_fld,
1055 NULL,
1056 branch_delim,
1057 start_with,
1058 max_depth,
1059 show_branch,
1060 show_serial,
1061 per_query_ctx,
1062 rsinfo->allowedModes & SFRM_Materialize_Random,
1063 attinmeta);
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
1073 * expecting.
1075 return (Datum) 0;
1078 PG_FUNCTION_INFO_V1(connectby_text_serial);
1079 Datum
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;
1092 TupleDesc tupdesc;
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))
1099 ereport(ERROR,
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)
1104 ereport(ERROR,
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));
1112 show_branch = true;
1114 else
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,
1133 key_fld,
1134 parent_key_fld,
1135 orderby_fld,
1136 branch_delim,
1137 start_with,
1138 max_depth,
1139 show_branch,
1140 show_serial,
1141 per_query_ctx,
1142 rsinfo->allowedModes & SFRM_Materialize_Random,
1143 attinmeta);
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
1153 * expecting.
1155 return (Datum) 0;
1160 * connectby - does the real work for connectby_text()
1162 static Tuplestorestate *
1163 connectby(char *relname,
1164 char *key_fld,
1165 char *parent_key_fld,
1166 char *orderby_fld,
1167 char *branch_delim,
1168 char *start_with,
1169 int max_depth,
1170 bool show_branch,
1171 bool show_serial,
1172 MemoryContext per_query_ctx,
1173 bool randomAccess,
1174 AttInMetadata *attinmeta)
1176 Tuplestorestate *tupstore = NULL;
1177 int ret;
1178 MemoryContext oldcontext;
1180 int serial = 1;
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,
1197 parent_key_fld,
1198 relname,
1199 orderby_fld,
1200 branch_delim,
1201 start_with,
1202 start_with, /* current_branch */
1203 0, /* initial level is 0 */
1204 &serial, /* initial serial is 1 */
1205 max_depth,
1206 show_branch,
1207 show_serial,
1208 per_query_ctx,
1209 attinmeta,
1210 tupstore);
1212 SPI_finish();
1214 return tupstore;
1217 static Tuplestorestate *
1218 build_tuplestore_recursively(char *key_fld,
1219 char *parent_key_fld,
1220 char *relname,
1221 char *orderby_fld,
1222 char *branch_delim,
1223 char *start_with,
1224 char *branch,
1225 int level,
1226 int *serial,
1227 int max_depth,
1228 bool show_branch,
1229 bool show_serial,
1230 MemoryContext per_query_ctx,
1231 AttInMetadata *attinmeta,
1232 Tuplestorestate *tupstore)
1234 TupleDesc tupdesc = attinmeta->tupdesc;
1235 MemoryContext oldcontext;
1236 int ret;
1237 int proc;
1238 int serial_column;
1239 StringInfoData sql;
1240 char **values;
1241 char *current_key;
1242 char *current_key_parent;
1243 char current_level[INT32_STRLEN];
1244 char serial_str[INT32_STRLEN];
1245 char *current_branch;
1246 HeapTuple tuple;
1248 if (max_depth > 0 && level > max_depth)
1249 return tupstore;
1251 initStringInfo(&sql);
1253 /* Build initial sql statement */
1254 if (!show_serial)
1256 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1257 key_fld,
1258 parent_key_fld,
1259 relname,
1260 parent_key_fld,
1261 quote_literal_cstr(start_with),
1262 key_fld, key_fld, parent_key_fld);
1263 serial_column = 0;
1265 else
1267 appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1268 key_fld,
1269 parent_key_fld,
1270 relname,
1271 parent_key_fld,
1272 quote_literal_cstr(start_with),
1273 key_fld, key_fld, parent_key_fld,
1274 orderby_fld);
1275 serial_column = 1;
1278 if (show_branch)
1279 values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1280 else
1281 values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1283 /* First time through, do a little setup */
1284 if (level == 0)
1286 /* root value is the one we initially start with */
1287 values[0] = start_with;
1289 /* root value has no parent */
1290 values[1] = NULL;
1292 /* root level is 0 */
1293 sprintf(current_level, "%d", level);
1294 values[2] = current_level;
1296 /* root branch is just starting root value */
1297 if (show_branch)
1298 values[3] = start_with;
1300 /* root starts the serial with 1 */
1301 if (show_serial)
1303 sprintf(serial_str, "%d", (*serial)++);
1304 if (show_branch)
1305 values[4] = serial_str;
1306 else
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);
1316 /* now store it */
1317 tuplestore_puttuple(tupstore, tuple);
1319 /* now reset the context */
1320 MemoryContextSwitchTo(oldcontext);
1322 /* increment level */
1323 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;
1336 int i;
1337 StringInfoData branchstr;
1338 StringInfoData chk_branchstr;
1339 StringInfoData chk_current_key;
1341 /* First time through, do a little more setup */
1342 if (level == 0)
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
1347 * than once
1350 if (!compatConnectbyTupleDescs(tupdesc, spi_tupdesc))
1351 ereport(ERROR,
1352 (errcode(ERRCODE_SYNTAX_ERROR),
1353 errmsg("invalid return type"),
1354 errdetail("Return and SQL tuple descriptions are " \
1355 "incompatible.")));
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;
1387 /* build a tuple */
1388 values[0] = pstrdup(current_key);
1389 values[1] = current_key_parent;
1390 values[2] = current_level;
1391 if (show_branch)
1392 values[3] = current_branch;
1393 if (show_serial)
1395 sprintf(serial_str, "%d", (*serial)++);
1396 if (show_branch)
1397 values[4] = serial_str;
1398 else
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,
1420 parent_key_fld,
1421 relname,
1422 orderby_fld,
1423 branch_delim,
1424 values[0],
1425 current_branch,
1426 level + 1,
1427 serial,
1428 max_depth,
1429 show_branch,
1430 show_serial,
1431 per_query_ctx,
1432 attinmeta,
1433 tupstore);
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);
1446 return tupstore;
1450 * Check expected (query runtime) tupdesc suitable for Connectby
1452 static void
1453 validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
1455 int serial_column = 0;
1457 if (show_serial)
1458 serial_column = 1;
1460 /* are there the correct number of columns */
1461 if (show_branch)
1463 if (tupdesc->natts != (CONNECTBY_NCOLS + serial_column))
1464 ereport(ERROR,
1465 (errcode(ERRCODE_SYNTAX_ERROR),
1466 errmsg("invalid return type"),
1467 errdetail("Query-specified return tuple has " \
1468 "wrong number of columns.")));
1470 else
1472 if (tupdesc->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1473 ereport(ERROR,
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)
1482 ereport(ERROR,
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)
1489 ereport(ERROR,
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)
1497 ereport(ERROR,
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
1519 static bool
1520 compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1522 Oid ret_atttypid;
1523 Oid sql_atttypid;
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)
1529 ereport(ERROR,
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)
1539 ereport(ERROR,
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 */
1546 return true;
1550 * Check if two tupdescs match in type of attributes
1552 static bool
1553 compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
1555 int i;
1556 Form_pg_attribute ret_attr;
1557 Oid ret_atttypid;
1558 Form_pg_attribute sql_attr;
1559 Oid sql_atttypid;
1561 if (ret_tupdesc->natts < 2 ||
1562 sql_tupdesc->natts < 3)
1563 return false;
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)
1569 ereport(ERROR,
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)
1586 return false;
1589 /* OK, the two tupdescs are compatible for our purposes */
1590 return true;
1594 * Return a properly quoted literal value.
1595 * Uses quote_literal in quote.c
1597 static char *
1598 quote_literal_cstr(char *rawstr)
1600 text *rawstr_text;
1601 text *result_text;
1602 char *result;
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);
1609 return result;