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, MemoryContext per_query_ctx, 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 983 of file tablefunc.c.

Referenced by build_tuplestore_recursively(), and validateConnectbyTupleDesc().

◆ CONNECTBY_NCOLS_NOBRANCH

#define CONNECTBY_NCOLS_NOBRANCH   3

Definition at line 984 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:125
int errcode(int sqlerrcode)
Definition: elog.c:610
#define MemSet(start, val, len)
Definition: c.h:971
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908
#define ERROR
Definition: elog.h:43
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:31

Definition at line 142 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:125
#define MemSet(start, val, len)
Definition: c.h:971
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:908

Definition at line 128 of file tablefunc.c.

Referenced by get_crosstab_tuplestore().

◆ INIT_CATS

#define INIT_CATS   64

Definition at line 126 of file tablefunc.c.

Referenced by load_categories_hash().

◆ INT32_STRLEN

#define INT32_STRLEN   12

Definition at line 116 of file tablefunc.c.

Referenced by build_tuplestore_recursively().

◆ MAX_CATNAME_LEN

#define MAX_CATNAME_LEN   NAMEDATALEN

Definition at line 125 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 94 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:1186

Definition at line 103 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 111 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 1202 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().

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

◆ compatConnectbyTupleDescs()

static void compatConnectbyTupleDescs ( TupleDesc  tupdesc1,
TupleDesc  tupdesc2 
)
static

Definition at line 1496 of file tablefunc.c.

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

Referenced by build_tuplestore_recursively().

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

◆ compatCrosstabTupleDescs()

static bool compatCrosstabTupleDescs ( TupleDesc  tupdesc1,
TupleDesc  tupdesc2 
)
static

Definition at line 1551 of file tablefunc.c.

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

Referenced by crosstab().

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

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

◆ connectby_text()

Datum connectby_text ( PG_FUNCTION_ARGS  )

Definition at line 987 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().

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

◆ connectby_text_serial()

Datum connectby_text_serial ( PG_FUNCTION_ARGS  )

Definition at line 1065 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().

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

◆ crosstab()

Datum crosstab ( PG_FUNCTION_ARGS  )

Definition at line 348 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().

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

◆ crosstab_hash()

Datum crosstab_hash ( PG_FUNCTION_ARGS  )

Definition at line 633 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().

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

◆ get_crosstab_tuplestore()

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

Definition at line 793 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().

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

◆ get_normal_pair()

static void get_normal_pair ( float8 x1,
float8 x2 
)
static

Definition at line 277 of file tablefunc.c.

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

Referenced by normal_rand().

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

◆ load_categories_hash()

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

Definition at line 703 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, HASHCTL::hcxt, i, INIT_CATS, HASHCTL::keysize, MAX_CATNAME_LEN, MemoryContextSwitchTo(), MemSet, 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().

704 {
706  HASHCTL ctl;
707  int ret;
708  uint64 proc;
709  MemoryContext SPIcontext;
710 
711  /* initialize the category hash table */
712  MemSet(&ctl, 0, sizeof(ctl));
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_SYNTAX_ERROR),
749  errmsg("provided \"categories\" SQL must " \
750  "return 1 column of at least one row")));
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_SYNTAX_ERROR),
766  errmsg("provided \"categories\" SQL must " \
767  "not return NULL values")));
768 
769  SPIcontext = MemoryContextSwitchTo(per_query_ctx);
770 
771  catdesc = (crosstab_cat_desc *) palloc(sizeof(crosstab_cat_desc));
772  catdesc->catname = catname;
773  catdesc->attidx = i;
774 
775  /* Add the proc description block to the hashtable */
776  crosstab_HashTableInsert(crosstab_hash, catdesc);
777 
778  MemoryContextSwitchTo(SPIcontext);
779  }
780  }
781 
782  if (SPI_finish() != SPI_OK_FINISH)
783  /* internal error */
784  elog(ERROR, "load_categories_hash: SPI_finish() failed");
785 
786  return crosstab_hash;
787 }
#define MAX_CATNAME_LEN
Definition: tablefunc.c:125
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
MemoryContext hcxt
Definition: hsearch.h:78
int SPI_connect(void)
Definition: spi.c:89
int SPI_finish(void)
Definition: spi.c:176
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Size entrysize
Definition: hsearch.h:73
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:610
#define MemSet(start, val, len)
Definition: c.h:971
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:996
Definition: dynahash.c:210
#define ERROR
Definition: elog.h:43
#define INIT_CATS
Definition: tablefunc.c:126
struct crosstab_hashent crosstab_HashEnt
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:318
Size keysize
Definition: hsearch.h:72
TupleDesc tupdesc
Definition: spi.h:25
#define SPI_OK_SELECT
Definition: spi.h:57
#define ereport(elevel,...)
Definition: elog.h:144
Datum crosstab_hash(PG_FUNCTION_ARGS)
Definition: tablefunc.c:633
#define SPI_OK_FINISH
Definition: spi.h:54
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
#define crosstab_HashTableInsert(HASHTAB, CATDESC)
Definition: tablefunc.c:142
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:496

