PostgreSQL Source Code  git master
tablefunc.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/htup_details.h"
#include "catalog/pg_type.h"
#include "common/pg_prng.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "tablefunc.h"
#include "utils/builtins.h"
Include dependency graph for tablefunc.c:

Go to the source code of this file.

Data Structures

struct  normal_rand_fctx
 
struct  crosstab_cat_desc
 
struct  crosstab_hashent
 

Macros

#define xpfree(var_)
 
#define xpstrdup(tgtvar_, srcvar_)
 
#define xstreq(tgtvar_, srcvar_)
 
#define INT32_STRLEN   12
 
#define MAX_CATNAME_LEN   NAMEDATALEN
 
#define INIT_CATS   64
 
#define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC)
 
#define crosstab_HashTableInsert(HASHTAB, CATDESC)
 
#define CONNECTBY_NCOLS   4
 
#define CONNECTBY_NCOLS_NOBRANCH   3
 

Typedefs

typedef struct crosstab_cat_desc crosstab_cat_desc
 
typedef struct crosstab_hashent crosstab_HashEnt
 

Functions

static HTABload_categories_hash (char *cats_sql, MemoryContext per_query_ctx)
 
static Tuplestorestateget_crosstab_tuplestore (char *sql, HTAB *crosstab_hash, TupleDesc tupdesc, bool randomAccess)
 
static void validateConnectbyTupleDesc (TupleDesc td, bool show_branch, bool show_serial)
 
static void compatCrosstabTupleDescs (TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
 
static void compatConnectbyTupleDescs (TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
 
static void get_normal_pair (float8 *x1, float8 *x2)
 
static Tuplestorestateconnectby (char *relname, char *key_fld, char *parent_key_fld, char *orderby_fld, char *branch_delim, char *start_with, int max_depth, bool show_branch, bool show_serial, MemoryContext per_query_ctx, bool randomAccess, AttInMetadata *attinmeta)
 
static void build_tuplestore_recursively (char *key_fld, char *parent_key_fld, char *relname, char *orderby_fld, char *branch_delim, char *start_with, char *branch, int level, int *serial, int max_depth, bool show_branch, bool show_serial, MemoryContext per_query_ctx, AttInMetadata *attinmeta, Tuplestorestate *tupstore)
 
 PG_FUNCTION_INFO_V1 (normal_rand)
 
Datum normal_rand (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (crosstab)
 
Datum crosstab (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (crosstab_hash)
 
Datum crosstab_hash (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (connectby_text)
 
Datum connectby_text (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (connectby_text_serial)
 
Datum connectby_text_serial (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Macro Definition Documentation

◆ CONNECTBY_NCOLS

#define CONNECTBY_NCOLS   4

Definition at line 976 of file tablefunc.c.

◆ CONNECTBY_NCOLS_NOBRANCH

#define CONNECTBY_NCOLS_NOBRANCH   3

Definition at line 977 of file tablefunc.c.

◆ crosstab_HashTableInsert

#define crosstab_HashTableInsert (   HASHTAB,
  CATDESC 
)
Value:
do { \
crosstab_HashEnt *hentry; bool found; char key[MAX_CATNAME_LEN]; \
snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATDESC->catname); \
hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
key, HASH_ENTER, &found); \
if (found) \
ereport(ERROR, \
errmsg("duplicate category name"))); \
hentry->catdesc = CATDESC; \
} while(0)
#define MemSet(start, val, len)
Definition: c.h:1020
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
int errcode(int sqlerrcode)
Definition: elog.c:857
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
@ HASH_ENTER
Definition: hsearch.h:114
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
#define MAX_CATNAME_LEN
Definition: tablefunc.c:125

Definition at line 142 of file tablefunc.c.

◆ crosstab_HashTableLookup

#define crosstab_HashTableLookup (   HASHTAB,
  CATNAME,
  CATDESC 
)
Value:
do { \
crosstab_HashEnt *hentry; char key[MAX_CATNAME_LEN]; \
snprintf(key, MAX_CATNAME_LEN - 1, "%s", CATNAME); \
hentry = (crosstab_HashEnt*) hash_search(HASHTAB, \
key, HASH_FIND, NULL); \
if (hentry) \
CATDESC = hentry->catdesc; \
else \
CATDESC = NULL; \
} while(0)
@ HASH_FIND
Definition: hsearch.h:113

Definition at line 128 of file tablefunc.c.

◆ INIT_CATS

#define INIT_CATS   64

Definition at line 126 of file tablefunc.c.

◆ INT32_STRLEN

#define INT32_STRLEN   12

Definition at line 116 of file tablefunc.c.

◆ MAX_CATNAME_LEN

#define MAX_CATNAME_LEN   NAMEDATALEN

Definition at line 125 of file tablefunc.c.

◆ xpfree

#define xpfree (   var_)
Value:
do { \
if (var_ != NULL) \
{ \
pfree(var_); \
var_ = NULL; \
} \
} while (0)

Definition at line 94 of file tablefunc.c.

◆ xpstrdup

#define xpstrdup (   tgtvar_,
  srcvar_ 
)
Value:
do { \
if (srcvar_) \
tgtvar_ = pstrdup(srcvar_); \
else \
tgtvar_ = NULL; \
} while (0)
char * pstrdup(const char *in)
Definition: mcxt.c:1695

Definition at line 103 of file tablefunc.c.

◆ xstreq

#define xstreq (   tgtvar_,
  srcvar_ 
)
Value:
(((tgtvar_ == NULL) && (srcvar_ == NULL)) || \
((tgtvar_ != NULL) && (srcvar_ != NULL) && (strcmp(tgtvar_, srcvar_) == 0)))

Definition at line 111 of file tablefunc.c.

Typedef Documentation

◆ crosstab_cat_desc

◆ crosstab_HashEnt

Function Documentation

◆ build_tuplestore_recursively()

static void build_tuplestore_recursively ( char *  key_fld,
char *  parent_key_fld,
char *  relname,
char *  orderby_fld,
char *  branch_delim,
char *  start_with,
char *  branch,
int  level,
int *  serial,
int  max_depth,
bool  show_branch,
bool  show_serial,
MemoryContext  per_query_ctx,
AttInMetadata attinmeta,
Tuplestorestate tupstore 
)
static

Definition at line 1195 of file tablefunc.c.

