PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
functions.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/functions.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parse_func.h"
#include "storage/proc.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
Include dependency graph for functions.c:

Go to the source code of this file.

Data Structures

struct  DR_sqlfunction
 
struct  execution_state
 
struct  SQLFunctionCache
 
struct  SQLFunctionParseInfo
 

Typedefs

typedef struct execution_state execution_state
 
typedef SQLFunctionCacheSQLFunctionCachePtr
 
typedef struct SQLFunctionParseInfo SQLFunctionParseInfo
 

Enumerations

enum  ExecStatus { F_EXEC_START, F_EXEC_RUN, F_EXEC_DONE }
 

Functions

static Nodesql_fn_param_ref (ParseState *pstate, ParamRef *pref)
 
static Nodesql_fn_post_column_ref (ParseState *pstate, ColumnRef *cref, Node *var)
 
static Nodesql_fn_make_param (SQLFunctionParseInfoPtr pinfo, int paramno, int location)
 
static Nodesql_fn_resolve_param_name (SQLFunctionParseInfoPtr pinfo, const char *paramname, int location)
 
static Listinit_execution_state (List *queryTree_list, SQLFunctionCachePtr fcache, bool lazyEvalOK)
 
static void init_sql_fcache (FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
 
static void postquel_start (execution_state *es, SQLFunctionCachePtr fcache)
 
static bool postquel_getnext (execution_state *es, SQLFunctionCachePtr fcache)
 
static void postquel_end (execution_state *es)
 
static void postquel_sub_params (SQLFunctionCachePtr fcache, FunctionCallInfo fcinfo)
 
static Datum postquel_get_single_result (TupleTableSlot *slot, FunctionCallInfo fcinfo, SQLFunctionCachePtr fcache, MemoryContext resultcontext)
 
static void sql_exec_error_callback (void *arg)
 
static void ShutdownSQLFunction (Datum arg)
 
static void sqlfunction_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
static bool sqlfunction_receive (TupleTableSlot *slot, DestReceiver *self)
 
static void sqlfunction_shutdown (DestReceiver *self)
 
static void sqlfunction_destroy (DestReceiver *self)
 
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info (HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
 
void sql_fn_parser_setup (struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
 
Datum fmgr_sql (PG_FUNCTION_ARGS)
 
bool check_sql_fn_retval (Oid func_id, Oid rettype, List *queryTreeList, bool *modifyTargetList, JunkFilter **junkFilter)
 
DestReceiverCreateSQLFunctionDestReceiver (void)
 

Typedef Documentation

Enumeration Type Documentation

enum ExecStatus
Enumerator
F_EXEC_START 
F_EXEC_RUN 
F_EXEC_DONE 

Definition at line 58 of file functions.c.

Function Documentation

bool check_sql_fn_retval ( Oid  func_id,
Oid  rettype,
List queryTreeList,
bool modifyTargetList,
JunkFilter **  junkFilter 
)

Definition at line 1527 of file functions.c.

References Assert, AssertArg, tupleDesc::attrs, Query::canSetTag, castNode, CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, COERCE_IMPLICIT_CAST, Query::commandType, CreateTupleDescCopy(), ereport, errcode(), errdetail(), errmsg(), ERROR, ExecCleanTargetListLength(), ExecInitJunkFilter(), ExecInitJunkFilterConversion(), TargetEntry::expr, exprType(), format_type_be(), get_func_result_type(), get_typcollation(), get_typtype(), INT4OID, InvalidOid, IsBinaryCoercible(), IsPolymorphicType, lappend(), lfirst, linitial, list_concat(), makeConst(), makeRelabelType(), makeTargetEntry(), tupleDesc::natts, NIL, NULL, parse(), RECORDOID, TargetEntry::resjunk, TargetEntry::resno, TargetEntry::ressortgroupref, Query::returningList, Query::setOperations, Query::targetList, TYPEFUNC_COMPOSITE, TYPTYPE_BASE, TYPTYPE_COMPOSITE, TYPTYPE_DOMAIN, TYPTYPE_ENUM, TYPTYPE_RANGE, and VOIDOID.

Referenced by fmgr_sql_validator(), init_sql_fcache(), inline_function(), and inline_set_returning_function().

1530 {
1531  Query *parse;
1532  List **tlist_ptr;
1533  List *tlist;
1534  int tlistlen;
1535  char fn_typtype;
1536  Oid restype;
1537  ListCell *lc;
1538 
1539  AssertArg(!IsPolymorphicType(rettype));
1540 
1541  if (modifyTargetList)
1542  *modifyTargetList = false; /* initialize for no change */
1543  if (junkFilter)
1544  *junkFilter = NULL; /* initialize in case of VOID result */
1545 
1546  /*
1547  * Find the last canSetTag query in the list. This isn't necessarily the
1548  * last parsetree, because rule rewriting can insert queries after what
1549  * the user wrote.
1550  */
1551  parse = NULL;
1552  foreach(lc, queryTreeList)
1553  {
1554  Query *q = castNode(Query, lfirst(lc));
1555 
1556  if (q->canSetTag)
1557  parse = q;
1558  }
1559 
1560  /*
1561  * If it's a plain SELECT, it returns whatever the targetlist says.
1562  * Otherwise, if it's INSERT/UPDATE/DELETE with RETURNING, it returns
1563  * that. Otherwise, the function return type must be VOID.
1564  *
1565  * Note: eventually replace this test with QueryReturnsTuples? We'd need
1566  * a more general method of determining the output type, though. Also, it
1567  * seems too dangerous to consider FETCH or EXECUTE as returning a
1568  * determinable rowtype, since they depend on relatively short-lived
1569  * entities.
1570  */
1571  if (parse &&
1572  parse->commandType == CMD_SELECT)
1573  {
1574  tlist_ptr = &parse->targetList;
1575  tlist = parse->targetList;
1576  }
1577  else if (parse &&
1578  (parse->commandType == CMD_INSERT ||
1579  parse->commandType == CMD_UPDATE ||
1580  parse->commandType == CMD_DELETE) &&
1581  parse->returningList)
1582  {
1583  tlist_ptr = &parse->returningList;
1584  tlist = parse->returningList;
1585  }
1586  else
1587  {
1588  /* Empty function body, or last statement is a utility command */
1589  if (rettype != VOIDOID)
1590  ereport(ERROR,
1591  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1592  errmsg("return type mismatch in function declared to return %s",
1593  format_type_be(rettype)),
1594  errdetail("Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.")));
1595  return false;
1596  }
1597 
1598  /*
1599  * OK, check that the targetlist returns something matching the declared
1600  * type. (We used to insist that the declared type not be VOID in this
1601  * case, but that makes it hard to write a void function that exits after
1602  * calling another void function. Instead, we insist that the tlist
1603  * return void ... so void is treated as if it were a scalar type below.)
1604  */
1605 
1606  /*
1607  * Count the non-junk entries in the result targetlist.
1608  */
1609  tlistlen = ExecCleanTargetListLength(tlist);
1610 
1611  fn_typtype = get_typtype(rettype);
1612 
1613  if (fn_typtype == TYPTYPE_BASE ||
1614  fn_typtype == TYPTYPE_DOMAIN ||
1615  fn_typtype == TYPTYPE_ENUM ||
1616  fn_typtype == TYPTYPE_RANGE ||
1617  rettype == VOIDOID)
1618  {
1619  /*
1620  * For scalar-type returns, the target list must have exactly one
1621  * non-junk entry, and its type must agree with what the user
1622  * declared; except we allow binary-compatible types too.
1623  */
1624  TargetEntry *tle;
1625 
1626  if (tlistlen != 1)
1627  ereport(ERROR,
1628  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1629  errmsg("return type mismatch in function declared to return %s",
1630  format_type_be(rettype)),
1631  errdetail("Final statement must return exactly one column.")));
1632 
1633  /* We assume here that non-junk TLEs must come first in tlists */
1634  tle = (TargetEntry *) linitial(tlist);
1635  Assert(!tle->resjunk);
1636 
1637  restype = exprType((Node *) tle->expr);
1638  if (!IsBinaryCoercible(restype, rettype))
1639  ereport(ERROR,
1640  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1641  errmsg("return type mismatch in function declared to return %s",
1642  format_type_be(rettype)),
1643  errdetail("Actual return type is %s.",
1644  format_type_be(restype))));
1645  if (modifyTargetList && restype != rettype)
1646  {
1647  tle->expr = (Expr *) makeRelabelType(tle->expr,
1648  rettype,
1649  -1,
1650  get_typcollation(rettype),
1652  /* Relabel is dangerous if TLE is a sort/group or setop column */
1653  if (tle->ressortgroupref != 0 || parse->setOperations)
1654  *modifyTargetList = true;
1655  }
1656 
1657  /* Set up junk filter if needed */
1658  if (junkFilter)
1659  *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1660  }
1661  else if (fn_typtype == TYPTYPE_COMPOSITE || rettype == RECORDOID)
1662  {
1663  /* Returns a rowtype */
1664  TupleDesc tupdesc;
1665  int tupnatts; /* physical number of columns in tuple */
1666  int tuplogcols; /* # of nondeleted columns in tuple */
1667  int colindex; /* physical column index */
1668  List *newtlist; /* new non-junk tlist entries */
1669  List *junkattrs; /* new junk tlist entries */
1670 
1671  /*
1672  * If the target list is of length 1, and the type of the varnode in
1673  * the target list matches the declared return type, this is okay.
1674  * This can happen, for example, where the body of the function is
1675  * 'SELECT func2()', where func2 has the same composite return type as
1676  * the function that's calling it.
1677  *
1678  * XXX Note that if rettype is RECORD, the IsBinaryCoercible check
1679  * will succeed for any composite restype. For the moment we rely on
1680  * runtime type checking to catch any discrepancy, but it'd be nice to
1681  * do better at parse time.
1682  */
1683  if (tlistlen == 1)
1684  {
1685  TargetEntry *tle = (TargetEntry *) linitial(tlist);
1686 
1687  Assert(!tle->resjunk);
1688  restype = exprType((Node *) tle->expr);
1689  if (IsBinaryCoercible(restype, rettype))
1690  {
1691  if (modifyTargetList && restype != rettype)
1692  {
1693  tle->expr = (Expr *) makeRelabelType(tle->expr,
1694  rettype,
1695  -1,
1696  get_typcollation(rettype),
1698  /* Relabel is dangerous if sort/group or setop column */
1699  if (tle->ressortgroupref != 0 || parse->setOperations)
1700  *modifyTargetList = true;
1701  }
1702  /* Set up junk filter if needed */
1703  if (junkFilter)
1704  *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1705  return false; /* NOT returning whole tuple */
1706  }
1707  }
1708 
1709  /* Is the rowtype fixed, or determined only at runtime? */
1710  if (get_func_result_type(func_id, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1711  {
1712  /*
1713  * Assume we are returning the whole tuple. Crosschecking against
1714  * what the caller expects will happen at runtime.
1715  */
1716  if (junkFilter)
1717  *junkFilter = ExecInitJunkFilter(tlist, false, NULL);
1718  return true;
1719  }
1720  Assert(tupdesc);
1721 
1722  /*
1723  * Verify that the targetlist matches the return tuple type. We scan
1724  * the non-deleted attributes to ensure that they match the datatypes
1725  * of the non-resjunk columns. For deleted attributes, insert NULL
1726  * result columns if the caller asked for that.
1727  */
1728  tupnatts = tupdesc->natts;
1729  tuplogcols = 0; /* we'll count nondeleted cols as we go */
1730  colindex = 0;
1731  newtlist = NIL; /* these are only used if modifyTargetList */
1732  junkattrs = NIL;
1733 
1734  foreach(lc, tlist)
1735  {
1736  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1737  Form_pg_attribute attr;
1738  Oid tletype;
1739  Oid atttype;
1740 
1741  if (tle->resjunk)
1742  {
1743  if (modifyTargetList)
1744  junkattrs = lappend(junkattrs, tle);
1745  continue;
1746  }
1747 
1748  do
1749  {
1750  colindex++;
1751  if (colindex > tupnatts)
1752  ereport(ERROR,
1753  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1754  errmsg("return type mismatch in function declared to return %s",
1755  format_type_be(rettype)),
1756  errdetail("Final statement returns too many columns.")));
1757  attr = tupdesc->attrs[colindex - 1];
1758  if (attr->attisdropped && modifyTargetList)
1759  {
1760  Expr *null_expr;
1761 
1762  /* The type of the null we insert isn't important */
1763  null_expr = (Expr *) makeConst(INT4OID,
1764  -1,
1765  InvalidOid,
1766  sizeof(int32),
1767  (Datum) 0,
1768  true, /* isnull */
1769  true /* byval */ );
1770  newtlist = lappend(newtlist,
1771  makeTargetEntry(null_expr,
1772  colindex,
1773  NULL,
1774  false));
1775  /* NULL insertion is dangerous in a setop */
1776  if (parse->setOperations)
1777  *modifyTargetList = true;
1778  }
1779  } while (attr->attisdropped);
1780  tuplogcols++;
1781 
1782  tletype = exprType((Node *) tle->expr);
1783  atttype = attr->atttypid;
1784  if (!IsBinaryCoercible(tletype, atttype))
1785  ereport(ERROR,
1786  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1787  errmsg("return type mismatch in function declared to return %s",
1788  format_type_be(rettype)),
1789  errdetail("Final statement returns %s instead of %s at column %d.",
1790  format_type_be(tletype),
1791  format_type_be(atttype),
1792  tuplogcols)));
1793  if (modifyTargetList)
1794  {
1795  if (tletype != atttype)
1796  {
1797  tle->expr = (Expr *) makeRelabelType(tle->expr,
1798  atttype,
1799  -1,
1800  get_typcollation(atttype),
1802  /* Relabel is dangerous if sort/group or setop column */
1803  if (tle->ressortgroupref != 0 || parse->setOperations)
1804  *modifyTargetList = true;
1805  }
1806  tle->resno = colindex;
1807  newtlist = lappend(newtlist, tle);
1808  }
1809  }
1810 
1811  /* remaining columns in tupdesc had better all be dropped */
1812  for (colindex++; colindex <= tupnatts; colindex++)
1813  {
1814  if (!tupdesc->attrs[colindex - 1]->attisdropped)
1815  ereport(ERROR,
1816  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1817  errmsg("return type mismatch in function declared to return %s",
1818  format_type_be(rettype)),
1819  errdetail("Final statement returns too few columns.")));
1820  if (modifyTargetList)
1821  {
1822  Expr *null_expr;
1823 
1824  /* The type of the null we insert isn't important */
1825  null_expr = (Expr *) makeConst(INT4OID,
1826  -1,
1827  InvalidOid,
1828  sizeof(int32),
1829  (Datum) 0,
1830  true, /* isnull */
1831  true /* byval */ );
1832  newtlist = lappend(newtlist,
1833  makeTargetEntry(null_expr,
1834  colindex,
1835  NULL,
1836  false));
1837  /* NULL insertion is dangerous in a setop */
1838  if (parse->setOperations)
1839  *modifyTargetList = true;
1840  }
1841  }
1842 
1843  if (modifyTargetList)
1844  {
1845  /* ensure resjunk columns are numbered correctly */
1846  foreach(lc, junkattrs)
1847  {
1848  TargetEntry *tle = (TargetEntry *) lfirst(lc);
1849 
1850  tle->resno = colindex++;
1851  }
1852  /* replace the tlist with the modified one */
1853  *tlist_ptr = list_concat(newtlist, junkattrs);
1854  }
1855 
1856  /* Set up junk filter if needed */
1857  if (junkFilter)
1858  *junkFilter = ExecInitJunkFilterConversion(tlist,
1859  CreateTupleDescCopy(tupdesc),
1860  NULL);
1861 
1862  /* Report that we are returning entire tuple result */
1863  return true;
1864  }
1865  else
1866  ereport(ERROR,
1867  (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
1868  errmsg("return type %s is not supported for SQL functions",
1869  format_type_be(rettype))));
1870 
1871  return false;
1872 }
#define NIL
Definition: pg_list.h:69
#define TYPTYPE_DOMAIN
Definition: pg_type.h:710
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
#define TYPTYPE_BASE
Definition: pg_type.h:708
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:709
JunkFilter * ExecInitJunkFilterConversion(List *targetList, TupleDesc cleanTupType, TupleTableSlot *slot)
Definition: execJunk.c:136
Form_pg_attribute * attrs
Definition: tupdesc.h:74
#define INT4OID
Definition: pg_type.h:316
Definition: nodes.h:508
int errcode(int sqlerrcode)
Definition: elog.c:575
char get_typtype(Oid typid)
Definition: lsyscache.c:2347
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
List * list_concat(List *list1, List *list2)
Definition: list.c:321
unsigned int Oid
Definition: postgres_ext.h:31
Const * makeConst(Oid consttype, int32 consttypmod, Oid constcollid, int constlen, Datum constvalue, bool constisnull, bool constbyval)
Definition: makefuncs.c:296
int natts
Definition: tupdesc.h:73
signed int int32
Definition: c.h:253
List * targetList
Definition: parsenodes.h:131
bool resjunk
Definition: primnodes.h:1337
#define linitial(l)
Definition: pg_list.h:110
#define VOIDOID
Definition: pg_type.h:678
#define ERROR
Definition: elog.h:43
#define IsPolymorphicType(typid)
Definition: pg_type.h:733
RelabelType * makeRelabelType(Expr *arg, Oid rtype, int32 rtypmod, Oid rcollid, CoercionForm rformat)
Definition: makefuncs.c:399
JunkFilter * ExecInitJunkFilter(List *targetList, bool hasoid, TupleTableSlot *slot)
Definition: execJunk.c:61
int errdetail(const char *fmt,...)
Definition: elog.c:873
AttrNumber resno
Definition: primnodes.h:1331
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:184
#define RECORDOID
Definition: pg_type.h:668
List * returningList
Definition: parsenodes.h:135
#define ereport(elevel, rest)
Definition: elog.h:122
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Definition: makefuncs.c:235
#define AssertArg(condition)
Definition: c.h:673
int ExecCleanTargetListLength(List *targetlist)
Definition: execQual.c:5132
bool IsBinaryCoercible(Oid srctype, Oid targettype)
List * lappend(List *list, void *datum)
Definition: list.c:128
TypeFuncClass get_func_result_type(Oid functionId, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:269
#define TYPTYPE_RANGE
Definition: pg_type.h:713
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2749
CmdType commandType
Definition: parsenodes.h:103
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
Expr * expr
Definition: primnodes.h:1330
bool canSetTag
Definition: parsenodes.h:109
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
Node * setOperations
Definition: parsenodes.h:154
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define TYPTYPE_ENUM
Definition: pg_type.h:711
Index ressortgroupref
Definition: primnodes.h:1333
Definition: pg_list.h:45
static struct subre * parse(struct vars *, int, int, struct state *, struct state *)
Definition: regcomp.c:651
DestReceiver* CreateSQLFunctionDestReceiver ( void  )

Definition at line 1879 of file functions.c.

References DestSQLFunction, palloc0(), sqlfunction_destroy(), sqlfunction_receive(), sqlfunction_shutdown(), and sqlfunction_startup().

Referenced by CreateDestReceiver().

1880 {
1881  DR_sqlfunction *self = (DR_sqlfunction *) palloc0(sizeof(DR_sqlfunction));
1882 
1883  self->pub.receiveSlot = sqlfunction_receive;
1884  self->pub.rStartup = sqlfunction_startup;
1885  self->pub.rShutdown = sqlfunction_shutdown;
1886  self->pub.rDestroy = sqlfunction_destroy;
1887  self->pub.mydest = DestSQLFunction;
1888 
1889  /* private fields will be set by postquel_start */
1890 
1891  return (DestReceiver *) self;
1892 }
static void sqlfunction_destroy(DestReceiver *self)
Definition: functions.c:1933
static void sqlfunction_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
Definition: functions.c:1898
static bool sqlfunction_receive(TupleTableSlot *slot, DestReceiver *self)
Definition: functions.c:1907
void * palloc0(Size size)
Definition: mcxt.c:920
static void sqlfunction_shutdown(DestReceiver *self)
Definition: functions.c:1924
Datum fmgr_sql ( PG_FUNCTION_ARGS  )

Definition at line 985 of file functions.c.

References ReturnSetInfo::allowedModes, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, CommandCounterIncrement(), CreateTupleDescCopy(), ReturnSetInfo::econtext, elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, ExprEndResult, ExprMultipleResult, F_EXEC_DONE, F_EXEC_START, SQLFunctionCache::fcontext, SQLFunctionCache::func_state, GetTransactionSnapshot(), init_sql_fcache(), IsA, ReturnSetInfo::isDone, JunkFilter::jf_cleanTupType, JunkFilter::jf_resultSlot, SQLFunctionCache::junkFilter, execution_state::lazyEval, SQLFunctionCache::lazyEval, lfirst, lnext, PGPROC::lxid, SQLFunctionCache::lxid, MemoryContextDelete(), MemoryContextSwitchTo(), MyProc, execution_state::next, NULL, PG_GET_COLLATION, PointerGetDatum, PopActiveSnapshot(), postquel_end(), postquel_get_single_result(), postquel_getnext(), postquel_start(), postquel_sub_params(), ErrorContextCallback::previous, PushActiveSnapshot(), execution_state::qd, SQLFunctionCache::readonly_func, RegisterExprContextCallback(), SQLFunctionCache::rettype, ReturnSetInfo::returnMode, SQLFunctionCache::returnsSet, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SFRM_Materialize_Preferred, SFRM_Materialize_Random, SFRM_ValuePerCall, SQLFunctionCache::shutdown_reg, ShutdownSQLFunction(), QueryDesc::snapshot, sql_exec_error_callback(), execution_state::status, SubTransactionIsActive(), SQLFunctionCache::subxid, SQLFunctionCache::tstore, tuplestore_begin_heap(), tuplestore_clear(), tuplestore_gettupleslot(), UnregisterExprContextCallback(), UpdateActiveSnapshotCommandId(), VOIDOID, and work_mem.

Referenced by fmgr_info_cxt_security().

986 {
987  SQLFunctionCachePtr fcache;
988  ErrorContextCallback sqlerrcontext;
989  MemoryContext oldcontext;
990  bool randomAccess;
991  bool lazyEvalOK;
992  bool is_first;
993  bool pushed_snapshot;
994  execution_state *es;
995  TupleTableSlot *slot;
996  Datum result;
997  List *eslist;
998  ListCell *eslc;
999 
1000  /*
1001  * Setup error traceback support for ereport()
1002  */
1003  sqlerrcontext.callback = sql_exec_error_callback;
1004  sqlerrcontext.arg = fcinfo->flinfo;
1005  sqlerrcontext.previous = error_context_stack;
1006  error_context_stack = &sqlerrcontext;
1007 
1008  /* Check call context */
1009  if (fcinfo->flinfo->fn_retset)
1010  {
1011  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1012 
1013  /*
1014  * For simplicity, we require callers to support both set eval modes.
1015  * There are cases where we must use one or must use the other, and
1016  * it's not really worthwhile to postpone the check till we know. But
1017  * note we do not require caller to provide an expectedDesc.
1018  */
1019  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1020  (rsi->allowedModes & SFRM_ValuePerCall) == 0 ||
1021  (rsi->allowedModes & SFRM_Materialize) == 0)
1022  ereport(ERROR,
1023  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1024  errmsg("set-valued function called in context that cannot accept a set")));
1025  randomAccess = rsi->allowedModes & SFRM_Materialize_Random;
1026  lazyEvalOK = !(rsi->allowedModes & SFRM_Materialize_Preferred);
1027  }
1028  else
1029  {
1030  randomAccess = false;
1031  lazyEvalOK = true;
1032  }
1033 
1034  /*
1035  * Initialize fcache (build plans) if first time through; or re-initialize
1036  * if the cache is stale.
1037  */
1038  fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1039 
1040  if (fcache != NULL)
1041  {
1042  if (fcache->lxid != MyProc->lxid ||
1043  !SubTransactionIsActive(fcache->subxid))
1044  {
1045  /* It's stale; unlink and delete */
1046  fcinfo->flinfo->fn_extra = NULL;
1047  MemoryContextDelete(fcache->fcontext);
1048  fcache = NULL;
1049  }
1050  }
1051 
1052  if (fcache == NULL)
1053  {
1054  init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK);
1055  fcache = (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra;
1056  }
1057 
1058  /*
1059  * Switch to context in which the fcache lives. This ensures that our
1060  * tuplestore etc will have sufficient lifetime. The sub-executor is
1061  * responsible for deleting per-tuple information. (XXX in the case of a
1062  * long-lived FmgrInfo, this policy represents more memory leakage, but
1063  * it's not entirely clear where to keep stuff instead.)
1064  */
1065  oldcontext = MemoryContextSwitchTo(fcache->fcontext);
1066 
1067  /*
1068  * Find first unfinished query in function, and note whether it's the
1069  * first query.
1070  */
1071  eslist = fcache->func_state;
1072  es = NULL;
1073  is_first = true;
1074  foreach(eslc, eslist)
1075  {
1076  es = (execution_state *) lfirst(eslc);
1077 
1078  while (es && es->status == F_EXEC_DONE)
1079  {
1080  is_first = false;
1081  es = es->next;
1082  }
1083 
1084  if (es)
1085  break;
1086  }
1087 
1088  /*
1089  * Convert params to appropriate format if starting a fresh execution. (If
1090  * continuing execution, we can re-use prior params.)
1091  */
1092  if (is_first && es && es->status == F_EXEC_START)
1093  postquel_sub_params(fcache, fcinfo);
1094 
1095  /*
1096  * Build tuplestore to hold results, if we don't have one already. Note
1097  * it's in the query-lifespan context.
1098  */
1099  if (!fcache->tstore)
1100  fcache->tstore = tuplestore_begin_heap(randomAccess, false, work_mem);
1101 
1102  /*
1103  * Execute each command in the function one after another until we either
1104  * run out of commands or get a result row from a lazily-evaluated SELECT.
1105  *
1106  * Notes about snapshot management:
1107  *
1108  * In a read-only function, we just use the surrounding query's snapshot.
1109  *
1110  * In a non-read-only function, we rely on the fact that we'll never
1111  * suspend execution between queries of the function: the only reason to
1112  * suspend execution before completion is if we are returning a row from a
1113  * lazily-evaluated SELECT. So, when first entering this loop, we'll
1114  * either start a new query (and push a fresh snapshot) or re-establish
1115  * the active snapshot from the existing query descriptor. If we need to
1116  * start a new query in a subsequent execution of the loop, either we need
1117  * a fresh snapshot (and pushed_snapshot is false) or the existing
1118  * snapshot is on the active stack and we can just bump its command ID.
1119  */
1120  pushed_snapshot = false;
1121  while (es)
1122  {
1123  bool completed;
1124 
1125  if (es->status == F_EXEC_START)
1126  {
1127  /*
1128  * If not read-only, be sure to advance the command counter for
1129  * each command, so that all work to date in this transaction is
1130  * visible. Take a new snapshot if we don't have one yet,
1131  * otherwise just bump the command ID in the existing snapshot.
1132  */
1133  if (!fcache->readonly_func)
1134  {
1136  if (!pushed_snapshot)
1137  {
1139  pushed_snapshot = true;
1140  }
1141  else
1143  }
1144 
1145  postquel_start(es, fcache);
1146  }
1147  else if (!fcache->readonly_func && !pushed_snapshot)
1148  {
1149  /* Re-establish active snapshot when re-entering function */
1151  pushed_snapshot = true;
1152  }
1153 
1154  completed = postquel_getnext(es, fcache);
1155 
1156  /*
1157  * If we ran the command to completion, we can shut it down now. Any
1158  * row(s) we need to return are safely stashed in the tuplestore, and
1159  * we want to be sure that, for example, AFTER triggers get fired
1160  * before we return anything. Also, if the function doesn't return
1161  * set, we can shut it down anyway because it must be a SELECT and we
1162  * don't care about fetching any more result rows.
1163  */
1164  if (completed || !fcache->returnsSet)
1165  postquel_end(es);
1166 
1167  /*
1168  * Break from loop if we didn't shut down (implying we got a
1169  * lazily-evaluated row). Otherwise we'll press on till the whole
1170  * function is done, relying on the tuplestore to keep hold of the
1171  * data to eventually be returned. This is necessary since an
1172  * INSERT/UPDATE/DELETE RETURNING that sets the result might be
1173  * followed by additional rule-inserted commands, and we want to
1174  * finish doing all those commands before we return anything.
1175  */
1176  if (es->status != F_EXEC_DONE)
1177  break;
1178 
1179  /*
1180  * Advance to next execution_state, which might be in the next list.
1181  */
1182  es = es->next;
1183  while (!es)
1184  {
1185  eslc = lnext(eslc);
1186  if (!eslc)
1187  break; /* end of function */
1188 
1189  es = (execution_state *) lfirst(eslc);
1190 
1191  /*
1192  * Flush the current snapshot so that we will take a new one for
1193  * the new query list. This ensures that new snaps are taken at
1194  * original-query boundaries, matching the behavior of interactive
1195  * execution.
1196  */
1197  if (pushed_snapshot)
1198  {
1200  pushed_snapshot = false;
1201  }
1202  }
1203  }
1204 
1205  /*
1206  * The tuplestore now contains whatever row(s) we are supposed to return.
1207  */
1208  if (fcache->returnsSet)
1209  {
1210  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1211 
1212  if (es)
1213  {
1214  /*
1215  * If we stopped short of being done, we must have a lazy-eval
1216  * row.
1217  */
1218  Assert(es->lazyEval);
1219  /* Re-use the junkfilter's output slot to fetch back the tuple */
1220  Assert(fcache->junkFilter);
1221  slot = fcache->junkFilter->jf_resultSlot;
1222  if (!tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1223  elog(ERROR, "failed to fetch lazy-eval tuple");
1224  /* Extract the result as a datum, and copy out from the slot */
1225  result = postquel_get_single_result(slot, fcinfo,
1226  fcache, oldcontext);
1227  /* Clear the tuplestore, but keep it for next time */
1228  /* NB: this might delete the slot's content, but we don't care */
1229  tuplestore_clear(fcache->tstore);
1230 
1231  /*
1232  * Let caller know we're not finished.
1233  */
1234  rsi->isDone = ExprMultipleResult;
1235 
1236  /*
1237  * Ensure we will get shut down cleanly if the exprcontext is not
1238  * run to completion.
1239  */
1240  if (!fcache->shutdown_reg)
1241  {
1244  PointerGetDatum(fcache));
1245  fcache->shutdown_reg = true;
1246  }
1247  }
1248  else if (fcache->lazyEval)
1249  {
1250  /*
1251  * We are done with a lazy evaluation. Clean up.
1252  */
1253  tuplestore_clear(fcache->tstore);
1254 
1255  /*
1256  * Let caller know we're finished.
1257  */
1258  rsi->isDone = ExprEndResult;
1259 
1260  fcinfo->isnull = true;
1261  result = (Datum) 0;
1262 
1263  /* Deregister shutdown callback, if we made one */
1264  if (fcache->shutdown_reg)
1265  {
1268  PointerGetDatum(fcache));
1269  fcache->shutdown_reg = false;
1270  }
1271  }
1272  else
1273  {
1274  /*
1275  * We are done with a non-lazy evaluation. Return whatever is in
1276  * the tuplestore. (It is now caller's responsibility to free the
1277  * tuplestore when done.)
1278  */
1280  rsi->setResult = fcache->tstore;
1281  fcache->tstore = NULL;
1282  /* must copy desc because execQual will free it */
1283  if (fcache->junkFilter)
1285 
1286  fcinfo->isnull = true;
1287  result = (Datum) 0;
1288 
1289  /* Deregister shutdown callback, if we made one */
1290  if (fcache->shutdown_reg)
1291  {
1294  PointerGetDatum(fcache));
1295  fcache->shutdown_reg = false;
1296  }
1297  }
1298  }
1299  else
1300  {
1301  /*
1302  * Non-set function. If we got a row, return it; else return NULL.
1303  */
1304  if (fcache->junkFilter)
1305  {
1306  /* Re-use the junkfilter's output slot to fetch back the tuple */
1307  slot = fcache->junkFilter->jf_resultSlot;
1308  if (tuplestore_gettupleslot(fcache->tstore, true, false, slot))
1309  result = postquel_get_single_result(slot, fcinfo,
1310  fcache, oldcontext);
1311  else
1312  {
1313  fcinfo->isnull = true;
1314  result = (Datum) 0;
1315  }
1316  }
1317  else
1318  {
1319  /* Should only get here for VOID functions */
1320  Assert(fcache->rettype == VOIDOID);
1321  fcinfo->isnull = true;
1322  result = (Datum) 0;
1323  }
1324 
1325  /* Clear the tuplestore, but keep it for next time */
1326  tuplestore_clear(fcache->tstore);
1327  }
1328 
1329  /* Pop snapshot if we have pushed one */
1330  if (pushed_snapshot)
1332 
1333  /*
1334  * If we've gone through every command in the function, we are done. Reset
1335  * the execution states to start over again on next call.
1336  */
1337  if (es == NULL)
1338  {
1339  foreach(eslc, fcache->func_state)
1340  {
1341  es = (execution_state *) lfirst(eslc);
1342  while (es)
1343  {
1344  es->status = F_EXEC_START;
1345  es = es->next;
1346  }
1347  }
1348  }
1349 
1350  error_context_stack = sqlerrcontext.previous;
1351 
1352  MemoryContextSwitchTo(oldcontext);
1353 
1354  return result;
1355 }
bool SubTransactionIsActive(SubTransactionId subxid)
Definition: xact.c:662
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:776
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext fcontext
Definition: functions.c:121
PGPROC * MyProc
Definition: proc.c:67
void UnregisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:897
#define PointerGetDatum(X)
Definition: postgres.h:564
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ExecStatus status
Definition: functions.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:296
JunkFilter * junkFilter
Definition: functions.c:111
static void postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
Definition: functions.c:779
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
Tuplestorestate * tstore
Definition: functions.c:109
struct ErrorContextCallback * previous
Definition: elog.h:238
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
#define PG_GET_COLLATION()
Definition: fmgr.h:155
Snapshot snapshot
Definition: execdesc.h:39
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define VOIDOID
Definition: pg_type.h:678
#define ERROR
Definition: elog.h:43
static void sql_exec_error_callback(void *arg)
Definition: functions.c:1362
void tuplestore_clear(Tuplestorestate *state)
Definition: tuplestore.c:416
static void postquel_end(execution_state *es)
Definition: functions.c:872
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
List * func_state
Definition: functions.c:119
LocalTransactionId lxid
Definition: functions.c:124
TupleDesc jf_cleanTupType
Definition: execnodes.h:294
static void ShutdownSQLFunction(Datum arg)
Definition: functions.c:1444
#define lnext(lc)
Definition: pg_list.h:105
#define ereport(elevel, rest)
Definition: elog.h:122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:316
uintptr_t Datum
Definition: postgres.h:374
void CommandCounterIncrement(void)
Definition: xact.c:921
int work_mem
Definition: globals.c:112
QueryDesc * qd
Definition: functions.c:70
int allowedModes
Definition: execnodes.h:199
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
static Datum postquel_get_single_result(TupleTableSlot *slot, FunctionCallInfo fcinfo, SQLFunctionCachePtr fcache, MemoryContext resultcontext)
Definition: functions.c:942
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1061
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define lfirst(lc)
Definition: pg_list.h:106
void RegisterExprContextCallback(ExprContext *econtext, ExprContextCallbackFunction function, Datum arg)
Definition: execUtils.c:871
Tuplestorestate * setResult
Definition: execnodes.h:204
ExprContext * econtext
Definition: execnodes.h:197
SQLFunctionCache * SQLFunctionCachePtr
Definition: functions.c:128
TupleDesc setDesc
Definition: execnodes.h:205
void(* callback)(void *arg)
Definition: elog.h:239
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
ExprDoneCond isDone
Definition: execnodes.h:202
SubTransactionId subxid
Definition: functions.c:125
struct execution_state * next
Definition: functions.c:65
static bool postquel_getnext(execution_state *es, SQLFunctionCachePtr fcache)
Definition: functions.c:839
Definition: pg_list.h:45
static void postquel_sub_params(SQLFunctionCachePtr fcache, FunctionCallInfo fcinfo)
Definition: functions.c:892
static void init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK)
Definition: functions.c:593
LocalTransactionId lxid
Definition: proc.h:95
static List * init_execution_state ( List queryTree_list,
SQLFunctionCachePtr  fcache,
bool  lazyEvalOK 
)
static