◆ normal_rand()

Datum normal_rand ( PG_FUNCTION_ARGS  )

Definition at line 173 of file tablefunc.c.

References FuncCallContext::call_cntr, normal_rand_fctx::carry_val, Float8GetDatum(), get_normal_pair(), FuncCallContext::max_calls, normal_rand_fctx::mean, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, palloc(), PG_GETARG_FLOAT8, PG_GETARG_UINT32, 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.

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  /* create a function context for cross-call persistence */
189  funcctx = SRF_FIRSTCALL_INIT();
190 
191  /*
192  * switch to memory context appropriate for multiple function calls
193  */
194  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
195 
196  /* total number of tuples to be returned */
197  funcctx->max_calls = PG_GETARG_UINT32(0);
198 
199  /* allocate memory for user context */
200  fctx = (normal_rand_fctx *) palloc(sizeof(normal_rand_fctx));
201 
202  /*
203  * Use fctx to keep track of upper and lower bounds from call to call.
204  * It will also be used to carry over the spare value we get from the
205  * Box-Muller algorithm so that we only actually calculate a new value
206  * every other call.
207  */
208  fctx->mean = PG_GETARG_FLOAT8(1);
209  fctx->stddev = PG_GETARG_FLOAT8(2);
210  fctx->carry_val = 0;
211  fctx->use_carry = false;
212 
213  funcctx->user_fctx = fctx;
214 
215  MemoryContextSwitchTo(oldcontext);
216  }
217 
218  /* stuff done on every call of the function */
219  funcctx = SRF_PERCALL_SETUP();
220 
221  call_cntr = funcctx->call_cntr;
222  max_calls = funcctx->max_calls;
223  fctx = funcctx->user_fctx;
224  mean = fctx->mean;
225  stddev = fctx->stddev;
226  carry_val = fctx->carry_val;
227  use_carry = fctx->use_carry;
228 
229  if (call_cntr < max_calls) /* do when there is more left to send */
230  {
231  float8 result;
232 
233  if (use_carry)
234  {
235  /*
236  * reset use_carry and use second value obtained on last pass
237  */
238  fctx->use_carry = false;
239  result = carry_val;
240  }
241  else
242  {
243  float8 normval_1;
244  float8 normval_2;
245 
246  /* Get the next two normal values */
247  get_normal_pair(&normval_1, &normval_2);
248 
249  /* use the first */
250  result = mean + (stddev * normval_1);
251 
252  /* and save the second */
253  fctx->carry_val = mean + (stddev * normval_2);
254  fctx->use_carry = true;
255  }
256 
257  /* send the result */
258  SRF_RETURN_NEXT(funcctx, Float8GetDatum(result));
259  }
260  else
261  /* do when there is no more left */
262  SRF_RETURN_DONE(funcctx);
263 }
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:281
uint64 call_cntr
Definition: funcapi.h:65
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:270
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
Datum Float8GetDatum(float8 X)
Definition: fmgr.c:1710
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
static void get_normal_pair(float8 *x1, float8 *x2)
Definition: tablefunc.c:277
double float8
Definition: c.h:491
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
uint64 max_calls
Definition: funcapi.h:74
float8 carry_val
Definition: tablefunc.c:90
#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 1421 of file tablefunc.c.

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

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

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 46 of file tablefunc.c.