1210 {
1211  TupleDesc tupdesc = attinmeta->tupdesc;
1212  int ret;
1213  uint64 proc;
1214  int serial_column;
1215  StringInfoData sql;
1216  char **values;
1217  char *current_key;
1218  char *current_key_parent;
1219  char current_level[INT32_STRLEN];
1220  char serial_str[INT32_STRLEN];
1221  char *current_branch;
1222  HeapTuple tuple;
1223 
1224  if (max_depth > 0 && level > max_depth)
1225  return;
1226 
1227  initStringInfo(&sql);
1228 
1229  /* Build initial sql statement */
1230  if (!show_serial)
1231  {
1232  appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s",
1233  key_fld,
1234  parent_key_fld,
1235  relname,
1236  parent_key_fld,
1237  quote_literal_cstr(start_with),
1238  key_fld, key_fld, parent_key_fld);
1239  serial_column = 0;
1240  }
1241  else
1242  {
1243  appendStringInfo(&sql, "SELECT %s, %s FROM %s WHERE %s = %s AND %s IS NOT NULL AND %s <> %s ORDER BY %s",
1244  key_fld,
1245  parent_key_fld,
1246  relname,
1247  parent_key_fld,
1248  quote_literal_cstr(start_with),
1249  key_fld, key_fld, parent_key_fld,
1250  orderby_fld);
1251  serial_column = 1;
1252  }
1253 
1254  if (show_branch)
1255  values = (char **) palloc((CONNECTBY_NCOLS + serial_column) * sizeof(char *));
1256  else
1257  values = (char **) palloc((CONNECTBY_NCOLS_NOBRANCH + serial_column) * sizeof(char *));
1258 
1259  /* First time through, do a little setup */
1260  if (level == 0)
1261  {
1262  /* root value is the one we initially start with */
1263  values[0] = start_with;
1264 
1265  /* root value has no parent */
1266  values[1] = NULL;
1267 
1268  /* root level is 0 */
1269  sprintf(current_level, "%d", level);
1270  values[2] = current_level;
1271 
1272  /* root branch is just starting root value */
1273  if (show_branch)
1274  values[3] = start_with;
1275 
1276  /* root starts the serial with 1 */
1277  if (show_serial)
1278  {
1279  sprintf(serial_str, "%d", (*serial)++);
1280  if (show_branch)
1281  values[4] = serial_str;
1282  else
1283  values[3] = serial_str;
1284  }
1285 
1286  /* construct the tuple */
1287  tuple = BuildTupleFromCStrings(attinmeta, values);
1288 
1289  /* now store it */
1290  tuplestore_puttuple(tupstore, tuple);
1291 
1292  /* increment level */
1293  level++;
1294  }
1295 
1296  /* Retrieve the desired rows */
1297  ret = SPI_execute(sql.data, true, 0);
1298  proc = SPI_processed;
1299 
1300  /* Check for qualifying tuples */
1301  if ((ret == SPI_OK_SELECT) && (proc > 0))
1302  {
1303  HeapTuple spi_tuple;
1304  SPITupleTable *tuptable = SPI_tuptable;
1305  TupleDesc spi_tupdesc = tuptable->tupdesc;
1306  uint64 i;
1307  StringInfoData branchstr;
1308  StringInfoData chk_branchstr;
1309  StringInfoData chk_current_key;
1310 
1311  /*
1312  * Check that return tupdesc is compatible with the one we got from
1313  * the query.
1314  */
1315  compatConnectbyTupleDescs(tupdesc, spi_tupdesc);
1316 
1317  initStringInfo(&branchstr);
1318  initStringInfo(&chk_branchstr);
1319  initStringInfo(&chk_current_key);
1320 
1321  for (i = 0; i < proc; i++)
1322  {
1323  /* initialize branch for this pass */
1324  appendStringInfoString(&branchstr, branch);
1325  appendStringInfo(&chk_branchstr, "%s%s%s", branch_delim, branch, branch_delim);
1326 
1327  /* get the next sql result tuple */
1328  spi_tuple = tuptable->vals[i];
1329 
1330  /* get the current key (might be NULL) */
1331  current_key = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
1332 
1333  /* get the parent key (might be NULL) */
1334  current_key_parent = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
1335 
1336  /* get the current level */
1337  sprintf(current_level, "%d", level);
1338 
1339  /* check to see if this key is also an ancestor */
1340  if (current_key)
1341  {
1342  appendStringInfo(&chk_current_key, "%s%s%s",
1343  branch_delim, current_key, branch_delim);
1344  if (strstr(chk_branchstr.data, chk_current_key.data))
1345  ereport(ERROR,
1346  (errcode(ERRCODE_INVALID_RECURSION),
1347  errmsg("infinite recursion detected")));
1348  }
1349 
1350  /* OK, extend the branch */
1351  if (current_key)
1352  appendStringInfo(&branchstr, "%s%s", branch_delim, current_key);
1353  current_branch = branchstr.data;
1354 
1355  /* build a tuple */
1356  values[0] = current_key;
1357  values[1] = current_key_parent;
1358  values[2] = current_level;
1359  if (show_branch)
1360  values[3] = current_branch;
1361  if (show_serial)
1362  {
1363  sprintf(serial_str, "%d", (*serial)++);
1364  if (show_branch)
1365  values[4] = serial_str;
1366  else
1367  values[3] = serial_str;
1368  }
1369 
1370  tuple = BuildTupleFromCStrings(attinmeta, values);
1371 
1372  /* store the tuple for later use */
1373  tuplestore_puttuple(tupstore, tuple);
1374 
1375  heap_freetuple(tuple);
1376 
1377  /* recurse using current_key as the new start_with */
1378  if (current_key)
1380  parent_key_fld,
1381  relname,
1382  orderby_fld,
1383  branch_delim,
1384  current_key,
1385  current_branch,
1386  level + 1,
1387  serial,
1388  max_depth,
1389  show_branch,
1390  show_serial,
1391  per_query_ctx,
1392  attinmeta,
1393  tupstore);
1394 
1395  xpfree(current_key);
1396  xpfree(current_key_parent);
1397 
1398  /* reset branch for next pass */
1399  resetStringInfo(&branchstr);
1400  resetStringInfo(&chk_branchstr);
1401  resetStringInfo(&chk_current_key);
1402  }
1403 
1404  xpfree(branchstr.data);
1405  xpfree(chk_branchstr.data);
1406  xpfree(chk_current_key.data);
1407  }
1408 }
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define ereport(elevel,...)
Definition: elog.h:149
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2222
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1434
int i
Definition: isn.c:73
void * palloc(Size size)
Definition: mcxt.c:1316
NameData relname
Definition: pg_class.h:38
#define sprintf
Definition: port.h:240
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
uint64 SPI_processed
Definition: spi.c:44
SPITupleTable * SPI_tuptable
Definition: spi.c:45
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1217
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:593
#define SPI_OK_SELECT
Definition: spi.h:86
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:78
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
TupleDesc tupdesc
Definition: funcapi.h:38
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26
#define CONNECTBY_NCOLS_NOBRANCH
Definition: tablefunc.c:977
#define INT32_STRLEN
Definition: tablefunc.c:116
static void build_tuplestore_recursively(char *key_fld, char *parent_key_fld, char *relname, char *orderby_fld, char *branch_delim, char *start_with, char *branch, int level, int *serial, int max_depth, bool show_branch, bool show_serial, MemoryContext per_query_ctx, AttInMetadata *attinmeta, Tuplestorestate *tupstore)
Definition: tablefunc.c:1195
#define CONNECTBY_NCOLS
Definition: tablefunc.c:976
static void compatConnectbyTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
Definition: tablefunc.c:1474
#define xpfree(var_)
Definition: tablefunc.c:94
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730

