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 "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 tupdesc, bool show_branch, bool show_serial)
 
static bool compatCrosstabTupleDescs (TupleDesc tupdesc1, TupleDesc tupdesc2)
 
static void compatConnectbyTupleDescs (TupleDesc tupdesc1, TupleDesc tupdesc2)
 
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 986 of file tablefunc.c.

Referenced by build_tuplestore_recursively(), and validateConnectbyTupleDesc().

◆ CONNECTBY_NCOLS_NOBRANCH

#define CONNECTBY_NCOLS_NOBRANCH   3

Definition at line 987 of file tablefunc.c.

Referenced by build_tuplestore_recursively(), and validateConnectbyTupleDesc().

◆ 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 MAX_CATNAME_LEN
Definition: tablefunc.c:124
int errcode(int sqlerrcode)
Definition: elog.c:698
#define MemSet(start, val, len)
Definition: c.h:1008
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954
#define ERROR
Definition: elog.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32

Definition at line 141 of file tablefunc.c.

Referenced by load_categories_hash().

◆ 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)
#define MAX_CATNAME_LEN
Definition: tablefunc.c:124
#define MemSet(start, val, len)
Definition: c.h:1008
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:954

Definition at line 127 of file tablefunc.c.

Referenced by get_crosstab_tuplestore().

◆ INIT_CATS

#define INIT_CATS   64

Definition at line 125 of file tablefunc.c.

Referenced by load_categories_hash().

◆ INT32_STRLEN

#define INT32_STRLEN   12

Definition at line 115 of file tablefunc.c.

Referenced by build_tuplestore_recursively().

◆ MAX_CATNAME_LEN

#define MAX_CATNAME_LEN   NAMEDATALEN

Definition at line 124 of file tablefunc.c.

Referenced by load_categories_hash().

◆ xpfree

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

Definition at line 93 of file tablefunc.c.

Referenced by build_tuplestore_recursively(), crosstab(), and get_crosstab_tuplestore().

◆ 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:1299

Definition at line 102 of file tablefunc.c.

Referenced by crosstab(), and get_crosstab_tuplestore().

◆ xstreq

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

Definition at line 110 of file tablefunc.c.

Referenced by crosstab(), and get_crosstab_tuplestore().

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 1205 of file tablefunc.c.

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(), 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().

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

◆ compatConnectbyTupleDescs()

static void compatConnectbyTupleDescs ( TupleDesc  tupdesc1,
TupleDesc  tupdesc2 
)
static

Definition at line 1499 of file tablefunc.c.

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

Referenced by build_tuplestore_recursively().