Definition at line 472 of file functions.c.

References PlannedStmt::canSetTag, Query::canSetTag, castNode, CMD_SELECT, CMD_UTILITY, CommandIsReadOnly(), PlannedStmt::commandType, Query::commandType, CreateCommandTag(), CURSOR_OPT_PARALLEL_OK, ereport, errcode(), errmsg(), ERROR, F_EXEC_START, PlannedStmt::hasModifyingCTE, IsA, IsInParallelMode(), SQLFunctionCache::junkFilter, lappend(), execution_state::lazyEval, SQLFunctionCache::lazyEval, lfirst, makeNode, execution_state::next, NIL, NULL, palloc(), pg_plan_query(), PreventCommandIfParallelMode(), execution_state::qd, SQLFunctionCache::readonly_func, execution_state::setsResult, execution_state::status, execution_state::stmt, PlannedStmt::stmt_len, Query::stmt_len, PlannedStmt::stmt_location, Query::stmt_location, PlannedStmt::utilityStmt, and Query::utilityStmt.

Referenced by init_sql_fcache().

475 {
476  List *eslist = NIL;
477  execution_state *lasttages = NULL;
478  ListCell *lc1;
479 
480  foreach(lc1, queryTree_list)
481  {
482  List *qtlist = castNode(List, lfirst(lc1));
483  execution_state *firstes = NULL;
484  execution_state *preves = NULL;
485  ListCell *lc2;
486 
487  foreach(lc2, qtlist)
488  {
489  Query *queryTree = castNode(Query, lfirst(lc2));
490  PlannedStmt *stmt;
491  execution_state *newes;
492 
493  /* Plan the query if needed */
494  if (queryTree->commandType == CMD_UTILITY)
495  {
496  /* Utility commands require no planning. */
497  stmt = makeNode(PlannedStmt);
498  stmt->commandType = CMD_UTILITY;
499  stmt->canSetTag = queryTree->canSetTag;
500  stmt->utilityStmt = queryTree->utilityStmt;
501  stmt->stmt_location = queryTree->stmt_location;
502  stmt->stmt_len = queryTree->stmt_len;
503  }
504  else
505  stmt = pg_plan_query(queryTree,
507  NULL);
508 
509  /*
510  * Precheck all commands for validity in a function. This should
511  * generally match the restrictions spi.c applies.
512  */
513  if (stmt->commandType == CMD_UTILITY)
514  {
515  if (IsA(stmt->utilityStmt, CopyStmt) &&
516  ((CopyStmt *) stmt->utilityStmt)->filename == NULL)
517  ereport(ERROR,
518  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
519  errmsg("cannot COPY to/from client in a SQL function")));
520 
521  if (IsA(stmt->utilityStmt, TransactionStmt))
522  ereport(ERROR,
523  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
524  /* translator: %s is a SQL statement name */
525  errmsg("%s is not allowed in a SQL function",
526  CreateCommandTag(stmt->utilityStmt))));
527  }
528 
529  if (fcache->readonly_func && !CommandIsReadOnly(stmt))
530  ereport(ERROR,
531  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
532  /* translator: %s is a SQL statement name */
533  errmsg("%s is not allowed in a non-volatile function",
534  CreateCommandTag((Node *) stmt))));
535 
536  if (IsInParallelMode() && !CommandIsReadOnly(stmt))
538 
539  /* OK, build the execution_state for this query */
540  newes = (execution_state *) palloc(sizeof(execution_state));
541  if (preves)
542  preves->next = newes;
543  else
544  firstes = newes;
545 
546  newes->next = NULL;
547  newes->status = F_EXEC_START;
548  newes->setsResult = false; /* might change below */
549  newes->lazyEval = false; /* might change below */
550  newes->stmt = stmt;
551  newes->qd = NULL;
552 
553  if (queryTree->canSetTag)
554  lasttages = newes;
555 
556  preves = newes;
557  }
558 
559  eslist = lappend(eslist, firstes);
560  }
561 
562  /*
563  * Mark the last canSetTag query as delivering the function result; then,
564  * if it is a plain SELECT, mark it for lazy evaluation. If it's not a
565  * SELECT we must always run it to completion.
566  *
567  * Note: at some point we might add additional criteria for whether to use
568  * lazy eval. However, we should prefer to use it whenever the function
569  * doesn't return set, since fetching more than one row is useless in that
570  * case.
571  *
572  * Note: don't set setsResult if the function returns VOID, as evidenced
573  * by not having made a junkfilter. This ensures we'll throw away any
574  * output from a utility statement that check_sql_fn_retval deemed to not
575  * have output.
576  */
577  if (lasttages && fcache->junkFilter)
578  {
579  lasttages->setsResult = true;
580  if (lazyEvalOK &&
581  lasttages->stmt->commandType == CMD_SELECT &&
582  !lasttages->stmt->hasModifyingCTE)
583  fcache->lazyEval = lasttages->lazyEval = true;
584  }
585 
586  return eslist;
587 }
#define NIL
Definition: pg_list.h:69
bool CommandIsReadOnly(PlannedStmt *pstmt)
Definition: utility.c:96
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
int stmt_location
Definition: parsenodes.h:171
void PreventCommandIfParallelMode(const char *cmdname)
Definition: utility.c:253
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
Definition: nodes.h:508
ExecStatus status
Definition: functions.c:66
int errcode(int sqlerrcode)
Definition: elog.c:575
JunkFilter * junkFilter
Definition: functions.c:111
Node * utilityStmt
Definition: parsenodes.h:111
int stmt_len
Definition: plannodes.h:84
bool IsInParallelMode(void)
Definition: xact.c:912
#define ERROR
Definition: elog.h:43
int stmt_location
Definition: plannodes.h:83
Node * utilityStmt
Definition: plannodes.h:80
#define ereport(elevel, rest)
Definition: elog.h:122
const char * CreateCommandTag(Node *parsetree)
Definition: utility.c:2012
List * lappend(List *list, void *datum)
Definition: list.c:128
bool canSetTag
Definition: plannodes.h:53
CmdType commandType
Definition: plannodes.h:45
QueryDesc * qd
Definition: functions.c:70
CmdType commandType
Definition: parsenodes.h:103
#define makeNode(_type_)
Definition: nodes.h:556
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
bool hasModifyingCTE
Definition: plannodes.h:51
bool canSetTag
Definition: parsenodes.h:109
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2528
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
struct execution_state * next
Definition: functions.c:65
PlannedStmt * stmt
Definition: functions.c:69
Definition: pg_list.h:45
int stmt_len
Definition: parsenodes.h:172
PlannedStmt * pg_plan_query(Query *querytree, int cursorOptions, ParamListInfo boundParams)
Definition: postgres.c:781
static void init_sql_fcache ( FmgrInfo finfo,
Oid  collation,
bool  lazyEvalOK 
)
static