References appendStringInfo(), appendStringInfoString(), BuildTupleFromCStrings(), compatConnectbyTupleDescs(), CONNECTBY_NCOLS, CONNECTBY_NCOLS_NOBRANCH, StringInfoData::data, ereport, errcode(), errmsg(), ERROR, heap_freetuple(), i, initStringInfo(), INT32_STRLEN, palloc(), quote_literal_cstr(), relname, resetStringInfo(), SPI_execute(), SPI_getvalue(), SPI_OK_SELECT, SPI_processed, SPI_tuptable, sprintf, SPITupleTable::tupdesc, AttInMetadata::tupdesc, tuplestore_puttuple(), SPITupleTable::vals, values, and xpfree.

Referenced by connectby().

◆ compatConnectbyTupleDescs()

static void compatConnectbyTupleDescs ( TupleDesc  ret_tupdesc,
TupleDesc  sql_tupdesc 
)
static

Definition at line 1474 of file tablefunc.c.

1475 {
1476  Oid ret_atttypid;
1477  Oid sql_atttypid;
1478  int32 ret_atttypmod;
1479  int32 sql_atttypmod;
1480 
1481  /*
1482  * Query result must have at least 2 columns.
1483  */
1484  if (sql_tupdesc->natts < 2)
1485  ereport(ERROR,
1486  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1487  errmsg("invalid connectby source data query"),
1488  errdetail("The query must return at least two columns.")));
1489 
1490  /*
1491  * These columns must match the result type indicated by the calling
1492  * query.
1493  */
1494  ret_atttypid = TupleDescAttr(ret_tupdesc, 0)->atttypid;
1495  sql_atttypid = TupleDescAttr(sql_tupdesc, 0)->atttypid;
1496  ret_atttypmod = TupleDescAttr(ret_tupdesc, 0)->atttypmod;
1497  sql_atttypmod = TupleDescAttr(sql_tupdesc, 0)->atttypmod;
1498  if (ret_atttypid != sql_atttypid ||
1499  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1500  ereport(ERROR,
1501  (errcode(ERRCODE_DATATYPE_MISMATCH),
1502  errmsg("invalid connectby return type"),
1503  errdetail("Source key type %s does not match return key type %s.",
1504  format_type_with_typemod(sql_atttypid, sql_atttypmod),
1505  format_type_with_typemod(ret_atttypid, ret_atttypmod))));
1506 
1507  ret_atttypid = TupleDescAttr(ret_tupdesc, 1)->atttypid;
1508  sql_atttypid = TupleDescAttr(sql_tupdesc, 1)->atttypid;
1509  ret_atttypmod = TupleDescAttr(ret_tupdesc, 1)->atttypmod;
1510  sql_atttypmod = TupleDescAttr(sql_tupdesc, 1)->atttypmod;
1511  if (ret_atttypid != sql_atttypid ||
1512  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1513  ereport(ERROR,
1514  (errcode(ERRCODE_DATATYPE_MISMATCH),
1515  errmsg("invalid connectby return type"),
1516  errdetail("Source parent key type %s does not match return parent key type %s.",
1517  format_type_with_typemod(sql_atttypid, sql_atttypmod),
1518  format_type_with_typemod(ret_atttypid, ret_atttypmod))));
1519 
1520  /* OK, the two tupdescs are compatible for our purposes */
1521 }
signed int int32
Definition: c.h:494
int errdetail(const char *fmt,...)
Definition: elog.c:1203
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:362
unsigned int Oid
Definition: postgres_ext.h:31
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_with_typemod(), TupleDescData::natts, and TupleDescAttr.

Referenced by build_tuplestore_recursively().

◆ compatCrosstabTupleDescs()

static void compatCrosstabTupleDescs ( TupleDesc  ret_tupdesc,
TupleDesc  sql_tupdesc 
)
static

Definition at line 1527 of file tablefunc.c.

1528 {
1529  int i;
1530  Oid ret_atttypid;
1531  Oid sql_atttypid;
1532  int32 ret_atttypmod;
1533  int32 sql_atttypmod;
1534 
1535  if (ret_tupdesc->natts < 2)
1536  ereport(ERROR,
1537  (errcode(ERRCODE_DATATYPE_MISMATCH),
1538  errmsg("invalid crosstab return type"),
1539  errdetail("Return row must have at least two columns.")));
1540  Assert(sql_tupdesc->natts == 3); /* already checked by caller */
1541 
1542  /* check the row_name types match */
1543  ret_atttypid = TupleDescAttr(ret_tupdesc, 0)->atttypid;
1544  sql_atttypid = TupleDescAttr(sql_tupdesc, 0)->atttypid;
1545  ret_atttypmod = TupleDescAttr(ret_tupdesc, 0)->atttypmod;
1546  sql_atttypmod = TupleDescAttr(sql_tupdesc, 0)->atttypmod;
1547  if (ret_atttypid != sql_atttypid ||
1548  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1549  ereport(ERROR,
1550  (errcode(ERRCODE_DATATYPE_MISMATCH),
1551  errmsg("invalid crosstab return type"),
1552  errdetail("Source row_name datatype %s does not match return row_name datatype %s.",
1553  format_type_with_typemod(sql_atttypid, sql_atttypmod),
1554  format_type_with_typemod(ret_atttypid, ret_atttypmod))));
1555 
1556  /*
1557  * attribute [1] of sql tuple is the category; no need to check it
1558  * attribute [2] of sql tuple should match attributes [1] to [natts - 1]
1559  * of the return tuple
1560  */
1561  sql_atttypid = TupleDescAttr(sql_tupdesc, 2)->atttypid;
1562  sql_atttypmod = TupleDescAttr(sql_tupdesc, 2)->atttypmod;
1563  for (i = 1; i < ret_tupdesc->natts; i++)
1564  {
1565  ret_atttypid = TupleDescAttr(ret_tupdesc, i)->atttypid;
1566  ret_atttypmod = TupleDescAttr(ret_tupdesc, i)->atttypmod;
1567 
1568  if (ret_atttypid != sql_atttypid ||
1569  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1570  ereport(ERROR,
1571  (errcode(ERRCODE_DATATYPE_MISMATCH),
1572  errmsg("invalid crosstab return type"),
1573  errdetail("Source value datatype %s does not match return value datatype %s in column %d.",
1574  format_type_with_typemod(sql_atttypid, sql_atttypmod),
1575  format_type_with_typemod(ret_atttypid, ret_atttypmod),
1576  i + 1)));
1577  }
1578 
1579  /* OK, the two tupdescs are compatible for our purposes */
1580 }
#define Assert(condition)
Definition: c.h:858

References Assert, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_with_typemod(), i, TupleDescData::natts, and TupleDescAttr.

Referenced by crosstab().

◆ connectby()

static Tuplestorestate * connectby ( char *  relname,
char *  key_fld,
char *  parent_key_fld,
char *  orderby_fld,
char *  branch_delim,
char *  start_with,
int  max_depth,
bool  show_branch,
bool  show_serial,
MemoryContext  per_query_ctx,
bool  randomAccess,
AttInMetadata attinmeta 
)
static

Definition at line 1140 of file tablefunc.c.

1152 {
1153  Tuplestorestate *tupstore = NULL;
1154  int ret;
1155  MemoryContext oldcontext;
1156 
1157  int serial = 1;
1158 
1159  /* Connect to SPI manager */
1160  if ((ret = SPI_connect()) < 0)
1161  /* internal error */
1162  elog(ERROR, "connectby: SPI_connect returned %d", ret);
1163 
1164  /* switch to longer term context to create the tuple store */
1165  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1166 
1167  /* initialize our tuplestore */
1168  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1169 
1170  MemoryContextSwitchTo(oldcontext);
1171 
1172  /* now go get the whole tree */
1174  parent_key_fld,
1175  relname,
1176  orderby_fld,
1177  branch_delim,
1178  start_with,
1179  start_with, /* current_branch */
1180  0, /* initial level is 0 */
1181  &serial, /* initial serial is 1 */
1182  max_depth,
1183  show_branch,
1184  show_serial,
1185  per_query_ctx,
1186  attinmeta,
1187  tupstore);
1188 
1189  SPI_finish();
1190 
1191  return tupstore;
1192 }
#define elog(elevel,...)
Definition: elog.h:224
int work_mem
Definition: globals.c:128
MemoryContextSwitchTo(old_ctx)
int SPI_connect(void)
Definition: spi.c:94
int SPI_finish(void)
Definition: spi.c:182
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318

References build_tuplestore_recursively(), elog, ERROR, MemoryContextSwitchTo(), relname, SPI_connect(), SPI_finish(), tuplestore_begin_heap(), and work_mem.

Referenced by connectby_text(), and connectby_text_serial().

◆ connectby_text()

Datum connectby_text ( PG_FUNCTION_ARGS  )

Definition at line 980 of file tablefunc.c.

981 {
983  char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
984  char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
985  char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
986  int max_depth = PG_GETARG_INT32(4);
987  char *branch_delim = NULL;
988  bool show_branch = false;
989  bool show_serial = false;
990  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
991  TupleDesc tupdesc;
992  AttInMetadata *attinmeta;
993  MemoryContext per_query_ctx;
994  MemoryContext oldcontext;
995 
996  /* check to see if caller supports us returning a tuplestore */
997  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
998  ereport(ERROR,
999  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1000  errmsg("set-valued function called in context that cannot accept a set")));
1001  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1002  rsinfo->expectedDesc == NULL)
1003  ereport(ERROR,
1004  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1005  errmsg("materialize mode required, but it is not allowed in this context")));
1006 
1007  if (fcinfo->nargs == 6)
1008  {
1009  branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1010  show_branch = true;
1011  }
1012  else
1013  /* default is no show, tilde for the delimiter */
1014  branch_delim = pstrdup("~");
1015 
1016  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1017  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1018 
1019  /* get the requested return tuple description */
1020  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1021 
1022  /* does it meet our needs */
1023  validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1024 
1025  /* OK, use it then */
1026  attinmeta = TupleDescGetAttInMetadata(tupdesc);
1027 
1028  /* OK, go to work */
1029  rsinfo->returnMode = SFRM_Materialize;
1030  rsinfo->setResult = connectby(relname,
1031  key_fld,
1032  parent_key_fld,
1033  NULL,
1034  branch_delim,
1035  start_with,
1036  max_depth,
1037  show_branch,
1038  show_serial,
1039  per_query_ctx,
1041  attinmeta);
1042  rsinfo->setDesc = tupdesc;
1043 
1044  MemoryContextSwitchTo(oldcontext);
1045 
1046  /*
1047  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1048  * tuples are in our tuplestore and passed back through rsinfo->setResult.
1049  * rsinfo->setDesc is set to the tuple description that we actually used
1050  * to build our tuples with, so the caller can verify we did what it was
1051  * expecting.
1052  */
1053  return (Datum) 0;
1054 }
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2173
@ SFRM_Materialize_Random
Definition: execnodes.h:318
@ SFRM_Materialize
Definition: execnodes.h:317
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
uintptr_t Datum
Definition: postgres.h:64
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:262
SetFunctionReturnMode returnMode
Definition: execnodes.h:336
ExprContext * econtext
Definition: execnodes.h:332
TupleDesc setDesc
Definition: execnodes.h:340
Tuplestorestate * setResult
Definition: execnodes.h:339
TupleDesc expectedDesc
Definition: execnodes.h:333
int allowedModes
Definition: execnodes.h:334
static void validateConnectbyTupleDesc(TupleDesc td, bool show_branch, bool show_serial)
Definition: tablefunc.c:1414
static Tuplestorestate * connectby(char *relname, char *key_fld, char *parent_key_fld, char *orderby_fld, char *branch_delim, char *start_with, int max_depth, bool show_branch, bool show_serial, MemoryContext per_query_ctx, bool randomAccess, AttInMetadata *attinmeta)
Definition: tablefunc.c:1140
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References ReturnSetInfo::allowedModes, connectby(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, if(), IsA, MemoryContextSwitchTo(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pstrdup(), relname, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, text_to_cstring(), TupleDescGetAttInMetadata(), and validateConnectbyTupleDesc().

◆ connectby_text_serial()

Datum connectby_text_serial ( PG_FUNCTION_ARGS  )

Definition at line 1058 of file tablefunc.c.

1059 {
1061  char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1062  char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1063  char *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1064  char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1065  int max_depth = PG_GETARG_INT32(5);
1066  char *branch_delim = NULL;
1067  bool show_branch = false;
1068  bool show_serial = true;
1069  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1070  TupleDesc tupdesc;
1071  AttInMetadata *attinmeta;
1072  MemoryContext per_query_ctx;
1073  MemoryContext oldcontext;
1074 
1075  /* check to see if caller supports us returning a tuplestore */
1076  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1077  ereport(ERROR,
1078  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1079  errmsg("set-valued function called in context that cannot accept a set")));
1080  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1081  rsinfo->expectedDesc == NULL)
1082  ereport(ERROR,
1083  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1084  errmsg("materialize mode required, but it is not allowed in this context")));
1085 
1086  if (fcinfo->nargs == 7)
1087  {
1088  branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1089  show_branch = true;
1090  }
1091  else
1092  /* default is no show, tilde for the delimiter */
1093  branch_delim = pstrdup("~");
1094 
1095  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1096  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1097 
1098  /* get the requested return tuple description */
1099  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1100 
1101  /* does it meet our needs */
1102  validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1103 
1104  /* OK, use it then */
1105  attinmeta = TupleDescGetAttInMetadata(tupdesc);
1106 
1107  /* OK, go to work */
1108  rsinfo->returnMode = SFRM_Materialize;
1109  rsinfo->setResult = connectby(relname,
1110  key_fld,
1111  parent_key_fld,
1112  orderby_fld,
1113  branch_delim,
1114  start_with,
1115  max_depth,
1116  show_branch,
1117  show_serial,
1118  per_query_ctx,
1120  attinmeta);
1121  rsinfo->setDesc = tupdesc;
1122 
1123  MemoryContextSwitchTo(oldcontext);
1124 
1125  /*
1126  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1127  * tuples are in our tuplestore and passed back through rsinfo->setResult.
1128  * rsinfo->setDesc is set to the tuple description that we actually used
1129  * to build our tuples with, so the caller can verify we did what it was
1130  * expecting.
1131  */
1132  return (Datum) 0;
1133 }