1500 {
1501  Oid ret_atttypid;
1502  Oid sql_atttypid;
1503  int32 ret_atttypmod;
1504  int32 sql_atttypmod;
1505 
1506  /*
1507  * Result must have at least 2 columns.
1508  */
1509  if (sql_tupdesc->natts < 2)
1510  ereport(ERROR,
1511  (errcode(ERRCODE_DATATYPE_MISMATCH),
1512  errmsg("invalid return type"),
1513  errdetail("Query must return at least two columns.")));
1514 
1515  /*
1516  * These columns must match the result type indicated by the calling
1517  * query.
1518  */
1519  ret_atttypid = TupleDescAttr(ret_tupdesc, 0)->atttypid;
1520  sql_atttypid = TupleDescAttr(sql_tupdesc, 0)->atttypid;
1521  ret_atttypmod = TupleDescAttr(ret_tupdesc, 0)->atttypmod;
1522  sql_atttypmod = TupleDescAttr(sql_tupdesc, 0)->atttypmod;
1523  if (ret_atttypid != sql_atttypid ||
1524  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1525  ereport(ERROR,
1526  (errcode(ERRCODE_DATATYPE_MISMATCH),
1527  errmsg("invalid return type"),
1528  errdetail("SQL key field type %s does " \
1529  "not match return key field type %s.",
1530  format_type_with_typemod(ret_atttypid, ret_atttypmod),
1531  format_type_with_typemod(sql_atttypid, sql_atttypmod))));
1532 
1533  ret_atttypid = TupleDescAttr(ret_tupdesc, 1)->atttypid;
1534  sql_atttypid = TupleDescAttr(sql_tupdesc, 1)->atttypid;
1535  ret_atttypmod = TupleDescAttr(ret_tupdesc, 1)->atttypmod;
1536  sql_atttypmod = TupleDescAttr(sql_tupdesc, 1)->atttypmod;
1537  if (ret_atttypid != sql_atttypid ||
1538  (ret_atttypmod >= 0 && ret_atttypmod != sql_atttypmod))
1539  ereport(ERROR,
1540  (errcode(ERRCODE_DATATYPE_MISMATCH),
1541  errmsg("invalid return type"),
1542  errdetail("SQL parent key field type %s does " \
1543  "not match return parent key field type %s.",
1544  format_type_with_typemod(ret_atttypid, ret_atttypmod),
1545  format_type_with_typemod(sql_atttypid, sql_atttypmod))));
1546 
1547  /* OK, the two tupdescs are compatible for our purposes */
1548 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:429
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:358
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ compatCrosstabTupleDescs()

static bool compatCrosstabTupleDescs ( TupleDesc  tupdesc1,
TupleDesc  tupdesc2 
)
static

Definition at line 1554 of file tablefunc.c.

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

Referenced by crosstab().

1555 {
1556  int i;
1557  Form_pg_attribute ret_attr;
1558  Oid ret_atttypid;
1559  Form_pg_attribute sql_attr;
1560  Oid sql_atttypid;
1561 
1562  if (ret_tupdesc->natts < 2 ||
1563  sql_tupdesc->natts < 3)
1564  return false;
1565 
1566  /* check the rowid types match */
1567  ret_atttypid = TupleDescAttr(ret_tupdesc, 0)->atttypid;
1568  sql_atttypid = TupleDescAttr(sql_tupdesc, 0)->atttypid;
1569  if (ret_atttypid != sql_atttypid)
1570  ereport(ERROR,
1571  (errcode(ERRCODE_DATATYPE_MISMATCH),
1572  errmsg("invalid return type"),
1573  errdetail("SQL rowid datatype does not match " \
1574  "return rowid datatype.")));
1575 
1576  /*
1577  * - attribute [1] of the sql tuple is the category; no need to check it -
1578  * attribute [2] of the sql tuple should match attributes [1] to [natts]
1579  * of the return tuple
1580  */
1581  sql_attr = TupleDescAttr(sql_tupdesc, 2);
1582  for (i = 1; i < ret_tupdesc->natts; i++)
1583  {
1584  ret_attr = TupleDescAttr(ret_tupdesc, i);
1585 
1586  if (ret_attr->atttypid != sql_attr->atttypid)
1587  return false;
1588  }
1589 
1590  /* OK, the two tupdescs are compatible for our purposes */
1591  return true;
1592 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i

◆ 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 1150 of file tablefunc.c.

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

Referenced by connectby_text(), and connectby_text_serial().

1162 {
1163  Tuplestorestate *tupstore = NULL;
1164  int ret;
1165  MemoryContext oldcontext;
1166 
1167  int serial = 1;
1168 
1169  /* Connect to SPI manager */
1170  if ((ret = SPI_connect()) < 0)
1171  /* internal error */
1172  elog(ERROR, "connectby: SPI_connect returned %d", ret);
1173 
1174  /* switch to longer term context to create the tuple store */
1175  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1176 
1177  /* initialize our tuplestore */
1178  tupstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1179 
1180  MemoryContextSwitchTo(oldcontext);
1181 
1182  /* now go get the whole tree */
1184  parent_key_fld,
1185  relname,
1186  orderby_fld,
1187  branch_delim,
1188  start_with,
1189  start_with, /* current_branch */
1190  0, /* initial level is 0 */
1191  &serial, /* initial serial is 1 */
1192  max_depth,
1193  show_branch,
1194  show_serial,
1195  per_query_ctx,
1196  attinmeta,
1197  tupstore);
1198 
1199  SPI_finish();
1200 
1201  return tupstore;
1202 }
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:1205
int SPI_connect(void)
Definition: spi.c:98
int SPI_finish(void)
Definition: spi.c:185
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
NameData relname
Definition: pg_class.h:38
#define ERROR
Definition: elog.h:46
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
int work_mem
Definition: globals.c:124
#define elog(elevel,...)
Definition: elog.h:232

◆ connectby_text()

Datum connectby_text ( PG_FUNCTION_ARGS  )

Definition at line 990 of file tablefunc.c.

References ReturnSetInfo::allowedModes, connectby(), connectby_text_serial(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, IsA, MemoryContextSwitchTo(), PG_FUNCTION_INFO_V1(), 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().

Referenced by get_crosstab_tuplestore().

991 {
993  char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
994  char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
995  char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(3));
996  int max_depth = PG_GETARG_INT32(4);
997  char *branch_delim = NULL;
998  bool show_branch = false;
999  bool show_serial = false;
1000  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1001  TupleDesc tupdesc;
1002  AttInMetadata *attinmeta;
1003  MemoryContext per_query_ctx;
1004  MemoryContext oldcontext;
1005 
1006  /* check to see if caller supports us returning a tuplestore */
1007  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1008  ereport(ERROR,
1009  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1010  errmsg("set-valued function called in context that cannot accept a set")));
1011  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1012  rsinfo->expectedDesc == NULL)
1013  ereport(ERROR,
1014  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015  errmsg("materialize mode required, but it is not allowed in this context")));
1016 
1017  if (fcinfo->nargs == 6)
1018  {
1019  branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(5));
1020  show_branch = true;
1021  }
1022  else
1023  /* default is no show, tilde for the delimiter */
1024  branch_delim = pstrdup("~");
1025 
1026  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1027  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1028 
1029  /* get the requested return tuple description */
1030  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1031 
1032  /* does it meet our needs */
1033  validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1034 
1035  /* OK, use it then */
1036  attinmeta = TupleDescGetAttInMetadata(tupdesc);
1037 
1038  /* OK, go to work */
1039  rsinfo->returnMode = SFRM_Materialize;
1040  rsinfo->setResult = connectby(relname,
1041  key_fld,
1042  parent_key_fld,
1043  NULL,
1044  branch_delim,
1045  start_with,
1046  max_depth,
1047  show_branch,
1048  show_serial,
1049  per_query_ctx,
1051  attinmeta);
1052  rsinfo->setDesc = tupdesc;
1053 
1054  MemoryContextSwitchTo(oldcontext);
1055 
1056  /*
1057  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1058  * tuples are in our tuplestore and passed back through rsinfo->setResult.
1059  * rsinfo->setDesc is set to the tuple description that we actually used
1060  * to build our tuples with, so the caller can verify we did what it was
1061  * expecting.
1062  */
1063  return (Datum) 0;
1064 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
Definition: tablefunc.c:1424
char * pstrdup(const char *in)
Definition: mcxt.c:1299
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
NameData relname
Definition: pg_class.h:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
TupleDesc expectedDesc
Definition: execnodes.h:304
#define ERROR
Definition: elog.h:46
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:1150
uintptr_t Datum
Definition: postgres.h:411
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
char * text_to_cstring(const text *t)
Definition: varlena.c:223
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ connectby_text_serial()