Definition at line 593 of file functions.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Anum_pg_proc_prosrc, BlessTupleDesc(), castNode, check_sql_fn_retval(), elog, ereport, errcode(), errmsg(), ERROR, SQLFunctionCache::fcontext, FmgrInfo::fn_expr, FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, SQLFunctionCache::fname, format_type_be(), SQLFunctionCache::func_state, get_fn_expr_rettype(), get_typlenbyval(), GetCurrentSubTransactionId(), GETSTRUCT, HeapTupleIsValid, init_execution_state(), InvalidOid, IsPolymorphicType, JunkFilter::jf_resultSlot, SQLFunctionCache::junkFilter, lappend(), lfirst, list_concat(), list_copy(), PGPROC::lxid, SQLFunctionCache::lxid, MemoryContextSwitchTo(), MyProc, NameStr, NIL, NULL, ObjectIdGetDatum, palloc0(), pg_analyze_and_rewrite_params(), pg_parse_query(), SQLFunctionCache::pinfo, prepare_sql_fn_parse_info(), PROCOID, PROVOLATILE_VOLATILE, pstrdup(), SQLFunctionCache::readonly_func, ReleaseSysCache(), SQLFunctionCache::rettype, SQLFunctionCache::returnsSet, SQLFunctionCache::returnsTuple, SearchSysCache1, sql_fn_parser_setup(), SQLFunctionCache::src, SQLFunctionCache::subxid, SysCacheGetAttr(), TextDatumGetCString, TupleTableSlot::tts_tupleDescriptor, SQLFunctionCache::typbyval, type_is_rowtype(), and SQLFunctionCache::typlen.