References ReturnSetInfo::allowedModes, connectby(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, if(), IsA, MemoryContextSwitchTo(), PG_GETARG_INT32, PG_GETARG_TEXT_PP, pstrdup(), relname, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, text_to_cstring(), TupleDescGetAttInMetadata(), and validateConnectbyTupleDesc().

◆ crosstab()

Datum crosstab ( PG_FUNCTION_ARGS  )

Definition at line 355 of file tablefunc.c.

356 {
357  char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
358  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
359  Tuplestorestate *tupstore;
360  TupleDesc tupdesc;
361  uint64 call_cntr;
362  uint64 max_calls;
363  AttInMetadata *attinmeta;
364  SPITupleTable *spi_tuptable;
365  TupleDesc spi_tupdesc;
366  bool firstpass;
367  char *lastrowid;
368  int i;
369  int num_categories;
370  MemoryContext per_query_ctx;
371  MemoryContext oldcontext;
372  int ret;
373  uint64 proc;
374 
375  /* check to see if caller supports us returning a tuplestore */
376  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
377  ereport(ERROR,
378  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
379  errmsg("set-valued function called in context that cannot accept a set")));
380  if (!(rsinfo->allowedModes & SFRM_Materialize))
381  ereport(ERROR,
382  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
383  errmsg("materialize mode required, but it is not allowed in this context")));
384 
385  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
386 
387  /* Connect to SPI manager */
388  if ((ret = SPI_connect()) < 0)
389  /* internal error */
390  elog(ERROR, "crosstab: SPI_connect returned %d", ret);
391 
392  /* Retrieve the desired rows */
393  ret = SPI_execute(sql, true, 0);
394  proc = SPI_processed;
395 
396  /* If no qualifying tuples, fall out early */
397  if (ret != SPI_OK_SELECT || proc == 0)
398  {
399  SPI_finish();
400  rsinfo->isDone = ExprEndResult;
401  PG_RETURN_NULL();
402  }
403 
404  spi_tuptable = SPI_tuptable;
405  spi_tupdesc = spi_tuptable->tupdesc;
406 
407  /*----------
408  * The provided SQL query must always return three columns.
409  *
410  * 1. rowname
411  * the label or identifier for each row in the final result
412  * 2. category
413  * the label or identifier for each column in the final result
414  * 3. values
415  * the value for each column in the final result
416  *----------
417  */
418  if (spi_tupdesc->natts != 3)
419  ereport(ERROR,
420  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
421  errmsg("invalid crosstab source data query"),
422  errdetail("The query must return 3 columns: row_name, category, and value.")));
423 
424  /* get a tuple descriptor for our result type */
425  switch (get_call_result_type(fcinfo, NULL, &tupdesc))
426  {
427  case TYPEFUNC_COMPOSITE:
428  /* success */
429  break;
430  case TYPEFUNC_RECORD:
431  /* failed to determine actual type of RECORD */
432  ereport(ERROR,
433  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
434  errmsg("function returning record called in context "
435  "that cannot accept type record")));
436  break;
437  default:
438  /* result type isn't composite */
439  ereport(ERROR,
440  (errcode(ERRCODE_DATATYPE_MISMATCH),
441  errmsg("return type must be a row type")));
442  break;
443  }
444 
445  /*
446  * Check that return tupdesc is compatible with the data we got from SPI,
447  * at least based on number and type of attributes
448  */
449  compatCrosstabTupleDescs(tupdesc, spi_tupdesc);
450 
451  /*
452  * switch to long-lived memory context
453  */
454  oldcontext = MemoryContextSwitchTo(per_query_ctx);
455 
456  /* make sure we have a persistent copy of the result tupdesc */
457  tupdesc = CreateTupleDescCopy(tupdesc);
458 
459  /* initialize our tuplestore in long-lived context */
460  tupstore =
462  false, work_mem);
463 
464  MemoryContextSwitchTo(oldcontext);
465 
466  /*
467  * Generate attribute metadata needed later to produce tuples from raw C
468  * strings
469  */
470  attinmeta = TupleDescGetAttInMetadata(tupdesc);
471 
472  /* total number of tuples to be examined */
473  max_calls = proc;
474 
475  /* the return tuple always must have 1 rowid + num_categories columns */
476  num_categories = tupdesc->natts - 1;
477 
478  firstpass = true;
479  lastrowid = NULL;
480 
481  for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
482  {
483  bool skip_tuple = false;
484  char **values;
485 
486  /* allocate and zero space */
487  values = (char **) palloc0((1 + num_categories) * sizeof(char *));
488 
489  /*
490  * now loop through the sql results and assign each value in sequence
491  * to the next category
492  */
493  for (i = 0; i < num_categories; i++)
494  {
495  HeapTuple spi_tuple;
496  char *rowid;
497 
498  /* see if we've gone too far already */
499  if (call_cntr >= max_calls)
500  break;
501 
502  /* get the next sql result tuple */
503  spi_tuple = spi_tuptable->vals[call_cntr];
504 
505  /* get the rowid from the current sql result tuple */
506  rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
507 
508  /*
509  * If this is the first pass through the values for this rowid,
510  * set the first column to rowid
511  */
512  if (i == 0)
513  {
514  xpstrdup(values[0], rowid);
515 
516  /*
517  * Check to see if the rowid is the same as that of the last
518  * tuple sent -- if so, skip this tuple entirely
519  */
520  if (!firstpass && xstreq(lastrowid, rowid))
521  {
522  xpfree(rowid);
523  skip_tuple = true;
524  break;
525  }
526  }
527 
528  /*
529  * If rowid hasn't changed on us, continue building the output
530  * tuple.
531  */
532  if (xstreq(rowid, values[0]))
533  {
534  /*
535  * Get the next category item value, which is always attribute
536  * number three.
537  *
538  * Be careful to assign the value to the array index based on
539  * which category we are presently processing.
540  */
541  values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
542 
543  /*
544  * increment the counter since we consume a row for each
545  * category, but not for last pass because the outer loop will
546  * do that for us
547  */
548  if (i < (num_categories - 1))
549  call_cntr++;
550  xpfree(rowid);
551  }
552  else
553  {
554  /*
555  * We'll fill in NULLs for the missing values, but we need to
556  * decrement the counter since this sql result row doesn't
557  * belong to the current output tuple.
558  */
559  call_cntr--;
560  xpfree(rowid);
561  break;
562  }
563  }
564 
565  if (!skip_tuple)
566  {
567  HeapTuple tuple;
568 
569  /* build the tuple and store it */
570  tuple = BuildTupleFromCStrings(attinmeta, values);
571  tuplestore_puttuple(tupstore, tuple);
572  heap_freetuple(tuple);
573  }
574 
575  /* Remember current rowid */
576  xpfree(lastrowid);
577  xpstrdup(lastrowid, values[0]);
578  firstpass = false;
579 
580  /* Clean up */
581  for (i = 0; i < num_categories + 1; i++)
582  if (values[i] != NULL)
583  pfree(values[i]);
584  pfree(values);
585  }
586 
587  /* let the caller know we're sending back a tuplestore */
588  rsinfo->returnMode = SFRM_Materialize;
589  rsinfo->setResult = tupstore;
590  rsinfo->setDesc = tupdesc;
591 
592  /* release SPI related resources (and return to caller's context) */
593  SPI_finish();
594 
595  return (Datum) 0;
596 }
@ ExprEndResult
Definition: execnodes.h:305
#define PG_RETURN_NULL()
Definition: fmgr.h:345
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
void pfree(void *pointer)
Definition: mcxt.c:1520
void * palloc0(Size size)
Definition: mcxt.c:1346
ExprDoneCond isDone
Definition: execnodes.h:337
#define xpstrdup(tgtvar_, srcvar_)
Definition: tablefunc.c:103
static void compatCrosstabTupleDescs(TupleDesc ret_tupdesc, TupleDesc sql_tupdesc)
Definition: tablefunc.c:1527
#define xstreq(tgtvar_, srcvar_)
Definition: tablefunc.c:111