Datum connectby_text_serial ( PG_FUNCTION_ARGS  )

Definition at line 1068 of file tablefunc.c.

References ReturnSetInfo::allowedModes, connectby(), CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, 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().

Referenced by connectby_text().

1069 {
1071  char *key_fld = text_to_cstring(PG_GETARG_TEXT_PP(1));
1072  char *parent_key_fld = text_to_cstring(PG_GETARG_TEXT_PP(2));
1073  char *orderby_fld = text_to_cstring(PG_GETARG_TEXT_PP(3));
1074  char *start_with = text_to_cstring(PG_GETARG_TEXT_PP(4));
1075  int max_depth = PG_GETARG_INT32(5);
1076  char *branch_delim = NULL;
1077  bool show_branch = false;
1078  bool show_serial = true;
1079  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1080  TupleDesc tupdesc;
1081  AttInMetadata *attinmeta;
1082  MemoryContext per_query_ctx;
1083  MemoryContext oldcontext;
1084 
1085  /* check to see if caller supports us returning a tuplestore */
1086  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1087  ereport(ERROR,
1088  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1089  errmsg("set-valued function called in context that cannot accept a set")));
1090  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
1091  rsinfo->expectedDesc == NULL)
1092  ereport(ERROR,
1093  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1094  errmsg("materialize mode required, but it is not allowed in this context")));
1095 
1096  if (fcinfo->nargs == 7)
1097  {
1098  branch_delim = text_to_cstring(PG_GETARG_TEXT_PP(6));
1099  show_branch = true;
1100  }
1101  else
1102  /* default is no show, tilde for the delimiter */
1103  branch_delim = pstrdup("~");
1104 
1105  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1106  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1107 
1108  /* get the requested return tuple description */
1109  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
1110 
1111  /* does it meet our needs */
1112  validateConnectbyTupleDesc(tupdesc, show_branch, show_serial);
1113 
1114  /* OK, use it then */
1115  attinmeta = TupleDescGetAttInMetadata(tupdesc);
1116 
1117  /* OK, go to work */
1118  rsinfo->returnMode = SFRM_Materialize;
1119  rsinfo->setResult = connectby(relname,
1120  key_fld,
1121  parent_key_fld,
1122  orderby_fld,
1123  branch_delim,
1124  start_with,
1125  max_depth,
1126  show_branch,
1127  show_serial,
1128  per_query_ctx,
1130  attinmeta);
1131  rsinfo->setDesc = tupdesc;
1132 
1133  MemoryContextSwitchTo(oldcontext);
1134 
1135  /*
1136  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
1137  * tuples are in our tuplestore and passed back through rsinfo->setResult.
1138  * rsinfo->setDesc is set to the tuple description that we actually used
1139  * to build our tuples with, so the caller can verify we did what it was
1140  * expecting.
1141  */
1142  return (Datum) 0;
1143 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static void validateConnectbyTupleDesc(TupleDesc tupdesc, bool show_branch, bool show_serial)
Definition: tablefunc.c:1424
char * pstrdup(const char *in)
Definition: mcxt.c:1299
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
NameData relname
Definition: pg_class.h:38
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
TupleDesc expectedDesc
Definition: execnodes.h:304
#define ERROR
Definition: elog.h:46
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:1150
uintptr_t Datum
Definition: postgres.h:411
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
char * text_to_cstring(const text *t)
Definition: varlena.c:223
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ crosstab()