Referenced by fmgr_sql().

594 {
595  Oid foid = finfo->fn_oid;
596  MemoryContext fcontext;
597  MemoryContext oldcontext;
598  Oid rettype;
599  HeapTuple procedureTuple;
600  Form_pg_proc procedureStruct;
601  SQLFunctionCachePtr fcache;
602  List *raw_parsetree_list;
603  List *queryTree_list;
604  List *flat_query_list;
605  ListCell *lc;
606  Datum tmp;
607  bool isNull;
608 
609  /*
610  * Create memory context that holds all the SQLFunctionCache data. It
611  * must be a child of whatever context holds the FmgrInfo.
612  */
613  fcontext = AllocSetContextCreate(finfo->fn_mcxt,
614  "SQL function data",
616 
617  oldcontext = MemoryContextSwitchTo(fcontext);
618 
619  /*
620  * Create the struct proper, link it to fcontext and fn_extra. Once this
621  * is done, we'll be able to recover the memory after failure, even if the
622  * FmgrInfo is long-lived.
623  */
624  fcache = (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache));
625  fcache->fcontext = fcontext;
626  finfo->fn_extra = (void *) fcache;
627 
628  /*
629  * get the procedure tuple corresponding to the given function Oid
630  */
631  procedureTuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(foid));
632  if (!HeapTupleIsValid(procedureTuple))
633  elog(ERROR, "cache lookup failed for function %u", foid);
634  procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
635 
636  /*
637  * copy function name immediately for use by error reporting callback
638  */
639  fcache->fname = pstrdup(NameStr(procedureStruct->proname));
640 
641  /*
642  * get the result type from the procedure tuple, and check for polymorphic
643  * result type; if so, find out the actual result type.
644  */
645  rettype = procedureStruct->prorettype;
646 
647  if (IsPolymorphicType(rettype))
648  {
649  rettype = get_fn_expr_rettype(finfo);
650  if (rettype == InvalidOid) /* this probably should not happen */
651  ereport(ERROR,
652  (errcode(ERRCODE_DATATYPE_MISMATCH),
653  errmsg("could not determine actual result type for function declared to return type %s",
654  format_type_be(procedureStruct->prorettype))));
655  }
656 
657  fcache->rettype = rettype;
658 
659  /* Fetch the typlen and byval info for the result type */
660  get_typlenbyval(rettype, &fcache->typlen, &fcache->typbyval);
661 
662  /* Remember whether we're returning setof something */
663  fcache->returnsSet = procedureStruct->proretset;
664 
665  /* Remember if function is STABLE/IMMUTABLE */
666  fcache->readonly_func =
667  (procedureStruct->provolatile != PROVOLATILE_VOLATILE);
668 
669  /*
670  * We need the actual argument types to pass to the parser. Also make
671  * sure that parameter symbols are considered to have the function's
672  * resolved input collation.
673  */
674  fcache->pinfo = prepare_sql_fn_parse_info(procedureTuple,
675  finfo->fn_expr,
676  collation);
677 
678  /*
679  * And of course we need the function body text.
680  */
681  tmp = SysCacheGetAttr(PROCOID,
682  procedureTuple,
684  &isNull);
685  if (isNull)
686  elog(ERROR, "null prosrc for function %u", foid);
687  fcache->src = TextDatumGetCString(tmp);
688 
689  /*
690  * Parse and rewrite the queries in the function text. Use sublists to
691  * keep track of the original query boundaries. But we also build a
692  * "flat" list of the rewritten queries to pass to check_sql_fn_retval.
693  * This is because the last canSetTag query determines the result type
694  * independently of query boundaries --- and it might not be in the last
695  * sublist, for example if the last query rewrites to DO INSTEAD NOTHING.
696  * (It might not be unreasonable to throw an error in such a case, but
697  * this is the historical behavior and it doesn't seem worth changing.)
698  *
699  * Note: since parsing and planning is done in fcontext, we will generate
700  * a lot of cruft that lives as long as the fcache does. This is annoying
701  * but we'll not worry about it until the module is rewritten to use
702  * plancache.c.
703  */
704  raw_parsetree_list = pg_parse_query(fcache->src);
705 
706  queryTree_list = NIL;
707  flat_query_list = NIL;
708  foreach(lc, raw_parsetree_list)
709  {
710  RawStmt *parsetree = castNode(RawStmt, lfirst(lc));
711  List *queryTree_sublist;
712 
713  queryTree_sublist = pg_analyze_and_rewrite_params(parsetree,
714  fcache->src,
716  fcache->pinfo);
717  queryTree_list = lappend(queryTree_list, queryTree_sublist);
718  flat_query_list = list_concat(flat_query_list,
719  list_copy(queryTree_sublist));
720  }
721 
722  /*
723  * Check that the function returns the type it claims to. Although in
724  * simple cases this was already done when the function was defined, we
725  * have to recheck because database objects used in the function's queries
726  * might have changed type. We'd have to do it anyway if the function had
727  * any polymorphic arguments.
728  *
729  * Note: we set fcache->returnsTuple according to whether we are returning
730  * the whole tuple result or just a single column. In the latter case we
731  * clear returnsTuple because we need not act different from the scalar
732  * result case, even if it's a rowtype column. (However, we have to force
733  * lazy eval mode in that case; otherwise we'd need extra code to expand
734  * the rowtype column into multiple columns, since we have no way to
735  * notify the caller that it should do that.)
736  *
737  * check_sql_fn_retval will also construct a JunkFilter we can use to
738  * coerce the returned rowtype to the desired form (unless the result type
739  * is VOID, in which case there's nothing to coerce to).
740  */
741  fcache->returnsTuple = check_sql_fn_retval(foid,
742  rettype,
743  flat_query_list,
744  NULL,
745  &fcache->junkFilter);
746 
747  if (fcache->returnsTuple)
748  {
749  /* Make sure output rowtype is properly blessed */
751  }
752  else if (fcache->returnsSet && type_is_rowtype(fcache->rettype))
753  {
754  /*
755  * Returning rowtype as if it were scalar --- materialize won't work.
756  * Right now it's sufficient to override any caller preference for
757  * materialize mode, but to add more smarts in init_execution_state
758  * about this, we'd probably need a three-way flag instead of bool.
759  */
760  lazyEvalOK = true;
761  }
762 
763  /* Finally, plan the queries */
764  fcache->func_state = init_execution_state(queryTree_list,
765  fcache,
766  lazyEvalOK);
767 
768  /* Mark fcache with time of creation to show it's valid */
769  fcache->lxid = MyProc->lxid;
771 
772  ReleaseSysCache(procedureTuple);
773 
774  MemoryContextSwitchTo(oldcontext);
775 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
MemoryContext fn_mcxt
Definition: fmgr.h:62
MemoryContext fcontext
Definition: functions.c:121
#define castNode(_type_, nodeptr)
Definition: nodes.h:577
PGPROC * MyProc
Definition: proc.c:67
char * pstrdup(const char *in)
Definition: mcxt.c:1165
void sql_fn_parser_setup(struct ParseState *pstate, SQLFunctionParseInfoPtr pinfo)
Definition: functions.c:273
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Anum_pg_proc_prosrc
Definition: pg_proc.h:115
List * list_copy(const List *oldlist)
Definition: list.c:1160
int errcode(int sqlerrcode)
Definition: elog.c:575
TupleTableSlot * jf_resultSlot
Definition: execnodes.h:296
bool check_sql_fn_retval(Oid func_id, Oid rettype, List *queryTreeList, bool *modifyTargetList, JunkFilter **junkFilter)
Definition: functions.c:1527
JunkFilter * junkFilter
Definition: functions.c:111
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
List * list_concat(List *list1, List *list2)
Definition: list.c:321
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define IsPolymorphicType(typid)
Definition: pg_type.h:733
List * pg_parse_query(const char *query_string)
Definition: postgres.c:602
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:1031
List * func_state
Definition: functions.c:119
LocalTransactionId lxid
Definition: functions.c:124
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:66
static List * init_execution_state(List *queryTree_list, SQLFunctionCachePtr fcache, bool lazyEvalOK)
Definition: functions.c:472
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2372
#define ereport(elevel, rest)
Definition: elog.h:122
List * pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg)
Definition: postgres.c:679
SQLFunctionParseInfoPtr pinfo
Definition: functions.c:96
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5374
List * lappend(List *list, void *datum)
Definition: list.c:128
TupleDesc tts_tupleDescriptor
Definition: tuptable.h:121
#define TextDatumGetCString(d)
Definition: builtins.h:91
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
Oid get_fn_expr_rettype(FmgrInfo *flinfo)
Definition: fmgr.c:2198
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:56
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:648
fmNodePtr fn_expr
Definition: fmgr.h:63
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:1969
void * fn_extra
Definition: fmgr.h:61
SQLFunctionCache * SQLFunctionCachePtr
Definition: functions.c:128
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:495
#define elog
Definition: elog.h:219
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple, Node *call_expr, Oid inputCollation)
Definition: functions.c:184
SubTransactionId subxid
Definition: functions.c:125
Definition: pg_list.h:45
LocalTransactionId lxid
Definition: proc.h:95
static void postquel_end ( execution_state es)
static