References ReturnSetInfo::allowedModes, BuildTupleFromCStrings(), compatCrosstabTupleDescs(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, ExprEndResult, get_call_result_type(), heap_freetuple(), i, if(), IsA, ReturnSetInfo::isDone, MemoryContextSwitchTo(), palloc0(), pfree(), PG_GETARG_TEXT_PP, PG_RETURN_NULL, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, SPI_connect(), SPI_execute(), SPI_finish(), SPI_getvalue(), SPI_OK_SELECT, SPI_processed, SPI_tuptable, text_to_cstring(), SPITupleTable::tupdesc, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), tuplestore_puttuple(), TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, values, work_mem, xpfree, xpstrdup, and xstreq.

◆ crosstab_hash()

Datum crosstab_hash ( PG_FUNCTION_ARGS  )

Definition at line 635 of file tablefunc.c.

636 {
637  char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
638  char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
639  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
640  TupleDesc tupdesc;
641  MemoryContext per_query_ctx;
642  MemoryContext oldcontext;
644 
645  /* check to see if caller supports us returning a tuplestore */
646  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
647  ereport(ERROR,
648  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
649  errmsg("set-valued function called in context that cannot accept a set")));
650  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
651  rsinfo->expectedDesc == NULL)
652  ereport(ERROR,
653  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
654  errmsg("materialize mode required, but it is not allowed in this context")));
655 
656  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
657  oldcontext = MemoryContextSwitchTo(per_query_ctx);
658 
659  /* get the requested return tuple description */
660  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
661 
662  /*
663  * Check to make sure we have a reasonable tuple descriptor
664  *
665  * Note we will attempt to coerce the values into whatever the return
666  * attribute type is and depend on the "in" function to complain if
667  * needed.
668  */
669  if (tupdesc->natts < 2)
670  ereport(ERROR,
671  (errcode(ERRCODE_DATATYPE_MISMATCH),
672  errmsg("invalid crosstab return type"),
673  errdetail("Return row must have at least two columns.")));
674 
675  /* load up the categories hash table */
676  crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
677 
678  /* let the caller know we're sending back a tuplestore */
679  rsinfo->returnMode = SFRM_Materialize;
680 
681  /* now go build it */
682  rsinfo->setResult = get_crosstab_tuplestore(sql,
684  tupdesc,
686 
687  /*
688  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
689  * tuples are in our tuplestore and passed back through rsinfo->setResult.
690  * rsinfo->setDesc is set to the tuple description that we actually used
691  * to build our tuples with, so the caller can verify we did what it was
692  * expecting.
693  */
694  rsinfo->setDesc = tupdesc;
695  MemoryContextSwitchTo(oldcontext);
696 
697  return (Datum) 0;
698 }
Definition: dynahash.c:220
static Tuplestorestate * get_crosstab_tuplestore(char *sql, HTAB *crosstab_hash, TupleDesc tupdesc, bool randomAccess)
Definition: tablefunc.c:792
Datum crosstab_hash(PG_FUNCTION_ARGS)
Definition: tablefunc.c:635
static HTAB * load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
Definition: tablefunc.c:704