Datum crosstab ( PG_FUNCTION_ARGS  )

Definition at line 354 of file tablefunc.c.

References ReturnSetInfo::allowedModes, BuildTupleFromCStrings(), compatCrosstabTupleDescs(), CreateTupleDescCopy(), crosstab_hash(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, ExprEndResult, get_call_result_type(), heap_freetuple(), i, IsA, ReturnSetInfo::isDone, MemoryContextSwitchTo(), palloc0(), pfree(), PG_FUNCTION_INFO_V1(), 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.

Referenced by get_normal_pair().

355 {
356  char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
357  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
358  Tuplestorestate *tupstore;
359  TupleDesc tupdesc;
360  uint64 call_cntr;
361  uint64 max_calls;
362  AttInMetadata *attinmeta;
363  SPITupleTable *spi_tuptable;
364  TupleDesc spi_tupdesc;
365  bool firstpass;
366  char *lastrowid;
367  int i;
368  int num_categories;
369  MemoryContext per_query_ctx;
370  MemoryContext oldcontext;
371  int ret;
372  uint64 proc;
373 
374  /* check to see if caller supports us returning a tuplestore */
375  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
376  ereport(ERROR,
377  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
378  errmsg("set-valued function called in context that cannot accept a set")));
379  if (!(rsinfo->allowedModes & SFRM_Materialize))
380  ereport(ERROR,
381  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
382  errmsg("materialize mode required, but it is not allowed in this context")));
383 
384  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
385 
386  /* Connect to SPI manager */
387  if ((ret = SPI_connect()) < 0)
388  /* internal error */
389  elog(ERROR, "crosstab: SPI_connect returned %d", ret);
390 
391  /* Retrieve the desired rows */
392  ret = SPI_execute(sql, true, 0);
393  proc = SPI_processed;
394 
395  /* If no qualifying tuples, fall out early */
396  if (ret != SPI_OK_SELECT || proc == 0)
397  {
398  SPI_finish();
399  rsinfo->isDone = ExprEndResult;
400  PG_RETURN_NULL();
401  }
402 
403  spi_tuptable = SPI_tuptable;
404  spi_tupdesc = spi_tuptable->tupdesc;
405 
406  /*----------
407  * The provided SQL query must always return three columns.
408  *
409  * 1. rowname
410  * the label or identifier for each row in the final result
411  * 2. category
412  * the label or identifier for each column in the final result
413  * 3. values
414  * the value for each column in the final result
415  *----------
416  */
417  if (spi_tupdesc->natts != 3)
418  ereport(ERROR,
419  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
420  errmsg("invalid source data SQL statement"),
421  errdetail("The provided SQL must return 3 "
422  "columns: rowid, category, and values.")));
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  if (!compatCrosstabTupleDescs(tupdesc, spi_tupdesc))
450  ereport(ERROR,
451  (errcode(ERRCODE_SYNTAX_ERROR),
452  errmsg("return and sql tuple descriptions are " \
453  "incompatible")));
454 
455  /*
456  * switch to long-lived memory context
457  */
458  oldcontext = MemoryContextSwitchTo(per_query_ctx);
459 
460  /* make sure we have a persistent copy of the result tupdesc */
461  tupdesc = CreateTupleDescCopy(tupdesc);
462 
463  /* initialize our tuplestore in long-lived context */
464  tupstore =
466  false, work_mem);
467 
468  MemoryContextSwitchTo(oldcontext);
469 
470  /*
471  * Generate attribute metadata needed later to produce tuples from raw C
472  * strings
473  */
474  attinmeta = TupleDescGetAttInMetadata(tupdesc);
475 
476  /* total number of tuples to be examined */
477  max_calls = proc;
478 
479  /* the return tuple always must have 1 rowid + num_categories columns */
480  num_categories = tupdesc->natts - 1;
481 
482  firstpass = true;
483  lastrowid = NULL;
484 
485  for (call_cntr = 0; call_cntr < max_calls; call_cntr++)
486  {
487  bool skip_tuple = false;
488  char **values;
489 
490  /* allocate and zero space */
491  values = (char **) palloc0((1 + num_categories) * sizeof(char *));
492 
493  /*
494  * now loop through the sql results and assign each value in sequence
495  * to the next category
496  */
497  for (i = 0; i < num_categories; i++)
498  {
499  HeapTuple spi_tuple;
500  char *rowid;
501 
502  /* see if we've gone too far already */
503  if (call_cntr >= max_calls)
504  break;
505 
506  /* get the next sql result tuple */
507  spi_tuple = spi_tuptable->vals[call_cntr];
508 
509  /* get the rowid from the current sql result tuple */
510  rowid = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
511 
512  /*
513  * If this is the first pass through the values for this rowid,
514  * set the first column to rowid
515  */
516  if (i == 0)
517  {
518  xpstrdup(values[0], rowid);
519 
520  /*
521  * Check to see if the rowid is the same as that of the last
522  * tuple sent -- if so, skip this tuple entirely
523  */
524  if (!firstpass && xstreq(lastrowid, rowid))
525  {
526  xpfree(rowid);
527  skip_tuple = true;
528  break;
529  }
530  }
531 
532  /*
533  * If rowid hasn't changed on us, continue building the output
534  * tuple.
535  */
536  if (xstreq(rowid, values[0]))
537  {
538  /*
539  * Get the next category item value, which is always attribute
540  * number three.
541  *
542  * Be careful to assign the value to the array index based on
543  * which category we are presently processing.
544  */
545  values[1 + i] = SPI_getvalue(spi_tuple, spi_tupdesc, 3);
546 
547  /*
548  * increment the counter since we consume a row for each
549  * category, but not for last pass because the outer loop will
550  * do that for us
551  */
552  if (i < (num_categories - 1))
553  call_cntr++;
554  xpfree(rowid);
555  }
556  else
557  {
558  /*
559  * We'll fill in NULLs for the missing values, but we need to
560  * decrement the counter since this sql result row doesn't
561  * belong to the current output tuple.
562  */
563  call_cntr--;
564  xpfree(rowid);
565  break;
566  }
567  }
568 
569  if (!skip_tuple)
570  {
571  HeapTuple tuple;
572 
573  /* build the tuple and store it */
574  tuple = BuildTupleFromCStrings(attinmeta, values);
575  tuplestore_puttuple(tupstore, tuple);
576  heap_freetuple(tuple);
577  }
578 
579  /* Remember current rowid */
580  xpfree(lastrowid);
581  xpstrdup(lastrowid, values[0]);
582  firstpass = false;
583 
584  /* Clean up */
585  for (i = 0; i < num_categories + 1; i++)
586  if (values[i] != NULL)
587  pfree(values[i]);
588  pfree(values);
589  }
590 
591  /* let the caller know we're sending back a tuplestore */
592  rsinfo->returnMode = SFRM_Materialize;
593  rsinfo->setResult = tupstore;
594  rsinfo->setDesc = tupdesc;
595 
596  /* release SPI related resources (and return to caller's context) */
597  SPI_finish();
598 
599  return (Datum) 0;
600 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
int SPI_connect(void)
Definition: spi.c:98
int SPI_finish(void)
Definition: spi.c:185
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:698
#define xpfree(var_)
Definition: tablefunc.c:93
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
uint64 SPI_processed
Definition: spi.c:45
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
Definition: execTuples.c:2146
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1115
void pfree(void *pointer)
Definition: mcxt.c:1169
#define ERROR
Definition: elog.h:46
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
#define xstreq(tgtvar_, srcvar_)
Definition: tablefunc.c:110
int errdetail(const char *fmt,...)
Definition: elog.c:1042
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
void * palloc0(Size size)
Definition: mcxt.c:1093
uintptr_t Datum
Definition: postgres.h:411
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
Definition: execTuples.c:2097
int work_mem
Definition: globals.c:124
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:85
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
static bool compatCrosstabTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tablefunc.c:1554
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
static Datum values[MAXATTR]
Definition: bootstrap.c:166
char * text_to_cstring(const text *t)
Definition: varlena.c:223
ExprContext * econtext
Definition: execnodes.h:303
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
ExprDoneCond isDone
Definition: execnodes.h:308
#define xpstrdup(tgtvar_, srcvar_)
Definition: tablefunc.c:102
#define PG_RETURN_NULL()
Definition: fmgr.h:345
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:504