Definition at line 872 of file functions.c.

References CMD_UTILITY, QueryDesc::dest, ExecutorEnd(), ExecutorFinish(), F_EXEC_DONE, FreeQueryDesc(), NULL, QueryDesc::operation, execution_state::qd, _DestReceiver::rDestroy, and execution_state::status.

Referenced by fmgr_sql(), and ShutdownSQLFunction().

873 {
874  /* mark status done to ensure we don't do ExecutorEnd twice */
875  es->status = F_EXEC_DONE;
876 
877  /* Utility commands don't need Executor. */
878  if (es->qd->operation != CMD_UTILITY)
879  {
880  ExecutorFinish(es->qd);
881  ExecutorEnd(es->qd);
882  }
883 
884  (*es->qd->dest->rDestroy) (es->qd->dest);
885 
886  FreeQueryDesc(es->qd);
887  es->qd = NULL;
888 }
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:126
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:100
ExecStatus status
Definition: functions.c:66
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:439
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:379
CmdType operation
Definition: execdesc.h:36
QueryDesc * qd
Definition: functions.c:70
#define NULL
Definition: c.h:226
DestReceiver * dest
Definition: execdesc.h:41
static Datum postquel_get_single_result ( TupleTableSlot slot,
FunctionCallInfo  fcinfo,
SQLFunctionCachePtr  fcache,
MemoryContext  resultcontext 
)
static