References ReturnSetInfo::allowedModes, CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errdetail(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, get_crosstab_tuplestore(), if(), IsA, load_categories_hash(), MemoryContextSwitchTo(), PG_GETARG_TEXT_PP, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Random, and text_to_cstring().

Referenced by get_crosstab_tuplestore(), and load_categories_hash().

◆ get_crosstab_tuplestore()

static Tuplestorestate * get_crosstab_tuplestore ( char *  sql,
HTAB crosstab_hash,
TupleDesc  tupdesc,
bool  randomAccess 
)
static

Definition at line 792 of file tablefunc.c.

796 {
797  Tuplestorestate *tupstore;
798  int num_categories = hash_get_num_entries(crosstab_hash);
799  AttInMetadata *attinmeta = TupleDescGetAttInMetadata(tupdesc);
800  char **values;
801  HeapTuple tuple;
802  int ret;
803  uint64 proc;
804 
805  /* initialize our tuplestore (while still in query context!) */
806  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
807 
808  /* Connect to SPI manager */
809  if ((ret = SPI_connect()) < 0)
810  /* internal error */
811  elog(ERROR, "get_crosstab_tuplestore: SPI_connect returned %d", ret);
812 
813  /* Now retrieve the crosstab source rows */
814  ret = SPI_execute(sql, true, 0);
815  proc = SPI_processed;
816 
817  /* Check for qualifying tuples */
818  if ((ret == SPI_OK_SELECT) && (proc > 0))
819  {
820  SPITupleTable *spi_tuptable = SPI_tuptable;
821  TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
822  int ncols = spi_tupdesc->natts;
823  char *rowid;
824  char *lastrowid = NULL;
825  bool firstpass = true;
826  uint64 i;
827  int j;
828  int result_ncols;
829 
830  if (num_categories == 0)
831  {
832  /* no qualifying category tuples */
833  ereport(ERROR,
834  (errcode(ERRCODE_CARDINALITY_VIOLATION),
835  errmsg("crosstab categories query must return at least one row")));
836  }
837 
838  /*
839  * The provided SQL query must always return at least three columns:
840  *
841  * 1. rowname the label for each row - column 1 in the final result
842  * 2. category the label for each value-column in the final result 3.
843  * value the values used to populate the value-columns
844  *
845  * If there are more than three columns, the last two are taken as
846  * "category" and "values". The first column is taken as "rowname".
847  * Additional columns (2 thru N-2) are assumed the same for the same
848  * "rowname", and are copied into the result tuple from the first time
849  * we encounter a particular rowname.
850  */
851  if (ncols < 3)
852  ereport(ERROR,
853  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
854  errmsg("invalid crosstab source data query"),
855  errdetail("The query must return at least 3 columns: row_name, category, and value.")));
856 
857  result_ncols = (ncols - 2) + num_categories;
858 
859  /* Recheck to make sure output tuple descriptor looks reasonable */
860  if (tupdesc->natts != result_ncols)
861  ereport(ERROR,
862  (errcode(ERRCODE_DATATYPE_MISMATCH),
863  errmsg("invalid crosstab return type"),
864  errdetail("Return row must have %d columns, not %d.",
865  result_ncols, tupdesc->natts)));
866 
867  /* allocate space and make sure it's clear */
868  values = (char **) palloc0(result_ncols * sizeof(char *));
869 
870  for (i = 0; i < proc; i++)
871  {
872  HeapTuple spi_tuple;
873  crosstab_cat_desc *catdesc;
874  char *catname;
875 
876  /* get the next sql result tuple */
877  spi_tuple = spi_tuptable->vals[i];
878 
879  /* get the rowid from the current sql result tuple */
880  rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
881 
882  /*
883  * if we're on a new output row, grab the column values up to
884  * column N-2 now
885  */
886  if (firstpass || !xstreq(lastrowid, rowid))
887  {
888  /*
889  * a new row means we need to flush the old one first, unless
890  * we're on the very first row
891  */
892  if (!firstpass)
893  {
894  /* rowid changed, flush the previous output row */
895  tuple = BuildTupleFromCStrings(attinmeta, values);
896 
897  tuplestore_puttuple(tupstore, tuple);
898 
899  for (j = 0; j < result_ncols; j++)
900  xpfree(values[j]);
901  }
902 
903  values[0] = rowid;
904  for (j = 1; j < ncols - 2; j++)
905  values[j] = SPI_getvalue(spi_tuple, spi_tupdesc, j + 1);
906 
907  /* we're no longer on the first pass */
908  firstpass = false;
909  }
910 
911  /* look up the category and fill in the appropriate column */
912  catname = SPI_getvalue(spi_tuple, spi_tupdesc, ncols - 1);
913 
914  if (catname != NULL)
915  {
916  crosstab_HashTableLookup(crosstab_hash, catname, catdesc);
917 
918  if (catdesc)
919  values[catdesc->attidx + ncols - 2] =
920  SPI_getvalue(spi_tuple, spi_tupdesc, ncols);
921  }
922 
923  xpfree(lastrowid);
924  xpstrdup(lastrowid, rowid);
925  }
926 
927  /* flush the last output row */
928  tuple = BuildTupleFromCStrings(attinmeta, values);
929 
930  tuplestore_puttuple(tupstore, tuple);
931  }
932 
933  if (SPI_finish() != SPI_OK_FINISH)
934  /* internal error */
935  elog(ERROR, "get_crosstab_tuplestore: SPI_finish() failed");
936 
937  return tupstore;
938 }
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1341
int j
Definition: isn.c:74
#define SPI_OK_FINISH
Definition: spi.h:83
#define crosstab_HashTableLookup(HASHTAB, CATNAME, CATDESC)
Definition: tablefunc.c:128

