PostgreSQL Source Code  git master
spi.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/printtup.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "catalog/heap.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/executor.h"
#include "executor/spi_priv.h"
#include "miscadmin.h"
#include "tcop/pquery.h"
#include "tcop/utility.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
Include dependency graph for spi.c:

Go to the source code of this file.

Data Structures

struct  SPICallbackArg
 

Typedefs

typedef struct SPICallbackArg SPICallbackArg
 

Functions

static Portal SPI_cursor_open_internal (const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
 
static void _SPI_prepare_plan (const char *src, SPIPlanPtr plan)
 
static void _SPI_prepare_oneshot_plan (const char *src, SPIPlanPtr plan)
 
static int _SPI_execute_plan (SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
 
static ParamListInfo _SPI_convert_params (int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
 
static int _SPI_pquery (QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
 
static void _SPI_error_callback (void *arg)
 
static void _SPI_cursor_operation (Portal portal, FetchDirection direction, long count, DestReceiver *dest)
 
static SPIPlanPtr _SPI_make_plan_non_temp (SPIPlanPtr plan)
 
static SPIPlanPtr _SPI_save_plan (SPIPlanPtr plan)
 
static int _SPI_begin_call (bool use_exec)
 
static int _SPI_end_call (bool use_exec)
 
static MemoryContext _SPI_execmem (void)
 
static MemoryContext _SPI_procmem (void)
 
static bool _SPI_checktuples (void)
 
int SPI_connect (void)
 
int SPI_connect_ext (int options)
 
int SPI_finish (void)
 
void SPI_start_transaction (void)
 
static void _SPI_commit (bool chain)
 
void SPI_commit (void)
 
void SPI_commit_and_chain (void)
 
static void _SPI_rollback (bool chain)
 
void SPI_rollback (void)
 
void SPI_rollback_and_chain (void)
 
void SPICleanup (void)
 
void AtEOXact_SPI (bool isCommit)
 
void AtEOSubXact_SPI (bool isCommit, SubTransactionId mySubid)
 
bool SPI_inside_nonatomic_context (void)
 
int SPI_execute (const char *src, bool read_only, long tcount)
 
int SPI_exec (const char *src, long tcount)
 
int SPI_execute_extended (const char *src, const SPIExecuteOptions *options)
 
int SPI_execute_plan (SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
 
int SPI_execp (SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
 
int SPI_execute_plan_extended (SPIPlanPtr plan, const SPIExecuteOptions *options)
 
int SPI_execute_plan_with_paramlist (SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
 
int SPI_execute_snapshot (SPIPlanPtr plan, Datum *Values, const char *Nulls, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, long tcount)
 
int SPI_execute_with_args (const char *src, int nargs, Oid *argtypes, Datum *Values, const char *Nulls, bool read_only, long tcount)
 
SPIPlanPtr SPI_prepare (const char *src, int nargs, Oid *argtypes)
 
SPIPlanPtr SPI_prepare_cursor (const char *src, int nargs, Oid *argtypes, int cursorOptions)
 
SPIPlanPtr SPI_prepare_extended (const char *src, const SPIPrepareOptions *options)
 
SPIPlanPtr SPI_prepare_params (const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
 
int SPI_keepplan (SPIPlanPtr plan)
 
SPIPlanPtr SPI_saveplan (SPIPlanPtr plan)
 
int SPI_freeplan (SPIPlanPtr plan)
 
HeapTuple SPI_copytuple (HeapTuple tuple)
 
HeapTupleHeader SPI_returntuple (HeapTuple tuple, TupleDesc tupdesc)
 
HeapTuple SPI_modifytuple (Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
 
int SPI_fnumber (TupleDesc tupdesc, const char *fname)
 
char * SPI_fname (TupleDesc tupdesc, int fnumber)
 
char * SPI_getvalue (HeapTuple tuple, TupleDesc tupdesc, int fnumber)
 
Datum SPI_getbinval (HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
 
char * SPI_gettype (TupleDesc tupdesc, int fnumber)
 
Oid SPI_gettypeid (TupleDesc tupdesc, int fnumber)
 
char * SPI_getrelname (Relation rel)
 
char * SPI_getnspname (Relation rel)
 
void * SPI_palloc (Size size)
 
void * SPI_repalloc (void *pointer, Size size)
 
void SPI_pfree (void *pointer)
 
Datum SPI_datumTransfer (Datum value, bool typByVal, int typLen)
 
void SPI_freetuple (HeapTuple tuple)
 
void SPI_freetuptable (SPITupleTable *tuptable)
 
Portal SPI_cursor_open (const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
 
Portal SPI_cursor_open_with_args (const char *name, const char *src, int nargs, Oid *argtypes, Datum *Values, const char *Nulls, bool read_only, int cursorOptions)
 
Portal SPI_cursor_open_with_paramlist (const char *name, SPIPlanPtr plan, ParamListInfo params, bool read_only)
 
Portal SPI_cursor_parse_open (const char *name, const char *src, const SPIParseOpenOptions *options)
 
Portal SPI_cursor_find (const char *name)
 
void SPI_cursor_fetch (Portal portal, bool forward, long count)
 
void SPI_cursor_move (Portal portal, bool forward, long count)
 
void SPI_scroll_cursor_fetch (Portal portal, FetchDirection direction, long count)
 
void SPI_scroll_cursor_move (Portal portal, FetchDirection direction, long count)
 
void SPI_cursor_close (Portal portal)
 
Oid SPI_getargtypeid (SPIPlanPtr plan, int argIndex)
 
int SPI_getargcount (SPIPlanPtr plan)
 
bool SPI_is_cursor_plan (SPIPlanPtr plan)
 
bool SPI_plan_is_valid (SPIPlanPtr plan)
 
const char * SPI_result_code_string (int code)
 
ListSPI_plan_get_plan_sources (SPIPlanPtr plan)
 
CachedPlanSPI_plan_get_cached_plan (SPIPlanPtr plan)
 
void spi_dest_startup (DestReceiver *self, int operation, TupleDesc typeinfo)
 
bool spi_printtup (TupleTableSlot *slot, DestReceiver *self)
 
static EphemeralNamedRelation _SPI_find_ENR_by_name (const char *name)
 
int SPI_register_relation (EphemeralNamedRelation enr)
 
int SPI_unregister_relation (const char *name)
 
int SPI_register_trigger_data (TriggerData *tdata)
 

Variables

uint64 SPI_processed = 0
 
SPITupleTableSPI_tuptable = NULL
 
int SPI_result = 0
 
static _SPI_connection_SPI_stack = NULL
 
static _SPI_connection_SPI_current = NULL
 
static int _SPI_stack_depth = 0
 
static int _SPI_connected = -1
 

Typedef Documentation

◆ SPICallbackArg

Function Documentation

◆ _SPI_begin_call()

static int _SPI_begin_call ( bool  use_exec)
static

Definition at line 2953 of file spi.c.

References _SPI_execmem(), _SPI_connection::execSubid, GetCurrentSubTransactionId(), and SPI_ERROR_UNCONNECTED.

Referenced by _SPI_cursor_operation(), SPI_cursor_open_internal(), SPI_cursor_open_with_args(), SPI_cursor_parse_open(), SPI_execute(), SPI_execute_extended(), SPI_execute_plan(), SPI_execute_plan_extended(), SPI_execute_plan_with_paramlist(), SPI_execute_snapshot(), SPI_execute_with_args(), SPI_finish(), SPI_prepare_cursor(), SPI_prepare_extended(), SPI_prepare_params(), SPI_register_relation(), SPI_saveplan(), and SPI_unregister_relation().

2954 {
2955  if (_SPI_current == NULL)
2956  return SPI_ERROR_UNCONNECTED;
2957 
2958  if (use_exec)
2959  {
2960  /* remember when the Executor operation started */
2962  /* switch to the Executor memory context */
2963  _SPI_execmem();
2964  }
2965 
2966  return 0;
2967 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:71
SubTransactionId execSubid
Definition: spi_priv.h:29
static _SPI_connection * _SPI_current
Definition: spi.c:50
static MemoryContext _SPI_execmem(void)
Definition: spi.c:2934
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:724

◆ _SPI_checktuples()

static bool _SPI_checktuples ( void  )
static

Definition at line 2993 of file spi.c.

References SPITupleTable::numvals, _SPI_connection::processed, and _SPI_connection::tuptable.

Referenced by _SPI_cursor_operation(), and _SPI_pquery().

2994 {
2995  uint64 processed = _SPI_current->processed;
2996  SPITupleTable *tuptable = _SPI_current->tuptable;
2997  bool failed = false;
2998 
2999  if (tuptable == NULL) /* spi_dest_startup was not called */
3000  failed = true;
3001  else if (processed != tuptable->numvals)
3002  failed = true;
3003 
3004  return failed;
3005 }
static _SPI_connection * _SPI_current
Definition: spi.c:50
uint64 numvals
Definition: spi.h:27
SPITupleTable * tuptable
Definition: spi_priv.h:26
uint64 processed
Definition: spi_priv.h:25

◆ _SPI_commit()

static void _SPI_commit ( bool  chain)
static

Definition at line 227 of file spi.c.

References _SPI_connection::atomic, CommitTransactionCommand(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, ForgetPortalSnapshots(), HoldPinnedPortals(), _SPI_connection::internal_xact, IsSubTransaction(), MemoryContextSwitchTo(), RestoreTransactionCharacteristics(), SaveTransactionCharacteristics(), and StartTransactionCommand().

Referenced by SPI_commit(), and SPI_commit_and_chain().

228 {
229  MemoryContext oldcontext = CurrentMemoryContext;
230 
231  if (_SPI_current->atomic)
232  ereport(ERROR,
233  (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
234  errmsg("invalid transaction termination")));
235 
236  /*
237  * This restriction is required by PLs implemented on top of SPI. They
238  * use subtransactions to establish exception blocks that are supposed to
239  * be rolled back together if there is an error. Terminating the
240  * top-level transaction in such a block violates that idea. A future PL
241  * implementation might have different ideas about this, in which case
242  * this restriction would have to be refined or the check possibly be
243  * moved out of SPI into the PLs.
244  */
245  if (IsSubTransaction())
246  ereport(ERROR,
247  (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
248  errmsg("cannot commit while a subtransaction is active")));
249 
250  /*
251  * Hold any pinned portals that any PLs might be using. We have to do
252  * this before changing transaction state, since this will run
253  * user-defined code that might throw an error.
254  */
256 
257  /* Start the actual commit */
258  _SPI_current->internal_xact = true;
259 
260  /* Release snapshots associated with portals */
262 
263  if (chain)
265 
267 
268  if (chain)
269  {
272  }
273 
274  MemoryContextSwitchTo(oldcontext);
275 
276  _SPI_current->internal_xact = false;
277 }
bool internal_xact
Definition: spi_priv.h:42
void CommitTransactionCommand(void)
Definition: xact.c:2959
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:46
void SaveTransactionCharacteristics(void)
Definition: xact.c:2939
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void ForgetPortalSnapshots(void)
Definition: portalmem.c:1303
#define ereport(elevel,...)
Definition: elog.h:157
void StartTransactionCommand(void)
Definition: xact.c:2858
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2947
bool IsSubTransaction(void)
Definition: xact.c:4776
int errmsg(const char *fmt,...)
Definition: elog.c:909
void HoldPinnedPortals(void)
Definition: portalmem.c:1254

◆ _SPI_convert_params()

static ParamListInfo _SPI_convert_params ( int  nargs,
Oid argtypes,
Datum Values,
const char *  Nulls 
)
static

Definition at line 2731 of file spi.c.

References i, ParamExternData::isnull, makeParamList(), PARAM_FLAG_CONST, ParamListInfoData::params, ParamExternData::pflags, ParamExternData::ptype, and ParamExternData::value.

Referenced by SPI_cursor_open(), SPI_cursor_open_with_args(), SPI_execute_plan(), SPI_execute_snapshot(), and SPI_execute_with_args().

2733 {
2734  ParamListInfo paramLI;
2735 
2736  if (nargs > 0)
2737  {
2738  paramLI = makeParamList(nargs);
2739 
2740  for (int i = 0; i < nargs; i++)
2741  {
2742  ParamExternData *prm = &paramLI->params[i];
2743 
2744  prm->value = Values[i];
2745  prm->isnull = (Nulls && Nulls[i] == 'n');
2746  prm->pflags = PARAM_FLAG_CONST;
2747  prm->ptype = argtypes[i];
2748  }
2749  }
2750  else
2751  paramLI = NULL;
2752  return paramLI;
2753 }
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
Datum value
Definition: params.h:92
ParamListInfo makeParamList(int numParams)
Definition: params.c:44
uint16 pflags
Definition: params.h:94
int i
bool isnull
Definition: params.h:93
#define PARAM_FLAG_CONST
Definition: params.h:88
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ _SPI_cursor_operation()

static void _SPI_cursor_operation ( Portal  portal,
FetchDirection  direction,
long  count,
DestReceiver dest 
)
static

Definition at line 2883 of file spi.c.

References _SPI_begin_call(), _SPI_checktuples(), _SPI_end_call(), DestSPI, elog, ERROR, _DestReceiver::mydest, PortalIsValid, PortalRunFetch(), _SPI_connection::processed, SPI_processed, and _SPI_connection::tuptable.

Referenced by SPI_cursor_fetch(), SPI_cursor_move(), SPI_scroll_cursor_fetch(), and SPI_scroll_cursor_move().

2885 {
2886  uint64 nfetched;
2887 
2888  /* Check that the portal is valid */
2889  if (!PortalIsValid(portal))
2890  elog(ERROR, "invalid portal in SPI cursor operation");
2891 
2892  /* Push the SPI stack */
2893  if (_SPI_begin_call(true) < 0)
2894  elog(ERROR, "SPI cursor operation called while not connected");
2895 
2896  /* Reset the SPI result (note we deliberately don't touch lastoid) */
2897  SPI_processed = 0;
2898  SPI_tuptable = NULL;
2899  _SPI_current->processed = 0;
2900  _SPI_current->tuptable = NULL;
2901 
2902  /* Run the cursor */
2903  nfetched = PortalRunFetch(portal,
2904  direction,
2905  count,
2906  dest);
2907 
2908  /*
2909  * Think not to combine this store with the preceding function call. If
2910  * the portal contains calls to functions that use SPI, then _SPI_stack is
2911  * likely to move around while the portal runs. When control returns,
2912  * _SPI_current will point to the correct stack entry... but the pointer
2913  * may be different than it was beforehand. So we must be sure to re-fetch
2914  * the pointer after the function call completes.
2915  */
2916  _SPI_current->processed = nfetched;
2917 
2918  if (dest->mydest == DestSPI && _SPI_checktuples())
2919  elog(ERROR, "consistency check on SPI tuple count failed");
2920 
2921  /* Put the result into place for access by caller */
2924 
2925  /* tuptable now is caller's responsibility, not SPI's */
2926  _SPI_current->tuptable = NULL;
2927 
2928  /* Pop the SPI stack */
2929  _SPI_end_call(true);
2930 }
Definition: dest.h:94
CommandDest mydest
Definition: dest.h:129
uint64 PortalRunFetch(Portal portal, FetchDirection fdirection, long count, DestReceiver *dest)
Definition: pquery.c:1388
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static _SPI_connection * _SPI_current
Definition: spi.c:50
uint64 SPI_processed
Definition: spi.c:45
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
static bool _SPI_checktuples(void)
Definition: spi.c:2993
#define ERROR
Definition: elog.h:46
SPITupleTable * tuptable
Definition: spi_priv.h:26
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
#define PortalIsValid(p)
Definition: portal.h:212
uint64 processed
Definition: spi_priv.h:25
#define elog(elevel,...)
Definition: elog.h:232

◆ _SPI_end_call()

static int _SPI_end_call ( bool  use_exec)
static

Definition at line 2977 of file spi.c.

References _SPI_procmem(), _SPI_connection::execCxt, _SPI_connection::execSubid, InvalidSubTransactionId, and MemoryContextResetAndDeleteChildren.

Referenced by _SPI_cursor_operation(), SPI_cursor_open_internal(), SPI_cursor_open_with_args(), SPI_cursor_parse_open(), SPI_execute(), SPI_execute_extended(), SPI_execute_plan(), SPI_execute_plan_extended(), SPI_execute_plan_with_paramlist(), SPI_execute_snapshot(), SPI_execute_with_args(), SPI_prepare_cursor(), SPI_prepare_extended(), SPI_prepare_params(), SPI_register_relation(), SPI_saveplan(), and SPI_unregister_relation().

2978 {
2979  if (use_exec)
2980  {
2981  /* switch to the procedure memory context */
2982  _SPI_procmem();
2983  /* mark Executor context no longer in use */
2985  /* and free Executor memory */
2987  }
2988 
2989  return 0;
2990 }
SubTransactionId execSubid
Definition: spi_priv.h:29
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
MemoryContext execCxt
Definition: spi_priv.h:34
#define InvalidSubTransactionId
Definition: c.h:593
static MemoryContext _SPI_procmem(void)
Definition: spi.c:2940

◆ _SPI_error_callback()

static void _SPI_error_callback ( void *  arg)
static

Definition at line 2837 of file spi.c.

References errcontext, errposition(), geterrposition(), internalerrposition(), internalerrquery(), SPICallbackArg::mode, SPICallbackArg::query, RAW_PARSE_PLPGSQL_ASSIGN1, RAW_PARSE_PLPGSQL_ASSIGN2, RAW_PARSE_PLPGSQL_ASSIGN3, and RAW_PARSE_PLPGSQL_EXPR.

Referenced by _SPI_execute_plan(), _SPI_prepare_oneshot_plan(), _SPI_prepare_plan(), SPI_cursor_open_internal(), and SPI_plan_get_cached_plan().

2838 {
2839  SPICallbackArg *carg = (SPICallbackArg *) arg;
2840  const char *query = carg->query;
2841  int syntaxerrposition;
2842 
2843  if (query == NULL) /* in case arg wasn't set yet */
2844  return;
2845 
2846  /*
2847  * If there is a syntax error position, convert to internal syntax error;
2848  * otherwise treat the query as an item of context stack
2849  */
2850  syntaxerrposition = geterrposition();
2851  if (syntaxerrposition > 0)
2852  {
2853  errposition(0);
2854  internalerrposition(syntaxerrposition);
2855  internalerrquery(query);
2856  }
2857  else
2858  {
2859  /* Use the parse mode to decide how to describe the query */
2860  switch (carg->mode)
2861  {
2863  errcontext("SQL expression \"%s\"", query);
2864  break;
2868  errcontext("PL/pgSQL assignment \"%s\"", query);
2869  break;
2870  default:
2871  errcontext("SQL statement \"%s\"", query);
2872  break;
2873  }
2874  }
2875 }
RawParseMode mode
Definition: spi.c:57
int geterrposition(void)
Definition: elog.c:1417
int internalerrquery(const char *query)
Definition: elog.c:1321
#define errcontext
Definition: elog.h:204
void * arg
const char * query
Definition: spi.c:56
int errposition(int cursorpos)
Definition: elog.c:1285
int internalerrposition(int cursorpos)
Definition: elog.c:1301

◆ _SPI_execmem()

static MemoryContext _SPI_execmem ( void  )
static

Definition at line 2934 of file spi.c.

References _SPI_connection::execCxt, and MemoryContextSwitchTo().

Referenced by _SPI_begin_call().

2935 {
2937 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
MemoryContext execCxt
Definition: spi_priv.h:34

◆ _SPI_execute_plan()

static int _SPI_execute_plan ( SPIPlanPtr  plan,
const SPIExecuteOptions options,
Snapshot  snapshot,
Snapshot  crosscheck_snapshot,
bool  fire_triggers 
)
static

Definition at line 2296 of file spi.c.

References _SPI_error_callback(), _SPI_pquery(), ActiveSnapshotSet(), SPIExecuteOptions::allow_nonatomic, ErrorContextCallback::arg, _SPI_plan::argtypes, Assert, _SPI_connection::atomic, ErrorContextCallback::callback, PlannedStmt::canSetTag, CommandCounterIncrement(), CommandIsReadOnly(), QueryCompletion::commandTag, CachedPlanSource::commandTag, CompleteCachedPlan(), CreateCommandName(), CreateDestReceiver(), CreateQueryDesc(), CurrentResourceOwner, _SPI_plan::cursor_options, SPIExecuteOptions::dest, generate_unaccent_rules::dest, DestNone, DestSPI, EnsurePortalSnapshotExists(), ereport, errcode(), errmsg(), ERROR, error_context_stack, CopyStmt::filename, FreeQueryDesc(), GetActiveSnapshot(), GetCachedPlan(), GetCommandTagName(), GetTransactionSnapshot(), CreateTableAsStmt::if_not_exists, InitializeQueryCompletion(), CreateTableAsStmt::into, InvalidSnapshot, CreateTableAsStmt::is_select_into, IsA, lfirst, lfirst_node, linitial_node, list_length(), SPICallbackArg::mode, SPIExecuteOptions::must_return_tuples, _SPI_plan::nargs, NIL, QueryCompletion::nprocessed, SPITupleTable::numvals, _SPI_plan::oneshot, SPIExecuteOptions::owner, SPIExecuteOptions::params, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, pg_analyze_and_rewrite(), pg_analyze_and_rewrite_params(), _SPI_plan::plancache_list, PlannedStmtRequiresSnapshot(), PopActiveSnapshot(), ErrorContextCallback::previous, PROCESS_UTILITY_QUERY, PROCESS_UTILITY_QUERY_NONATOMIC, _SPI_connection::processed, ProcessUtility(), PushActiveSnapshot(), PushCopiedSnapshot(), SPICallbackArg::query, CachedPlanSource::query_string, _SPI_connection::queryEnv, CachedPlanSource::raw_parse_tree, SPIExecuteOptions::read_only, ReleaseCachedPlan(), CachedPlanSource::resultDesc, _SPI_plan::saved, IntoClause::skipData, SPI_ERROR_COPY, SPI_ERROR_TRANSACTION, SPI_freetuptable(), SPI_OK_REWRITTEN, SPI_OK_SELINTO, SPI_OK_UTILITY, SPI_processed, CachedPlan::stmt_list, SPIExecuteOptions::tcount, _SPI_connection::tuptable, UpdateActiveSnapshotCommandId(), and PlannedStmt::utilityStmt.

Referenced by SPI_execute(), SPI_execute_extended(), SPI_execute_plan(), SPI_execute_plan_extended(), SPI_execute_plan_with_paramlist(), SPI_execute_snapshot(), and SPI_execute_with_args().

2299 {
2300  int my_res = 0;
2301  uint64 my_processed = 0;
2302  SPITupleTable *my_tuptable = NULL;
2303  int res = 0;
2304  bool pushed_active_snap = false;
2305  ResourceOwner plan_owner = options->owner;
2306  SPICallbackArg spicallbackarg;
2307  ErrorContextCallback spierrcontext;
2308  CachedPlan *cplan = NULL;
2309  ListCell *lc1;
2310 
2311  /*
2312  * Setup error traceback support for ereport()
2313  */
2314  spicallbackarg.query = NULL; /* we'll fill this below */
2315  spicallbackarg.mode = plan->parse_mode;
2316  spierrcontext.callback = _SPI_error_callback;
2317  spierrcontext.arg = &spicallbackarg;
2318  spierrcontext.previous = error_context_stack;
2319  error_context_stack = &spierrcontext;
2320 
2321  /*
2322  * We support four distinct snapshot management behaviors:
2323  *
2324  * snapshot != InvalidSnapshot, read_only = true: use exactly the given
2325  * snapshot.
2326  *
2327  * snapshot != InvalidSnapshot, read_only = false: use the given snapshot,
2328  * modified by advancing its command ID before each querytree.
2329  *
2330  * snapshot == InvalidSnapshot, read_only = true: use the entry-time
2331  * ActiveSnapshot, if any (if there isn't one, we run with no snapshot).
2332  *
2333  * snapshot == InvalidSnapshot, read_only = false: take a full new
2334  * snapshot for each user command, and advance its command ID before each
2335  * querytree within the command.
2336  *
2337  * In the first two cases, we can just push the snap onto the stack once
2338  * for the whole plan list.
2339  *
2340  * Note that snapshot != InvalidSnapshot implies an atomic execution
2341  * context.
2342  */
2343  if (snapshot != InvalidSnapshot)
2344  {
2345  Assert(!options->allow_nonatomic);
2346  if (options->read_only)
2347  {
2348  PushActiveSnapshot(snapshot);
2349  pushed_active_snap = true;
2350  }
2351  else
2352  {
2353  /* Make sure we have a private copy of the snapshot to modify */
2354  PushCopiedSnapshot(snapshot);
2355  pushed_active_snap = true;
2356  }
2357  }
2358 
2359  /*
2360  * Ensure that we have a resource owner if plan is saved, and not if it
2361  * isn't.
2362  */
2363  if (!plan->saved)
2364  plan_owner = NULL;
2365  else if (plan_owner == NULL)
2366  plan_owner = CurrentResourceOwner;
2367 
2368  /*
2369  * We interpret must_return_tuples as "there must be at least one query,
2370  * and all of them must return tuples". This is a bit laxer than
2371  * SPI_is_cursor_plan's check, but there seems no reason to enforce that
2372  * there be only one query.
2373  */
2374  if (options->must_return_tuples && plan->plancache_list == NIL)
2375  ereport(ERROR,
2376  (errcode(ERRCODE_SYNTAX_ERROR),
2377  errmsg("empty query does not return tuples")));
2378 
2379  foreach(lc1, plan->plancache_list)
2380  {
2381  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc1);
2382  List *stmt_list;
2383  ListCell *lc2;
2384 
2385  spicallbackarg.query = plansource->query_string;
2386 
2387  /*
2388  * If this is a one-shot plan, we still need to do parse analysis.
2389  */
2390  if (plan->oneshot)
2391  {
2392  RawStmt *parsetree = plansource->raw_parse_tree;
2393  const char *src = plansource->query_string;
2394  List *stmt_list;
2395 
2396  /*
2397  * Parameter datatypes are driven by parserSetup hook if provided,
2398  * otherwise we use the fixed parameter list.
2399  */
2400  if (parsetree == NULL)
2401  stmt_list = NIL;
2402  else if (plan->parserSetup != NULL)
2403  {
2404  Assert(plan->nargs == 0);
2405  stmt_list = pg_analyze_and_rewrite_params(parsetree,
2406  src,
2407  plan->parserSetup,
2408  plan->parserSetupArg,
2410  }
2411  else
2412  {
2413  stmt_list = pg_analyze_and_rewrite(parsetree,
2414  src,
2415  plan->argtypes,
2416  plan->nargs,
2418  }
2419 
2420  /* Finish filling in the CachedPlanSource */
2421  CompleteCachedPlan(plansource,
2422  stmt_list,
2423  NULL,
2424  plan->argtypes,
2425  plan->nargs,
2426  plan->parserSetup,
2427  plan->parserSetupArg,
2428  plan->cursor_options,
2429  false); /* not fixed result */
2430  }
2431 
2432  /*
2433  * If asked to, complain when query does not return tuples.
2434  * (Replanning can't change this, so we can check it before that.
2435  * However, we can't check it till after parse analysis, so in the
2436  * case of a one-shot plan this is the earliest we could check.)
2437  */
2438  if (options->must_return_tuples && !plansource->resultDesc)
2439  {
2440  /* try to give a good error message */
2441  const char *cmdtag;
2442 
2443  /* A SELECT without resultDesc must be SELECT INTO */
2444  if (plansource->commandTag == CMDTAG_SELECT)
2445  cmdtag = "SELECT INTO";
2446  else
2447  cmdtag = GetCommandTagName(plansource->commandTag);
2448  ereport(ERROR,
2449  (errcode(ERRCODE_SYNTAX_ERROR),
2450  /* translator: %s is name of a SQL command, eg INSERT */
2451  errmsg("%s query does not return tuples", cmdtag)));
2452  }
2453 
2454  /*
2455  * Replan if needed, and increment plan refcount. If it's a saved
2456  * plan, the refcount must be backed by the plan_owner.
2457  */
2458  cplan = GetCachedPlan(plansource, options->params,
2459  plan_owner, _SPI_current->queryEnv);
2460 
2461  stmt_list = cplan->stmt_list;
2462 
2463  /*
2464  * If we weren't given a specific snapshot to use, and the statement
2465  * list requires a snapshot, set that up.
2466  */
2467  if (snapshot == InvalidSnapshot &&
2468  (list_length(stmt_list) > 1 ||
2469  (list_length(stmt_list) == 1 &&
2471  stmt_list)))))
2472  {
2473  /*
2474  * First, ensure there's a Portal-level snapshot. This back-fills
2475  * the snapshot stack in case the previous operation was a COMMIT
2476  * or ROLLBACK inside a procedure or DO block. (We can't put back
2477  * the Portal snapshot any sooner, or we'd break cases like doing
2478  * SET or LOCK just after COMMIT.) It's enough to check once per
2479  * statement list, since COMMIT/ROLLBACK/CALL/DO can't appear
2480  * within a multi-statement list.
2481  */
2483 
2484  /*
2485  * In the default non-read-only case, get a new per-statement-list
2486  * snapshot, replacing any that we pushed in a previous cycle.
2487  * Skip it when doing non-atomic execution, though (we rely
2488  * entirely on the Portal snapshot in that case).
2489  */
2490  if (!options->read_only && !options->allow_nonatomic)
2491  {
2492  if (pushed_active_snap)
2495  pushed_active_snap = true;
2496  }
2497  }
2498 
2499  foreach(lc2, stmt_list)
2500  {
2501  PlannedStmt *stmt = lfirst_node(PlannedStmt, lc2);
2502  bool canSetTag = stmt->canSetTag;
2503  DestReceiver *dest;
2504 
2505  /*
2506  * Reset output state. (Note that if a non-SPI receiver is used,
2507  * _SPI_current->processed will stay zero, and that's what we'll
2508  * report to the caller. It's the receiver's job to count tuples
2509  * in that case.)
2510  */
2511  _SPI_current->processed = 0;
2512  _SPI_current->tuptable = NULL;
2513 
2514  /* Check for unsupported cases. */
2515  if (stmt->utilityStmt)
2516  {
2517  if (IsA(stmt->utilityStmt, CopyStmt))
2518  {
2519  CopyStmt *cstmt = (CopyStmt *) stmt->utilityStmt;
2520 
2521  if (cstmt->filename == NULL)
2522  {
2523  my_res = SPI_ERROR_COPY;
2524  goto fail;
2525  }
2526  }
2527  else if (IsA(stmt->utilityStmt, TransactionStmt))
2528  {
2529  my_res = SPI_ERROR_TRANSACTION;
2530  goto fail;
2531  }
2532  }
2533 
2534  if (options->read_only && !CommandIsReadOnly(stmt))
2535  ereport(ERROR,
2536  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2537  /* translator: %s is a SQL statement name */
2538  errmsg("%s is not allowed in a non-volatile function",
2539  CreateCommandName((Node *) stmt))));
2540 
2541  /*
2542  * If not read-only mode, advance the command counter before each
2543  * command and update the snapshot. (But skip it if the snapshot
2544  * isn't under our control.)
2545  */
2546  if (!options->read_only && pushed_active_snap)
2547  {
2550  }
2551 
2552  /*
2553  * Select appropriate tuple receiver. Output from non-canSetTag
2554  * subqueries always goes to the bit bucket.
2555  */
2556  if (!canSetTag)
2557  dest = CreateDestReceiver(DestNone);
2558  else if (options->dest)
2559  dest = options->dest;
2560  else
2561  dest = CreateDestReceiver(DestSPI);
2562 
2563  if (stmt->utilityStmt == NULL)
2564  {
2565  QueryDesc *qdesc;
2566  Snapshot snap;
2567 
2568  if (ActiveSnapshotSet())
2569  snap = GetActiveSnapshot();
2570  else
2571  snap = InvalidSnapshot;
2572 
2573  qdesc = CreateQueryDesc(stmt,
2574  plansource->query_string,
2575  snap, crosscheck_snapshot,
2576  dest,
2577  options->params,
2579  0);
2580  res = _SPI_pquery(qdesc, fire_triggers,
2581  canSetTag ? options->tcount : 0);
2582  FreeQueryDesc(qdesc);
2583  }
2584  else
2585  {
2586  ProcessUtilityContext context;
2587  QueryCompletion qc;
2588 
2589  /*
2590  * If the SPI context is atomic, or we were not told to allow
2591  * nonatomic operations, tell ProcessUtility this is an atomic
2592  * execution context.
2593  */
2594  if (_SPI_current->atomic || !options->allow_nonatomic)
2595  context = PROCESS_UTILITY_QUERY;
2596  else
2598 
2600  ProcessUtility(stmt,
2601  plansource->query_string,
2602  true, /* protect plancache's node tree */
2603  context,
2604  options->params,
2606  dest,
2607  &qc);
2608 
2609  /* Update "processed" if stmt returned tuples */
2610  if (_SPI_current->tuptable)
2612 
2613  res = SPI_OK_UTILITY;
2614 
2615  /*
2616  * Some utility statements return a row count, even though the
2617  * tuples are not returned to the caller.
2618  */
2619  if (IsA(stmt->utilityStmt, CreateTableAsStmt))
2620  {
2621  CreateTableAsStmt *ctastmt = (CreateTableAsStmt *) stmt->utilityStmt;
2622 
2623  if (qc.commandTag == CMDTAG_SELECT)
2625  else
2626  {
2627  /*
2628  * Must be an IF NOT EXISTS that did nothing, or a
2629  * CREATE ... WITH NO DATA.
2630  */
2631  Assert(ctastmt->if_not_exists ||
2632  ctastmt->into->skipData);
2633  _SPI_current->processed = 0;
2634  }
2635 
2636  /*
2637  * For historical reasons, if CREATE TABLE AS was spelled
2638  * as SELECT INTO, return a special return code.
2639  */
2640  if (ctastmt->is_select_into)
2641  res = SPI_OK_SELINTO;
2642  }
2643  else if (IsA(stmt->utilityStmt, CopyStmt))
2644  {
2645  Assert(qc.commandTag == CMDTAG_COPY);
2647  }
2648  }
2649 
2650  /*
2651  * The last canSetTag query sets the status values returned to the
2652  * caller. Be careful to free any tuptables not returned, to
2653  * avoid intra-transaction memory leak.
2654  */
2655  if (canSetTag)
2656  {
2657  my_processed = _SPI_current->processed;
2658  SPI_freetuptable(my_tuptable);
2659  my_tuptable = _SPI_current->tuptable;
2660  my_res = res;
2661  }
2662  else
2663  {
2665  _SPI_current->tuptable = NULL;
2666  }
2667 
2668  /*
2669  * We don't issue a destroy call to the receiver. The SPI and
2670  * None receivers would ignore it anyway, while if the caller
2671  * supplied a receiver, it's not our job to destroy it.
2672  */
2673 
2674  if (res < 0)
2675  {
2676  my_res = res;
2677  goto fail;
2678  }
2679  }
2680 
2681  /* Done with this plan, so release refcount */
2682  ReleaseCachedPlan(cplan, plan_owner);
2683  cplan = NULL;
2684 
2685  /*
2686  * If not read-only mode, advance the command counter after the last
2687  * command. This ensures that its effects are visible, in case it was
2688  * DDL that would affect the next CachedPlanSource.
2689  */
2690  if (!options->read_only)
2692  }
2693 
2694 fail:
2695 
2696  /* Pop the snapshot off the stack if we pushed one */
2697  if (pushed_active_snap)
2699 
2700  /* We no longer need the cached plan refcount, if any */
2701  if (cplan)
2702  ReleaseCachedPlan(cplan, plan_owner);
2703 
2704  /*
2705  * Pop the error context stack
2706  */
2707  error_context_stack = spierrcontext.previous;
2708 
2709  /* Save results for caller */
2710  SPI_processed = my_processed;
2711  SPI_tuptable = my_tuptable;
2712 
2713  /* tuptable now is caller's responsibility, not SPI's */
2714  _SPI_current->tuptable = NULL;
2715 
2716  /*
2717  * If none of the queries had canSetTag, return SPI_OK_REWRITTEN. Prior to
2718  * 8.4, we used return the last query's result code, but not its auxiliary
2719  * results, but that's confusing.
2720  */
2721  if (my_res == 0)
2722  my_res = SPI_OK_REWRITTEN;
2723 
2724  return my_res;
2725 }
#define NIL
Definition: pg_list.h:65
Oid * argtypes
Definition: spi_priv.h:100
bool CommandIsReadOnly(PlannedStmt *pstmt)
Definition: utility.c:100
void UpdateActiveSnapshotCommandId(void)
Definition: snapmgr.c:743
#define IsA(nodeptr, _type_)
Definition: nodes.h:589
RawParseMode mode
Definition: spi.c:57
List * plancache_list
Definition: spi_priv.h:95
void FreeQueryDesc(QueryDesc *qdesc)
Definition: pquery.c:105
Definition: dest.h:94
void EnsurePortalSnapshotExists(void)
Definition: pquery.c:1777
static const char * CreateCommandName(Node *parsetree)
Definition: utility.h:103
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
DestReceiver * dest
Definition: spi.h:53
#define SPI_ERROR_COPY
Definition: spi.h:69
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:801
SPITupleTable * SPI_tuptable
Definition: spi.c:46
Definition: nodes.h:538
int errcode(int sqlerrcode)
Definition: elog.c:698
ProcessUtilityContext
Definition: utility.h:20
uint64 tcount
Definition: spi.h:52
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1264
static _SPI_connection * _SPI_current
Definition: spi.c:50
void PopActiveSnapshot(void)
Definition: snapmgr.c:774
bool skipData
Definition: primnodes.h:119
List * pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: postgres.c:642
#define linitial_node(type, l)
Definition: pg_list.h:177
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
uint64 SPI_processed
Definition: spi.c:45
TupleDesc resultDesc
Definition: plancache.h:108
ErrorContextCallback * error_context_stack
Definition: elog.c:93
RawParseMode parse_mode
Definition: spi_priv.h:97
static void _SPI_error_callback(void *arg)
Definition: spi.c:2837
void ProcessUtility(PlannedStmt *pstmt, const char *queryString, bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc)
Definition: utility.c:503
uint64 numvals
Definition: spi.h:27
#define ERROR
Definition: elog.h:46
uint64 nprocessed
Definition: cmdtag.h:31
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
void InitializeQueryCompletion(QueryCompletion *qc)
Definition: cmdtag.c:38
SPITupleTable * tuptable
Definition: spi_priv.h:26
bool must_return_tuples
Definition: spi.h:51
void PushCopiedSnapshot(Snapshot snapshot)
Definition: snapmgr.c:731
QueryDesc * CreateQueryDesc(PlannedStmt *plannedstmt, const char *sourceText, Snapshot snapshot, Snapshot crosscheck_snapshot, DestReceiver *dest, ParamListInfo params, QueryEnvironment *queryEnv, int instrument_options)
Definition: pquery.c:67
#define lfirst_node(type, lc)
Definition: pg_list.h:172
static int _SPI_pquery(QueryDesc *queryDesc, bool fire_triggers, uint64 tcount)
Definition: spi.c:2756
Definition: dest.h:89
ResourceOwner owner
Definition: spi.h:54
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:680
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
#define SPI_OK_UTILITY
Definition: spi.h:85
Node * utilityStmt
Definition: plannodes.h:86
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:813
#define SPI_OK_REWRITTEN
Definition: spi.h:95
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, ResourceOwner owner, QueryEnvironment *queryEnv)
Definition: plancache.c:1141
#define SPI_ERROR_TRANSACTION
Definition: spi.h:75
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
#define SPI_OK_SELINTO
Definition: spi.h:87
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1291
#define InvalidSnapshot
Definition: snapshot.h:123
bool canSetTag
Definition: plannodes.h:54
void CommandCounterIncrement(void)
Definition: xact.c:1022
bool allow_nonatomic
Definition: spi.h:50
bool saved
Definition: spi_priv.h:93
int nargs
Definition: spi_priv.h:99
CommandTag commandTag
Definition: cmdtag.h:30
#define ereport(elevel,...)
Definition: elog.h:157
IntoClause * into
Definition: parsenodes.h:3410
uint64 processed
Definition: spi_priv.h:25
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void CompleteCachedPlan(CachedPlanSource *plansource, List *querytree_list, MemoryContext querytree_context, Oid *param_types, int num_params, ParserSetupHook parserSetup, void *parserSetupArg, int cursor_options, bool fixed_result)
Definition: plancache.c:338
bool oneshot
Definition: spi_priv.h:94
const char * query_string
Definition: plancache.h:100
CommandTag commandTag
Definition: plancache.h:101
static int list_length(const List *l)
Definition: pg_list.h:149
void * parserSetupArg
Definition: spi_priv.h:102
List * pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:679
struct RawStmt * raw_parse_tree
Definition: plancache.h:99
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
const char * query
Definition: spi.c:56
char * filename
Definition: parsenodes.h:2129
List * stmt_list
Definition: plancache.h:150
int cursor_options
Definition: spi_priv.h:98
bool PlannedStmtRequiresSnapshot(PlannedStmt *pstmt)
Definition: pquery.c:1729
Definition: pg_list.h:50

◆ _SPI_find_ENR_by_name()

static EphemeralNamedRelation _SPI_find_ENR_by_name ( const char *  name)
static

Definition at line 3156 of file spi.c.

References Assert, get_ENR(), and _SPI_connection::queryEnv.

Referenced by SPI_register_relation(), and SPI_unregister_relation().

3157 {
3158  /* internal static function; any error is bug in SPI itself */
3159  Assert(name != NULL);
3160 
3161  /* fast exit if no tuplestores have been added */
3162  if (_SPI_current->queryEnv == NULL)
3163  return NULL;
3164 
3165  return get_ENR(_SPI_current->queryEnv, name);
3166 }
static _SPI_connection * _SPI_current
Definition: spi.c:50
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:561
EphemeralNamedRelation get_ENR(QueryEnvironment *queryEnv, const char *name)

◆ _SPI_make_plan_non_temp()

static SPIPlanPtr _SPI_make_plan_non_temp ( SPIPlanPtr  plan)
static

Definition at line 3017 of file spi.c.

References _SPI_PLAN_MAGIC, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, _SPI_plan::argtypes, Assert, CachedPlanSetParentContext(), _SPI_plan::cursor_options, lappend(), lfirst, _SPI_plan::magic, MemoryContextSwitchTo(), _SPI_plan::nargs, NIL, _SPI_plan::oneshot, palloc(), palloc0(), _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, _SPI_plan::plancache_list, _SPI_plan::plancxt, and _SPI_connection::procCxt.

Referenced by SPI_prepare_cursor(), SPI_prepare_extended(), and SPI_prepare_params().

3018 {
3019  SPIPlanPtr newplan;
3020  MemoryContext parentcxt = _SPI_current->procCxt;
3021  MemoryContext plancxt;
3022  MemoryContext oldcxt;
3023  ListCell *lc;
3024 
3025  /* Assert the input is a temporary SPIPlan */
3026  Assert(plan->magic == _SPI_PLAN_MAGIC);
3027  Assert(plan->plancxt == NULL);
3028  /* One-shot plans can't be saved */
3029  Assert(!plan->oneshot);
3030 
3031  /*
3032  * Create a memory context for the plan, underneath the procedure context.
3033  * We don't expect the plan to be very large.
3034  */
3035  plancxt = AllocSetContextCreate(parentcxt,
3036  "SPI Plan",
3038  oldcxt = MemoryContextSwitchTo(plancxt);
3039 
3040  /* Copy the _SPI_plan struct and subsidiary data into the new context */
3041  newplan = (SPIPlanPtr) palloc0(sizeof(_SPI_plan));
3042  newplan->magic = _SPI_PLAN_MAGIC;
3043  newplan->plancxt = plancxt;
3044  newplan->parse_mode = plan->parse_mode;
3045  newplan->cursor_options = plan->cursor_options;
3046  newplan->nargs = plan->nargs;
3047  if (plan->nargs > 0)
3048  {
3049  newplan->argtypes = (Oid *) palloc(plan->nargs * sizeof(Oid));
3050  memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
3051  }
3052  else
3053  newplan->argtypes = NULL;
3054  newplan->parserSetup = plan->parserSetup;
3055  newplan->parserSetupArg = plan->parserSetupArg;
3056 
3057  /*
3058  * Reparent all the CachedPlanSources into the procedure context. In
3059  * theory this could fail partway through due to the pallocs, but we don't
3060  * care too much since both the procedure context and the executor context
3061  * would go away on error.
3062  */
3063  foreach(lc, plan->plancache_list)
3064  {
3065  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
3066 
3067  CachedPlanSetParentContext(plansource, parentcxt);
3068 
3069  /* Build new list, with list cells in plancxt */
3070  newplan->plancache_list = lappend(newplan->plancache_list, plansource);
3071  }
3072 
3073  MemoryContextSwitchTo(oldcxt);
3074 
3075  /* For safety, unlink the CachedPlanSources from the temporary plan */
3076  plan->plancache_list = NIL;
3077 
3078  return newplan;
3079 }
#define NIL
Definition: pg_list.h:65
Oid * argtypes
Definition: spi_priv.h:100
#define AllocSetContextCreate
Definition: memutils.h:173
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
unsigned int Oid
Definition: postgres_ext.h:31
RawParseMode parse_mode
Definition: spi_priv.h:97
void CachedPlanSetParentContext(CachedPlanSource *plansource, MemoryContext newcontext)
Definition: plancache.c:1469
MemoryContext plancxt
Definition: spi_priv.h:96
List * lappend(List *list, void *datum)
Definition: list.c:336
void * palloc0(Size size)
Definition: mcxt.c:1093
MemoryContext procCxt
Definition: spi_priv.h:33
int nargs
Definition: spi_priv.h:99
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
ParserSetupHook parserSetup
Definition: spi_priv.h:101
bool oneshot
Definition: spi_priv.h:94
void * parserSetupArg
Definition: spi_priv.h:102
void * palloc(Size size)
Definition: mcxt.c:1062
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:66
int cursor_options
Definition: spi_priv.h:98

◆ _SPI_pquery()

static int _SPI_pquery ( QueryDesc queryDesc,
bool  fire_triggers,
uint64  tcount 
)
static

Definition at line 2756 of file spi.c.

References _SPI_checktuples(), CMD_DELETE, CMD_INSERT, CMD_SELECT, CMD_UPDATE, QueryDesc::dest, DestNone, DestSPI, elog, ERROR, EState::es_processed, QueryDesc::estate, EXEC_FLAG_SKIP_TRIGGERS, ExecutorEnd(), ExecutorFinish(), ExecutorRun(), ExecutorStart(), ForwardScanDirection, PlannedStmt::hasReturning, _DestReceiver::mydest, QueryDesc::operation, QueryDesc::plannedstmt, _SPI_connection::processed, ResetUsage(), ShowUsage(), SPI_ERROR_OPUNKNOWN, SPI_OK_DELETE, SPI_OK_DELETE_RETURNING, SPI_OK_INSERT, SPI_OK_INSERT_RETURNING, SPI_OK_SELECT, SPI_OK_UPDATE, SPI_OK_UPDATE_RETURNING, and SPI_OK_UTILITY.

Referenced by _SPI_execute_plan().

2757 {
2758  int operation = queryDesc->operation;
2759  int eflags;
2760  int res;
2761 
2762  switch (operation)
2763  {
2764  case CMD_SELECT:
2765  if (queryDesc->dest->mydest == DestNone)
2766  {
2767  /* Don't return SPI_OK_SELECT if we're discarding result */
2768  res = SPI_OK_UTILITY;
2769  }
2770  else
2771  res = SPI_OK_SELECT;
2772  break;
2773  case CMD_INSERT:
2774  if (queryDesc->plannedstmt->hasReturning)
2776  else
2777  res = SPI_OK_INSERT;
2778  break;
2779  case CMD_DELETE:
2780  if (queryDesc->plannedstmt->hasReturning)
2782  else
2783  res = SPI_OK_DELETE;
2784  break;
2785  case CMD_UPDATE:
2786  if (queryDesc->plannedstmt->hasReturning)
2788  else
2789  res = SPI_OK_UPDATE;
2790  break;
2791  default:
2792  return SPI_ERROR_OPUNKNOWN;
2793  }
2794 
2795 #ifdef SPI_EXECUTOR_STATS
2796  if (ShowExecutorStats)
2797  ResetUsage();
2798 #endif
2799 
2800  /* Select execution options */
2801  if (fire_triggers)
2802  eflags = 0; /* default run-to-completion flags */
2803  else
2804  eflags = EXEC_FLAG_SKIP_TRIGGERS;
2805 
2806  ExecutorStart(queryDesc, eflags);
2807 
2808  ExecutorRun(queryDesc, ForwardScanDirection, tcount, true);
2809 
2810  _SPI_current->processed = queryDesc->estate->es_processed;
2811 
2812  if ((res == SPI_OK_SELECT || queryDesc->plannedstmt->hasReturning) &&
2813  queryDesc->dest->mydest == DestSPI)
2814  {
2815  if (_SPI_checktuples())
2816  elog(ERROR, "consistency check on SPI tuple count failed");
2817  }
2818 
2819  ExecutorFinish(queryDesc);
2820  ExecutorEnd(queryDesc);
2821  /* FreeQueryDesc is done by the caller */
2822 
2823 #ifdef SPI_EXECUTOR_STATS
2824  if (ShowExecutorStats)
2825  ShowUsage("SPI EXECUTOR STATS");
2826 #endif
2827 
2828  return res;
2829 }
EState * estate
Definition: execdesc.h:48
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:93
Definition: dest.h:94
void ShowUsage(const char *title)
Definition: postgres.c:4806
#define SPI_OK_DELETE
Definition: spi.h:89
CommandDest mydest
Definition: dest.h:129
void ExecutorStart(QueryDesc *queryDesc, int eflags)
Definition: execMain.c:130
#define SPI_ERROR_OPUNKNOWN
Definition: spi.h:70
static _SPI_connection * _SPI_current
Definition: spi.c:50
static bool _SPI_checktuples(void)
Definition: spi.c:2993
void ExecutorEnd(QueryDesc *queryDesc)
Definition: execMain.c:459
void ResetUsage(void)
Definition: postgres.c:4799
#define ERROR
Definition: elog.h:46
void ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count, bool execute_once)
Definition: execMain.c:298
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:92
Definition: dest.h:89
bool hasReturning
Definition: plannodes.h:50
#define SPI_OK_UTILITY
Definition: spi.h:85
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:94
void ExecutorFinish(QueryDesc *queryDesc)
Definition: execMain.c:399
CmdType operation
Definition: execdesc.h:36
#define SPI_OK_SELECT
Definition: spi.h:86
uint64 processed
Definition: spi_priv.h:25
uint64 es_processed
Definition: execnodes.h:605
#define EXEC_FLAG_SKIP_TRIGGERS
Definition: executor.h:60
DestReceiver * dest
Definition: execdesc.h:41
#define SPI_OK_UPDATE
Definition: spi.h:90
#define elog(elevel,...)
Definition: elog.h:232
#define SPI_OK_INSERT
Definition: spi.h:88
PlannedStmt * plannedstmt
Definition: execdesc.h:37

◆ _SPI_prepare_oneshot_plan()

static void _SPI_prepare_oneshot_plan ( const char *  src,
SPIPlanPtr  plan 
)
static

Definition at line 2226 of file spi.c.

References _SPI_error_callback(), ErrorContextCallback::arg, ErrorContextCallback::callback, CreateCommandTag(), CreateOneShotCachedPlan(), error_context_stack, lappend(), lfirst_node, SPICallbackArg::mode, NIL, _SPI_plan::oneshot, _SPI_plan::parse_mode, _SPI_plan::plancache_list, ErrorContextCallback::previous, SPICallbackArg::query, raw_parser(), and RawStmt::stmt.

Referenced by SPI_execute(), SPI_execute_extended(), and SPI_execute_with_args().

2227 {
2228  List *raw_parsetree_list;
2229  List *plancache_list;
2230  ListCell *list_item;
2231  SPICallbackArg spicallbackarg;
2232  ErrorContextCallback spierrcontext;
2233 
2234  /*
2235  * Setup error traceback support for ereport()
2236  */
2237  spicallbackarg.query = src;
2238  spicallbackarg.mode = plan->parse_mode;
2239  spierrcontext.callback = _SPI_error_callback;
2240  spierrcontext.arg = &spicallbackarg;
2241  spierrcontext.previous = error_context_stack;
2242  error_context_stack = &spierrcontext;
2243 
2244  /*
2245  * Parse the request string into a list of raw parse trees.
2246  */
2247  raw_parsetree_list = raw_parser(src, plan->parse_mode);
2248 
2249  /*
2250  * Construct plancache entries, but don't do parse analysis yet.
2251  */
2252  plancache_list = NIL;
2253 
2254  foreach(list_item, raw_parsetree_list)
2255  {
2256  RawStmt *parsetree = lfirst_node(RawStmt, list_item);
2257  CachedPlanSource *plansource;
2258 
2259  plansource = CreateOneShotCachedPlan(parsetree,
2260  src,
2261  CreateCommandTag(parsetree->stmt));
2262 
2263  plancache_list = lappend(plancache_list, plansource);
2264  }
2265 
2266  plan->plancache_list = plancache_list;
2267  plan->oneshot = true;
2268 
2269  /*
2270  * Pop the error context stack
2271  */
2272  error_context_stack = spierrcontext.previous;
2273 }
#define NIL
Definition: pg_list.h:65
RawParseMode mode
Definition: spi.c:57
List * plancache_list
Definition: spi_priv.h:95
CachedPlanSource * CreateOneShotCachedPlan(RawStmt *raw_parse_tree, const char *query_string, CommandTag commandTag)
Definition: plancache.c:248
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
ErrorContextCallback * error_context_stack
Definition: elog.c:93
RawParseMode parse_mode
Definition: spi_priv.h:97
static void _SPI_error_callback(void *arg)
Definition: spi.c:2837
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Node * stmt
Definition: parsenodes.h:1572
List * lappend(List *list, void *datum)
Definition: list.c:336
CommandTag CreateCommandTag(Node *parsetree)
Definition: utility.c:2337
bool oneshot
Definition: spi_priv.h:94
const char * query
Definition: spi.c:56
List * raw_parser(const char *str, RawParseMode mode)
Definition: parser.c:42
Definition: pg_list.h:50

◆ _SPI_prepare_plan()

static void _SPI_prepare_plan ( const char *  src,
SPIPlanPtr  plan 
)
static

Definition at line 2118 of file spi.c.

References _SPI_error_callback(), ErrorContextCallback::arg, _SPI_plan::argtypes, Assert, ErrorContextCallback::callback, CompleteCachedPlan(), CreateCachedPlan(), CreateCommandTag(), _SPI_plan::cursor_options, error_context_stack, lappend(), lfirst_node, SPICallbackArg::mode, _SPI_plan::nargs, NIL, _SPI_plan::oneshot, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, pg_analyze_and_rewrite(), pg_analyze_and_rewrite_params(), _SPI_plan::plancache_list, ErrorContextCallback::previous, SPICallbackArg::query, _SPI_connection::queryEnv, raw_parser(), and RawStmt::stmt.

Referenced by SPI_cursor_open_with_args(), SPI_cursor_parse_open(), SPI_prepare_cursor(), SPI_prepare_extended(), and SPI_prepare_params().

2119 {
2120  List *raw_parsetree_list;
2121  List *plancache_list;
2122  ListCell *list_item;
2123  SPICallbackArg spicallbackarg;
2124  ErrorContextCallback spierrcontext;
2125 
2126  /*
2127  * Setup error traceback support for ereport()
2128  */
2129  spicallbackarg.query = src;
2130  spicallbackarg.mode = plan->parse_mode;
2131  spierrcontext.callback = _SPI_error_callback;
2132  spierrcontext.arg = &spicallbackarg;
2133  spierrcontext.previous = error_context_stack;
2134  error_context_stack = &spierrcontext;
2135 
2136  /*
2137  * Parse the request string into a list of raw parse trees.
2138  */
2139  raw_parsetree_list = raw_parser(src, plan->parse_mode);
2140 
2141  /*
2142  * Do parse analysis and rule rewrite for each raw parsetree, storing the
2143  * results into unsaved plancache entries.
2144  */
2145  plancache_list = NIL;
2146 
2147  foreach(list_item, raw_parsetree_list)
2148  {
2149  RawStmt *parsetree = lfirst_node(RawStmt, list_item);
2150  List *stmt_list;
2151  CachedPlanSource *plansource;
2152 
2153  /*
2154  * Create the CachedPlanSource before we do parse analysis, since it
2155  * needs to see the unmodified raw parse tree.
2156  */
2157  plansource = CreateCachedPlan(parsetree,
2158  src,
2159  CreateCommandTag(parsetree->stmt));
2160 
2161  /*
2162  * Parameter datatypes are driven by parserSetup hook if provided,
2163  * otherwise we use the fixed parameter list.
2164  */
2165  if (plan->parserSetup != NULL)
2166  {
2167  Assert(plan->nargs == 0);
2168  stmt_list = pg_analyze_and_rewrite_params(parsetree,
2169  src,
2170  plan->parserSetup,
2171  plan->parserSetupArg,
2173  }
2174  else
2175  {
2176  stmt_list = pg_analyze_and_rewrite(parsetree,
2177  src,
2178  plan->argtypes,
2179  plan->nargs,
2181  }
2182 
2183  /* Finish filling in the CachedPlanSource */
2184  CompleteCachedPlan(plansource,
2185  stmt_list,
2186  NULL,
2187  plan->argtypes,
2188  plan->nargs,
2189  plan->parserSetup,
2190  plan->parserSetupArg,
2191  plan->cursor_options,
2192  false); /* not fixed result */
2193 
2194  plancache_list = lappend(plancache_list, plansource);
2195  }
2196 
2197  plan->plancache_list = plancache_list;
2198  plan->oneshot = false;
2199 
2200  /*
2201  * Pop the error context stack
2202  */
2203  error_context_stack = spierrcontext.previous;
2204 }
#define NIL
Definition: pg_list.h:65
Oid * argtypes
Definition: spi_priv.h:100
RawParseMode mode
Definition: spi.c:57
List * plancache_list
Definition: spi_priv.h:95
CachedPlanSource * CreateCachedPlan(RawStmt *raw_parse_tree, const char *query_string, CommandTag commandTag)
Definition: plancache.c:164
static _SPI_connection * _SPI_current
Definition: spi.c:50
List * pg_analyze_and_rewrite(RawStmt *parsetree, const char *query_string, Oid *paramTypes, int numParams, QueryEnvironment *queryEnv)
Definition: postgres.c:642
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
ErrorContextCallback * error_context_stack
Definition: elog.c:93
RawParseMode parse_mode
Definition: spi_priv.h:97
static void _SPI_error_callback(void *arg)
Definition: spi.c:2837
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
#define lfirst_node(type, lc)
Definition: pg_list.h:172
Node * stmt
Definition: parsenodes.h:1572
List * lappend(List *list, void *datum)
Definition: list.c:336
int nargs
Definition: spi_priv.h:99
#define Assert(condition)
Definition: c.h:804
ParserSetupHook parserSetup
Definition: spi_priv.h:101
CommandTag CreateCommandTag(Node *parsetree)
Definition: utility.c:2337
void CompleteCachedPlan(CachedPlanSource *plansource, List *querytree_list, MemoryContext querytree_context, Oid *param_types, int num_params, ParserSetupHook parserSetup, void *parserSetupArg, int cursor_options, bool fixed_result)
Definition: plancache.c:338
bool oneshot
Definition: spi_priv.h:94
void * parserSetupArg
Definition: spi_priv.h:102
List * pg_analyze_and_rewrite_params(RawStmt *parsetree, const char *query_string, ParserSetupHook parserSetup, void *parserSetupArg, QueryEnvironment *queryEnv)
Definition: postgres.c:679
const char * query
Definition: spi.c:56
int cursor_options
Definition: spi_priv.h:98
List * raw_parser(const char *str, RawParseMode mode)
Definition: parser.c:42
Definition: pg_list.h:50

◆ _SPI_procmem()

static MemoryContext _SPI_procmem ( void  )
static

Definition at line 2940 of file spi.c.

References MemoryContextSwitchTo(), and _SPI_connection::procCxt.

Referenced by _SPI_end_call(), and spi_dest_startup().

2941 {
2943 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
MemoryContext procCxt
Definition: spi_priv.h:33

◆ _SPI_rollback()

static void _SPI_rollback ( bool  chain)
static

Definition at line 292 of file spi.c.

References AbortCurrentTransaction(), _SPI_connection::atomic, CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, ForgetPortalSnapshots(), HoldPinnedPortals(), _SPI_connection::internal_xact, IsSubTransaction(), MemoryContextSwitchTo(), RestoreTransactionCharacteristics(), SaveTransactionCharacteristics(), and StartTransactionCommand().

Referenced by SPI_rollback(), and SPI_rollback_and_chain().

293 {
294  MemoryContext oldcontext = CurrentMemoryContext;
295 
296  if (_SPI_current->atomic)
297  ereport(ERROR,
298  (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
299  errmsg("invalid transaction termination")));
300 
301  /* see under SPI_commit() */
302  if (IsSubTransaction())
303  ereport(ERROR,
304  (errcode(ERRCODE_INVALID_TRANSACTION_TERMINATION),
305  errmsg("cannot roll back while a subtransaction is active")));
306 
307  /*
308  * Hold any pinned portals that any PLs might be using. We have to do
309  * this before changing transaction state, since this will run
310  * user-defined code that might throw an error, and in any case couldn't
311  * be run in an already-aborted transaction.
312  */
314 
315  /* Start the actual rollback */
316  _SPI_current->internal_xact = true;
317 
318  /* Release snapshots associated with portals */
320 
321  if (chain)
323 
325 
326  if (chain)
327  {
330  }
331 
332  MemoryContextSwitchTo(oldcontext);
333 
334  _SPI_current->internal_xact = false;
335 }
void AbortCurrentTransaction(void)
Definition: xact.c:3230
bool internal_xact
Definition: spi_priv.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:698
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:46
void SaveTransactionCharacteristics(void)
Definition: xact.c:2939
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void ForgetPortalSnapshots(void)
Definition: portalmem.c:1303
#define ereport(elevel,...)
Definition: elog.h:157
void StartTransactionCommand(void)
Definition: xact.c:2858
void RestoreTransactionCharacteristics(void)
Definition: xact.c:2947
bool IsSubTransaction(void)
Definition: xact.c:4776
int errmsg(const char *fmt,...)
Definition: elog.c:909
void HoldPinnedPortals(void)
Definition: portalmem.c:1254

◆ _SPI_save_plan()

static SPIPlanPtr _SPI_save_plan ( SPIPlanPtr  plan)
static

Definition at line 3085 of file spi.c.

References _SPI_PLAN_MAGIC, ALLOCSET_SMALL_SIZES, AllocSetContextCreate, _SPI_plan::argtypes, Assert, CacheMemoryContext, CopyCachedPlan(), CurrentMemoryContext, _SPI_plan::cursor_options, lappend(), lfirst, _SPI_plan::magic, MemoryContextSetParent(), MemoryContextSwitchTo(), _SPI_plan::nargs, _SPI_plan::oneshot, palloc(), palloc0(), _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, _SPI_plan::plancache_list, _SPI_plan::plancxt, SaveCachedPlan(), and _SPI_plan::saved.

Referenced by SPI_saveplan().

3086 {
3087  SPIPlanPtr newplan;
3088  MemoryContext plancxt;
3089  MemoryContext oldcxt;
3090  ListCell *lc;
3091 
3092  /* One-shot plans can't be saved */
3093  Assert(!plan->oneshot);
3094 
3095  /*
3096  * Create a memory context for the plan. We don't expect the plan to be
3097  * very large, so use smaller-than-default alloc parameters. It's a
3098  * transient context until we finish copying everything.
3099  */
3101  "SPI Plan",
3103  oldcxt = MemoryContextSwitchTo(plancxt);
3104 
3105  /* Copy the SPI plan into its own context */
3106  newplan = (SPIPlanPtr) palloc0(sizeof(_SPI_plan));
3107  newplan->magic = _SPI_PLAN_MAGIC;
3108  newplan->plancxt = plancxt;
3109  newplan->parse_mode = plan->parse_mode;
3110  newplan->cursor_options = plan->cursor_options;
3111  newplan->nargs = plan->nargs;
3112  if (plan->nargs > 0)
3113  {
3114  newplan->argtypes = (Oid *) palloc(plan->nargs * sizeof(Oid));
3115  memcpy(newplan->argtypes, plan->argtypes, plan->nargs * sizeof(Oid));
3116  }
3117  else
3118  newplan->argtypes = NULL;
3119  newplan->parserSetup = plan->parserSetup;
3120  newplan->parserSetupArg = plan->parserSetupArg;
3121 
3122  /* Copy all the plancache entries */
3123  foreach(lc, plan->plancache_list)
3124  {
3125  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
3126  CachedPlanSource *newsource;
3127 
3128  newsource = CopyCachedPlan(plansource);
3129  newplan->plancache_list = lappend(newplan->plancache_list, newsource);
3130  }
3131 
3132  MemoryContextSwitchTo(oldcxt);
3133 
3134  /*
3135  * Mark it saved, reparent it under CacheMemoryContext, and mark all the
3136  * component CachedPlanSources as saved. This sequence cannot fail
3137  * partway through, so there's no risk of long-term memory leakage.
3138  */
3139  newplan->saved = true;
3141 
3142  foreach(lc, newplan->plancache_list)
3143  {
3144  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
3145 
3146  SaveCachedPlan(plansource);
3147  }
3148 
3149  return newplan;
3150 }
Oid * argtypes
Definition: spi_priv.h:100
#define AllocSetContextCreate
Definition: memutils.h:173
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:361
int magic
Definition: spi_priv.h:92
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:205
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
RawParseMode parse_mode
Definition: spi_priv.h:97
CachedPlanSource * CopyCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:1507
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
MemoryContext plancxt
Definition: spi_priv.h:96
List * lappend(List *list, void *datum)
Definition: list.c:336
void * palloc0(Size size)
Definition: mcxt.c:1093
bool saved
Definition: spi_priv.h:93
int nargs
Definition: spi_priv.h:99
void SaveCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:454
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
ParserSetupHook parserSetup
Definition: spi_priv.h:101
bool oneshot
Definition: spi_priv.h:94
void * parserSetupArg
Definition: spi_priv.h:102
void * palloc(Size size)
Definition: mcxt.c:1062
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:66
int cursor_options
Definition: spi_priv.h:98
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ AtEOSubXact_SPI()

void AtEOSubXact_SPI ( bool  isCommit,
SubTransactionId  mySubid 
)

Definition at line 390 of file spi.c.

References _SPI_connected, _SPI_connection::connectSubid, slist_mutable_iter::cur, ereport, errcode(), errhint(), errmsg(), _SPI_connection::execCxt, _SPI_connection::execSubid, _SPI_connection::internal_xact, InvalidSubTransactionId, MemoryContextDelete(), MemoryContextResetAndDeleteChildren, next, _SPI_connection::outer_processed, _SPI_connection::outer_result, _SPI_connection::outer_tuptable, _SPI_connection::procCxt, slist_container, slist_delete_current(), slist_foreach_modify, SPI_processed, SPI_result, SPITupleTable::subid, SPITupleTable::tuptabcxt, _SPI_connection::tuptable, _SPI_connection::tuptables, and WARNING.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

391 {
392  bool found = false;
393 
394  while (_SPI_connected >= 0)
395  {
397 
398  if (connection->connectSubid != mySubid)
399  break; /* couldn't be any underneath it either */
400 
401  if (connection->internal_xact)
402  break;
403 
404  found = true;
405 
406  /*
407  * Release procedure memory explicitly (see note in SPI_connect)
408  */
409  if (connection->execCxt)
410  {
411  MemoryContextDelete(connection->execCxt);
412  connection->execCxt = NULL;
413  }
414  if (connection->procCxt)
415  {
416  MemoryContextDelete(connection->procCxt);
417  connection->procCxt = NULL;
418  }
419 
420  /*
421  * Restore outer global variables and pop the stack entry. Unlike
422  * SPI_finish(), we don't risk switching to memory contexts that might
423  * be already gone.
424  */
425  SPI_processed = connection->outer_processed;
426  SPI_tuptable = connection->outer_tuptable;
427  SPI_result = connection->outer_result;
428 
429  _SPI_connected--;
430  if (_SPI_connected < 0)
431  _SPI_current = NULL;
432  else
434  }
435 
436  if (found && isCommit)
438  (errcode(ERRCODE_WARNING),
439  errmsg("subtransaction left non-empty SPI stack"),
440  errhint("Check for missing \"SPI_finish\" calls.")));
441 
442  /*
443  * If we are aborting a subtransaction and there is an open SPI context
444  * surrounding the subxact, clean up to prevent memory leakage.
445  */
446  if (_SPI_current && !isCommit)
447  {
448  slist_mutable_iter siter;
449 
450  /*
451  * Throw away executor state if current executor operation was started
452  * within current subxact (essentially, force a _SPI_end_call(true)).
453  */
454  if (_SPI_current->execSubid >= mySubid)
455  {
458  }
459 
460  /* throw away any tuple tables created within current subxact */
462  {
463  SPITupleTable *tuptable;
464 
465  tuptable = slist_container(SPITupleTable, next, siter.cur);
466  if (tuptable->subid >= mySubid)
467  {
468  /*
469  * If we used SPI_freetuptable() here, its internal search of
470  * the tuptables list would make this operation O(N^2).
471  * Instead, just free the tuptable manually. This should
472  * match what SPI_freetuptable() does.
473  */
474  slist_delete_current(&siter);
475  if (tuptable == _SPI_current->tuptable)
476  _SPI_current->tuptable = NULL;
477  if (tuptable == SPI_tuptable)
478  SPI_tuptable = NULL;
479  MemoryContextDelete(tuptable->tuptabcxt);
480  }
481  }
482  }
483 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
int errhint(const char *fmt,...)
Definition: elog.c:1156
static int32 next
Definition: blutils.c:219
bool internal_xact
Definition: spi_priv.h:42
slist_node * cur
Definition: ilist.h:241
SPITupleTable * SPI_tuptable
Definition: spi.c:46
int errcode(int sqlerrcode)
Definition: elog.c:698
static int _SPI_connected
Definition: spi.c:52
SubTransactionId execSubid
Definition: spi_priv.h:29
static _SPI_connection * _SPI_current
Definition: spi.c:50
uint64 SPI_processed
Definition: spi.c:45
SPITupleTable * outer_tuptable
Definition: spi_priv.h:47
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:735
int SPI_result
Definition: spi.c:47
SubTransactionId connectSubid
Definition: spi_priv.h:36
SPITupleTable * tuptable
Definition: spi_priv.h:26
static _SPI_connection * _SPI_stack
Definition: spi.c:49
#define WARNING
Definition: elog.h:40
uint64 outer_processed
Definition: spi_priv.h:46
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
MemoryContext procCxt
Definition: spi_priv.h:33
MemoryContext execCxt
Definition: spi_priv.h:34
#define ereport(elevel,...)
Definition: elog.h:157
int outer_result
Definition: spi_priv.h:48
#define InvalidSubTransactionId
Definition: c.h:593
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:671
SubTransactionId subid
Definition: spi.h:33
slist_head tuptables
Definition: spi_priv.h:32
MemoryContext tuptabcxt
Definition: spi.h:31

◆ AtEOXact_SPI()

void AtEOXact_SPI ( bool  isCommit)

Definition at line 368 of file spi.c.

References _SPI_connected, ereport, errcode(), errhint(), errmsg(), _SPI_connection::internal_xact, SPICleanup(), and WARNING.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

369 {
370  /* Do nothing if the transaction end was initiated by SPI. */
372  return;
373 
374  if (isCommit && _SPI_connected != -1)
376  (errcode(ERRCODE_WARNING),
377  errmsg("transaction left non-empty SPI stack"),
378  errhint("Check for missing \"SPI_finish\" calls.")));
379 
380  SPICleanup();
381 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
bool internal_xact
Definition: spi_priv.h:42
void SPICleanup(void)
Definition: spi.c:354
int errcode(int sqlerrcode)
Definition: elog.c:698
static int _SPI_connected
Definition: spi.c:52
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ SPI_commit()

void SPI_commit ( void  )

Definition at line 280 of file spi.c.

References _SPI_commit().

Referenced by exec_stmt_commit(), plperl_spi_commit(), pltcl_commit(), and PLy_commit().

281 {
282  _SPI_commit(false);
283 }
static void _SPI_commit(bool chain)
Definition: spi.c:227

◆ SPI_commit_and_chain()

void SPI_commit_and_chain ( void  )

Definition at line 286 of file spi.c.

References _SPI_commit().

Referenced by exec_stmt_commit().

287 {
288  _SPI_commit(true);
289 }
static void _SPI_commit(bool chain)
Definition: spi.c:227

◆ SPI_connect()

◆ SPI_connect_ext()

int SPI_connect_ext ( int  options)

Definition at line 101 of file spi.c.

References _SPI_connected, _SPI_stack_depth, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, _SPI_connection::atomic, _SPI_connection::connectSubid, elog, ERROR, _SPI_connection::execCxt, _SPI_connection::execSubid, false, GetCurrentSubTransactionId(), _SPI_connection::internal_xact, InvalidSubTransactionId, MemoryContextAlloc(), MemoryContextSwitchTo(), _SPI_connection::outer_processed, _SPI_connection::outer_result, _SPI_connection::outer_tuptable, PortalContext, _SPI_connection::procCxt, _SPI_connection::processed, _SPI_connection::queryEnv, repalloc(), _SPI_connection::savedcxt, slist_init(), SPI_OK_CONNECT, SPI_OPT_NONATOMIC, SPI_processed, SPI_result, SPI_tuptable, TopMemoryContext, TopTransactionContext, _SPI_connection::tuptable, and _SPI_connection::tuptables.

Referenced by plperl_func_handler(), plperl_inline_handler(), plpgsql_call_handler(), plpgsql_inline_handler(), plpython_call_handler(), plpython_inline_handler(), pltcl_func_handler(), and SPI_connect().

102 {
103  int newdepth;
104 
105  /* Enlarge stack if necessary */
106  if (_SPI_stack == NULL)
107  {
108  if (_SPI_connected != -1 || _SPI_stack_depth != 0)
109  elog(ERROR, "SPI stack corrupted");
110  newdepth = 16;
113  newdepth * sizeof(_SPI_connection));
114  _SPI_stack_depth = newdepth;
115  }
116  else
117  {
119  elog(ERROR, "SPI stack corrupted");
120  if (_SPI_stack_depth == _SPI_connected + 1)
121  {
122  newdepth = _SPI_stack_depth * 2;
125  newdepth * sizeof(_SPI_connection));
126  _SPI_stack_depth = newdepth;
127  }
128  }
129 
130  /* Enter new stack level */
131  _SPI_connected++;
133 
135  _SPI_current->processed = 0;
136  _SPI_current->tuptable = NULL;
139  _SPI_current->procCxt = NULL; /* in case we fail to create 'em */
140  _SPI_current->execCxt = NULL;
142  _SPI_current->queryEnv = NULL;
144  _SPI_current->internal_xact = false;
148 
149  /*
150  * Create memory contexts for this procedure
151  *
152  * In atomic contexts (the normal case), we use TopTransactionContext,
153  * otherwise PortalContext, so that it lives across transaction
154  * boundaries.
155  *
156  * XXX It could be better to use PortalContext as the parent context in
157  * all cases, but we may not be inside a portal (consider deferred-trigger
158  * execution). Perhaps CurTransactionContext could be an option? For now
159  * it doesn't matter because we clean up explicitly in AtEOSubXact_SPI().
160  */
162  "SPI Proc",
165  "SPI Exec",
167  /* ... and switch to procedure's context */
169 
170  /*
171  * Reset API global variables so that current caller cannot accidentally
172  * depend on state of an outer caller.
173  */
174  SPI_processed = 0;
175  SPI_tuptable = NULL;
176  SPI_result = 0;
177 
178  return SPI_OK_CONNECT;
179 }
#define SPI_OK_CONNECT
Definition: spi.h:82
#define AllocSetContextCreate
Definition: memutils.h:173
MemoryContext TopTransactionContext
Definition: mcxt.c:53
static int _SPI_stack_depth
Definition: spi.c:51
bool internal_xact
Definition: spi_priv.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static int _SPI_connected
Definition: spi.c:52
SubTransactionId execSubid
Definition: spi_priv.h:29
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define false
Definition: c.h:399
uint64 SPI_processed
Definition: spi.c:45
SPITupleTable * outer_tuptable
Definition: spi_priv.h:47
MemoryContext PortalContext
Definition: mcxt.c:57
int SPI_result
Definition: spi.c:47
SubTransactionId connectSubid
Definition: spi_priv.h:36
static void slist_init(slist_head *head)
Definition: ilist.h:573
#define ERROR
Definition: elog.h:46
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
#define SPI_OPT_NONATOMIC
Definition: spi.h:100
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static _SPI_connection * _SPI_stack
Definition: spi.c:49
MemoryContext TopMemoryContext
Definition: mcxt.c:48
MemoryContext savedcxt
Definition: spi_priv.h:35
uint64 outer_processed
Definition: spi_priv.h:46
MemoryContext procCxt
Definition: spi_priv.h:33
MemoryContext execCxt
Definition: spi_priv.h:34
uint64 processed
Definition: spi_priv.h:25
#define Assert(condition)
Definition: c.h:804
int outer_result
Definition: spi_priv.h:48
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:724
#define InvalidSubTransactionId
Definition: c.h:593
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232
slist_head tuptables
Definition: spi_priv.h:32

◆ SPI_copytuple()

HeapTuple SPI_copytuple ( HeapTuple  tuple)

Definition at line 952 of file spi.c.

References heap_copytuple(), MemoryContextSwitchTo(), _SPI_connection::savedcxt, SPI_ERROR_ARGUMENT, SPI_ERROR_UNCONNECTED, and SPI_result.

Referenced by get_tuple_of_interest(), and plpgsql_exec_trigger().

953 {
954  MemoryContext oldcxt;
955  HeapTuple ctuple;
956 
957  if (tuple == NULL)
958  {
960  return NULL;
961  }
962 
963  if (_SPI_current == NULL)
964  {
966  return NULL;
967  }
968 
970 
971  ctuple = heap_copytuple(tuple);
972 
973  MemoryContextSwitchTo(oldcxt);
974 
975  return ctuple;
976 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:71
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
MemoryContext savedcxt
Definition: spi_priv.h:35

◆ SPI_cursor_close()

void SPI_cursor_close ( Portal  portal)

Definition at line 1767 of file spi.c.

References elog, ERROR, PortalDrop(), and PortalIsValid.

Referenced by exec_stmt_close(), exec_stmt_dynfors(), exec_stmt_forc(), exec_stmt_fors(), plperl_spi_cursor_close(), plperl_spi_fetchrow(), PLy_cursor_close(), PLy_cursor_dealloc(), query_to_xml_and_xmlschema(), query_to_xmlschema(), ts_stat_sql(), and tsquery_rewrite_query().

1768 {
1769  if (!PortalIsValid(portal))
1770  elog(ERROR, "invalid portal in SPI cursor operation");
1771 
1772  PortalDrop(portal, false);
1773 }
#define ERROR
Definition: elog.h:46
#define PortalIsValid(p)
Definition: portal.h:212
void PortalDrop(Portal portal, bool isTopCommit)
Definition: portalmem.c:468
#define elog(elevel,...)
Definition: elog.h:232

◆ SPI_cursor_fetch()

void SPI_cursor_fetch ( Portal  portal,
bool  forward,
long  count 
)

Definition at line 1711 of file spi.c.

References _SPI_cursor_operation(), CreateDestReceiver(), DestSPI, FETCH_BACKWARD, and FETCH_FORWARD.

Referenced by cursor_to_xml(), exec_for_query(), plperl_spi_fetchrow(), PLy_cursor_fetch(), PLy_cursor_iternext(), ts_stat_sql(), and tsquery_rewrite_query().

1712 {
1713  _SPI_cursor_operation(portal,
1714  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1716  /* we know that the DestSPI receiver doesn't need a destroy call */
1717 }
Definition: dest.h:94
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:113
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2883

◆ SPI_cursor_find()

Portal SPI_cursor_find ( const char *  name)

Definition at line 1699 of file spi.c.

References GetPortalByName().

Referenced by cursor_to_xml(), cursor_to_xmlschema(), exec_stmt_close(), exec_stmt_fetch(), exec_stmt_forc(), exec_stmt_open(), plperl_spi_cursor_close(), and plperl_spi_fetchrow().

1700 {
1701  return GetPortalByName(name);
1702 }
Portal GetPortalByName(const char *name)
Definition: portalmem.c:130
const char * name
Definition: encode.c:561

◆ SPI_cursor_move()

void SPI_cursor_move ( Portal  portal,
bool  forward,
long  count 
)

Definition at line 1726 of file spi.c.

References _SPI_cursor_operation(), FETCH_BACKWARD, FETCH_FORWARD, and None_Receiver.

1727 {
1728  _SPI_cursor_operation(portal,
1729  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1730  None_Receiver);
1731 }
DestReceiver * None_Receiver
Definition: dest.c:96
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2883

◆ SPI_cursor_open()

Portal SPI_cursor_open ( const char *  name,
SPIPlanPtr  plan,
Datum Values,
const char *  Nulls,
bool  read_only 
)

Definition at line 1350 of file spi.c.

References _SPI_convert_params(), _SPI_plan::argtypes, _SPI_plan::nargs, pfree(), and SPI_cursor_open_internal().

Referenced by plperl_spi_query(), plperl_spi_query_prepared(), PLy_cursor_plan(), PLy_cursor_query(), query_to_xml_and_xmlschema(), query_to_xmlschema(), ts_stat_sql(), and tsquery_rewrite_query().

1353 {
1354  Portal portal;
1355  ParamListInfo paramLI;
1356 
1357  /* build transient ParamListInfo in caller's context */
1358  paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
1359  Values, Nulls);
1360 
1361  portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
1362 
1363  /* done with the transient ParamListInfo */
1364  if (paramLI)
1365  pfree(paramLI);
1366 
1367  return portal;
1368 }
Oid * argtypes
Definition: spi_priv.h:100
void pfree(void *pointer)
Definition: mcxt.c:1169
int nargs
Definition: spi_priv.h:99
const char * name
Definition: encode.c:561
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1482
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2731
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_cursor_open_internal()

static Portal SPI_cursor_open_internal ( const char *  name,
SPIPlanPtr  plan,
ParamListInfo  paramLI,
bool  read_only 
)
static

Definition at line 1482 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_error_callback(), ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, CMD_UTILITY, CommandCounterIncrement(), CommandIsReadOnly(), CachedPlanSource::commandTag, copyObject, copyParamList(), CreateCommandName(), CreateNewPortal(), CreatePortal(), CURSOR_OPT_NO_SCROLL, CURSOR_OPT_SCROLL, _SPI_plan::cursor_options, PortalData::cursorOptions, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, error_context_stack, ExecSupportsBackwardScan(), GetActiveSnapshot(), GetCachedPlan(), GetCommandTagName(), GetTransactionSnapshot(), lfirst_node, linitial, linitial_node, list_length(), MemoryContextStrdup(), MemoryContextSwitchTo(), SPICallbackArg::mode, NIL, _SPI_plan::parse_mode, _SPI_plan::plancache_list, PORTAL_MULTI_QUERY, PortalData::portalContext, PortalDefineQuery(), PortalStart(), ErrorContextCallback::previous, _SPI_connection::processed, SPICallbackArg::query, CachedPlanSource::query_string, _SPI_connection::queryEnv, PortalData::queryEnv, ReleaseCachedPlan(), _SPI_plan::saved, SPI_is_cursor_plan(), SPI_processed, CachedPlan::stmt_list, PortalData::strategy, and _SPI_connection::tuptable.

Referenced by SPI_cursor_open(), SPI_cursor_open_with_args(), SPI_cursor_open_with_paramlist(), and SPI_cursor_parse_open().

1484 {
1485  CachedPlanSource *plansource;
1486  CachedPlan *cplan;
1487  List *stmt_list;
1488  char *query_string;
1489  Snapshot snapshot;
1490  MemoryContext oldcontext;
1491  Portal portal;
1492  SPICallbackArg spicallbackarg;
1493  ErrorContextCallback spierrcontext;
1494 
1495  /*
1496  * Check that the plan is something the Portal code will special-case as
1497  * returning one tupleset.
1498  */
1499  if (!SPI_is_cursor_plan(plan))
1500  {
1501  /* try to give a good error message */
1502  const char *cmdtag;
1503 
1504  if (list_length(plan->plancache_list) != 1)
1505  ereport(ERROR,
1506  (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1507  errmsg("cannot open multi-query plan as cursor")));
1508  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1509  /* A SELECT that fails SPI_is_cursor_plan() must be SELECT INTO */
1510  if (plansource->commandTag == CMDTAG_SELECT)
1511  cmdtag = "SELECT INTO";
1512  else
1513  cmdtag = GetCommandTagName(plansource->commandTag);
1514  ereport(ERROR,
1515  (errcode(ERRCODE_INVALID_CURSOR_DEFINITION),
1516  /* translator: %s is name of a SQL command, eg INSERT */
1517  errmsg("cannot open %s query as cursor", cmdtag)));
1518  }
1519 
1520  Assert(list_length(plan->plancache_list) == 1);
1521  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1522 
1523  /* Push the SPI stack */
1524  if (_SPI_begin_call(true) < 0)
1525  elog(ERROR, "SPI_cursor_open called while not connected");
1526 
1527  /* Reset SPI result (note we deliberately don't touch lastoid) */
1528  SPI_processed = 0;
1529  SPI_tuptable = NULL;
1530  _SPI_current->processed = 0;
1531  _SPI_current->tuptable = NULL;
1532 
1533  /* Create the portal */
1534  if (name == NULL || name[0] == '\0')
1535  {
1536  /* Use a random nonconflicting name */
1537  portal = CreateNewPortal();
1538  }
1539  else
1540  {
1541  /* In this path, error if portal of same name already exists */
1542  portal = CreatePortal(name, false, false);
1543  }
1544 
1545  /* Copy the plan's query string into the portal */
1546  query_string = MemoryContextStrdup(portal->portalContext,
1547  plansource->query_string);
1548 
1549  /*
1550  * Setup error traceback support for ereport(), in case GetCachedPlan
1551  * throws an error.
1552  */
1553  spicallbackarg.query = plansource->query_string;
1554  spicallbackarg.mode = plan->parse_mode;
1555  spierrcontext.callback = _SPI_error_callback;
1556  spierrcontext.arg = &spicallbackarg;
1557  spierrcontext.previous = error_context_stack;
1558  error_context_stack = &spierrcontext;
1559 
1560  /*
1561  * Note: for a saved plan, we mustn't have any failure occur between
1562  * GetCachedPlan and PortalDefineQuery; that would result in leaking our
1563  * plancache refcount.
1564  */
1565 
1566  /* Replan if needed, and increment plan refcount for portal */
1567  cplan = GetCachedPlan(plansource, paramLI, NULL, _SPI_current->queryEnv);
1568  stmt_list = cplan->stmt_list;
1569 
1570  if (!plan->saved)
1571  {
1572  /*
1573  * We don't want the portal to depend on an unsaved CachedPlanSource,
1574  * so must copy the plan into the portal's context. An error here
1575  * will result in leaking our refcount on the plan, but it doesn't
1576  * matter because the plan is unsaved and hence transient anyway.
1577  */
1578  oldcontext = MemoryContextSwitchTo(portal->portalContext);
1579  stmt_list = copyObject(stmt_list);
1580  MemoryContextSwitchTo(oldcontext);
1581  ReleaseCachedPlan(cplan, NULL);
1582  cplan = NULL; /* portal shouldn't depend on cplan */
1583  }
1584 
1585  /*
1586  * Set up the portal.
1587  */
1588  PortalDefineQuery(portal,
1589  NULL, /* no statement name */
1590  query_string,
1591  plansource->commandTag,
1592  stmt_list,
1593  cplan);
1594 
1595  /*
1596  * Set up options for portal. Default SCROLL type is chosen the same way
1597  * as PerformCursorOpen does it.
1598  */
1599  portal->cursorOptions = plan->cursor_options;
1601  {
1602  if (list_length(stmt_list) == 1 &&
1603  linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
1604  linitial_node(PlannedStmt, stmt_list)->rowMarks == NIL &&
1605  ExecSupportsBackwardScan(linitial_node(PlannedStmt, stmt_list)->planTree))
1606  portal->cursorOptions |= CURSOR_OPT_SCROLL;
1607  else
1609  }
1610 
1611  /*
1612  * Disallow SCROLL with SELECT FOR UPDATE. This is not redundant with the
1613  * check in transformDeclareCursorStmt because the cursor options might
1614  * not have come through there.
1615  */
1616  if (portal->cursorOptions & CURSOR_OPT_SCROLL)
1617  {
1618  if (list_length(stmt_list) == 1 &&
1619  linitial_node(PlannedStmt, stmt_list)->commandType != CMD_UTILITY &&
1620  linitial_node(PlannedStmt, stmt_list)->rowMarks != NIL)
1621  ereport(ERROR,
1622  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1623  errmsg("DECLARE SCROLL CURSOR ... FOR UPDATE/SHARE is not supported"),
1624  errdetail("Scrollable cursors must be READ ONLY.")));
1625  }
1626 
1627  /* Make current query environment available to portal at execution time. */
1628  portal->queryEnv = _SPI_current->queryEnv;
1629 
1630  /*
1631  * If told to be read-only, we'd better check for read-only queries. This
1632  * can't be done earlier because we need to look at the finished, planned
1633  * queries. (In particular, we don't want to do it between GetCachedPlan
1634  * and PortalDefineQuery, because throwing an error between those steps
1635  * would result in leaking our plancache refcount.)
1636  */
1637  if (read_only)
1638  {
1639  ListCell *lc;
1640 
1641  foreach(lc, stmt_list)
1642  {
1643  PlannedStmt *pstmt = lfirst_node(PlannedStmt, lc);
1644 
1645  if (!CommandIsReadOnly(pstmt))
1646  ereport(ERROR,
1647  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1648  /* translator: %s is a SQL statement name */
1649  errmsg("%s is not allowed in a non-volatile function",
1650  CreateCommandName((Node *) pstmt))));
1651  }
1652  }
1653 
1654  /* Set up the snapshot to use. */
1655  if (read_only)
1656  snapshot = GetActiveSnapshot();
1657  else
1658  {
1660  snapshot = GetTransactionSnapshot();
1661  }
1662 
1663  /*
1664  * If the plan has parameters, copy them into the portal. Note that this
1665  * must be done after revalidating the plan, because in dynamic parameter
1666  * cases the set of parameters could have changed during re-parsing.
1667  */
1668  if (paramLI)
1669  {
1670  oldcontext = MemoryContextSwitchTo(portal->portalContext);
1671  paramLI = copyParamList(paramLI);
1672  MemoryContextSwitchTo(oldcontext);
1673  }
1674 
1675  /*
1676  * Start portal execution.
1677  */
1678  PortalStart(portal, paramLI, 0, snapshot);
1679 
1680  Assert(portal->strategy != PORTAL_MULTI_QUERY);
1681 
1682  /* Pop the error context stack */
1683  error_context_stack = spierrcontext.previous;
1684 
1685  /* Pop the SPI stack */
1686  _SPI_end_call(true);
1687 
1688  /* Return the created portal */
1689  return portal;
1690 }
ParamListInfo copyParamList(ParamListInfo from)
Definition: params.c:78
#define NIL
Definition: pg_list.h:65
bool CommandIsReadOnly(PlannedStmt *pstmt)
Definition: utility.c:100
Portal CreatePortal(const char *name, bool allowDup, bool dupSilent)
Definition: portalmem.c:175
RawParseMode mode
Definition: spi.c:57
List * plancache_list
Definition: spi_priv.h:95
Portal CreateNewPortal(void)
Definition: portalmem.c:235
void PortalStart(Portal portal, ParamListInfo params, int eflags, Snapshot snapshot)
Definition: pquery.c:430
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1815
static const char * CreateCommandName(Node *parsetree)
Definition: utility.h:103
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Snapshot GetActiveSnapshot(void)
Definition: snapmgr.c:801
SPITupleTable * SPI_tuptable
Definition: spi.c:46
Definition: nodes.h:538
int errcode(int sqlerrcode)
Definition: elog.c:698
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1264
static _SPI_connection * _SPI_current
Definition: spi.c:50
void PortalDefineQuery(Portal portal, const char *prepStmtName, const char *sourceText, CommandTag commandTag, List *stmts, CachedPlan *cplan)
Definition: portalmem.c:282
#define linitial_node(type, l)
Definition: pg_list.h:177
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
MemoryContext portalContext
Definition: portal.h:120
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:2821
uint64 SPI_processed
Definition: spi.c:45
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
ErrorContextCallback * error_context_stack
Definition: elog.c:93
RawParseMode parse_mode
Definition: spi_priv.h:97
static void _SPI_error_callback(void *arg)
Definition: spi.c:2837
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define lfirst_node(type, lc)
Definition: pg_list.h:172
int errdetail(const char *fmt,...)
Definition: elog.c:1042
PortalStrategy strategy
Definition: portal.h:146
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, ResourceOwner owner, QueryEnvironment *queryEnv)
Definition: plancache.c:1141
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
void CommandCounterIncrement(void)
Definition: xact.c:1022
bool ExecSupportsBackwardScan(Plan *node)
Definition: execAmi.c:512
bool saved
Definition: spi_priv.h:93
#define ereport(elevel,...)
Definition: elog.h:157
uint64 processed
Definition: spi_priv.h:25
#define Assert(condition)
Definition: c.h:804
const char * query_string
Definition: plancache.h:100
CommandTag commandTag
Definition: plancache.h:101
static int list_length(const List *l)
Definition: pg_list.h:149
const char * name
Definition: encode.c:561
QueryEnvironment * queryEnv
Definition: portal.h:143
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:45
int errmsg(const char *fmt,...)
Definition: elog.c:909
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1286
#define elog(elevel,...)
Definition: elog.h:232
#define CURSOR_OPT_SCROLL
Definition: parsenodes.h:2820
const char * query
Definition: spi.c:56
List * stmt_list
Definition: plancache.h:150
int cursor_options
Definition: spi_priv.h:98
int cursorOptions
Definition: portal.h:147
#define copyObject(obj)
Definition: nodes.h:654
Definition: pg_list.h:50

◆ SPI_cursor_open_with_args()

Portal SPI_cursor_open_with_args ( const char *  name,
const char *  src,
int  nargs,
Oid argtypes,
Datum Values,
const char *  Nulls,
bool  read_only,
int  cursorOptions 
)

Definition at line 1377 of file spi.c.

References _SPI_begin_call(), _SPI_convert_params(), _SPI_end_call(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::argtypes, _SPI_plan::cursor_options, elog, ERROR, _SPI_plan::magic, _SPI_plan::nargs, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, RAW_PARSE_DEFAULT, SPI_cursor_open_internal(), and SPI_result.

1382 {
1383  Portal result;
1384  _SPI_plan plan;
1385  ParamListInfo paramLI;
1386 
1387  if (src == NULL || nargs < 0)
1388  elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
1389 
1390  if (nargs > 0 && (argtypes == NULL || Values == NULL))
1391  elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
1392 
1393  SPI_result = _SPI_begin_call(true);
1394  if (SPI_result < 0)
1395  elog(ERROR, "SPI_cursor_open_with_args called while not connected");
1396 
1397  memset(&plan, 0, sizeof(_SPI_plan));
1398  plan.magic = _SPI_PLAN_MAGIC;
1400  plan.cursor_options = cursorOptions;
1401  plan.nargs = nargs;
1402  plan.argtypes = argtypes;
1403  plan.parserSetup = NULL;
1404  plan.parserSetupArg = NULL;
1405 
1406  /* build transient ParamListInfo in executor context */
1407  paramLI = _SPI_convert_params(nargs, argtypes,
1408  Values, Nulls);
1409 
1410  _SPI_prepare_plan(src, &plan);
1411 
1412  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1413 
1414  result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
1415 
1416  /* And clean up */
1417  _SPI_end_call(true);
1418 
1419  return result;
1420 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2118
Oid * argtypes
Definition: spi_priv.h:100
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:46
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
const char * name
Definition: encode.c:561
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1482
#define elog(elevel,...)
Definition: elog.h:232
int cursor_options
Definition: spi_priv.h:98
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2731
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_cursor_open_with_paramlist()

Portal SPI_cursor_open_with_paramlist ( const char *  name,
SPIPlanPtr  plan,
ParamListInfo  params,
bool  read_only 
)

Definition at line 1430 of file spi.c.

References SPI_cursor_open_internal().

Referenced by exec_run_select(), exec_stmt_forc(), and exec_stmt_open().

1432 {
1433  return SPI_cursor_open_internal(name, plan, params, read_only);
1434 }
const char * name
Definition: encode.c:561
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1482

◆ SPI_cursor_parse_open()

Portal SPI_cursor_parse_open ( const char *  name,
const char *  src,
const SPIParseOpenOptions options 
)

Definition at line 1438 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::cursor_options, SPIParseOpenOptions::cursorOptions, elog, ERROR, _SPI_plan::magic, SPIParseOpenOptions::params, _SPI_plan::parse_mode, _SPI_plan::parserSetup, ParamListInfoData::parserSetup, _SPI_plan::parserSetupArg, ParamListInfoData::parserSetupArg, RAW_PARSE_DEFAULT, SPIParseOpenOptions::read_only, SPI_cursor_open_internal(), and SPI_result.

Referenced by exec_dynquery_with_params().

1441 {
1442  Portal result;
1443  _SPI_plan plan;
1444 
1445  if (src == NULL || options == NULL)
1446  elog(ERROR, "SPI_cursor_parse_open called with invalid arguments");
1447 
1448  SPI_result = _SPI_begin_call(true);
1449  if (SPI_result < 0)
1450  elog(ERROR, "SPI_cursor_parse_open called while not connected");
1451 
1452  memset(&plan, 0, sizeof(_SPI_plan));
1453  plan.magic = _SPI_PLAN_MAGIC;
1455  plan.cursor_options = options->cursorOptions;
1456  if (options->params)
1457  {
1458  plan.parserSetup = options->params->parserSetup;
1459  plan.parserSetupArg = options->params->parserSetupArg;
1460  }
1461 
1462  _SPI_prepare_plan(src, &plan);
1463 
1464  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1465 
1466  result = SPI_cursor_open_internal(name, &plan,
1467  options->params, options->read_only);
1468 
1469  /* And clean up */
1470  _SPI_end_call(true);
1471 
1472  return result;
1473 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2118
void * parserSetupArg
Definition: params.h:117
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
ParamListInfo params
Definition: spi.h:60
int magic
Definition: spi_priv.h:92
ParserSetupHook parserSetup
Definition: params.h:116
bool read_only
Definition: spi.h:62
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:46
int cursorOptions
Definition: spi.h:61
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
const char * name
Definition: encode.c:561
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1482
#define elog(elevel,...)
Definition: elog.h:232
int cursor_options
Definition: spi_priv.h:98

◆ SPI_datumTransfer()

Datum SPI_datumTransfer ( Datum  value,
bool  typByVal,
int  typLen 
)

Definition at line 1266 of file spi.c.

References datumTransfer(), elog, ERROR, MemoryContextSwitchTo(), and _SPI_connection::savedcxt.

Referenced by coerce_function_result_tuple(), and plpgsql_exec_function().

1267 {
1268  MemoryContext oldcxt;
1269  Datum result;
1270 
1271  if (_SPI_current == NULL)
1272  elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
1273 
1275 
1276  result = datumTransfer(value, typByVal, typLen);
1277 
1278  MemoryContextSwitchTo(oldcxt);
1279 
1280  return result;
1281 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:46
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:411
static struct @143 value
Datum datumTransfer(Datum value, bool typByVal, int typLen)
Definition: datum.c:193
#define elog(elevel,...)
Definition: elog.h:232

◆ spi_dest_startup()

void spi_dest_startup ( DestReceiver self,
int  operation,
TupleDesc  typeinfo 
)

Definition at line 2020 of file spi.c.

References _SPI_procmem(), SPITupleTable::alloced, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CreateTupleDescCopy(), CurrentMemoryContext, elog, ERROR, GetCurrentSubTransactionId(), MemoryContextSwitchTo(), SPITupleTable::next, SPITupleTable::numvals, palloc(), palloc0(), slist_push_head(), SPITupleTable::subid, SPITupleTable::tupdesc, SPITupleTable::tuptabcxt, _SPI_connection::tuptable, _SPI_connection::tuptables, and SPITupleTable::vals.

2021 {
2022  SPITupleTable *tuptable;
2023  MemoryContext oldcxt;
2024  MemoryContext tuptabcxt;
2025 
2026  if (_SPI_current == NULL)
2027  elog(ERROR, "spi_dest_startup called while not connected to SPI");
2028 
2029  if (_SPI_current->tuptable != NULL)
2030  elog(ERROR, "improper call to spi_dest_startup");
2031 
2032  /* We create the tuple table context as a child of procCxt */
2033 
2034  oldcxt = _SPI_procmem(); /* switch to procedure memory context */
2035 
2037  "SPI TupTable",
2039  MemoryContextSwitchTo(tuptabcxt);
2040 
2041  _SPI_current->tuptable = tuptable = (SPITupleTable *)
2042  palloc0(sizeof(SPITupleTable));
2043  tuptable->tuptabcxt = tuptabcxt;
2044  tuptable->subid = GetCurrentSubTransactionId();
2045 
2046  /*
2047  * The tuptable is now valid enough to be freed by AtEOSubXact_SPI, so put
2048  * it onto the SPI context's tuptables list. This will ensure it's not
2049  * leaked even in the unlikely event the following few lines fail.
2050  */
2051  slist_push_head(&_SPI_current->tuptables, &tuptable->next);
2052 
2053  /* set up initial allocations */
2054  tuptable->alloced = 128;
2055  tuptable->vals = (HeapTuple *) palloc(tuptable->alloced * sizeof(HeapTuple));
2056  tuptable->numvals = 0;
2057  tuptable->tupdesc = CreateTupleDescCopy(typeinfo);
2058 
2059  MemoryContextSwitchTo(oldcxt);
2060 }
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define AllocSetContextCreate
Definition: memutils.h:173
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static void slist_push_head(slist_head *head, slist_node *node)
Definition: ilist.h:593
slist_node next
Definition: spi.h:32
static _SPI_connection * _SPI_current
Definition: spi.c:50
HeapTuple * vals
Definition: spi.h:26
uint64 numvals
Definition: spi.h:27
#define ERROR
Definition: elog.h:46
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void * palloc0(Size size)
Definition: mcxt.c:1093
TupleDesc tupdesc
Definition: spi.h:25
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:724
void * palloc(Size size)
Definition: mcxt.c:1062
#define elog(elevel,...)
Definition: elog.h:232
uint64 alloced
Definition: spi.h:30
static MemoryContext _SPI_procmem(void)
Definition: spi.c:2940
SubTransactionId subid
Definition: spi.h:33
slist_head tuptables
Definition: spi_priv.h:32
MemoryContext tuptabcxt
Definition: spi.h:31

◆ SPI_exec()

int SPI_exec ( const char *  src,
long  tcount 
)

Definition at line 535 of file spi.c.

References SPI_execute().

Referenced by get_tuple_of_interest(), refresh_by_match_merge(), and xpath_table().

536 {
537  return SPI_execute(src, false, tcount);
538 }
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:501

◆ SPI_execp()

int SPI_execp ( SPIPlanPtr  plan,
Datum Values,
const char *  Nulls,
long  tcount 
)

Definition at line 609 of file spi.c.

References SPI_execute_plan().

Referenced by check_foreign_key(), check_primary_key(), and ttdummy().

610 {
611  return SPI_execute_plan(plan, Values, Nulls, false, tcount);
612 }
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:577
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_execute()

int SPI_execute ( const char *  src,
bool  read_only,
long  tcount 
)

Definition at line 501 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_prepare_oneshot_plan(), CURSOR_OPT_PARALLEL_OK, _SPI_plan::cursor_options, InvalidSnapshot, _SPI_plan::magic, options, _SPI_plan::parse_mode, RAW_PARSE_DEFAULT, SPIExecuteOptions::read_only, SPI_ERROR_ARGUMENT, and SPIExecuteOptions::tcount.

Referenced by build_tuplestore_recursively(), crosstab(), get_crosstab_tuplestore(), initialize_worker_spi(), load_categories_hash(), plperl_spi_exec(), pltcl_SPI_execute(), PLy_spi_execute_query(), query_to_oid_list(), query_to_xml_internal(), refresh_by_match_merge(), and SPI_exec().

502 {
503  _SPI_plan plan;
505  int res;
506 
507  if (src == NULL || tcount < 0)
508  return SPI_ERROR_ARGUMENT;
509 
510  res = _SPI_begin_call(true);
511  if (res < 0)
512  return res;
513 
514  memset(&plan, 0, sizeof(_SPI_plan));
515  plan.magic = _SPI_PLAN_MAGIC;
518 
519  _SPI_prepare_oneshot_plan(src, &plan);
520 
521  memset(&options, 0, sizeof(options));
522  options.read_only = read_only;
523  options.tcount = tcount;
524 
525  res = _SPI_execute_plan(&plan, &options,
527  true);
528 
529  _SPI_end_call(true);
530  return res;
531 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2226
uint64 tcount
Definition: spi.h:52
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
static char ** options
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2829
int cursor_options
Definition: spi_priv.h:98

◆ SPI_execute_extended()

int SPI_execute_extended ( const char *  src,
const SPIExecuteOptions options 
)

Definition at line 542 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_prepare_oneshot_plan(), CURSOR_OPT_PARALLEL_OK, _SPI_plan::cursor_options, InvalidSnapshot, _SPI_plan::magic, SPIExecuteOptions::params, _SPI_plan::parse_mode, _SPI_plan::parserSetup, ParamListInfoData::parserSetup, _SPI_plan::parserSetupArg, ParamListInfoData::parserSetupArg, RAW_PARSE_DEFAULT, and SPI_ERROR_ARGUMENT.

Referenced by exec_stmt_dynexecute(), and exec_stmt_return_query().

544 {
545  int res;
546  _SPI_plan plan;
547 
548  if (src == NULL || options == NULL)
549  return SPI_ERROR_ARGUMENT;
550 
551  res = _SPI_begin_call(true);
552  if (res < 0)
553  return res;
554 
555  memset(&plan, 0, sizeof(_SPI_plan));
556  plan.magic = _SPI_PLAN_MAGIC;
559  if (options->params)
560  {
561  plan.parserSetup = options->params->parserSetup;
562  plan.parserSetupArg = options->params->parserSetupArg;
563  }
564 
565  _SPI_prepare_oneshot_plan(src, &plan);
566 
567  res = _SPI_execute_plan(&plan, options,
569  true);
570 
571  _SPI_end_call(true);
572  return res;
573 }
void * parserSetupArg
Definition: params.h:117
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2226
ParserSetupHook parserSetup
Definition: params.h:116
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
ParamListInfo params
Definition: spi.h:48
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
#define InvalidSnapshot
Definition: snapshot.h:123
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2829
int cursor_options
Definition: spi_priv.h:98

◆ SPI_execute_plan()

int SPI_execute_plan ( SPIPlanPtr  plan,
Datum Values,
const char *  Nulls,
bool  read_only,
long  tcount 
)

Definition at line 577 of file spi.c.

References _SPI_begin_call(), _SPI_convert_params(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_plan::argtypes, InvalidSnapshot, _SPI_plan::magic, _SPI_plan::nargs, options, SPIExecuteOptions::params, SPIExecuteOptions::read_only, SPI_ERROR_ARGUMENT, SPI_ERROR_PARAM, and SPIExecuteOptions::tcount.

Referenced by pg_get_ruledef_worker(), pg_get_viewdef_worker(), plperl_spi_exec_prepared(), pltcl_SPI_execute_plan(), PLy_spi_execute_plan(), SPI_execp(), and test_predtest().

579 {
581  int res;
582 
583  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
584  return SPI_ERROR_ARGUMENT;
585 
586  if (plan->nargs > 0 && Values == NULL)
587  return SPI_ERROR_PARAM;
588 
589  res = _SPI_begin_call(true);
590  if (res < 0)
591  return res;
592 
593  memset(&options, 0, sizeof(options));
594  options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
595  Values, Nulls);
596  options.read_only = read_only;
597  options.tcount = tcount;
598 
599  res = _SPI_execute_plan(plan, &options,
601  true);
602 
603  _SPI_end_call(true);
604  return res;
605 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:74
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
uint64 tcount
Definition: spi.h:52
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
static char ** options
#define InvalidSnapshot
Definition: snapshot.h:123
int nargs
Definition: spi_priv.h:99
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2731
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_execute_plan_extended()

int SPI_execute_plan_extended ( SPIPlanPtr  plan,
const SPIExecuteOptions options 
)

Definition at line 616 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, InvalidSnapshot, _SPI_plan::magic, and SPI_ERROR_ARGUMENT.

Referenced by exec_stmt_call(), and exec_stmt_return_query().

618 {
619  int res;
620 
621  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || options == NULL)
622  return SPI_ERROR_ARGUMENT;
623 
624  res = _SPI_begin_call(true);
625  if (res < 0)
626  return res;
627 
628  res = _SPI_execute_plan(plan, options,
630  true);
631 
632  _SPI_end_call(true);
633  return res;
634 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296

◆ SPI_execute_plan_with_paramlist()

int SPI_execute_plan_with_paramlist ( SPIPlanPtr  plan,
ParamListInfo  params,
bool  read_only,
long  tcount 
)

Definition at line 638 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, InvalidSnapshot, _SPI_plan::magic, options, SPIExecuteOptions::params, SPIExecuteOptions::read_only, SPI_ERROR_ARGUMENT, and SPIExecuteOptions::tcount.

Referenced by exec_run_select(), and exec_stmt_execsql().

640 {
642  int res;
643 
644  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
645  return SPI_ERROR_ARGUMENT;
646 
647  res = _SPI_begin_call(true);
648  if (res < 0)
649  return res;
650 
651  memset(&options, 0, sizeof(options));
652  options.params = params;
653  options.read_only = read_only;
654  options.tcount = tcount;
655 
656  res = _SPI_execute_plan(plan, &options,
658  true);
659 
660  _SPI_end_call(true);
661  return res;
662 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
uint64 tcount
Definition: spi.h:52
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
static char ** options
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296

◆ SPI_execute_snapshot()

int SPI_execute_snapshot ( SPIPlanPtr  plan,
Datum Values,
const char *  Nulls,
Snapshot  snapshot,
Snapshot  crosscheck_snapshot,
bool  read_only,
bool  fire_triggers,
long  tcount 
)

Definition at line 678 of file spi.c.

References _SPI_begin_call(), _SPI_convert_params(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_plan::argtypes, _SPI_plan::magic, _SPI_plan::nargs, options, SPIExecuteOptions::params, SPIExecuteOptions::read_only, SPI_ERROR_ARGUMENT, SPI_ERROR_PARAM, and SPIExecuteOptions::tcount.

Referenced by RI_Initial_Check(), RI_PartitionRemove_Check(), and ri_PerformCheck().

682 {
684  int res;
685 
686  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
687  return SPI_ERROR_ARGUMENT;
688 
689  if (plan->nargs > 0 && Values == NULL)
690  return SPI_ERROR_PARAM;
691 
692  res = _SPI_begin_call(true);
693  if (res < 0)
694  return res;
695 
696  memset(&options, 0, sizeof(options));
697  options.params = _SPI_convert_params(plan->nargs, plan->argtypes,
698  Values, Nulls);
699  options.read_only = read_only;
700  options.tcount = tcount;
701 
702  res = _SPI_execute_plan(plan, &options,
703  snapshot, crosscheck_snapshot,
704  fire_triggers);
705 
706  _SPI_end_call(true);
707  return res;
708 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:74
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
uint64 tcount
Definition: spi.h:52
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
static char ** options
int nargs
Definition: spi_priv.h:99
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2731
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_execute_with_args()

int SPI_execute_with_args ( const char *  src,
int  nargs,
Oid argtypes,
Datum Values,
const char *  Nulls,
bool  read_only,
long  tcount 
)

Definition at line 717 of file spi.c.

References _SPI_begin_call(), _SPI_convert_params(), _SPI_end_call(), _SPI_execute_plan(), _SPI_PLAN_MAGIC, _SPI_prepare_oneshot_plan(), _SPI_plan::argtypes, CURSOR_OPT_PARALLEL_OK, _SPI_plan::cursor_options, InvalidSnapshot, _SPI_plan::magic, _SPI_plan::nargs, options, SPIExecuteOptions::params, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, RAW_PARSE_DEFAULT, SPIExecuteOptions::read_only, SPI_ERROR_ARGUMENT, SPI_ERROR_PARAM, and SPIExecuteOptions::tcount.

721 {
722  int res;
723  _SPI_plan plan;
724  ParamListInfo paramLI;
726 
727  if (src == NULL || nargs < 0 || tcount < 0)
728  return SPI_ERROR_ARGUMENT;
729 
730  if (nargs > 0 && (argtypes == NULL || Values == NULL))
731  return SPI_ERROR_PARAM;
732 
733  res = _SPI_begin_call(true);
734  if (res < 0)
735  return res;
736 
737  memset(&plan, 0, sizeof(_SPI_plan));
738  plan.magic = _SPI_PLAN_MAGIC;
741  plan.nargs = nargs;
742  plan.argtypes = argtypes;
743  plan.parserSetup = NULL;
744  plan.parserSetupArg = NULL;
745 
746  paramLI = _SPI_convert_params(nargs, argtypes,
747  Values, Nulls);
748 
749  _SPI_prepare_oneshot_plan(src, &plan);
750 
751  memset(&options, 0, sizeof(options));
752  options.params = paramLI;
753  options.read_only = read_only;
754  options.tcount = tcount;
755 
756  res = _SPI_execute_plan(&plan, &options,
758  true);
759 
760  _SPI_end_call(true);
761  return res;
762 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:74
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2226
uint64 tcount
Definition: spi.h:52
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
static char ** options
#define InvalidSnapshot
Definition: snapshot.h:123
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
static int _SPI_execute_plan(SPIPlanPtr plan, const SPIExecuteOptions *options, Snapshot snapshot, Snapshot crosscheck_snapshot, bool fire_triggers)
Definition: spi.c:2296
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2829
int cursor_options
Definition: spi_priv.h:98
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2731
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_finish()

int SPI_finish ( void  )

Definition at line 182 of file spi.c.

References _SPI_begin_call(), _SPI_connected, _SPI_connection::execCxt, MemoryContextDelete(), MemoryContextSwitchTo(), _SPI_connection::outer_processed, _SPI_connection::outer_result, _SPI_connection::outer_tuptable, _SPI_connection::procCxt, _SPI_connection::savedcxt, SPI_OK_FINISH, SPI_processed, and SPI_result.

Referenced by check_foreign_key(), check_primary_key(), connectby(), crosstab(), cursor_to_xml(), cursor_to_xmlschema(), database_to_xml_internal(), database_to_xmlschema_internal(), get_crosstab_tuplestore(), get_tuple_of_interest(), initialize_worker_spi(), load_categories_hash(), pg_get_ruledef_worker(), pg_get_viewdef_worker(), plperl_event_trigger_handler(), plperl_func_handler(), plperl_inline_handler(), plperl_trigger_handler(), plpgsql_call_handler(), plpgsql_inline_handler(), plpgsql_validator(), pltcl_event_trigger_handler(), pltcl_func_handler(), pltcl_trigger_handler(), PLy_exec_function(), PLy_exec_trigger(), query_to_xml_and_xmlschema(), query_to_xml_internal(), query_to_xmlschema(), refresh_by_match_merge(), ri_Check_Pk_Match(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_restrict(), ri_set(), schema_to_xml_internal(), schema_to_xmlschema_internal(), test_predtest(), ts_stat1(), ts_stat2(), tsquery_rewrite_query(), ttdummy(), and xpath_table().

183 {
184  int res;
185 
186  res = _SPI_begin_call(false); /* just check we're connected */
187  if (res < 0)
188  return res;
189 
190  /* Restore memory context as it was before procedure call */
192 
193  /* Release memory used in procedure call (including tuptables) */
195  _SPI_current->execCxt = NULL;
197  _SPI_current->procCxt = NULL;
198 
199  /*
200  * Restore outer API variables, especially SPI_tuptable which is probably
201  * pointing at a just-deleted tuptable
202  */
206 
207  /* Exit stack level */
208  _SPI_connected--;
209  if (_SPI_connected < 0)
210  _SPI_current = NULL;
211  else
213 
214  return SPI_OK_FINISH;
215 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static int _SPI_connected
Definition: spi.c:52
static _SPI_connection * _SPI_current
Definition: spi.c:50
uint64 SPI_processed
Definition: spi.c:45
SPITupleTable * outer_tuptable
Definition: spi_priv.h:47
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
int SPI_result
Definition: spi.c:47
static _SPI_connection * _SPI_stack
Definition: spi.c:49
MemoryContext savedcxt
Definition: spi_priv.h:35
uint64 outer_processed
Definition: spi_priv.h:46
MemoryContext procCxt
Definition: spi_priv.h:33
MemoryContext execCxt
Definition: spi_priv.h:34
int outer_result
Definition: spi_priv.h:48
#define SPI_OK_FINISH
Definition: spi.h:83

◆ SPI_fname()

char* SPI_fname ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1103 of file spi.c.

References FirstLowInvalidHeapAttributeNumber, FormData_pg_attribute, NameStr, TupleDescData::natts, pstrdup(), SPI_ERROR_NOATTRIBUTE, SPI_result, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by get_pkey_attnames(), and SPI_sql_row_to_xmlelement().

1104 {
1105  const FormData_pg_attribute *att;
1106 
1107  SPI_result = 0;
1108 
1109  if (fnumber > tupdesc->natts || fnumber == 0 ||
1111  {
1113  return NULL;
1114  }
1115 
1116  if (fnumber > 0)
1117  att = TupleDescAttr(tupdesc, fnumber - 1);
1118  else
1119  att = SystemAttributeDefinition(fnumber);
1120 
1121  return pstrdup(NameStr(att->attname));
1122 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
FormData_pg_attribute
Definition: pg_attribute.h:191
#define NameStr(name)
Definition: c.h:681

◆ SPI_fnumber()

int SPI_fnumber ( TupleDesc  tupdesc,
const char *  fname 
)

Definition at line 1080 of file spi.c.

References FormData_pg_attribute, namestrcmp(), TupleDescData::natts, SPI_ERROR_NOATTRIBUTE, SystemAttributeByName(), and TupleDescAttr.

Referenced by autoinc(), check_foreign_key(), check_primary_key(), insert_username(), lo_manage(), make_ruledef(), make_viewdef(), moddatetime(), plperl_build_tuple_result(), plperl_modify_tuple(), pltcl_build_tuple_result(), PLy_modify_tuple(), tsvector_update_trigger(), and ttdummy().

1081 {
1082  int res;
1083  const FormData_pg_attribute *sysatt;
1084 
1085  for (res = 0; res < tupdesc->natts; res++)
1086  {
1087  Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
1088 
1089  if (namestrcmp(&attr->attname, fname) == 0 &&
1090  !attr->attisdropped)
1091  return res + 1;
1092  }
1093 
1094  sysatt = SystemAttributeByName(fname);
1095  if (sysatt != NULL)
1096  return sysatt->attnum;
1097 
1098  /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
1099  return SPI_ERROR_NOATTRIBUTE;
1100 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:247
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:263
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
FormData_pg_attribute
Definition: pg_attribute.h:191

◆ SPI_freeplan()

int SPI_freeplan ( SPIPlanPtr  plan)

Definition at line 930 of file spi.c.

References _SPI_PLAN_MAGIC, DropCachedPlan(), lfirst, _SPI_plan::magic, MemoryContextDelete(), _SPI_plan::plancache_list, _SPI_plan::plancxt, and SPI_ERROR_ARGUMENT.

Referenced by free_expr(), plperl_spi_freeplan(), plperl_spi_prepare(), plperl_spi_query(), PLy_cursor_query(), PLy_plan_dealloc(), ri_FetchPreparedPlan(), ts_stat_sql(), and tsquery_rewrite_query().

931 {
932  ListCell *lc;
933 
934  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
935  return SPI_ERROR_ARGUMENT;
936 
937  /* Release the plancache entries */
938  foreach(lc, plan->plancache_list)
939  {
940  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
941 
942  DropCachedPlan(plansource);
943  }
944 
945  /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
947 
948  return 0;
949 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
MemoryContext plancxt
Definition: spi_priv.h:96
#define lfirst(lc)
Definition: pg_list.h:169
void DropCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:498

◆ SPI_freetuple()

void SPI_freetuple ( HeapTuple  tuple)

Definition at line 1284 of file spi.c.

References heap_freetuple().

1285 {
1286  /* No longer need to worry which context tuple was in... */
1287  heap_freetuple(tuple);
1288 }
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338

◆ SPI_freetuptable()

void SPI_freetuptable ( SPITupleTable tuptable)

Definition at line 1291 of file spi.c.

References slist_mutable_iter::cur, elog, MemoryContextDelete(), next, slist_container, slist_delete_current(), slist_foreach_modify, SPITupleTable::tuptabcxt, _SPI_connection::tuptable, _SPI_connection::tuptables, and WARNING.

Referenced by _SPI_execute_plan(), exec_eval_cleanup(), exec_for_query(), exec_stmt_call(), exec_stmt_dynexecute(), exec_stmt_execsql(), exec_stmt_fetch(), plperl_spi_execute_fetch_result(), plperl_spi_fetchrow(), pltcl_process_SPI_result(), PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_spi_execute_fetch_result(), ts_stat_sql(), and tsquery_rewrite_query().

1292 {
1293  bool found = false;
1294 
1295  /* ignore call if NULL pointer */
1296  if (tuptable == NULL)
1297  return;
1298 
1299  /*
1300  * Search only the topmost SPI context for a matching tuple table.
1301  */
1302  if (_SPI_current != NULL)
1303  {
1304  slist_mutable_iter siter;
1305 
1306  /* find tuptable in active list, then remove it */
1308  {
1309  SPITupleTable *tt;
1310 
1311  tt = slist_container(SPITupleTable, next, siter.cur);
1312  if (tt == tuptable)
1313  {
1314  slist_delete_current(&siter);
1315  found = true;
1316  break;
1317  }
1318  }
1319  }
1320 
1321  /*
1322  * Refuse the deletion if we didn't find it in the topmost SPI context.
1323  * This is primarily a guard against double deletion, but might prevent
1324  * other errors as well. Since the worst consequence of not deleting a
1325  * tuptable would be a transient memory leak, this is just a WARNING.
1326  */
1327  if (!found)
1328  {
1329  elog(WARNING, "attempt to delete invalid SPITupleTable %p", tuptable);
1330  return;
1331  }
1332 
1333  /* for safety, reset global variables that might point at tuptable */
1334  if (tuptable == _SPI_current->tuptable)
1335  _SPI_current->tuptable = NULL;
1336  if (tuptable == SPI_tuptable)
1337  SPI_tuptable = NULL;
1338 
1339  /* release all memory belonging to tuptable */
1340  MemoryContextDelete(tuptable->tuptabcxt);
1341 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static int32 next
Definition: blutils.c:219
slist_node * cur
Definition: ilist.h:241
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:735
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define WARNING
Definition: elog.h:40
#define slist_container(type, membername, ptr)
Definition: ilist.h:693
#define elog(elevel,...)
Definition: elog.h:232
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:671
slist_head tuptables
Definition: spi_priv.h:32
MemoryContext tuptabcxt
Definition: spi.h:31

◆ SPI_getargcount()

int SPI_getargcount ( SPIPlanPtr  plan)

Definition at line 1795 of file spi.c.

References _SPI_PLAN_MAGIC, _SPI_plan::magic, _SPI_plan::nargs, SPI_ERROR_ARGUMENT, and SPI_result.

1796 {
1797  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1798  {
1800  return -1;
1801  }
1802  return plan->nargs;
1803 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
int nargs
Definition: spi_priv.h:99

◆ SPI_getargtypeid()

Oid SPI_getargtypeid ( SPIPlanPtr  plan,
int  argIndex 
)

Definition at line 1780 of file spi.c.

References _SPI_PLAN_MAGIC, _SPI_plan::argtypes, InvalidOid, _SPI_plan::magic, _SPI_plan::nargs, SPI_ERROR_ARGUMENT, and SPI_result.

1781 {
1782  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
1783  argIndex < 0 || argIndex >= plan->nargs)
1784  {
1786  return InvalidOid;
1787  }
1788  return plan->argtypes[argIndex];
1789 }
Oid * argtypes
Definition: spi_priv.h:100
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
int nargs
Definition: spi_priv.h:99
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getbinval()

Datum SPI_getbinval ( HeapTuple  tuple,
TupleDesc  tupdesc,
int  fnumber,
bool isnull 
)

Definition at line 1157 of file spi.c.

References FirstLowInvalidHeapAttributeNumber, heap_getattr, TupleDescData::natts, SPI_ERROR_NOATTRIBUTE, and SPI_result.

Referenced by autoinc(), check_foreign_key(), check_primary_key(), exec_eval_expr(), initialize_worker_spi(), make_ruledef(), make_viewdef(), query_to_oid_list(), SPI_sql_row_to_xmlelement(), test_predtest(), ts_stat_sql(), tsquery_rewrite_query(), tsvector_update_trigger(), and ttdummy().

1158 {
1159  SPI_result = 0;
1160 
1161  if (fnumber > tupdesc->natts || fnumber == 0 ||
1163  {
1165  *isnull = true;
1166  return (Datum) NULL;
1167  }
1168 
1169  return heap_getattr(tuple, fnumber, tupdesc, isnull);
1170 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411

◆ SPI_getnspname()

char* SPI_getnspname ( Relation  rel)

Definition at line 1237 of file spi.c.

References get_namespace_name(), and RelationGetNamespace.

Referenced by plperl_trigger_build_args(), pltcl_trigger_handler(), and PLy_trigger_build_args().

1238 {
1240 }
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3316
#define RelationGetNamespace(relation)
Definition: rel.h:518

◆ SPI_getrelname()

char* SPI_getrelname ( Relation  rel)

Definition at line 1231 of file spi.c.

References pstrdup(), and RelationGetRelationName.

Referenced by autoinc(), check_foreign_key(), check_primary_key(), insert_username(), moddatetime(), plperl_trigger_build_args(), pltcl_trigger_handler(), PLy_trigger_build_args(), and ttdummy().

1232 {
1233  return pstrdup(RelationGetRelationName(rel));
1234 }
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define RelationGetRelationName(relation)
Definition: rel.h:511

◆ SPI_gettype()

char* SPI_gettype ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1173 of file spi.c.

References FirstLowInvalidHeapAttributeNumber, GETSTRUCT, HeapTupleIsValid, NameStr, TupleDescData::natts, ObjectIdGetDatum, pstrdup(), ReleaseSysCache(), SearchSysCache1(), SPI_ERROR_NOATTRIBUTE, SPI_ERROR_TYPUNKNOWN, SPI_result, SystemAttributeDefinition(), TupleDescAttr, TYPEOID, and typname.

Referenced by check_foreign_key().

1174 {
1175  Oid typoid;
1176  HeapTuple typeTuple;
1177  char *result;
1178 
1179  SPI_result = 0;
1180 
1181  if (fnumber > tupdesc->natts || fnumber == 0 ||
1183  {
1185  return NULL;
1186  }
1187 
1188  if (fnumber > 0)
1189  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1190  else
1191  typoid = (SystemAttributeDefinition(fnumber))->atttypid;
1192 
1193  typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
1194 
1195  if (!HeapTupleIsValid(typeTuple))
1196  {
1198  return NULL;
1199  }
1200 
1201  result = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname));
1202  ReleaseSysCache(typeTuple);
1203  return result;
1204 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
unsigned int Oid
Definition: postgres_ext.h:31
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
NameData typname
Definition: pg_type.h:41
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1150
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1198
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:78
#define NameStr(name)
Definition: c.h:681

◆ SPI_gettypeid()

Oid SPI_gettypeid ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1213 of file spi.c.

References FirstLowInvalidHeapAttributeNumber, InvalidOid, TupleDescData::natts, SPI_ERROR_NOATTRIBUTE, SPI_result, SystemAttributeDefinition(), and TupleDescAttr.

Referenced by autoinc(), check_foreign_key(), check_primary_key(), insert_username(), moddatetime(), SPI_sql_row_to_xmlelement(), ts_stat_sql(), tsquery_rewrite_query(), tsvector_update_trigger(), and ttdummy().

1214 {
1215  SPI_result = 0;
1216 
1217  if (fnumber > tupdesc->natts || fnumber == 0 ||
1219  {
1221  return InvalidOid;
1222  }
1223 
1224  if (fnumber > 0)
1225  return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1226  else
1227  return (SystemAttributeDefinition(fnumber))->atttypid;
1228 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getvalue()

char* SPI_getvalue ( HeapTuple  tuple,
TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1125 of file spi.c.

References FirstLowInvalidHeapAttributeNumber, getTypeOutputInfo(), heap_getattr, TupleDescData::natts, OidOutputFunctionCall(), SPI_ERROR_NOATTRIBUTE, SPI_result, SystemAttributeDefinition(), TupleDescAttr, and val.

Referenced by build_tuplestore_recursively(), check_foreign_key(), crosstab(), get_crosstab_tuplestore(), get_sql_insert(), get_sql_update(), lo_manage(), load_categories_hash(), make_ruledef(), make_viewdef(), refresh_by_match_merge(), triggered_change_notification(), and xpath_table().

1126 {
1127  Datum val;
1128  bool isnull;
1129  Oid typoid,
1130  foutoid;
1131  bool typisvarlena;
1132 
1133  SPI_result = 0;
1134 
1135  if (fnumber > tupdesc->natts || fnumber == 0 ||
1137  {
1139  return NULL;
1140  }
1141 
1142  val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
1143  if (isnull)
1144  return NULL;
1145 
1146  if (fnumber > 0)
1147  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1148  else
1149  typoid = (SystemAttributeDefinition(fnumber))->atttypid;
1150 
1151  getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
1152 
1153  return OidOutputFunctionCall(foutoid, val);
1154 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
unsigned int Oid
Definition: postgres_ext.h:31
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1653
long val
Definition: informix.c:664

◆ SPI_inside_nonatomic_context()

bool SPI_inside_nonatomic_context ( void  )

Definition at line 489 of file spi.c.

References _SPI_connection::atomic.

Referenced by StartTransaction().

490 {
491  if (_SPI_current == NULL)
492  return false; /* not in any SPI context at all */
493  if (_SPI_current->atomic)
494  return false; /* it's atomic (ie function not procedure) */
495  return true;
496 }
static _SPI_connection * _SPI_current
Definition: spi.c:50

◆ SPI_is_cursor_plan()

bool SPI_is_cursor_plan ( SPIPlanPtr  plan)

Definition at line 1815 of file spi.c.

References _SPI_PLAN_MAGIC, linitial, list_length(), _SPI_plan::magic, _SPI_plan::plancache_list, CachedPlanSource::resultDesc, SPI_ERROR_ARGUMENT, and SPI_result.

Referenced by SPI_cursor_open_internal().

1816 {
1817  CachedPlanSource *plansource;
1818 
1819  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1820  {
1822  return false;
1823  }
1824 
1825  if (list_length(plan->plancache_list) != 1)
1826  {
1827  SPI_result = 0;
1828  return false; /* not exactly 1 pre-rewrite command */
1829  }
1830  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1831 
1832  /*
1833  * We used to force revalidation of the cached plan here, but that seems
1834  * unnecessary: invalidation could mean a change in the rowtype of the
1835  * tuples returned by a plan, but not whether it returns tuples at all.
1836  */
1837  SPI_result = 0;
1838 
1839  /* Does it return tuples? */
1840  if (plansource->resultDesc)
1841  return true;
1842 
1843  return false;
1844 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
TupleDesc resultDesc
Definition: plancache.h:108
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
#define linitial(l)
Definition: pg_list.h:174
static int list_length(const List *l)
Definition: pg_list.h:149

◆ SPI_keepplan()

int SPI_keepplan ( SPIPlanPtr  plan)

Definition at line 881 of file spi.c.

References _SPI_PLAN_MAGIC, CacheMemoryContext, lfirst, _SPI_plan::magic, MemoryContextSetParent(), _SPI_plan::oneshot, _SPI_plan::plancache_list, _SPI_plan::plancxt, SaveCachedPlan(), _SPI_plan::saved, and SPI_ERROR_ARGUMENT.

Referenced by check_foreign_key(), check_primary_key(), exec_prepare_plan(), pg_get_ruledef_worker(), pg_get_viewdef_worker(), plperl_spi_prepare(), pltcl_SPI_prepare(), PLy_spi_prepare(), ri_PlanCheck(), and ttdummy().

882 {
883  ListCell *lc;
884 
885  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
886  plan->saved || plan->oneshot)
887  return SPI_ERROR_ARGUMENT;
888 
889  /*
890  * Mark it saved, reparent it under CacheMemoryContext, and mark all the
891  * component CachedPlanSources as saved. This sequence cannot fail
892  * partway through, so there's no risk of long-term memory leakage.
893  */
894  plan->saved = true;
896 
897  foreach(lc, plan->plancache_list)
898  {
899  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
900 
901  SaveCachedPlan(plansource);
902  }
903 
904  return 0;
905 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:361
int magic
Definition: spi_priv.h:92
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
MemoryContext plancxt
Definition: spi_priv.h:96
bool saved
Definition: spi_priv.h:93
void SaveCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:454
#define lfirst(lc)
Definition: pg_list.h:169
bool oneshot
Definition: spi_priv.h:94
MemoryContext CacheMemoryContext
Definition: mcxt.c:51

◆ SPI_modifytuple()

HeapTuple SPI_modifytuple ( Relation  rel,
HeapTuple  tuple,
int  natts,
int *  attnum,
Datum Values,
const char *  Nulls 
)

Definition at line 1011 of file spi.c.

References heap_deform_tuple(), heap_form_tuple(), i, MemoryContextSwitchTo(), TupleDescData::natts, palloc(), pfree(), RelationData::rd_att, _SPI_connection::savedcxt, SPI_ERROR_ARGUMENT, SPI_ERROR_NOATTRIBUTE, SPI_ERROR_UNCONNECTED, SPI_result, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by ttdummy().

1013 {
1014  MemoryContext oldcxt;
1015  HeapTuple mtuple;
1016  int numberOfAttributes;
1017  Datum *v;
1018  bool *n;
1019  int i;
1020 
1021  if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
1022  {
1024  return NULL;
1025  }
1026 
1027  if (_SPI_current == NULL)
1028  {
1030  return NULL;
1031  }
1032 
1034 
1035  SPI_result = 0;
1036 
1037  numberOfAttributes = rel->rd_att->natts;
1038  v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
1039  n = (bool *) palloc(numberOfAttributes * sizeof(bool));
1040 
1041  /* fetch old values and nulls */
1042  heap_deform_tuple(tuple, rel->rd_att, v, n);
1043 
1044  /* replace values and nulls */
1045  for (i = 0; i < natts; i++)
1046  {
1047  if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
1048  break;
1049  v[attnum[i] - 1] = Values[i];
1050  n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n');
1051  }
1052 
1053  if (i == natts) /* no errors in *attnum */
1054  {
1055  mtuple = heap_form_tuple(rel->rd_att, v, n);
1056 
1057  /*
1058  * copy the identification info of the old tuple: t_ctid, t_self, and
1059  * OID (if any)
1060  */
1061  mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
1062  mtuple->t_self = tuple->t_self;
1063  mtuple->t_tableOid = tuple->t_tableOid;
1064  }
1065  else
1066  {
1067  mtuple = NULL;
1069  }
1070 
1071  pfree(v);
1072  pfree(n);
1073 
1074  MemoryContextSwitchTo(oldcxt);
1075 
1076  return mtuple;
1077 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:71
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
HeapTupleHeader t_data
Definition: htup.h:68
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
void pfree(void *pointer)
Definition: mcxt.c:1169
ItemPointerData t_ctid
Definition: htup_details.h:160
ItemPointerData t_self
Definition: htup.h:65
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
Oid t_tableOid
Definition: htup.h:66
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:411
TupleDesc rd_att
Definition: rel.h:110
int16 attnum
Definition: pg_attribute.h:83
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
void * palloc(Size size)
Definition: mcxt.c:1062
int i
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_palloc()

void* SPI_palloc ( Size  size)

Definition at line 1243 of file spi.c.

References elog, ERROR, MemoryContextAlloc(), and _SPI_connection::savedcxt.

Referenced by _SPI_strdup(), and coerce_function_result_tuple().

1244 {
1245  if (_SPI_current == NULL)
1246  elog(ERROR, "SPI_palloc called while not connected to SPI");
1247 
1248  return MemoryContextAlloc(_SPI_current->savedcxt, size);
1249 }
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:46
MemoryContext savedcxt
Definition: spi_priv.h:35
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
#define elog(elevel,...)
Definition: elog.h:232

◆ SPI_pfree()

void SPI_pfree ( void *  pointer)

Definition at line 1259 of file spi.c.

References pfree().

1260 {
1261  /* No longer need to worry which context chunk was in... */
1262  pfree(pointer);
1263 }
void pfree(void *pointer)
Definition: mcxt.c:1169

◆ SPI_plan_get_cached_plan()

CachedPlan* SPI_plan_get_cached_plan ( SPIPlanPtr  plan)

Definition at line 1973 of file spi.c.

References _SPI_error_callback(), _SPI_PLAN_MAGIC, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, CurrentResourceOwner, error_context_stack, GetCachedPlan(), CachedPlanSource::gplan, linitial, list_length(), _SPI_plan::magic, SPICallbackArg::mode, _SPI_plan::oneshot, _SPI_plan::parse_mode, _SPI_plan::plancache_list, ErrorContextCallback::previous, SPICallbackArg::query, CachedPlanSource::query_string, _SPI_connection::queryEnv, and _SPI_plan::saved.

Referenced by exec_eval_simple_expr(), exec_simple_check_plan(), and test_predtest().

1974 {
1975  CachedPlanSource *plansource;
1976  CachedPlan *cplan;
1977  SPICallbackArg spicallbackarg;
1978  ErrorContextCallback spierrcontext;
1979 
1980  Assert(plan->magic == _SPI_PLAN_MAGIC);
1981 
1982  /* Can't support one-shot plans here */
1983  if (plan->oneshot)
1984  return NULL;
1985 
1986  /* Must have exactly one CachedPlanSource */
1987  if (list_length(plan->plancache_list) != 1)
1988  return NULL;
1989  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1990 
1991  /* Setup error traceback support for ereport() */
1992  spicallbackarg.query = plansource->query_string;
1993  spicallbackarg.mode = plan->parse_mode;
1994  spierrcontext.callback = _SPI_error_callback;
1995  spierrcontext.arg = &spicallbackarg;
1996  spierrcontext.previous = error_context_stack;
1997  error_context_stack = &spierrcontext;
1998 
1999  /* Get the generic plan for the query */
2000  cplan = GetCachedPlan(plansource, NULL,
2001  plan->saved ? CurrentResourceOwner : NULL,
2003  Assert(cplan == plansource->gplan);
2004 
2005  /* Pop the error context stack */
2006  error_context_stack = spierrcontext.previous;
2007 
2008  return cplan;
2009 }
RawParseMode mode
Definition: spi.c:57
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
static _SPI_connection * _SPI_current
Definition: spi.c:50
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
struct CachedPlan * gplan
Definition: plancache.h:121
ErrorContextCallback * error_context_stack
Definition: elog.c:93
RawParseMode parse_mode
Definition: spi_priv.h:97
static void _SPI_error_callback(void *arg)
Definition: spi.c:2837
#define linitial(l)
Definition: pg_list.h:174
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, ResourceOwner owner, QueryEnvironment *queryEnv)
Definition: plancache.c:1141
bool saved
Definition: spi_priv.h:93
#define Assert(condition)
Definition: c.h:804
bool oneshot
Definition: spi_priv.h:94
const char * query_string
Definition: plancache.h:100
static int list_length(const List *l)
Definition: pg_list.h:149
const char * query
Definition: spi.c:56

◆ SPI_plan_get_plan_sources()

List* SPI_plan_get_plan_sources ( SPIPlanPtr  plan)

Definition at line 1954 of file spi.c.

References _SPI_PLAN_MAGIC, Assert, _SPI_plan::magic, and _SPI_plan::plancache_list.

Referenced by exec_simple_check_plan(), exec_stmt_execsql(), and make_callstmt_target().

1955 {
1956  Assert(plan->magic == _SPI_PLAN_MAGIC);
1957  return plan->plancache_list;
1958 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:804

◆ SPI_plan_is_valid()

bool SPI_plan_is_valid ( SPIPlanPtr  plan)

Definition at line 1853 of file spi.c.

References _SPI_PLAN_MAGIC, Assert, CachedPlanIsValid(), lfirst, _SPI_plan::magic, and _SPI_plan::plancache_list.

Referenced by ri_FetchPreparedPlan().

1854 {
1855  ListCell *lc;
1856 
1857  Assert(plan->magic == _SPI_PLAN_MAGIC);
1858 
1859  foreach(lc, plan->plancache_list)
1860  {
1861  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
1862 
1863  if (!CachedPlanIsValid(plansource))
1864  return false;
1865  }
1866  return true;
1867 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool CachedPlanIsValid(CachedPlanSource *plansource)
Definition: plancache.c:1598

◆ SPI_prepare()

SPIPlanPtr SPI_prepare ( const char *  src,
int  nargs,
Oid argtypes 
)

◆ SPI_prepare_cursor()

SPIPlanPtr SPI_prepare_cursor ( const char *  src,
int  nargs,
Oid argtypes,
int  cursorOptions 
)

Definition at line 771 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_make_plan_non_temp(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::argtypes, _SPI_plan::cursor_options, _SPI_plan::magic, _SPI_plan::nargs, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, RAW_PARSE_DEFAULT, SPI_ERROR_ARGUMENT, and SPI_result.

Referenced by SPI_prepare().

773 {
774  _SPI_plan plan;
775  SPIPlanPtr result;
776 
777  if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
778  {
780  return NULL;
781  }
782 
783  SPI_result = _SPI_begin_call(true);
784  if (SPI_result < 0)
785  return NULL;
786 
787  memset(&plan, 0, sizeof(_SPI_plan));
788  plan.magic = _SPI_PLAN_MAGIC;
790  plan.cursor_options = cursorOptions;
791  plan.nargs = nargs;
792  plan.argtypes = argtypes;
793  plan.parserSetup = NULL;
794  plan.parserSetupArg = NULL;
795 
796  _SPI_prepare_plan(src, &plan);
797 
798  /* copy plan to procedure context */
799  result = _SPI_make_plan_non_temp(&plan);
800 
801  _SPI_end_call(true);
802 
803  return result;
804 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2118
Oid * argtypes
Definition: spi_priv.h:100
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:3017
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
int cursor_options
Definition: spi_priv.h:98

◆ SPI_prepare_extended()

SPIPlanPtr SPI_prepare_extended ( const char *  src,
const SPIPrepareOptions options 
)

Definition at line 807 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_make_plan_non_temp(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::argtypes, _SPI_plan::cursor_options, SPIPrepareOptions::cursorOptions, _SPI_plan::magic, _SPI_plan::nargs, _SPI_plan::parse_mode, SPIPrepareOptions::parseMode, SPIPrepareOptions::parserSetup, _SPI_plan::parserSetup, SPIPrepareOptions::parserSetupArg, _SPI_plan::parserSetupArg, SPI_ERROR_ARGUMENT, and SPI_result.

Referenced by exec_prepare_plan().

809 {
810  _SPI_plan plan;
811  SPIPlanPtr result;
812 
813  if (src == NULL || options == NULL)
814  {
816  return NULL;
817  }
818 
819  SPI_result = _SPI_begin_call(true);
820  if (SPI_result < 0)
821  return NULL;
822 
823  memset(&plan, 0, sizeof(_SPI_plan));
824  plan.magic = _SPI_PLAN_MAGIC;
825  plan.parse_mode = options->parseMode;
826  plan.cursor_options = options->cursorOptions;
827  plan.nargs = 0;
828  plan.argtypes = NULL;
829  plan.parserSetup = options->parserSetup;
830  plan.parserSetupArg = options->parserSetupArg;
831 
832  _SPI_prepare_plan(src, &plan);
833 
834  /* copy plan to procedure context */
835  result = _SPI_make_plan_non_temp(&plan);
836 
837  _SPI_end_call(true);
838 
839  return result;
840 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2118
Oid * argtypes
Definition: spi_priv.h:100
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:3017
ParserSetupHook parserSetup
Definition: spi.h:39
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
int cursorOptions
Definition: spi.h:42
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
int cursor_options
Definition: spi_priv.h:98
void * parserSetupArg
Definition: spi.h:40
RawParseMode parseMode
Definition: spi.h:41

◆ SPI_prepare_params()

SPIPlanPtr SPI_prepare_params ( const char *  src,
ParserSetupHook  parserSetup,
void *  parserSetupArg,
int  cursorOptions 
)

Definition at line 843 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_make_plan_non_temp(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::argtypes, _SPI_plan::cursor_options, _SPI_plan::magic, _SPI_plan::nargs, _SPI_plan::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, RAW_PARSE_DEFAULT, SPI_ERROR_ARGUMENT, and SPI_result.

847 {
848  _SPI_plan plan;
849  SPIPlanPtr result;
850 
851  if (src == NULL)
852  {
854  return NULL;
855  }
856 
857  SPI_result = _SPI_begin_call(true);
858  if (SPI_result < 0)
859  return NULL;
860 
861  memset(&plan, 0, sizeof(_SPI_plan));
862  plan.magic = _SPI_PLAN_MAGIC;
864  plan.cursor_options = cursorOptions;
865  plan.nargs = 0;
866  plan.argtypes = NULL;
867  plan.parserSetup = parserSetup;
868  plan.parserSetupArg = parserSetupArg;
869 
870  _SPI_prepare_plan(src, &plan);
871 
872  /* copy plan to procedure context */
873  result = _SPI_make_plan_non_temp(&plan);
874 
875  _SPI_end_call(true);
876 
877  return result;
878 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2118
Oid * argtypes
Definition: spi_priv.h:100
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:3017
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
int cursor_options
Definition: spi_priv.h:98

◆ spi_printtup()

bool spi_printtup ( TupleTableSlot slot,
DestReceiver self 
)

Definition at line 2068 of file spi.c.

References SPITupleTable::alloced, elog, ERROR, ExecCopySlotHeapTuple(), MemoryContextSwitchTo(), SPITupleTable::numvals, repalloc_huge(), SPITupleTable::tuptabcxt, _SPI_connection::tuptable, and SPITupleTable::vals.

2069 {
2070  SPITupleTable *tuptable;
2071  MemoryContext oldcxt;
2072 
2073  if (_SPI_current == NULL)
2074  elog(ERROR, "spi_printtup called while not connected to SPI");
2075 
2076  tuptable = _SPI_current->tuptable;
2077  if (tuptable == NULL)
2078  elog(ERROR, "improper call to spi_printtup");
2079 
2080  oldcxt = MemoryContextSwitchTo(tuptable->tuptabcxt);
2081 
2082  if (tuptable->numvals >= tuptable->alloced)
2083  {
2084  /* Double the size of the pointer array */
2085  uint64 newalloced = tuptable->alloced * 2;
2086 
2087  tuptable->vals = (HeapTuple *) repalloc_huge(tuptable->vals,
2088  newalloced * sizeof(HeapTuple));
2089  tuptable->alloced = newalloced;
2090  }
2091 
2092  tuptable->vals[tuptable->numvals] = ExecCopySlotHeapTuple(slot);
2093  (tuptable->numvals)++;
2094 
2095  MemoryContextSwitchTo(oldcxt);
2096 
2097  return true;
2098 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
HeapTuple * vals
Definition: spi.h:26
uint64 numvals
Definition: spi.h:27
#define ERROR
Definition: elog.h:46
SPITupleTable * tuptable
Definition: spi_priv.h:26
static HeapTuple ExecCopySlotHeapTuple(TupleTableSlot *slot)
Definition: tuptable.h:452
void * repalloc_huge(void *pointer, Size size)
Definition: mcxt.c:1252
#define elog(elevel,...)
Definition: elog.h:232
uint64 alloced
Definition: spi.h:30
MemoryContext tuptabcxt
Definition: spi.h:31

◆ SPI_register_relation()

int SPI_register_relation ( EphemeralNamedRelation  enr)

Definition at line 3173 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_find_ENR_by_name(), create_queryEnv(), EphemeralNamedRelationData::md, EphemeralNamedRelationMetadataData::name, _SPI_connection::queryEnv, register_ENR(), SPI_ERROR_ARGUMENT, SPI_ERROR_REL_DUPLICATE, and SPI_OK_REL_REGISTER.

Referenced by SPI_register_trigger_data().

3174 {
3175  EphemeralNamedRelation match;
3176  int res;
3177 
3178  if (enr == NULL || enr->md.name == NULL)
3179  return SPI_ERROR_ARGUMENT;
3180 
3181  res = _SPI_begin_call(false); /* keep current memory context */
3182  if (res < 0)
3183  return res;
3184 
3185  match = _SPI_find_ENR_by_name(enr->md.name);
3186  if (match)
3188  else
3189  {
3190  if (_SPI_current->queryEnv == NULL)
3192 
3194  res = SPI_OK_REL_REGISTER;
3195  }
3196 
3197  _SPI_end_call(false);
3198 
3199  return res;
3200 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:79
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:3156
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define SPI_OK_REL_REGISTER
Definition: spi.h:96
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2953
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
QueryEnvironment * create_queryEnv(void)
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2977
void register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)

◆ SPI_register_trigger_data()

int SPI_register_trigger_data ( TriggerData tdata)

Definition at line 3240 of file spi.c.

References ENR_NAMED_TUPLESTORE, EphemeralNamedRelationMetadataData::enrtuples, EphemeralNamedRelationMetadataData::enrtype, EphemeralNamedRelationData::md, EphemeralNamedRelationMetadataData::name, palloc(), RelationData::rd_id, EphemeralNamedRelationData::reldata, EphemeralNamedRelationMetadataData::reliddesc, SPI_ERROR_ARGUMENT, SPI_OK_REL_REGISTER, SPI_OK_TD_REGISTER, SPI_register_relation(), TriggerData::tg_newtable, TriggerData::tg_oldtable, TriggerData::tg_relation, TriggerData::tg_trigger, Trigger::tgnewtable, Trigger::tgoldtable, EphemeralNamedRelationMetadataData::tupdesc, and tuplestore_tuple_count().

Referenced by plperl_trigger_handler(), plpgsql_exec_trigger(), pltcl_trigger_handler(), and PLy_exec_trigger().

3241 {
3242  if (tdata == NULL)
3243  return SPI_ERROR_ARGUMENT;
3244 
3245  if (tdata->tg_newtable)
3246  {
3249  int rc;
3250 
3251  enr->md.name = tdata->tg_trigger->tgnewtable;
3252  enr->md.reliddesc = tdata->tg_relation->rd_id;
3253  enr->md.tupdesc = NULL;
3256  enr->reldata = tdata->tg_newtable;
3257  rc = SPI_register_relation(enr);
3258  if (rc != SPI_OK_REL_REGISTER)
3259  return rc;
3260  }
3261 
3262  if (tdata->tg_oldtable)
3263  {
3266  int rc;
3267 
3268  enr->md.name = tdata->tg_trigger->tgoldtable;
3269  enr->md.reliddesc = tdata->tg_relation->rd_id;
3270  enr->md.tupdesc = NULL;
3273  enr->reldata = tdata->tg_oldtable;
3274  rc = SPI_register_relation(enr);
3275  if (rc != SPI_OK_REL_REGISTER)
3276  return rc;
3277  }
3278 
3279  return SPI_OK_TD_REGISTER;
3280 }
EphemeralNamedRelationMetadataData md
#define SPI_OK_REL_REGISTER
Definition: spi.h:96
#define SPI_ERROR_ARGUMENT
Definition: spi.h:73
#define SPI_OK_TD_REGISTER
Definition: spi.h:98
Oid rd_id
Definition: rel.h:111