Definition at line 942 of file functions.c.

References datumCopy(), ExecFetchSlotTupleDatum(), FunctionCallInfoData::isnull, MemoryContextSwitchTo(), SQLFunctionCache::returnsTuple, slot_getattr(), SQLFunctionCache::typbyval, SQLFunctionCache::typlen, and value.

Referenced by fmgr_sql().

946 {
947  Datum value;
948  MemoryContext oldcontext;
949 
950  /*
951  * Set up to return the function value. For pass-by-reference datatypes,
952  * be sure to allocate the result in resultcontext, not the current memory
953  * context (which has query lifespan). We can't leave the data in the
954  * TupleTableSlot because we intend to clear the slot before returning.
955  */
956  oldcontext = MemoryContextSwitchTo(resultcontext);
957 
958  if (fcache->returnsTuple)
959  {
960  /* We must return the whole tuple as a Datum. */
961  fcinfo->isnull = false;
962  value = ExecFetchSlotTupleDatum(slot);
963  }
964  else
965  {
966  /*
967  * Returning a scalar, which we have to extract from the first column
968  * of the SELECT result, and then copy into result context if needed.
969  */
970  value = slot_getattr(slot, 1, &(fcinfo->isnull));
971 
972  if (!fcinfo->isnull)
973  value = datumCopy(value, fcache->typbyval, fcache->typlen);
974  }
975 
976  MemoryContextSwitchTo(oldcontext);
977 
978  return value;
979 }
static struct @76 value
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:128
uintptr_t Datum
Definition: postgres.h:374
Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: heaptuple.c:1143
Datum ExecFetchSlotTupleDatum(TupleTableSlot *slot)
Definition: execTuples.c:698
static bool postquel_getnext ( execution_state es,
SQLFunctionCachePtr  fcache 
)
static

Definition at line 839 of file functions.c.

References CMD_UTILITY, QueryDesc::dest, EState::es_processed, QueryDesc::estate, ExecutorRun(), ForwardScanDirection, execution_state::lazyEval, NULL, QueryDesc::operation, QueryDesc::params, QueryDesc::plannedstmt, PROCESS_UTILITY_QUERY, ProcessUtility(), execution_state::qd, and SQLFunctionCache::src.

Referenced by fmgr_sql().

840 {
841  bool result;
842 
843  if (es->qd->operation == CMD_UTILITY)
844  {
846  fcache->src,
848  es->qd->params,
849  es->qd->dest,
850  NULL);
851  result = true; /* never stops early */
852  }
853  else
854  {
855  /* Run regular commands to completion unless lazyEval */
856  uint64 count = (es->lazyEval) ? 1 : 0;
857 
858  ExecutorRun(es->qd, ForwardScanDirection, count);
859 
860  /*
861  * If we requested run to completion OR there was no tuple returned,
862  * command must be complete.
863  */
864  result = (count == 0 || es->qd->estate->es_processed == 0);
865  }
866 
867  return result;
868 }
EState * estate
Definition: execdesc.h:47
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
Definition: execMain.c:285
ParamListInfo params
Definition: execdesc.h:42
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params, DestReceiver *dest, char *completionTag)
Definition: utility.c:332
CmdType operation
Definition: execdesc.h:36
QueryDesc * qd
Definition: functions.c:70
#define NULL
Definition: c.h:226
uint64 es_processed
Definition: execnodes.h:403
DestReceiver * dest
Definition: execdesc.h:41
PlannedStmt * plannedstmt
Definition: execdesc.h:37
static void postquel_start ( execution_state es,
SQLFunctionCachePtr  fcache 
)
static

Definition at line 779 of file functions.c.

References ActiveSnapshotSet(), Assert, CMD_UTILITY, CreateDestReceiver(), CreateQueryDesc(), CurrentMemoryContext, DR_sqlfunction::cxt, DestSQLFunction, EXEC_FLAG_SKIP_TRIGGERS, ExecutorStart(), F_EXEC_RUN, DR_sqlfunction::filter, GetActiveSnapshot(), InvalidSnapshot, SQLFunctionCache::junkFilter, execution_state::lazyEval, _DestReceiver::mydest, None_Receiver, NULL, QueryDesc::operation, SQLFunctionCache::paramLI, DR_sqlfunction::pub, execution_state::qd, execution_state::setsResult, SQLFunctionCache::src, execution_state::status, execution_state::stmt, DR_sqlfunction::tstore, and SQLFunctionCache::tstore.

Referenced by fmgr_sql().

780 {
781  DestReceiver *dest;
782 
783  Assert(es->qd == NULL);
784 
785  /* Caller should have ensured a suitable snapshot is active */
787 
788  /*
789  * If this query produces the function result, send its output to the
790  * tuplestore; else discard any output.
791  */
792  if (es->setsResult)
793  {
794  DR_sqlfunction *myState;
795 
797  /* pass down the needed info to the dest receiver routines */
798  myState = (DR_sqlfunction *) dest;
799  Assert(myState->pub.mydest == DestSQLFunction);
800  myState->tstore = fcache->tstore;
801  myState->cxt = CurrentMemoryContext;
802  myState->filter = fcache->junkFilter;
803  }
804  else
805  dest = None_Receiver;
806 
807  es->qd = CreateQueryDesc(es->stmt,
808  fcache->src,
811  dest,
812  fcache->paramLI, 0);
813 
814  /* Utility commands don't need Executor. */
815  if (es->qd->operation != CMD_UTILITY)
816  {
817  /*
818  * In lazyEval mode, do not let the executor set up an AfterTrigger
819  * context. This is necessary not just an optimization, because we
820  * mustn't exit from the function execution with a stacked
821  * AfterTrigger level still active. We are careful not to select
822  * lazyEval mode for any statement that could possibly queue triggers.
823  */
824  int eflags;
825 
826  if (es->lazyEval)
827  eflags = EXEC_FLAG_SKIP_TRIGGERS;
828  else
829  eflags = 0; /* default run-to-completion flags */
830  ExecutorStart(es->qd, eflags);
831  }
832 
833  es->status = F_EXEC_RUN;
834 }
DestReceiver pub
Definition: functions.c:43
CommandDest mydest
Definition: dest.h:128
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:138
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:834
ExecStatus status
Definition: functions.c:66
JunkFilter * junkFilter
Definition: functions.c:111
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, int instrument_options)
Definition: pquery.c:66
Tuplestorestate * tstore
Definition: functions.c:109
DestReceiver * None_Receiver
Definition: dest.c:91
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:109
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:846
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
CmdType operation
Definition: execdesc.h:36
#define InvalidSnapshot
Definition: snapshot.h:25
QueryDesc * qd
Definition: functions.c:70
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
ParamListInfo paramLI
Definition: functions.c:107
MemoryContext cxt
Definition: functions.c:45
#define EXEC_FLAG_SKIP_TRIGGERS
Definition: executor.h:62
JunkFilter * filter
Definition: functions.c:46
PlannedStmt * stmt
Definition: functions.c:69
Tuplestorestate * tstore
Definition: functions.c:44
static void postquel_sub_params ( SQLFunctionCachePtr  fcache,
FunctionCallInfo  fcinfo 
)
static

Definition at line 892 of file functions.c.

References FunctionCallInfoData::arg, FunctionCallInfoData::argnull, SQLFunctionParseInfo::argtypes, Assert, i, ParamExternData::isnull, FunctionCallInfoData::nargs, NULL, ParamListInfoData::numParams, offsetof, palloc(), ParamListInfoData::paramFetch, ParamListInfoData::paramFetchArg, SQLFunctionCache::paramLI, ParamListInfoData::paramMask, ParamListInfoData::params, ParamListInfoData::parserSetup, ParamListInfoData::parserSetupArg, ParamExternData::pflags, SQLFunctionCache::pinfo, ParamExternData::ptype, and ParamExternData::value.

Referenced by fmgr_sql().