References crosstab_cat_desc::attidx, BuildTupleFromCStrings(), crosstab_hash(), crosstab_HashTableLookup, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, hash_get_num_entries(), i, j, TupleDescData::natts, palloc0(), SPI_connect(), SPI_execute(), SPI_finish(), SPI_getvalue(), SPI_OK_FINISH, SPI_OK_SELECT, SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, TupleDescGetAttInMetadata(), tuplestore_begin_heap(), tuplestore_puttuple(), SPITupleTable::vals, values, work_mem, xpfree, xpstrdup, and xstreq.

Referenced by crosstab_hash().

◆ get_normal_pair()

static void get_normal_pair ( float8 x1,
float8 x2 
)
static

Definition at line 284 of file tablefunc.c.

285 {
286  float8 u1,
287  u2,
288  v1,
289  v2,
290  s;
291 
292  do
293  {
296 
297  v1 = (2.0 * u1) - 1.0;
298  v2 = (2.0 * u2) - 1.0;
299 
300  s = v1 * v1 + v2 * v2;
301  } while (s >= 1.0);
302 
303  if (s == 0)
304  {
305  *x1 = 0;
306  *x2 = 0;
307  }
308  else
309  {
310  s = sqrt((-2.0 * log(s)) / s);
311  *x1 = v1 * s;
312  *x2 = v2 * s;
313  }
314 }
double float8
Definition: c.h:630
double pg_prng_double(pg_prng_state *state)
Definition: pg_prng.c:268
pg_prng_state pg_global_prng_state
Definition: pg_prng.c:34

References pg_global_prng_state, and pg_prng_double().

Referenced by normal_rand().

◆ load_categories_hash()

static HTAB * load_categories_hash ( char *  cats_sql,
MemoryContext  per_query_ctx 
)
static

Definition at line 704 of file tablefunc.c.

705 {
707  HASHCTL ctl;
708  int ret;
709  uint64 proc;
710  MemoryContext SPIcontext;
711 
712  /* initialize the category hash table */
713  ctl.keysize = MAX_CATNAME_LEN;
714  ctl.entrysize = sizeof(crosstab_HashEnt);
715  ctl.hcxt = per_query_ctx;
716 
717  /*
718  * use INIT_CATS, defined above as a guess of how many hash table entries
719  * to create, initially
720  */
721  crosstab_hash = hash_create("crosstab hash",
722  INIT_CATS,
723  &ctl,
725 
726  /* Connect to SPI manager */
727  if ((ret = SPI_connect()) < 0)
728  /* internal error */
729  elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
730 
731  /* Retrieve the category name rows */
732  ret = SPI_execute(cats_sql, true, 0);
733  proc = SPI_processed;
734 
735  /* Check for qualifying tuples */
736  if ((ret == SPI_OK_SELECT) && (proc > 0))
737  {
738  SPITupleTable *spi_tuptable = SPI_tuptable;
739  TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
740  uint64 i;
741 
742  /*
743  * The provided categories SQL query must always return one column:
744  * category - the label or identifier for each column
745  */
746  if (spi_tupdesc->natts != 1)
747  ereport(ERROR,
748  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
749  errmsg("invalid crosstab categories query"),
750  errdetail("The query must return one column.")));
751 
752  for (i = 0; i < proc; i++)
753  {
754  crosstab_cat_desc *catdesc;
755  char *catname;
756  HeapTuple spi_tuple;
757 
758  /* get the next sql result tuple */
759  spi_tuple = spi_tuptable->vals[i];
760 
761  /* get the category from the current sql result tuple */
762  catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
763  if (catname == NULL)
764  ereport(ERROR,
765  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
766  errmsg("crosstab category value must not be null")));
767 
768  SPIcontext = MemoryContextSwitchTo(per_query_ctx);
769 
770  catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
771  catdesc->catname = catname;
772  catdesc->attidx = i;
773 
774  /* Add the proc description block to the hashtable */
776 
777  MemoryContextSwitchTo(SPIcontext);
778  }
779  }
780 
781  if (SPI_finish() != SPI_OK_FINISH)
782  /* internal error */
783  elog(ERROR, "load_categories_hash: SPI_finish() failed");
784 
785  return crosstab_hash;
786 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_STRINGS
Definition: hsearch.h:96
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
tree ctl
Definition: radixtree.h:1853
#define crosstab_HashTableInsert(HASHTAB, CATDESC)
Definition: tablefunc.c:142
#define INIT_CATS
Definition: tablefunc.c:126
struct crosstab_hashent crosstab_HashEnt