◆ crosstab_hash()

Datum crosstab_hash ( PG_FUNCTION_ARGS  )

Definition at line 639 of file tablefunc.c.

References ReturnSetInfo::allowedModes, CreateTupleDescCopy(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, get_crosstab_tuplestore(), 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 crosstab(), and load_categories_hash().

640 {
641  char *sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
642  char *cats_sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
643  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
644  TupleDesc tupdesc;
645  MemoryContext per_query_ctx;
646  MemoryContext oldcontext;
648 
649  /* check to see if caller supports us returning a tuplestore */
650  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
651  ereport(ERROR,
652  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
653  errmsg("set-valued function called in context that cannot accept a set")));
654  if (!(rsinfo->allowedModes & SFRM_Materialize) ||
655  rsinfo->expectedDesc == NULL)
656  ereport(ERROR,
657  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
658  errmsg("materialize mode required, but it is not allowed in this context")));
659 
660  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
661  oldcontext = MemoryContextSwitchTo(per_query_ctx);
662 
663  /* get the requested return tuple description */
664  tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
665 
666  /*
667  * Check to make sure we have a reasonable tuple descriptor
668  *
669  * Note we will attempt to coerce the values into whatever the return
670  * attribute type is and depend on the "in" function to complain if
671  * needed.
672  */
673  if (tupdesc->natts < 2)
674  ereport(ERROR,
675  (errcode(ERRCODE_SYNTAX_ERROR),
676  errmsg("query-specified return tuple and " \
677  "crosstab function are not compatible")));
678 
679  /* load up the categories hash table */
680  crosstab_hash = load_categories_hash(cats_sql, per_query_ctx);
681 
682  /* let the caller know we're sending back a tuplestore */
683  rsinfo->returnMode = SFRM_Materialize;
684 
685  /* now go build it */
686  rsinfo->setResult = get_crosstab_tuplestore(sql,
688  tupdesc,
690 
691  /*
692  * SFRM_Materialize mode expects us to return a NULL Datum. The actual
693  * tuples are in our tuplestore and passed back through rsinfo->setResult.
694  * rsinfo->setDesc is set to the tuple description that we actually used
695  * to build our tuples with, so the caller can verify we did what it was
696  * expecting.
697  */
698  rsinfo->setDesc = tupdesc;
699  MemoryContextSwitchTo(oldcontext);
700 
701  return (Datum) 0;
702 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define IsA(nodeptr, _type_)
Definition: nodes.h:590
static HTAB * load_categories_hash(char *cats_sql, MemoryContext per_query_ctx)
Definition: tablefunc.c:708
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
Definition: dynahash.c:219
TupleDesc expectedDesc
Definition: execnodes.h:304
#define ERROR
Definition: elog.h:46
uintptr_t Datum
Definition: postgres.h:411
#define ereport(elevel,...)
Definition: elog.h:157
int allowedModes
Definition: execnodes.h:305
Datum crosstab_hash(PG_FUNCTION_ARGS)
Definition: tablefunc.c:639
SetFunctionReturnMode returnMode
Definition: execnodes.h:307
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:233
Tuplestorestate * setResult
Definition: execnodes.h:310
char * text_to_cstring(const text *t)
Definition: varlena.c:223
ExprContext * econtext
Definition: execnodes.h:303
static Tuplestorestate * get_crosstab_tuplestore(char *sql, HTAB *crosstab_hash, TupleDesc tupdesc, bool randomAccess)
Definition: tablefunc.c:797
TupleDesc setDesc
Definition: execnodes.h:311
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ get_crosstab_tuplestore()

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

Definition at line 797 of file tablefunc.c.

References crosstab_cat_desc::attidx, BuildTupleFromCStrings(), crosstab_cat_desc::catname, connectby_text(), crosstab_HashTableLookup, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, hash_get_num_entries(), i, TupleDescData::natts, palloc0(), PG_FUNCTION_INFO_V1(), 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_donestoring, tuplestore_puttuple(), SPITupleTable::vals, values, work_mem, xpfree, xpstrdup, and xstreq.

Referenced by crosstab_hash().

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

◆ get_normal_pair()

static void get_normal_pair ( float8 x1,
float8 x2 
)
static

Definition at line 283 of file tablefunc.c.

References crosstab(), MAX_RANDOM_VALUE, PG_FUNCTION_INFO_V1(), and random().

Referenced by normal_rand().

284 {
285  float8 u1,
286  u2,
287  v1,
288  v2,
289  s;
290 
291  do
292  {
293  u1 = (float8) random() / (float8) MAX_RANDOM_VALUE;
294  u2 = (float8) random() / (float8) MAX_RANDOM_VALUE;
295 
296  v1 = (2.0 * u1) - 1.0;
297  v2 = (2.0 * u2) - 1.0;
298 
299  s = v1 * v1 + v2 * v2;
300  } while (s >= 1.0);
301 
302  if (s == 0)
303  {
304  *x1 = 0;
305  *x2 = 0;
306  }
307  else
308  {
309  s = sqrt((-2.0 * log(s)) / s);
310  *x1 = v1 * s;
311  *x2 = v2 * s;
312  }
313 }
long random(void)
Definition: random.c:22
double float8
Definition: c.h:565
#define MAX_RANDOM_VALUE

◆ load_categories_hash()

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

Definition at line 708 of file tablefunc.c.

References crosstab_cat_desc::attidx, crosstab_cat_desc::catname, crosstab_hash(), crosstab_HashTableInsert, elog, HASHCTL::entrysize, ereport, errcode(), errmsg(), ERROR, HASH_CONTEXT, hash_create(), HASH_ELEM, HASH_STRINGS, HASHCTL::hcxt, i, INIT_CATS, HASHCTL::keysize, 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().

709 {
711  HASHCTL ctl;
712  int ret;
713  uint64 proc;
714  MemoryContext SPIcontext;
715 
716  /* initialize the category hash table */
717  ctl.keysize = MAX_CATNAME_LEN;
718  ctl.entrysize = sizeof(crosstab_HashEnt);
719  ctl.hcxt = per_query_ctx;
720 
721  /*
722  * use INIT_CATS, defined above as a guess of how many hash table entries
723  * to create, initially
724  */
725  crosstab_hash = hash_create("crosstab hash",
726  INIT_CATS,
727  &ctl,
729 
730  /* Connect to SPI manager */
731  if ((ret = SPI_connect()) < 0)
732  /* internal error */
733  elog(ERROR, "load_categories_hash: SPI_connect returned %d", ret);
734 
735  /* Retrieve the category name rows */
736  ret = SPI_execute(cats_sql, true, 0);
737  proc = SPI_processed;
738 
739  /* Check for qualifying tuples */
740  if ((ret == SPI_OK_SELECT) && (proc > 0))
741  {
742  SPITupleTable *spi_tuptable = SPI_tuptable;
743  TupleDesc spi_tupdesc = spi_tuptable->tupdesc;
744  uint64 i;
745 
746  /*
747  * The provided categories SQL query must always return one column:
748  * category - the label or identifier for each column
749  */
750  if (spi_tupdesc->natts != 1)
751  ereport(ERROR,
752  (errcode(ERRCODE_SYNTAX_ERROR),
753  errmsg("provided \"categories\" SQL must " \
754  "return 1 column of at least one row")));
755 
756  for (i = 0; i < proc; i++)
757  {
758  crosstab_cat_desc *catdesc;
759  char *catname;
760  HeapTuple spi_tuple;
761 
762  /* get the next sql result tuple */
763  spi_tuple = spi_tuptable->vals[i];
764 
765  /* get the category from the current sql result tuple */
766  catname = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
767  if (catname == NULL)
768  ereport(ERROR,
769  (errcode(ERRCODE_SYNTAX_ERROR),
770  errmsg("provided \"categories\" SQL must " \
771  "not return NULL values")));
772 
773  SPIcontext = MemoryContextSwitchTo(per_query_ctx);
774 
775  catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
776  catdesc->catname = catname;
777  catdesc->attidx = i;
778 
779  /* Add the proc description block to the hashtable */
780  crosstab_HashTableInsert(crosstab_hash, catdesc);
781 
782  MemoryContextSwitchTo(SPIcontext);
783  }
784  }
785 
786  if (SPI_finish() != SPI_OK_FINISH)
787  /* internal error */
788  elog(ERROR, "load_categories_hash: SPI_finish() failed");
789 
790  return crosstab_hash;
791 }
#define MAX_CATNAME_LEN
Definition: tablefunc.c:124
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
MemoryContext hcxt
Definition: hsearch.h:86
int SPI_connect(void)
Definition: spi.c:98
int SPI_finish(void)
Definition: spi.c:185
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:76
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:698
HeapTuple * vals
Definition: spi.h:26
uint64 SPI_processed
Definition: spi.c:45
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1115
Definition: dynahash.c:219
#define ERROR
Definition: elog.h:46
#define INIT_CATS
Definition: tablefunc.c:125
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
struct crosstab_hashent crosstab_HashEnt
Size keysize
Definition: hsearch.h:75
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:85
#define ereport(elevel,...)
Definition: elog.h:157
Datum crosstab_hash(PG_FUNCTION_ARGS)
Definition: tablefunc.c:639
#define SPI_OK_FINISH
Definition: spi.h:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
#define crosstab_HashTableInsert(HASHTAB, CATDESC)
Definition: tablefunc.c:141
#define HASH_STRINGS
Definition: hsearch.h:96
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:504

◆ normal_rand()

Datum normal_rand ( PG_FUNCTION_ARGS  )

Definition at line 172 of file tablefunc.c.

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.

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

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( normal_rand  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( crosstab  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( crosstab_hash  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( connectby_text  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( connectby_text_serial  )

◆ validateConnectbyTupleDesc()

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

Definition at line 1424 of file tablefunc.c.

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().

1425 {
1426  int serial_column = 0;
1427 
1428  if (show_serial)
1429  serial_column = 1;
1430 
1431  /* are there the correct number of columns */
1432  if (show_branch)
1433  {
1434  if (td->natts != (CONNECTBY_NCOLS + serial_column))
1435  ereport(ERROR,
1436  (errcode(ERRCODE_DATATYPE_MISMATCH),
1437  errmsg("invalid return type"),
1438  errdetail("Query-specified return tuple has " \
1439  "wrong number of columns.")));
1440  }
1441  else
1442  {
1443  if (td->natts != CONNECTBY_NCOLS_NOBRANCH + serial_column)
1444  ereport(ERROR,
1445  (errcode(ERRCODE_DATATYPE_MISMATCH),
1446  errmsg("invalid return type"),
1447  errdetail("Query-specified return tuple has " \
1448  "wrong number of columns.")));
1449  }
1450 
1451  /* check that the types of the first two columns match */
1452  if (TupleDescAttr(td, 0)->atttypid != TupleDescAttr(td, 1)->atttypid)
1453  ereport(ERROR,
1454  (errcode(ERRCODE_DATATYPE_MISMATCH),
1455  errmsg("invalid return type"),
1456  errdetail("First two columns must be the same type.")));
1457 
1458  /* check that the type of the third column is INT4 */
1459  if (TupleDescAttr(td, 2)->atttypid != INT4OID)
1460  ereport(ERROR,
1461  (errcode(ERRCODE_DATATYPE_MISMATCH),
1462  errmsg("invalid return type"),
1463  errdetail("Third column must be type %s.",
1464  format_type_be(INT4OID))));
1465 
1466  /* check that the type of the fourth column is TEXT if applicable */
1467  if (show_branch && TupleDescAttr(td, 3)->atttypid != TEXTOID)
1468  ereport(ERROR,
1469  (errcode(ERRCODE_DATATYPE_MISMATCH),
1470  errmsg("invalid return type"),
1471  errdetail("Fourth column must be type %s.",
1472  format_type_be(TEXTOID))));
1473 
1474  /* check that the type of the fifth column is INT4 */
1475  if (show_branch && show_serial &&
1476  TupleDescAttr(td, 4)->atttypid != INT4OID)
1477  ereport(ERROR,
1478  (errcode(ERRCODE_DATATYPE_MISMATCH),
1479  errmsg("query-specified return tuple not valid for Connectby: "
1480  "fifth column must be type %s",
1481  format_type_be(INT4OID))));
1482 
1483  /* check that the type of the fifth column is INT4 */
1484  if (!show_branch && show_serial &&
1485  TupleDescAttr(td, 3)->atttypid != INT4OID)
1486  ereport(ERROR,
1487  (errcode(ERRCODE_DATATYPE_MISMATCH),
1488  errmsg("query-specified return tuple not valid for Connectby: "
1489  "fourth column must be type %s",
1490  format_type_be(INT4OID))));
1491 
1492  /* OK, the tupdesc is valid for our purposes */
1493 }
#define CONNECTBY_NCOLS_NOBRANCH
Definition: tablefunc.c:987
#define CONNECTBY_NCOLS
Definition: tablefunc.c:986
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
#define ERROR
Definition: elog.h:46
int errdetail(const char *fmt,...)
Definition: elog.c:1042
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 46 of file tablefunc.c.