894 {
895  int nargs = fcinfo->nargs;
896 
897  if (nargs > 0)
898  {
899  ParamListInfo paramLI;
900  int i;
901 
902  if (fcache->paramLI == NULL)
903  {
904  paramLI = (ParamListInfo)
906  nargs * sizeof(ParamExternData));
907  /* we have static list of params, so no hooks needed */
908  paramLI->paramFetch = NULL;
909  paramLI->paramFetchArg = NULL;
910  paramLI->parserSetup = NULL;
911  paramLI->parserSetupArg = NULL;
912  paramLI->numParams = nargs;
913  paramLI->paramMask = NULL;
914  fcache->paramLI = paramLI;
915  }
916  else
917  {
918  paramLI = fcache->paramLI;
919  Assert(paramLI->numParams == nargs);
920  }
921 
922  for (i = 0; i < nargs; i++)
923  {
924  ParamExternData *prm = &paramLI->params[i];
925 
926  prm->value = fcinfo->arg[i];
927  prm->isnull = fcinfo->argnull[i];
928  prm->pflags = 0;
929  prm->ptype = fcache->pinfo->argtypes[i];
930  }
931  }
932  else
933  fcache->paramLI = NULL;
934 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:76
Datum value
Definition: params.h:56
void * parserSetupArg
Definition: params.h:73
ParserSetupHook parserSetup
Definition: params.h:72
struct ParamListInfoData * ParamListInfo
Definition: params.h:62
void * paramFetchArg
Definition: params.h:71
ParamFetchHook paramFetch
Definition: params.h:70
struct Bitmapset * paramMask
Definition: params.h:75
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:78
SQLFunctionParseInfoPtr pinfo
Definition: functions.c:96
struct ParamExternData ParamExternData
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
ParamListInfo paramLI
Definition: functions.c:107
uint16 pflags
Definition: params.h:58
void * palloc(Size size)
Definition: mcxt.c:891
int i
bool isnull
Definition: params.h:57
#define offsetof(type, field)
Definition: c.h:551
SQLFunctionParseInfoPtr prepare_sql_fn_parse_info ( HeapTuple  procedureTuple,
Node call_expr,
Oid  inputCollation 
)

Definition at line 184 of file functions.c.

References Anum_pg_proc_proargmodes, Anum_pg_proc_proargnames, SQLFunctionParseInfo::argnames, SQLFunctionParseInfo::argtypes, SQLFunctionParseInfo::collation, ereport, errcode(), errmsg(), ERROR, SQLFunctionParseInfo::fname, format_type_be(), get_call_expr_argtype(), get_func_input_arg_names(), GETSTRUCT, InvalidOid, IsPolymorphicType, NameStr, SQLFunctionParseInfo::nargs, NULL, palloc(), palloc0(), PointerGetDatum, PROCNAMEARGSNSP, pstrdup(), and SysCacheGetAttr().

Referenced by fmgr_sql_validator(), init_sql_fcache(), inline_function(), and inline_set_returning_function().

187 {
189  Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
190  int nargs;
191 
193 
194  /* Function's name (only) can be used to qualify argument names */
195  pinfo->fname = pstrdup(NameStr(procedureStruct->proname));
196 
197  /* Save the function's input collation */
198  pinfo->collation = inputCollation;
199 
200  /*
201  * Copy input argument types from the pg_proc entry, then resolve any
202  * polymorphic types.
203  */
204  pinfo->nargs = nargs = procedureStruct->pronargs;
205  if (nargs > 0)
206  {
207  Oid *argOidVect;
208  int argnum;
209 
210  argOidVect = (Oid *) palloc(nargs * sizeof(Oid));
211  memcpy(argOidVect,
212  procedureStruct->proargtypes.values,
213  nargs * sizeof(Oid));
214 
215  for (argnum = 0; argnum < nargs; argnum++)
216  {
217  Oid argtype = argOidVect[argnum];
218 
219  if (IsPolymorphicType(argtype))
220  {
221  argtype = get_call_expr_argtype(call_expr, argnum);
222  if (argtype == InvalidOid)
223  ereport(ERROR,
224  (errcode(ERRCODE_DATATYPE_MISMATCH),
225  errmsg("could not determine actual type of argument declared %s",
226  format_type_be(argOidVect[argnum]))));
227  argOidVect[argnum] = argtype;
228  }
229  }
230 
231  pinfo->argtypes = argOidVect;
232  }
233 
234  /*
235  * Collect names of arguments, too, if any
236  */
237  if (nargs > 0)
238  {
239  Datum proargnames;
240  Datum proargmodes;
241  int n_arg_names;
242  bool isNull;
243 
244  proargnames = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
246  &isNull);
247  if (isNull)
248  proargnames = PointerGetDatum(NULL); /* just to be sure */
249 
250  proargmodes = SysCacheGetAttr(PROCNAMEARGSNSP, procedureTuple,
252  &isNull);
253  if (isNull)
254  proargmodes = PointerGetDatum(NULL); /* just to be sure */
255 
256  n_arg_names = get_func_input_arg_names(proargnames, proargmodes,
257  &pinfo->argnames);
258 
259  /* Paranoia: ignore the result if too few array entries */
260  if (n_arg_names < nargs)
261  pinfo->argnames = NULL;
262  }
263  else
264  pinfo->argnames = NULL;
265 
266  return pinfo;
267 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define PointerGetDatum(X)
Definition: postgres.h:564
char * pstrdup(const char *in)
Definition: mcxt.c:1165
struct SQLFunctionParseInfo * SQLFunctionParseInfoPtr
Definition: functions.h:21
int errcode(int sqlerrcode)
Definition: elog.c:575
Oid get_call_expr_argtype(Node *expr, int argnum)
Definition: fmgr.c:2239
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
#define IsPolymorphicType(typid)
Definition: pg_type.h:733
#define Anum_pg_proc_proargmodes
Definition: pg_proc.h:111
#define ereport(elevel, rest)
Definition: elog.h:122
#define Anum_pg_proc_proargnames
Definition: pg_proc.h:112
int get_func_input_arg_names(Datum proargnames, Datum proargmodes, char ***arg_names)
Definition: funcapi.c:933
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:495
static void ShutdownSQLFunction ( Datum  arg)
static

Definition at line 1444 of file functions.c.

References DatumGetPointer, F_EXEC_RUN, F_EXEC_START, SQLFunctionCache::func_state, lfirst, execution_state::next, NULL, PopActiveSnapshot(), postquel_end(), PushActiveSnapshot(), execution_state::qd, SQLFunctionCache::readonly_func, SQLFunctionCache::shutdown_reg, QueryDesc::snapshot, execution_state::status, SQLFunctionCache::tstore, and tuplestore_end().

Referenced by fmgr_sql().

1445 {
1447  execution_state *es;
1448  ListCell *lc;
1449 
1450  foreach(lc, fcache->func_state)
1451  {
1452  es = (execution_state *) lfirst(lc);
1453  while (es)
1454  {
1455  /* Shut down anything still running */
1456  if (es->status == F_EXEC_RUN)
1457  {
1458  /* Re-establish active snapshot for any called functions */
1459  if (!fcache->readonly_func)
1461 
1462  postquel_end(es);
1463 
1464  if (!fcache->readonly_func)
1466  }
1467 
1468  /* Reset states to START in case we're called again */
1469  es->status = F_EXEC_START;
1470  es = es->next;
1471  }
1472  }
1473 
1474  /* Release tuplestore if we have one */
1475  if (fcache->tstore)
1476  tuplestore_end(fcache->tstore);
1477  fcache->tstore = NULL;
1478 
1479  /* execUtils will deregister the callback... */
1480  fcache->shutdown_reg = false;
1481 }
ExecStatus status
Definition: functions.c:66
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
Tuplestorestate * tstore
Definition: functions.c:109
Snapshot snapshot
Definition: execdesc.h:39
static void postquel_end(execution_state *es)
Definition: functions.c:872
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
List * func_state
Definition: functions.c:119
QueryDesc * qd
Definition: functions.c:70
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
void tuplestore_end(Tuplestorestate *state)
Definition: tuplestore.c:450
#define DatumGetPointer(X)
Definition: postgres.h:557
SQLFunctionCache * SQLFunctionCachePtr
Definition: functions.c:128
void * arg
struct execution_state * next
Definition: functions.c:65
static void sql_exec_error_callback ( void *  arg)
static

Definition at line 1362 of file functions.c.

References errcontext, errposition(), FmgrInfo::fn_extra, SQLFunctionCache::fname, SQLFunctionCache::func_state, geterrposition(), internalerrposition(), internalerrquery(), lfirst, execution_state::next, NULL, execution_state::qd, and SQLFunctionCache::src.

Referenced by fmgr_sql().

1363 {
1364  FmgrInfo *flinfo = (FmgrInfo *) arg;
1366  int syntaxerrposition;
1367 
1368  /*
1369  * We can do nothing useful if init_sql_fcache() didn't get as far as
1370  * saving the function name
1371  */
1372  if (fcache == NULL || fcache->fname == NULL)
1373  return;
1374 
1375  /*
1376  * If there is a syntax error position, convert to internal syntax error
1377  */
1378  syntaxerrposition = geterrposition();
1379  if (syntaxerrposition > 0 && fcache->src != NULL)
1380  {
1381  errposition(0);
1382  internalerrposition(syntaxerrposition);
1383  internalerrquery(fcache->src);
1384  }
1385 
1386  /*
1387  * Try to determine where in the function we failed. If there is a query
1388  * with non-null QueryDesc, finger it. (We check this rather than looking
1389  * for F_EXEC_RUN state, so that errors during ExecutorStart or
1390  * ExecutorEnd are blamed on the appropriate query; see postquel_start and
1391  * postquel_end.)
1392  */
1393  if (fcache->func_state)
1394  {
1395  execution_state *es;
1396  int query_num;
1397  ListCell *lc;
1398 
1399  es = NULL;
1400  query_num = 1;
1401  foreach(lc, fcache->func_state)
1402  {
1403  es = (execution_state *) lfirst(lc);
1404  while (es)
1405  {
1406  if (es->qd)
1407  {
1408  errcontext("SQL function \"%s\" statement %d",
1409  fcache->fname, query_num);
1410  break;
1411  }
1412  es = es->next;
1413  }
1414  if (es)
1415  break;
1416  query_num++;
1417  }
1418  if (es == NULL)
1419  {
1420  /*
1421  * couldn't identify a running query; might be function entry,
1422  * function exit, or between queries.
1423  */
1424  errcontext("SQL function \"%s\"", fcache->fname);
1425  }
1426  }
1427  else
1428  {
1429  /*
1430  * Assume we failed during init_sql_fcache(). (It's possible that the
1431  * function actually has an empty body, but in that case we may as
1432  * well report all errors as being "during startup".)
1433  */
1434  errcontext("SQL function \"%s\" during startup", fcache->fname);
1435  }
1436 }
Definition: fmgr.h:53
List * func_state
Definition: functions.c:119
int geterrposition(void)
Definition: elog.c:1257
QueryDesc * qd
Definition: functions.c:70
int internalerrquery(const char *query)
Definition: elog.c:1161
#define NULL
Definition: c.h:226
#define lfirst(lc)
Definition: pg_list.h:106
void * fn_extra
Definition: fmgr.h:61
SQLFunctionCache * SQLFunctionCachePtr
Definition: functions.c:128
#define errcontext
Definition: elog.h:164
void * arg
struct execution_state * next
Definition: functions.c:65
int errposition(int cursorpos)
Definition: elog.c:1125
int internalerrposition(int cursorpos)
Definition: elog.c:1141
static Node * sql_fn_make_param ( SQLFunctionParseInfoPtr  pinfo,
int  paramno,
int  location 
)
static