References crosstab_cat_desc::attidx, crosstab_cat_desc::catname, crosstab_hash(), crosstab_HashTableInsert, ctl, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_STRINGS, i, INIT_CATS, MAX_CATNAME_LEN, MemoryContextSwitchTo(), TupleDescData::natts, palloc(), SPI_connect(), SPI_execute(), SPI_finish(), SPI_getvalue(), SPI_OK_FINISH, SPI_OK_SELECT, SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by crosstab_hash().

◆ normal_rand()

Datum normal_rand ( PG_FUNCTION_ARGS  )

Definition at line 173 of file tablefunc.c.

174 {
175  FuncCallContext *funcctx;
176  uint64 call_cntr;
177  uint64 max_calls;
178  normal_rand_fctx *fctx;
179  float8 mean;
180  float8 stddev;
181  float8 carry_val;
182  bool use_carry;
183  MemoryContext oldcontext;
184 
185  /* stuff done only on the first call of the function */
186  if (SRF_IS_FIRSTCALL())
187  {
188  int32 num_tuples;
189 
190  /* create a function context for cross-call persistence */
191  funcctx = SRF_FIRSTCALL_INIT();
192 
193  /*
194  * switch to memory context appropriate for multiple function calls
195  */
196  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
197 
198  /* total number of tuples to be returned */
199  num_tuples = PG_GETARG_INT32(0);
200  if (num_tuples < 0)
201  ereport(ERROR,
202  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
203  errmsg("number of rows cannot be negative")));
204  funcctx->max_calls = num_tuples;
205 
206  /* allocate memory for user context */
207  fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
208 
209  /*
210  * Use fctx to keep track of upper and lower bounds from call to call.
211  * It will also be used to carry over the spare value we get from the
212  * Box-Muller algorithm so that we only actually calculate a new value
213  * every other call.
214  */
215  fctx->mean = PG_GETARG_FLOAT8(1);
216  fctx->stddev = PG_GETARG_FLOAT8(2);
217  fctx->carry_val = 0;
218  fctx->use_carry = false;
219 
220  funcctx->user_fctx = fctx;
221 
222  MemoryContextSwitchTo(oldcontext);
223  }
224 
225  /* stuff done on every call of the function */
226  funcctx = SRF_PERCALL_SETUP();
227 
228  call_cntr = funcctx->call_cntr;
229  max_calls = funcctx->max_calls;
230  fctx = funcctx->user_fctx;
231  mean = fctx->mean;
232  stddev = fctx->stddev;
233  carry_val = fctx->carry_val;
234  use_carry = fctx->use_carry;
235 
236  if (call_cntr < max_calls) /* do when there is more left to send */
237  {
238  float8 result;
239 
240  if (use_carry)
241  {
242  /*
243  * reset use_carry and use second value obtained on last pass
244  */
245  fctx->use_carry = false;
246  result = carry_val;
247  }
248  else
249  {
250  float8 normval_1;
251  float8 normval_2;
252 
253  /* Get the next two normal values */
254  get_normal_pair(&normval_1, &normval_2);
255 
256  /* use the first */
257  result = mean + (stddev * normval_1);
258 
259  /* and save the second */
260  fctx->carry_val = mean + (stddev * normval_2);
261  fctx->use_carry = true;
262  }
263 
264  /* send the result */
265  SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
266  }
267  else
268  /* do when there is no more left */
269  SRF_RETURN_DONE(funcctx);
270 }
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1816
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
float8 carry_val
Definition: tablefunc.c:90
static void get_normal_pair(float8 *x1, float8 *x2)
Definition: tablefunc.c:284

References FuncCallContext::call_cntr, normal_rand_fctx::carry_val, ereport, errcode(), errmsg(), ERROR, Float8GetDatum(), get_normal_pair(), FuncCallContext::max_calls, normal_rand_fctx::mean, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_FLOAT8, PG_GETARG_INT32, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, normal_rand_fctx::stddev, normal_rand_fctx::use_carry, and FuncCallContext::user_fctx.

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( connectby_text  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( connectby_text_serial  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( crosstab  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( crosstab_hash  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( normal_rand  )

◆ validateConnectbyTupleDesc()

static void validateConnectbyTupleDesc ( TupleDesc  td,
bool  show_branch,
bool  show_serial 
)
static

Definition at line 1414 of file tablefunc.c.

1415 {
1416  int expected_cols;
1417 
1418  /* are there the correct number of columns */
1419  if (show_branch)
1420  expected_cols = CONNECTBY_NCOLS;
1421  else
1422  expected_cols = CONNECTBY_NCOLS_NOBRANCH;
1423  if (show_serial)
1424  expected_cols++;
1425 
1426  if (td->natts != expected_cols)
1427  ereport(ERROR,
1428  (errcode(ERRCODE_DATATYPE_MISMATCH),
1429  errmsg("invalid connectby return type"),
1430  errdetail("Return row must have %d columns, not %d.",
1431  expected_cols, td->natts)));
1432 
1433  /* the first two columns will be checked against the input tuples later */
1434 
1435  /* check that the type of the third column is INT4 */
1436  if (TupleDescAttr(td, 2)->atttypid != INT4OID)
1437  ereport(ERROR,
1438  (errcode(ERRCODE_DATATYPE_MISMATCH),
1439  errmsg("invalid connectby return type"),
1440  errdetail("Third return column (depth) must be type %s.",
1441  format_type_be(INT4OID))));
1442 
1443  /* check that the type of the branch column is TEXT if applicable */
1444  if (show_branch && TupleDescAttr(td, 3)->atttypid != TEXTOID)
1445  ereport(ERROR,
1446  (errcode(ERRCODE_DATATYPE_MISMATCH),
1447  errmsg("invalid connectby return type"),
1448  errdetail("Fourth return column (branch) must be type %s.",
1449  format_type_be(TEXTOID))));
1450 
1451  /* check that the type of the serial column is INT4 if applicable */
1452  if (show_branch && show_serial &&
1453  TupleDescAttr(td, 4)->atttypid != INT4OID)
1454  ereport(ERROR,
1455  (errcode(ERRCODE_DATATYPE_MISMATCH),
1456  errmsg("invalid connectby return type"),
1457  errdetail("Fifth return column (serial) must be type %s.",
1458  format_type_be(INT4OID))));
1459  if (!show_branch && show_serial &&
1460  TupleDescAttr(td, 3)->atttypid != INT4OID)
1461  ereport(ERROR,
1462  (errcode(ERRCODE_DATATYPE_MISMATCH),
1463  errmsg("invalid connectby return type"),
1464  errdetail("Fourth return column (serial) must be type %s.",
1465  format_type_be(INT4OID))));
1466 
1467  /* OK, the tupdesc is valid for our purposes */
1468 }
char * format_type_be(Oid type_oid)
Definition: format_type.c:343

References CONNECTBY_NCOLS, CONNECTBY_NCOLS_NOBRANCH, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), TupleDescData::natts, and TupleDescAttr.

Referenced by connectby_text(), and connectby_text_serial().

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 47 of file tablefunc.c.