Definition at line 418 of file functions.c.

References SQLFunctionParseInfo::argtypes, SQLFunctionParseInfo::collation, get_typcollation(), Param::location, makeNode, OidIsValid, PARAM_EXTERN, Param::paramcollid, Param::paramid, Param::paramkind, Param::paramtype, and Param::paramtypmod.

Referenced by sql_fn_param_ref(), and sql_fn_resolve_param_name().

420 {
421  Param *param;
422 
423  param = makeNode(Param);
424  param->paramkind = PARAM_EXTERN;
425  param->paramid = paramno;
426  param->paramtype = pinfo->argtypes[paramno - 1];
427  param->paramtypmod = -1;
428  param->paramcollid = get_typcollation(param->paramtype);
429  param->location = location;
430 
431  /*
432  * If we have a function input collation, allow it to override the
433  * type-derived collation for parameter symbols. (XXX perhaps this should
434  * not happen if the type collation is not default?)
435  */
436  if (OidIsValid(pinfo->collation) && OidIsValid(param->paramcollid))
437  param->paramcollid = pinfo->collation;
438 
439  return (Node *) param;
440 }
ParamKind paramkind
Definition: primnodes.h:222
Definition: nodes.h:508
#define OidIsValid(objectId)
Definition: c.h:534
Oid paramcollid
Definition: primnodes.h:226
int location
Definition: primnodes.h:227
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:2749
int32 paramtypmod
Definition: primnodes.h:225
#define makeNode(_type_)
Definition: nodes.h:556
int paramid
Definition: primnodes.h:223
Oid paramtype
Definition: primnodes.h:224
static Node * sql_fn_param_ref ( ParseState pstate,
ParamRef pref 
)
static

Definition at line 402 of file functions.c.

References ParamRef::location, SQLFunctionParseInfo::nargs, NULL, ParamRef::number, ParseState::p_ref_hook_state, and sql_fn_make_param().

Referenced by sql_fn_parser_setup().

403 {
405  int paramno = pref->number;
406 
407  /* Check parameter number is valid */
408  if (paramno <= 0 || paramno > pinfo->nargs)
409  return NULL; /* unknown parameter number */
410 
411  return sql_fn_make_param(pinfo, paramno, pref->location);
412 }
struct SQLFunctionParseInfo * SQLFunctionParseInfoPtr
Definition: functions.h:21
int number
Definition: parsenodes.h:236
int location
Definition: parsenodes.h:237
#define NULL
Definition: c.h:226
void * p_ref_hook_state
Definition: parse_node.h:206
static Node * sql_fn_make_param(SQLFunctionParseInfoPtr pinfo, int paramno, int location)
Definition: functions.c:418
void sql_fn_parser_setup ( struct ParseState pstate,
SQLFunctionParseInfoPtr  pinfo 
)

Definition at line 273 of file functions.c.

References NULL, ParseState::p_paramref_hook, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseState::p_ref_hook_state, sql_fn_param_ref(), and sql_fn_post_column_ref().

Referenced by fmgr_sql_validator(), init_sql_fcache(), inline_function(), and inline_set_returning_function().

274 {
275  pstate->p_pre_columnref_hook = NULL;
278  /* no need to use p_coerce_param_hook */
279  pstate->p_ref_hook_state = (void *) pinfo;
280 }
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:203
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:204
static Node * sql_fn_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
Definition: functions.c:286
#define NULL
Definition: c.h:226
void * p_ref_hook_state
Definition: parse_node.h:206
static Node * sql_fn_param_ref(ParseState *pstate, ParamRef *pref)
Definition: functions.c:402
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:202
static Node * sql_fn_post_column_ref ( ParseState pstate,
ColumnRef cref,
Node var 
)
static

Definition at line 286 of file functions.c.

References Assert, ColumnRef::fields, SQLFunctionParseInfo::fname, IsA, linitial, list_length(), list_make1, llast, ColumnRef::location, lsecond, lthird, NULL, ParseState::p_ref_hook_state, ParseFuncOrColumn(), sql_fn_resolve_param_name(), and strVal.

Referenced by sql_fn_parser_setup().

287 {
289  int nnames;
290  Node *field1;
291  Node *subfield = NULL;
292  const char *name1;
293  const char *name2 = NULL;
294  Node *param;
295 
296  /*
297  * Never override a table-column reference. This corresponds to
298  * considering the parameter names to appear in a scope outside the
299  * individual SQL commands, which is what we want.
300  */
301  if (var != NULL)
302  return NULL;
303 
304  /*----------
305  * The allowed syntaxes are:
306  *
307  * A A = parameter name
308  * A.B A = function name, B = parameter name
309  * OR: A = record-typed parameter name, B = field name
310  * (the first possibility takes precedence)
311  * A.B.C A = function name, B = record-typed parameter name,
312  * C = field name
313  * A.* Whole-row reference to composite parameter A.
314  * A.B.* Same, with A = function name, B = parameter name
315  *
316  * Here, it's sufficient to ignore the "*" in the last two cases --- the
317  * main parser will take care of expanding the whole-row reference.
318  *----------
319  */
320  nnames = list_length(cref->fields);
321 
322  if (nnames > 3)
323  return NULL;
324 
325  if (IsA(llast(cref->fields), A_Star))
326  nnames--;
327 
328  field1 = (Node *) linitial(cref->fields);
329  Assert(IsA(field1, String));
330  name1 = strVal(field1);
331  if (nnames > 1)
332  {
333  subfield = (Node *) lsecond(cref->fields);
334  Assert(IsA(subfield, String));
335  name2 = strVal(subfield);
336  }
337 
338  if (nnames == 3)
339  {
340  /*
341  * Three-part name: if the first part doesn't match the function name,
342  * we can fail immediately. Otherwise, look up the second part, and
343  * take the third part to be a field reference.
344  */
345  if (strcmp(name1, pinfo->fname) != 0)
346  return NULL;
347 
348  param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
349 
350  subfield = (Node *) lthird(cref->fields);
351  Assert(IsA(subfield, String));
352  }
353  else if (nnames == 2 && strcmp(name1, pinfo->fname) == 0)
354  {
355  /*
356  * Two-part name with first part matching function name: first see if
357  * second part matches any parameter name.
358  */
359  param = sql_fn_resolve_param_name(pinfo, name2, cref->location);
360 
361  if (param)
362  {
363  /* Yes, so this is a parameter reference, no subfield */
364  subfield = NULL;
365  }
366  else
367  {
368  /* No, so try to match as parameter name and subfield */
369  param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
370  }
371  }
372  else
373  {
374  /* Single name, or parameter name followed by subfield */
375  param = sql_fn_resolve_param_name(pinfo, name1, cref->location);
376  }
377 
378  if (!param)
379  return NULL; /* No match */
380 
381  if (subfield)
382  {
383  /*
384  * Must be a reference to a field of a composite parameter; otherwise
385  * ParseFuncOrColumn will return NULL, and we'll fail back at the
386  * caller.
387  */
388  param = ParseFuncOrColumn(pstate,
389  list_make1(subfield),
390  list_make1(param),
391  NULL,
392  cref->location);
393  }
394 
395  return param;
396 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
struct SQLFunctionParseInfo * SQLFunctionParseInfoPtr
Definition: functions.h:21
#define llast(l)
Definition: pg_list.h:126
Definition: nodes.h:508
#define strVal(v)
Definition: value.h:54
static Node * sql_fn_resolve_param_name(SQLFunctionParseInfoPtr pinfo, const char *paramname, int location)
Definition: functions.c:448
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, FuncCall *fn, int location)
Definition: parse_func.c:69
#define lsecond(l)
Definition: pg_list.h:114
int location
Definition: parsenodes.h:227
#define list_make1(x1)
Definition: pg_list.h:133
#define linitial(l)
Definition: pg_list.h:110
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * p_ref_hook_state
Definition: parse_node.h:206
static int list_length(const List *l)
Definition: pg_list.h:89
#define lthird(l)
Definition: pg_list.h:118
List * fields
Definition: parsenodes.h:226
static Node * sql_fn_resolve_param_name ( SQLFunctionParseInfoPtr  pinfo,
const char *  paramname,
int  location 
)
static

Definition at line 448 of file functions.c.

References SQLFunctionParseInfo::argnames, i, SQLFunctionParseInfo::nargs, NULL, and sql_fn_make_param().

Referenced by sql_fn_post_column_ref().

450 {
451  int i;
452 
453  if (pinfo->argnames == NULL)
454  return NULL;
455 
456  for (i = 0; i < pinfo->nargs; i++)
457  {
458  if (pinfo->argnames[i] && strcmp(pinfo->argnames[i], paramname) == 0)
459  return sql_fn_make_param(pinfo, i + 1, location);
460  }
461 
462  return NULL;
463 }
#define NULL
Definition: c.h:226
int i
static Node * sql_fn_make_param(SQLFunctionParseInfoPtr pinfo, int paramno, int location)
Definition: functions.c:418
static void sqlfunction_destroy ( DestReceiver self)
static

Definition at line 1933 of file functions.c.

References pfree().

Referenced by CreateSQLFunctionDestReceiver().

1934 {
1935  pfree(self);
1936 }
void pfree(void *pointer)
Definition: mcxt.c:992
static bool sqlfunction_receive ( TupleTableSlot slot,
DestReceiver self 
)
static

Definition at line 1907 of file functions.c.

References ExecFilterJunk(), DR_sqlfunction::filter, DR_sqlfunction::tstore, and tuplestore_puttupleslot().

Referenced by CreateSQLFunctionDestReceiver().

1908 {
1909  DR_sqlfunction *myState = (DR_sqlfunction *) self;
1910 
1911  /* Filter tuple as needed */
1912  slot = ExecFilterJunk(myState->filter, slot);
1913 
1914  /* Store the filtered tuple into the tuplestore */
1915  tuplestore_puttupleslot(myState->tstore, slot);
1916 
1917  return true;
1918 }
void tuplestore_puttupleslot(Tuplestorestate *state, TupleTableSlot *slot)
Definition: tuplestore.c:693
TupleTableSlot * ExecFilterJunk(JunkFilter *junkfilter, TupleTableSlot *slot)
Definition: execJunk.c:262
JunkFilter * filter
Definition: functions.c:46
Tuplestorestate * tstore
Definition: functions.c:44
static void sqlfunction_shutdown ( DestReceiver self)
static

Definition at line 1924 of file functions.c.

Referenced by CreateSQLFunctionDestReceiver().

1925 {
1926  /* no-op */
1927 }
static void sqlfunction_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)
static

Definition at line 1898 of file functions.c.

Referenced by CreateSQLFunctionDestReceiver().

1899 {
1900  /* no-op */
1901 }