PostgreSQL Source Code  git master
spi.h File Reference
#include "commands/trigger.h"
#include "lib/ilist.h"
#include "nodes/parsenodes.h"
#include "utils/portal.h"
Include dependency graph for spi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SPITupleTable
 

Macros

#define SPI_ERROR_CONNECT   (-1)
 
#define SPI_ERROR_COPY   (-2)
 
#define SPI_ERROR_OPUNKNOWN   (-3)
 
#define SPI_ERROR_UNCONNECTED   (-4)
 
#define SPI_ERROR_CURSOR   (-5) /* not used anymore */
 
#define SPI_ERROR_ARGUMENT   (-6)
 
#define SPI_ERROR_PARAM   (-7)
 
#define SPI_ERROR_TRANSACTION   (-8)
 
#define SPI_ERROR_NOATTRIBUTE   (-9)
 
#define SPI_ERROR_NOOUTFUNC   (-10)
 
#define SPI_ERROR_TYPUNKNOWN   (-11)
 
#define SPI_ERROR_REL_DUPLICATE   (-12)
 
#define SPI_ERROR_REL_NOT_FOUND   (-13)
 
#define SPI_OK_CONNECT   1
 
#define SPI_OK_FINISH   2
 
#define SPI_OK_FETCH   3
 
#define SPI_OK_UTILITY   4
 
#define SPI_OK_SELECT   5
 
#define SPI_OK_SELINTO   6
 
#define SPI_OK_INSERT   7
 
#define SPI_OK_DELETE   8
 
#define SPI_OK_UPDATE   9
 
#define SPI_OK_CURSOR   10
 
#define SPI_OK_INSERT_RETURNING   11
 
#define SPI_OK_DELETE_RETURNING   12
 
#define SPI_OK_UPDATE_RETURNING   13
 
#define SPI_OK_REWRITTEN   14
 
#define SPI_OK_REL_REGISTER   15
 
#define SPI_OK_REL_UNREGISTER   16
 
#define SPI_OK_TD_REGISTER   17
 
#define SPI_OPT_NONATOMIC   (1 << 0)
 
#define SPI_push()   ((void) 0)
 
#define SPI_pop()   ((void) 0)
 
#define SPI_push_conditional()   false
 
#define SPI_pop_conditional(pushed)   ((void) 0)
 
#define SPI_restore_connection()   ((void) 0)
 

Typedefs

typedef struct SPITupleTable SPITupleTable
 
typedef struct _SPI_planSPIPlanPtr
 

Functions

int SPI_connect (void)
 
int SPI_connect_ext (int options)
 
int SPI_finish (void)
 
int SPI_execute (const char *src, bool read_only, long tcount)
 
int SPI_execute_plan (SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
 
int SPI_execute_plan_with_paramlist (SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
 
int SPI_execute_plan_with_receiver (SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount, DestReceiver *dest)
 
int SPI_exec (const char *src, long tcount)
 
int SPI_execp (SPIPlanPtr plan, Datum *Values, const char *Nulls, 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)
 
int SPI_execute_with_receiver (const char *src, ParamListInfo params, bool read_only, long tcount, DestReceiver *dest)
 
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_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)
 
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)
 
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 pointer)
 
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_with_paramlist (const char *name, const char *src, ParamListInfo params, bool read_only, int cursorOptions)
 
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, FetchDirection direction, long count)
 
void SPI_scroll_cursor_move (Portal, FetchDirection direction, long count)
 
void SPI_cursor_close (Portal portal)
 
int SPI_register_relation (EphemeralNamedRelation enr)
 
int SPI_unregister_relation (const char *name)
 
int SPI_register_trigger_data (TriggerData *tdata)
 
void SPI_start_transaction (void)
 
void SPI_commit (void)
 
void SPI_commit_and_chain (void)
 
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)
 

Variables

PGDLLIMPORT uint64 SPI_processed
 
PGDLLIMPORT SPITupleTableSPI_tuptable
 
PGDLLIMPORT int SPI_result
 

Macro Definition Documentation

◆ SPI_ERROR_ARGUMENT

◆ SPI_ERROR_CONNECT

#define SPI_ERROR_CONNECT   (-1)

Definition at line 39 of file spi.h.

Referenced by SPI_result_code_string().

◆ SPI_ERROR_COPY

#define SPI_ERROR_COPY   (-2)

◆ SPI_ERROR_CURSOR

#define SPI_ERROR_CURSOR   (-5) /* not used anymore */

Definition at line 43 of file spi.h.

◆ SPI_ERROR_NOATTRIBUTE

◆ SPI_ERROR_NOOUTFUNC

#define SPI_ERROR_NOOUTFUNC   (-10)

Definition at line 48 of file spi.h.

Referenced by SPI_result_code_string().

◆ SPI_ERROR_OPUNKNOWN

#define SPI_ERROR_OPUNKNOWN   (-3)

Definition at line 41 of file spi.h.

Referenced by _SPI_pquery(), and SPI_result_code_string().

◆ SPI_ERROR_PARAM

#define SPI_ERROR_PARAM   (-7)

◆ SPI_ERROR_REL_DUPLICATE

#define SPI_ERROR_REL_DUPLICATE   (-12)

Definition at line 50 of file spi.h.

Referenced by SPI_register_relation(), and SPI_result_code_string().

◆ SPI_ERROR_REL_NOT_FOUND

#define SPI_ERROR_REL_NOT_FOUND   (-13)

Definition at line 51 of file spi.h.

Referenced by SPI_result_code_string(), and SPI_unregister_relation().

◆ SPI_ERROR_TRANSACTION

#define SPI_ERROR_TRANSACTION   (-8)

◆ SPI_ERROR_TYPUNKNOWN

#define SPI_ERROR_TYPUNKNOWN   (-11)

Definition at line 49 of file spi.h.

Referenced by SPI_gettype(), and SPI_result_code_string().

◆ SPI_ERROR_UNCONNECTED

#define SPI_ERROR_UNCONNECTED   (-4)

◆ SPI_OK_CONNECT

◆ SPI_OK_CURSOR

#define SPI_OK_CURSOR   10

Definition at line 62 of file spi.h.

Referenced by exec_run_select(), and SPI_result_code_string().

◆ SPI_OK_DELETE

◆ SPI_OK_DELETE_RETURNING

#define SPI_OK_DELETE_RETURNING   12

◆ SPI_OK_FETCH

#define SPI_OK_FETCH   3

Definition at line 55 of file spi.h.

Referenced by PLy_cursor_fetch(), and SPI_result_code_string().

◆ SPI_OK_FINISH

◆ SPI_OK_INSERT

#define SPI_OK_INSERT   7

◆ SPI_OK_INSERT_RETURNING

#define SPI_OK_INSERT_RETURNING   11

◆ SPI_OK_REL_REGISTER

#define SPI_OK_REL_REGISTER   15

Definition at line 67 of file spi.h.

Referenced by SPI_register_relation(), SPI_register_trigger_data(), and SPI_result_code_string().

◆ SPI_OK_REL_UNREGISTER

#define SPI_OK_REL_UNREGISTER   16

Definition at line 68 of file spi.h.

Referenced by SPI_result_code_string(), and SPI_unregister_relation().

◆ SPI_OK_REWRITTEN

#define SPI_OK_REWRITTEN   14

◆ SPI_OK_SELECT

◆ SPI_OK_SELINTO

#define SPI_OK_SELINTO   6

◆ SPI_OK_TD_REGISTER

#define SPI_OK_TD_REGISTER   17

Definition at line 69 of file spi.h.

Referenced by SPI_register_trigger_data().

◆ SPI_OK_UPDATE

◆ SPI_OK_UPDATE_RETURNING

#define SPI_OK_UPDATE_RETURNING   13

◆ SPI_OK_UTILITY

◆ SPI_OPT_NONATOMIC

◆ SPI_pop

#define SPI_pop ( )    ((void) 0)

Definition at line 75 of file spi.h.

◆ SPI_pop_conditional

#define SPI_pop_conditional (   pushed)    ((void) 0)

Definition at line 77 of file spi.h.

◆ SPI_push

#define SPI_push ( )    ((void) 0)

Definition at line 74 of file spi.h.

◆ SPI_push_conditional

#define SPI_push_conditional ( )    false

Definition at line 76 of file spi.h.

◆ SPI_restore_connection

#define SPI_restore_connection ( )    ((void) 0)

Definition at line 78 of file spi.h.

Typedef Documentation

◆ SPIPlanPtr

typedef struct _SPI_plan* SPIPlanPtr

Definition at line 37 of file spi.h.

◆ SPITupleTable

typedef struct SPITupleTable SPITupleTable

Function Documentation

◆ AtEOSubXact_SPI()

void AtEOSubXact_SPI ( bool  isCommit,
SubTransactionId  mySubid 
)

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

387 {
388  bool found = false;
389 
390  while (_SPI_connected >= 0)
391  {
393 
394  if (connection->connectSubid != mySubid)
395  break; /* couldn't be any underneath it either */
396 
397  if (connection->internal_xact)
398  break;
399 
400  found = true;
401 
402  /*
403  * Release procedure memory explicitly (see note in SPI_connect)
404  */
405  if (connection->execCxt)
406  {
407  MemoryContextDelete(connection->execCxt);
408  connection->execCxt = NULL;
409  }
410  if (connection->procCxt)
411  {
412  MemoryContextDelete(connection->procCxt);
413  connection->procCxt = NULL;
414  }
415 
416  /*
417  * Restore outer global variables and pop the stack entry. Unlike
418  * SPI_finish(), we don't risk switching to memory contexts that might
419  * be already gone.
420  */
421  SPI_processed = connection->outer_processed;
422  SPI_tuptable = connection->outer_tuptable;
423  SPI_result = connection->outer_result;
424 
425  _SPI_connected--;
426  if (_SPI_connected < 0)
427  _SPI_current = NULL;
428  else
430  }
431 
432  if (found && isCommit)
434  (errcode(ERRCODE_WARNING),
435  errmsg("subtransaction left non-empty SPI stack"),
436  errhint("Check for missing \"SPI_finish\" calls.")));
437 
438  /*
439  * If we are aborting a subtransaction and there is an open SPI context
440  * surrounding the subxact, clean up to prevent memory leakage.
441  */
442  if (_SPI_current && !isCommit)
443  {
444  slist_mutable_iter siter;
445 
446  /*
447  * Throw away executor state if current executor operation was started
448  * within current subxact (essentially, force a _SPI_end_call(true)).
449  */
450  if (_SPI_current->execSubid >= mySubid)
451  {
454  }
455 
456  /* throw away any tuple tables created within current subxact */
458  {
459  SPITupleTable *tuptable;
460 
461  tuptable = slist_container(SPITupleTable, next, siter.cur);
462  if (tuptable->subid >= mySubid)
463  {
464  /*
465  * If we used SPI_freetuptable() here, its internal search of
466  * the tuptables list would make this operation O(N^2).
467  * Instead, just free the tuptable manually. This should
468  * match what SPI_freetuptable() does.
469  */
470  slist_delete_current(&siter);
471  if (tuptable == _SPI_current->tuptable)
472  _SPI_current->tuptable = NULL;
473  if (tuptable == SPI_tuptable)
474  SPI_tuptable = NULL;
475  MemoryContextDelete(tuptable->tuptabcxt);
476  }
477  }
478  }
479 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
int errhint(const char *fmt,...)
Definition: elog.c:1071
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:610
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:716
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:674
MemoryContext procCxt
Definition: spi_priv.h:33
MemoryContext execCxt
Definition: spi_priv.h:34
#define ereport(elevel,...)
Definition: elog.h:144
int outer_result
Definition: spi_priv.h:48
#define InvalidSubTransactionId
Definition: c.h:526
int errmsg(const char *fmt,...)
Definition: elog.c:824
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
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 364 of file spi.c.

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

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

365 {
366  /* Do nothing if the transaction end was initiated by SPI. */
368  return;
369 
370  if (isCommit && _SPI_connected != -1)
372  (errcode(ERRCODE_WARNING),
373  errmsg("transaction left non-empty SPI stack"),
374  errhint("Check for missing \"SPI_finish\" calls.")));
375 
376  SPICleanup();
377 }
int errhint(const char *fmt,...)
Definition: elog.c:1071
bool internal_xact
Definition: spi_priv.h:42
void SPICleanup(void)
Definition: spi.c:350
int errcode(int sqlerrcode)
Definition: elog.c:610
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:144
int errmsg(const char *fmt,...)
Definition: elog.c:824

◆ SPI_commit()

void SPI_commit ( void  )

Definition at line 279 of file spi.c.

References _SPI_commit().

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

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

◆ SPI_commit_and_chain()

void SPI_commit_and_chain ( void  )

Definition at line 285 of file spi.c.

References _SPI_commit().

Referenced by exec_stmt_commit().

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

◆ SPI_connect()

◆ SPI_connect_ext()

int SPI_connect_ext ( int  options)

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

97 {
98  int newdepth;
99 
100  /* Enlarge stack if necessary */
101  if (_SPI_stack == NULL)
102  {
103  if (_SPI_connected != -1 || _SPI_stack_depth != 0)
104  elog(ERROR, "SPI stack corrupted");
105  newdepth = 16;
108  newdepth * sizeof(_SPI_connection));
109  _SPI_stack_depth = newdepth;
110  }
111  else
112  {
114  elog(ERROR, "SPI stack corrupted");
115  if (_SPI_stack_depth == _SPI_connected + 1)
116  {
117  newdepth = _SPI_stack_depth * 2;
120  newdepth * sizeof(_SPI_connection));
121  _SPI_stack_depth = newdepth;
122  }
123  }
124 
125  /* Enter new stack level */
126  _SPI_connected++;
128 
130  _SPI_current->processed = 0;
131  _SPI_current->tuptable = NULL;
134  _SPI_current->procCxt = NULL; /* in case we fail to create 'em */
135  _SPI_current->execCxt = NULL;
137  _SPI_current->queryEnv = NULL;
139  _SPI_current->internal_xact = false;
143 
144  /*
145  * Create memory contexts for this procedure
146  *
147  * In atomic contexts (the normal case), we use TopTransactionContext,
148  * otherwise PortalContext, so that it lives across transaction
149  * boundaries.
150  *
151  * XXX It could be better to use PortalContext as the parent context in
152  * all cases, but we may not be inside a portal (consider deferred-trigger
153  * execution). Perhaps CurTransactionContext could be an option? For now
154  * it doesn't matter because we clean up explicitly in AtEOSubXact_SPI().
155  */
157  "SPI Proc",
160  "SPI Exec",
162  /* ... and switch to procedure's context */
164 
165  /*
166  * Reset API global variables so that current caller cannot accidentally
167  * depend on state of an outer caller.
168  */
169  SPI_processed = 0;
170  SPI_tuptable = NULL;
171  SPI_result = 0;
172 
173  return SPI_OK_CONNECT;
174 }
#define SPI_OK_CONNECT
Definition: spi.h:53
#define AllocSetContextCreate
Definition: memutils.h:170
MemoryContext TopTransactionContext
Definition: mcxt.c:49
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:332
uint64 SPI_processed
Definition: spi.c:45
SPITupleTable * outer_tuptable
Definition: spi_priv.h:47
MemoryContext PortalContext
Definition: mcxt.c:53
int SPI_result
Definition: spi.c:47
SubTransactionId connectSubid
Definition: spi_priv.h:36
static void slist_init(slist_head *head)
Definition: ilist.h:554
#define ERROR
Definition: elog.h:43
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static _SPI_connection * _SPI_stack
Definition: spi.c:49
MemoryContext TopMemoryContext
Definition: mcxt.c:44
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:745
int outer_result
Definition: spi_priv.h:48
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:723
#define InvalidSubTransactionId
Definition: c.h:526
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:214
slist_head tuptables
Definition: spi_priv.h:32

◆ SPI_copytuple()

HeapTuple SPI_copytuple ( HeapTuple  tuple)

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

897 {
898  MemoryContext oldcxt;
899  HeapTuple ctuple;
900 
901  if (tuple == NULL)
902  {
904  return NULL;
905  }
906 
907  if (_SPI_current == NULL)
908  {
910  return NULL;
911  }
912 
914 
915  ctuple = heap_copytuple(tuple);
916 
917  MemoryContextSwitchTo(oldcxt);
918 
919  return ctuple;
920 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:42
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:44
MemoryContext savedcxt
Definition: spi_priv.h:35

◆ SPI_cursor_close()

void SPI_cursor_close ( Portal  portal)

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

1706 {
1707  if (!PortalIsValid(portal))
1708  elog(ERROR, "invalid portal in SPI cursor operation");
1709 
1710  PortalDrop(portal, false);
1711 }
#define ERROR
Definition: elog.h:43
#define PortalIsValid(p)
Definition: portal.h:203
void PortalDrop(Portal portal, bool isTopCommit)
Definition: portalmem.c:467
#define elog(elevel,...)
Definition: elog.h:214

◆ SPI_cursor_fetch()

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

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

1650 {
1651  _SPI_cursor_operation(portal,
1652  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1654  /* we know that the DestSPI receiver doesn't need a destroy call */
1655 }
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:2708

◆ SPI_cursor_find()

Portal SPI_cursor_find ( const char *  name)

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

1638 {
1639  return GetPortalByName(name);
1640 }
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 1664 of file spi.c.

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

1665 {
1666  _SPI_cursor_operation(portal,
1667  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1668  None_Receiver);
1669 }
DestReceiver * None_Receiver
Definition: dest.c:96
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2708

◆ SPI_cursor_open()

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

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

1297 {
1298  Portal portal;
1299  ParamListInfo paramLI;
1300 
1301  /* build transient ParamListInfo in caller's context */
1302  paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
1303  Values, Nulls);
1304 
1305  portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
1306 
1307  /* done with the transient ParamListInfo */
1308  if (paramLI)
1309  pfree(paramLI);
1310 
1311  return portal;
1312 }
Oid * argtypes
Definition: spi_priv.h:100
void pfree(void *pointer)
Definition: mcxt.c:1057
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:1429
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2573
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ 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 1321 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::parserSetup, _SPI_plan::parserSetupArg, SPI_cursor_open_internal(), and SPI_result.

1326 {
1327  Portal result;
1328  _SPI_plan plan;
1329  ParamListInfo paramLI;
1330 
1331  if (src == NULL || nargs < 0)
1332  elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
1333 
1334  if (nargs > 0 && (argtypes == NULL || Values == NULL))
1335  elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
1336 
1337  SPI_result = _SPI_begin_call(true);
1338  if (SPI_result < 0)
1339  elog(ERROR, "SPI_cursor_open_with_args called while not connected");
1340 
1341  memset(&plan, 0, sizeof(_SPI_plan));
1342  plan.magic = _SPI_PLAN_MAGIC;
1343  plan.cursor_options = cursorOptions;
1344  plan.nargs = nargs;
1345  plan.argtypes = argtypes;
1346  plan.parserSetup = NULL;
1347  plan.parserSetupArg = NULL;
1348 
1349  /* build transient ParamListInfo in executor context */
1350  paramLI = _SPI_convert_params(nargs, argtypes,
1351  Values, Nulls);
1352 
1353  _SPI_prepare_plan(src, &plan);
1354 
1355  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1356 
1357  result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
1358 
1359  /* And clean up */
1360  _SPI_end_call(true);
1361 
1362  return result;
1363 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2048
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:2778
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:43
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
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:1429
#define elog(elevel,...)
Definition: elog.h:214
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:2573
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ SPI_cursor_open_with_paramlist()

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

Definition at line 1373 of file spi.c.

References SPI_cursor_open_internal().

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

1375 {
1376  return SPI_cursor_open_internal(name, plan, params, read_only);
1377 }
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:1429

◆ SPI_cursor_parse_open_with_paramlist()

Portal SPI_cursor_parse_open_with_paramlist ( const char *  name,
const char *  src,
ParamListInfo  params,
bool  read_only,
int  cursorOptions 
)

Definition at line 1386 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_PLAN_MAGIC, _SPI_prepare_plan(), _SPI_plan::cursor_options, elog, ERROR, _SPI_plan::magic, _SPI_plan::parserSetup, ParamListInfoData::parserSetup, _SPI_plan::parserSetupArg, ParamListInfoData::parserSetupArg, SPI_cursor_open_internal(), and SPI_result.

Referenced by exec_dynquery_with_params().

1390 {
1391  Portal result;
1392  _SPI_plan plan;
1393 
1394  if (src == NULL)
1395  elog(ERROR, "SPI_cursor_parse_open_with_paramlist called with invalid arguments");
1396 
1397  SPI_result = _SPI_begin_call(true);
1398  if (SPI_result < 0)
1399  elog(ERROR, "SPI_cursor_parse_open_with_paramlist called while not connected");
1400 
1401  memset(&plan, 0, sizeof(_SPI_plan));
1402  plan.magic = _SPI_PLAN_MAGIC;
1403  plan.cursor_options = cursorOptions;
1404  if (params)
1405  {
1406  plan.parserSetup = params->parserSetup;
1407  plan.parserSetupArg = params->parserSetupArg;
1408  }
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, params, 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:2048
void * parserSetupArg
Definition: params.h:117
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
ParserSetupHook parserSetup
Definition: params.h:116
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:43
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
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:1429
#define elog(elevel,...)
Definition: elog.h:214
int cursor_options
Definition: spi_priv.h:98

◆ SPI_datumTransfer()

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

Definition at line 1210 of file spi.c.

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

Referenced by coerce_function_result_tuple(), and plpgsql_exec_function().

1211 {
1212  MemoryContext oldcxt;
1213  Datum result;
1214 
1215  if (_SPI_current == NULL)
1216  elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
1217 
1219 
1220  result = datumTransfer(value, typByVal, typLen);
1221 
1222  MemoryContextSwitchTo(oldcxt);
1223 
1224  return result;
1225 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:43
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:367
static struct @143 value
Datum datumTransfer(Datum value, bool typByVal, int typLen)
Definition: datum.c:193
#define elog(elevel,...)
Definition: elog.h:214

◆ SPI_exec()

int SPI_exec ( const char *  src,
long  tcount 
)

Definition at line 525 of file spi.c.

References SPI_execute().

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

526 {
527  return SPI_execute(src, false, tcount);
528 }
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:497

◆ SPI_execp()

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

Definition at line 559 of file spi.c.

References SPI_execute_plan().

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

560 {
561  return SPI_execute_plan(plan, Values, Nulls, false, tcount);
562 }
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:532
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ SPI_execute()

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

Definition at line 497 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, and SPI_ERROR_ARGUMENT.

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

498 {
499  _SPI_plan plan;
500  int res;
501 
502  if (src == NULL || tcount < 0)
503  return SPI_ERROR_ARGUMENT;
504 
505  res = _SPI_begin_call(true);
506  if (res < 0)
507  return res;
508 
509  memset(&plan, 0, sizeof(_SPI_plan));
510  plan.magic = _SPI_PLAN_MAGIC;
512 
513  _SPI_prepare_oneshot_plan(src, &plan);
514 
515  res = _SPI_execute_plan(&plan, NULL,
517  read_only, true, tcount, NULL);
518 
519  _SPI_end_call(true);
520  return res;
521 }
#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:2153
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2717
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 532 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, SPI_ERROR_ARGUMENT, and SPI_ERROR_PARAM.

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

534 {
535  int res;
536 
537  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
538  return SPI_ERROR_ARGUMENT;
539 
540  if (plan->nargs > 0 && Values == NULL)
541  return SPI_ERROR_PARAM;
542 
543  res = _SPI_begin_call(true);
544  if (res < 0)
545  return res;
546 
547  res = _SPI_execute_plan(plan,
548  _SPI_convert_params(plan->nargs, plan->argtypes,
549  Values, Nulls),
551  read_only, true, tcount, NULL);
552 
553  _SPI_end_call(true);
554  return res;
555 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:45
#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:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212
int nargs
Definition: spi_priv.h:99
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2573
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ SPI_execute_plan_with_paramlist()

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

Definition at line 566 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_run_select(), exec_stmt_call(), and exec_stmt_execsql().

568 {
569  int res;
570 
571  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
572  return SPI_ERROR_ARGUMENT;
573 
574  res = _SPI_begin_call(true);
575  if (res < 0)
576  return res;
577 
578  res = _SPI_execute_plan(plan, params,
580  read_only, true, tcount, NULL);
581 
582  _SPI_end_call(true);
583  return res;
584 }
#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:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212

◆ SPI_execute_plan_with_receiver()

int SPI_execute_plan_with_receiver ( SPIPlanPtr  plan,
ParamListInfo  params,
bool  read_only,
long  tcount,
DestReceiver dest 
)

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

597 {
598  int res;
599 
600  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
601  return SPI_ERROR_ARGUMENT;
602 
603  res = _SPI_begin_call(true);
604  if (res < 0)
605  return res;
606 
607  res = _SPI_execute_plan(plan, params,
609  read_only, true, tcount, dest);
610 
611  _SPI_end_call(true);
612  return res;
613 }
#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:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212

◆ 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 629 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, SPI_ERROR_ARGUMENT, and SPI_ERROR_PARAM.

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

633 {
634  int res;
635 
636  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
637  return SPI_ERROR_ARGUMENT;
638 
639  if (plan->nargs > 0 && Values == NULL)
640  return SPI_ERROR_PARAM;
641 
642  res = _SPI_begin_call(true);
643  if (res < 0)
644  return res;
645 
646  res = _SPI_execute_plan(plan,
647  _SPI_convert_params(plan->nargs, plan->argtypes,
648  Values, Nulls),
649  snapshot, crosscheck_snapshot,
650  read_only, fire_triggers, tcount, NULL);
651 
652  _SPI_end_call(true);
653  return res;
654 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:45
#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:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212
int nargs
Definition: spi_priv.h:99
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2573
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ 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 663 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, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, SPI_ERROR_ARGUMENT, and SPI_ERROR_PARAM.

667 {
668  int res;
669  _SPI_plan plan;
670  ParamListInfo paramLI;
671 
672  if (src == NULL || nargs < 0 || tcount < 0)
673  return SPI_ERROR_ARGUMENT;
674 
675  if (nargs > 0 && (argtypes == NULL || Values == NULL))
676  return SPI_ERROR_PARAM;
677 
678  res = _SPI_begin_call(true);
679  if (res < 0)
680  return res;
681 
682  memset(&plan, 0, sizeof(_SPI_plan));
683  plan.magic = _SPI_PLAN_MAGIC;
685  plan.nargs = nargs;
686  plan.argtypes = argtypes;
687  plan.parserSetup = NULL;
688  plan.parserSetupArg = NULL;
689 
690  paramLI = _SPI_convert_params(nargs, argtypes,
691  Values, Nulls);
692 
693  _SPI_prepare_oneshot_plan(src, &plan);
694 
695  res = _SPI_execute_plan(&plan, paramLI,
697  read_only, true, tcount, NULL);
698 
699  _SPI_end_call(true);
700  return res;
701 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:45
#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:2153
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2717
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:2573
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ SPI_execute_with_receiver()

int SPI_execute_with_receiver ( const char *  src,
ParamListInfo  params,
bool  read_only,
long  tcount,
DestReceiver dest 
)

Definition at line 712 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, _SPI_plan::parserSetup, ParamListInfoData::parserSetup, _SPI_plan::parserSetupArg, ParamListInfoData::parserSetupArg, and SPI_ERROR_ARGUMENT.

Referenced by exec_stmt_dynexecute(), and exec_stmt_return_query().

716 {
717  int res;
718  _SPI_plan plan;
719 
720  if (src == NULL || tcount < 0)
721  return SPI_ERROR_ARGUMENT;
722 
723  res = _SPI_begin_call(true);
724  if (res < 0)
725  return res;
726 
727  memset(&plan, 0, sizeof(_SPI_plan));
728  plan.magic = _SPI_PLAN_MAGIC;
730  if (params)
731  {
732  plan.parserSetup = params->parserSetup;
733  plan.parserSetupArg = params->parserSetupArg;
734  }
735 
736  _SPI_prepare_oneshot_plan(src, &plan);
737 
738  res = _SPI_execute_plan(&plan, params,
740  read_only, true, tcount, dest);
741 
742  _SPI_end_call(true);
743  return res;
744 }
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:2153
ParserSetupHook parserSetup
Definition: params.h:116
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
#define InvalidSnapshot
Definition: snapshot.h:123
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest)
Definition: spi.c:2212
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2717
int cursor_options
Definition: spi_priv.h:98

◆ SPI_finish()

int SPI_finish ( void  )

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

178 {
179  int res;
180 
181  res = _SPI_begin_call(false); /* just check we're connected */
182  if (res < 0)
183  return res;
184 
185  /* Restore memory context as it was before procedure call */
187 
188  /* Release memory used in procedure call (including tuptables) */
190  _SPI_current->execCxt = NULL;
192  _SPI_current->procCxt = NULL;
193 
194  /*
195  * Restore outer API variables, especially SPI_tuptable which is probably
196  * pointing at a just-deleted tuptable
197  */
201 
202  /* Exit stack level */
203  _SPI_connected--;
204  if (_SPI_connected < 0)
205  _SPI_current = NULL;
206  else
208 
209  return SPI_OK_FINISH;
210 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
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:2778
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:54

◆ SPI_fname()

char* SPI_fname ( TupleDesc  tupdesc,
int  fnumber 
)

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

1048 {
1049  const FormData_pg_attribute *att;
1050 
1051  SPI_result = 0;
1052 
1053  if (fnumber > tupdesc->natts || fnumber == 0 ||
1055  {
1057  return NULL;
1058  }
1059 
1060  if (fnumber > 0)
1061  att = TupleDescAttr(tupdesc, fnumber - 1);
1062  else
1063  att = SystemAttributeDefinition(fnumber);
1064 
1065  return pstrdup(NameStr(att->attname));
1066 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
FormData_pg_attribute
Definition: pg_attribute.h:177
#define NameStr(name)
Definition: c.h:622

◆ SPI_fnumber()

int SPI_fnumber ( TupleDesc  tupdesc,
const char *  fname 
)

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

1025 {
1026  int res;
1027  const FormData_pg_attribute *sysatt;
1028 
1029  for (res = 0; res < tupdesc->natts; res++)
1030  {
1031  Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
1032 
1033  if (namestrcmp(&attr->attname, fname) == 0 &&
1034  !attr->attisdropped)
1035  return res + 1;
1036  }
1037 
1038  sysatt = SystemAttributeByName(fname);
1039  if (sysatt != NULL)
1040  return sysatt->attnum;
1041 
1042  /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
1043  return SPI_ERROR_NOATTRIBUTE;
1044 }
#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:261
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
FormData_pg_attribute
Definition: pg_attribute.h:177

◆ SPI_freeplan()

int SPI_freeplan ( SPIPlanPtr  plan)

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

875 {
876  ListCell *lc;
877 
878  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
879  return SPI_ERROR_ARGUMENT;
880 
881  /* Release the plancache entries */
882  foreach(lc, plan->plancache_list)
883  {
884  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
885 
886  DropCachedPlan(plansource);
887  }
888 
889  /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
891 
892  return 0;
893 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
List * plancache_list
Definition: spi_priv.h:96
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
MemoryContext plancxt
Definition: spi_priv.h:97
#define lfirst(lc)
Definition: pg_list.h:190
void DropCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:498

◆ SPI_freetuple()

void SPI_freetuple ( HeapTuple  pointer)

Definition at line 1228 of file spi.c.

References heap_freetuple().

1229 {
1230  /* No longer need to worry which context tuple was in... */
1231  heap_freetuple(tuple);
1232 }
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338

◆ SPI_freetuptable()

void SPI_freetuptable ( SPITupleTable tuptable)

Definition at line 1235 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_assign_value(), 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().

1236 {
1237  bool found = false;
1238 
1239  /* ignore call if NULL pointer */
1240  if (tuptable == NULL)
1241  return;
1242 
1243  /*
1244  * Search only the topmost SPI context for a matching tuple table.
1245  */
1246  if (_SPI_current != NULL)
1247  {
1248  slist_mutable_iter siter;
1249 
1250  /* find tuptable in active list, then remove it */
1252  {
1253  SPITupleTable *tt;
1254 
1255  tt = slist_container(SPITupleTable, next, siter.cur);
1256  if (tt == tuptable)
1257  {
1258  slist_delete_current(&siter);
1259  found = true;
1260  break;
1261  }
1262  }
1263  }
1264 
1265  /*
1266  * Refuse the deletion if we didn't find it in the topmost SPI context.
1267  * This is primarily a guard against double deletion, but might prevent
1268  * other errors as well. Since the worst consequence of not deleting a
1269  * tuptable would be a transient memory leak, this is just a WARNING.
1270  */
1271  if (!found)
1272  {
1273  elog(WARNING, "attempt to delete invalid SPITupleTable %p", tuptable);
1274  return;
1275  }
1276 
1277  /* for safety, reset global variables that might point at tuptable */
1278  if (tuptable == _SPI_current->tuptable)
1279  _SPI_current->tuptable = NULL;
1280  if (tuptable == SPI_tuptable)
1281  SPI_tuptable = NULL;
1282 
1283  /* release all memory belonging to tuptable */
1284  MemoryContextDelete(tuptable->tuptabcxt);
1285 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
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:716
SPITupleTable * tuptable
Definition: spi_priv.h:26
#define WARNING
Definition: elog.h:40
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
#define elog(elevel,...)
Definition: elog.h:214
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
slist_head tuptables
Definition: spi_priv.h:32
MemoryContext tuptabcxt
Definition: spi.h:31

◆ SPI_getargcount()

int SPI_getargcount ( SPIPlanPtr  plan)

Definition at line 1733 of file spi.c.

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

1734 {
1735  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1736  {
1738  return -1;
1739  }
1740  return plan->nargs;
1741 }
#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:44
int nargs
Definition: spi_priv.h:99

◆ SPI_getargtypeid()

Oid SPI_getargtypeid ( SPIPlanPtr  plan,
int  argIndex 
)

Definition at line 1718 of file spi.c.

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

1719 {
1720  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
1721  argIndex < 0 || argIndex >= plan->nargs)
1722  {
1724  return InvalidOid;
1725  }
1726  return plan->argtypes[argIndex];
1727 }
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:44
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 1101 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().

1102 {
1103  SPI_result = 0;
1104 
1105  if (fnumber > tupdesc->natts || fnumber == 0 ||
1107  {
1109  *isnull = true;
1110  return (Datum) NULL;
1111  }
1112 
1113  return heap_getattr(tuple, fnumber, tupdesc, isnull);
1114 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
uintptr_t Datum
Definition: postgres.h:367

◆ SPI_getnspname()

char* SPI_getnspname ( Relation  rel)

Definition at line 1181 of file spi.c.

References get_namespace_name(), and RelationGetNamespace.

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

1182 {
1184 }
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3191
#define RelationGetNamespace(relation)
Definition: rel.h:497

◆ SPI_getrelname()

char* SPI_getrelname ( Relation  rel)

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

1176 {
1177  return pstrdup(RelationGetRelationName(rel));
1178 }
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define RelationGetRelationName(relation)
Definition: rel.h:490

◆ SPI_gettype()

char* SPI_gettype ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1117 of file spi.c.

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

1118 {
1119  Oid typoid;
1120  HeapTuple typeTuple;
1121  char *result;
1122 
1123  SPI_result = 0;
1124 
1125  if (fnumber > tupdesc->natts || fnumber == 0 ||
1127  {
1129  return NULL;
1130  }
1131 
1132  if (fnumber > 0)
1133  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1134  else
1135  typoid = (SystemAttributeDefinition(fnumber))->atttypid;
1136 
1137  typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
1138 
1139  if (!HeapTupleIsValid(typeTuple))
1140  {
1142  return NULL;
1143  }
1144 
1145  result = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname));
1146  ReleaseSysCache(typeTuple);
1147  return result;
1148 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
unsigned int Oid
Definition: postgres_ext.h:31
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
int SPI_result
Definition: spi.c:47
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
Oid atttypid
Definition: pg_attribute.h:49
NameData typname
Definition: pg_type.h:41
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:49
#define NameStr(name)
Definition: c.h:622

◆ SPI_gettypeid()

Oid SPI_gettypeid ( TupleDesc  tupdesc,
int  fnumber 
)

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

1158 {
1159  SPI_result = 0;
1160 
1161  if (fnumber > tupdesc->natts || fnumber == 0 ||
1163  {
1165  return InvalidOid;
1166  }
1167 
1168  if (fnumber > 0)
1169  return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1170  else
1171  return (SystemAttributeDefinition(fnumber))->atttypid;
1172 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:249
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getvalue()

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

Definition at line 1069 of file spi.c.

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

1070 {
1071  Datum val;
1072  bool isnull;
1073  Oid typoid,
1074  foutoid;
1075  bool typisvarlena;
1076 
1077  SPI_result = 0;
1078 
1079  if (fnumber > tupdesc->natts || fnumber == 0 ||
1081  {
1083  return NULL;
1084  }
1085 
1086  val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
1087  if (isnull)
1088  return NULL;
1089 
1090  if (fnumber > 0)
1091  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1092  else
1093  typoid = (SystemAttributeDefinition(fnumber))->atttypid;
1094 
1095  getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
1096 
1097  return OidOutputFunctionCall(foutoid, val);
1098 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2784
#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:249
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
Oid atttypid
Definition: pg_attribute.h:49
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
uintptr_t Datum
Definition: postgres.h:367
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
long val
Definition: informix.c:664

◆ SPI_inside_nonatomic_context()

bool SPI_inside_nonatomic_context ( void  )

Definition at line 485 of file spi.c.

References _SPI_connection::atomic.

Referenced by StartTransaction().

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

◆ SPI_is_cursor_plan()

bool SPI_is_cursor_plan ( SPIPlanPtr  plan)

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

1754 {
1755  CachedPlanSource *plansource;
1756 
1757  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1758  {
1760  return false;
1761  }
1762 
1763  if (list_length(plan->plancache_list) != 1)
1764  {
1765  SPI_result = 0;
1766  return false; /* not exactly 1 pre-rewrite command */
1767  }
1768  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1769 
1770  /*
1771  * We used to force revalidation of the cached plan here, but that seems
1772  * unnecessary: invalidation could mean a change in the rowtype of the
1773  * tuples returned by a plan, but not whether it returns tuples at all.
1774  */
1775  SPI_result = 0;
1776 
1777  /* Does it return tuples? */
1778  if (plansource->resultDesc)
1779  return true;
1780 
1781  return false;
1782 }
List * plancache_list
Definition: spi_priv.h:96
#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:44
#define linitial(l)
Definition: pg_list.h:195
static int list_length(const List *l)
Definition: pg_list.h:169

◆ SPI_keepplan()

int SPI_keepplan ( SPIPlanPtr  plan)

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

826 {
827  ListCell *lc;
828 
829  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
830  plan->saved || plan->oneshot)
831  return SPI_ERROR_ARGUMENT;
832 
833  /*
834  * Mark it saved, reparent it under CacheMemoryContext, and mark all the
835  * component CachedPlanSources as saved. This sequence cannot fail
836  * partway through, so there's no risk of long-term memory leakage.
837  */
838  plan->saved = true;
840 
841  foreach(lc, plan->plancache_list)
842  {
843  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
844 
845  SaveCachedPlan(plansource);
846  }
847 
848  return 0;
849 }
List * plancache_list
Definition: spi_priv.h:96
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:355
int magic
Definition: spi_priv.h:92
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
MemoryContext plancxt
Definition: spi_priv.h:97
bool saved
Definition: spi_priv.h:93
void SaveCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:454
#define lfirst(lc)
Definition: pg_list.h:190
bool oneshot
Definition: spi_priv.h:94
MemoryContext CacheMemoryContext
Definition: mcxt.c:47

◆ SPI_modifytuple()

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

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

957 {
958  MemoryContext oldcxt;
959  HeapTuple mtuple;
960  int numberOfAttributes;
961  Datum *v;
962  bool *n;
963  int i;
964 
965  if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
966  {
968  return NULL;
969  }
970 
971  if (_SPI_current == NULL)
972  {
974  return NULL;
975  }
976 
978 
979  SPI_result = 0;
980 
981  numberOfAttributes = rel->rd_att->natts;
982  v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
983  n = (bool *) palloc(numberOfAttributes * sizeof(bool));
984 
985  /* fetch old values and nulls */
986  heap_deform_tuple(tuple, rel->rd_att, v, n);
987 
988  /* replace values and nulls */
989  for (i = 0; i < natts; i++)
990  {
991  if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
992  break;
993  v[attnum[i] - 1] = Values[i];
994  n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n') ? true : false;
995  }
996 
997  if (i == natts) /* no errors in *attnum */
998  {
999  mtuple = heap_form_tuple(rel->rd_att, v, n);
1000 
1001  /*
1002  * copy the identification info of the old tuple: t_ctid, t_self, and
1003  * OID (if any)
1004  */
1005  mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
1006  mtuple->t_self = tuple->t_self;
1007  mtuple->t_tableOid = tuple->t_tableOid;
1008  }
1009  else
1010  {
1011  mtuple = NULL;
1013  }
1014 
1015  pfree(v);
1016  pfree(n);
1017 
1018  MemoryContextSwitchTo(oldcxt);
1019 
1020  return mtuple;
1021 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:42
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:44
void pfree(void *pointer)
Definition: mcxt.c:1057
ItemPointerData t_ctid
Definition: htup_details.h:160
ItemPointerData t_self
Definition: htup.h:65
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
Oid t_tableOid
Definition: htup.h:66
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:110
int16 attnum
Definition: pg_attribute.h:79
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
void * palloc(Size size)
Definition: mcxt.c:950
int i
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166

◆ SPI_palloc()

void* SPI_palloc ( Size  size)

Definition at line 1187 of file spi.c.

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

Referenced by _SPI_strdup(), and coerce_function_result_tuple().

1188 {
1189  if (_SPI_current == NULL)
1190  elog(ERROR, "SPI_palloc called while not connected to SPI");
1191 
1192  return MemoryContextAlloc(_SPI_current->savedcxt, size);
1193 }
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:43
MemoryContext savedcxt
Definition: spi_priv.h:35
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
#define elog(elevel,...)
Definition: elog.h:214

◆ SPI_pfree()

void SPI_pfree ( void *  pointer)

Definition at line 1203 of file spi.c.

References pfree().

1204 {
1205  /* No longer need to worry which context chunk was in... */
1206  pfree(pointer);
1207 }
void pfree(void *pointer)
Definition: mcxt.c:1057

◆ SPI_plan_get_cached_plan()

CachedPlan* SPI_plan_get_cached_plan ( SPIPlanPtr  plan)

Definition at line 1908 of file spi.c.

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

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

1909 {
1910  CachedPlanSource *plansource;
1911  CachedPlan *cplan;
1912  ErrorContextCallback spierrcontext;
1913 
1914  Assert(plan->magic == _SPI_PLAN_MAGIC);
1915 
1916  /* Can't support one-shot plans here */
1917  if (plan->oneshot)
1918  return NULL;
1919 
1920  /* Must have exactly one CachedPlanSource */
1921  if (list_length(plan->plancache_list) != 1)
1922  return NULL;
1923  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1924 
1925  /* Setup error traceback support for ereport() */
1926  spierrcontext.callback = _SPI_error_callback;
1927  spierrcontext.arg = unconstify(char *, plansource->query_string);
1928  spierrcontext.previous = error_context_stack;
1929  error_context_stack = &spierrcontext;
1930 
1931  /* Get the generic plan for the query */
1932  cplan = GetCachedPlan(plansource, NULL, plan->saved,
1934  Assert(cplan == plansource->gplan);
1935 
1936  /* Pop the error context stack */
1937  error_context_stack = spierrcontext.previous;
1938 
1939  return cplan;
1940 }
List * plancache_list
Definition: spi_priv.h:96
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, bool useResOwner, QueryEnvironment *queryEnv)
Definition: plancache.c:1141
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static _SPI_connection * _SPI_current
Definition: spi.c:50
void(* callback)(void *arg)
Definition: elog.h:229
struct ErrorContextCallback * previous
Definition: elog.h:228
struct CachedPlan * gplan
Definition: plancache.h:121
ErrorContextCallback * error_context_stack
Definition: elog.c:92
static void _SPI_error_callback(void *arg)
Definition: spi.c:2679
#define linitial(l)
Definition: pg_list.h:195
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
#define unconstify(underlying_type, expr)
Definition: c.h:1183
bool saved
Definition: spi_priv.h:93
#define Assert(condition)
Definition: c.h:745
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:169

◆ SPI_plan_get_plan_sources()

List* SPI_plan_get_plan_sources ( SPIPlanPtr  plan)

Definition at line 1892 of file spi.c.

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

Referenced by exec_simple_check_plan(), and exec_stmt_execsql().

1893 {
1894  Assert(plan->magic == _SPI_PLAN_MAGIC);
1895  return plan->plancache_list;
1896 }
List * plancache_list
Definition: spi_priv.h:96
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:745

◆ SPI_plan_is_valid()

bool SPI_plan_is_valid ( SPIPlanPtr  plan)

Definition at line 1791 of file spi.c.

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

Referenced by ri_FetchPreparedPlan().

1792 {
1793  ListCell *lc;
1794 
1795  Assert(plan->magic == _SPI_PLAN_MAGIC);
1796 
1797  foreach(lc, plan->plancache_list)
1798  {
1799  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
1800 
1801  if (!CachedPlanIsValid(plansource))
1802  return false;
1803  }
1804  return true;
1805 }
List * plancache_list
Definition: spi_priv.h:96
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
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 753 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::parserSetup, _SPI_plan::parserSetupArg, SPI_ERROR_ARGUMENT, and SPI_result.

Referenced by SPI_prepare().

755 {
756  _SPI_plan plan;
757  SPIPlanPtr result;
758 
759  if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
760  {
762  return NULL;
763  }
764 
765  SPI_result = _SPI_begin_call(true);
766  if (SPI_result < 0)
767  return NULL;
768 
769  memset(&plan, 0, sizeof(_SPI_plan));
770  plan.magic = _SPI_PLAN_MAGIC;
771  plan.cursor_options = cursorOptions;
772  plan.nargs = nargs;
773  plan.argtypes = argtypes;
774  plan.parserSetup = NULL;
775  plan.parserSetupArg = NULL;
776 
777  _SPI_prepare_plan(src, &plan);
778 
779  /* copy plan to procedure context */
780  result = _SPI_make_plan_non_temp(&plan);
781 
782  _SPI_end_call(true);
783 
784  return result;
785 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2048
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:2778
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2842
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
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_params()

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

Definition at line 788 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::parserSetup, _SPI_plan::parserSetupArg, SPI_ERROR_ARGUMENT, and SPI_result.

Referenced by exec_prepare_plan().

792 {
793  _SPI_plan plan;
794  SPIPlanPtr result;
795 
796  if (src == NULL)
797  {
799  return NULL;
800  }
801 
802  SPI_result = _SPI_begin_call(true);
803  if (SPI_result < 0)
804  return NULL;
805 
806  memset(&plan, 0, sizeof(_SPI_plan));
807  plan.magic = _SPI_PLAN_MAGIC;
808  plan.cursor_options = cursorOptions;
809  plan.nargs = 0;
810  plan.argtypes = NULL;
811  plan.parserSetup = parserSetup;
812  plan.parserSetupArg = parserSetupArg;
813 
814  _SPI_prepare_plan(src, &plan);
815 
816  /* copy plan to procedure context */
817  result = _SPI_make_plan_non_temp(&plan);
818 
819  _SPI_end_call(true);
820 
821  return result;
822 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2048
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:2778
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2842
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
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_register_relation()

int SPI_register_relation ( EphemeralNamedRelation  enr)

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

2997 {
2998  EphemeralNamedRelation match;
2999  int res;
3000 
3001  if (enr == NULL || enr->md.name == NULL)
3002  return SPI_ERROR_ARGUMENT;
3003 
3004  res = _SPI_begin_call(false); /* keep current memory context */
3005  if (res < 0)
3006  return res;
3007 
3008  match = _SPI_find_ENR_by_name(enr->md.name);
3009  if (match)
3011  else
3012  {
3013  if (_SPI_current->queryEnv == NULL)
3015 
3017  res = SPI_OK_REL_REGISTER;
3018  }
3019 
3020  _SPI_end_call(false);
3021 
3022  return res;
3023 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:50
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:2979
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define SPI_OK_REL_REGISTER
Definition: spi.h:67
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
QueryEnvironment * create_queryEnv(void)
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
void register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)

◆ SPI_register_trigger_data()

int SPI_register_trigger_data ( TriggerData tdata)

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

3064 {
3065  if (tdata == NULL)
3066  return SPI_ERROR_ARGUMENT;
3067 
3068  if (tdata->tg_newtable)
3069  {
3072  int rc;
3073 
3074  enr->md.name = tdata->tg_trigger->tgnewtable;
3075  enr->md.reliddesc = tdata->tg_relation->rd_id;
3076  enr->md.tupdesc = NULL;
3079  enr->reldata = tdata->tg_newtable;
3080  rc = SPI_register_relation(enr);
3081  if (rc != SPI_OK_REL_REGISTER)
3082  return rc;
3083  }
3084 
3085  if (tdata->tg_oldtable)
3086  {
3089  int rc;
3090 
3091  enr->md.name = tdata->tg_trigger->tgoldtable;
3092  enr->md.reliddesc = tdata->tg_relation->rd_id;
3093  enr->md.tupdesc = NULL;
3096  enr->reldata = tdata->tg_oldtable;
3097  rc = SPI_register_relation(enr);
3098  if (rc != SPI_OK_REL_REGISTER)
3099  return rc;
3100  }
3101 
3102  return SPI_OK_TD_REGISTER;
3103 }
EphemeralNamedRelationMetadataData md
#define SPI_OK_REL_REGISTER
Definition: spi.h:67
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
#define SPI_OK_TD_REGISTER
Definition: spi.h:69
Oid rd_id
Definition: rel.h:111
char * tgnewtable
Definition: reltrigger.h:44
Trigger * tg_trigger
Definition: trigger.h:37
int64 tuplestore_tuple_count(Tuplestorestate *state)
Definition: tuplestore.c:546
Tuplestorestate * tg_oldtable
Definition: trigger.h:40
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:2996
void * palloc(Size size)
Definition: mcxt.c:950
Tuplestorestate * tg_newtable
Definition: trigger.h:41
EphemeralNameRelationType enrtype
char * tgoldtable
Definition: reltrigger.h:43
Relation tg_relation
Definition: trigger.h:34

◆ SPI_repalloc()

void* SPI_repalloc ( void *  pointer,
Size  size 
)

Definition at line 1196 of file spi.c.

References repalloc().

1197 {
1198  /* No longer need to worry which context chunk was in... */
1199  return repalloc(pointer, size);
1200 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1070

◆ SPI_result_code_string()

const char* SPI_result_code_string ( int  code)

Definition at line 1815 of file spi.c.

References buf, SPI_ERROR_ARGUMENT, SPI_ERROR_CONNECT, SPI_ERROR_COPY, SPI_ERROR_NOATTRIBUTE, SPI_ERROR_NOOUTFUNC, SPI_ERROR_OPUNKNOWN, SPI_ERROR_PARAM, SPI_ERROR_REL_DUPLICATE, SPI_ERROR_REL_NOT_FOUND, SPI_ERROR_TRANSACTION, SPI_ERROR_TYPUNKNOWN, SPI_ERROR_UNCONNECTED, SPI_OK_CONNECT, SPI_OK_CURSOR, SPI_OK_DELETE, SPI_OK_DELETE_RETURNING, SPI_OK_FETCH, SPI_OK_FINISH, SPI_OK_INSERT, SPI_OK_INSERT_RETURNING, SPI_OK_REL_REGISTER, SPI_OK_REL_UNREGISTER, SPI_OK_REWRITTEN, SPI_OK_SELECT, SPI_OK_SELINTO, SPI_OK_UPDATE, SPI_OK_UPDATE_RETURNING, SPI_OK_UTILITY, and sprintf.

Referenced by check_foreign_key(), check_primary_key(), exec_dynquery_with_params(), exec_prepare_plan(), exec_run_select(), exec_stmt_call(), exec_stmt_dynexecute(), exec_stmt_execsql(), exec_stmt_forc(), exec_stmt_open(), exec_stmt_return_query(), exec_stmt_set(), plperl_spi_execute_fetch_result(), plperl_spi_prepare(), plperl_spi_query(), plperl_spi_query_prepared(), plpgsql_call_handler(), plpgsql_inline_handler(), plpgsql_validator(), pltcl_process_SPI_result(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), PLy_spi_prepare(), query_to_oid_list(), RI_Initial_Check(), RI_PartitionRemove_Check(), ri_PerformCheck(), ri_PlanCheck(), and ttdummy().

1816 {
1817  static char buf[64];
1818 
1819  switch (code)
1820  {
1821  case SPI_ERROR_CONNECT:
1822  return "SPI_ERROR_CONNECT";
1823  case SPI_ERROR_COPY:
1824  return "SPI_ERROR_COPY";
1825  case SPI_ERROR_OPUNKNOWN:
1826  return "SPI_ERROR_OPUNKNOWN";
1827  case SPI_ERROR_UNCONNECTED:
1828  return "SPI_ERROR_UNCONNECTED";
1829  case SPI_ERROR_ARGUMENT:
1830  return "SPI_ERROR_ARGUMENT";
1831  case SPI_ERROR_PARAM:
1832  return "SPI_ERROR_PARAM";
1833  case SPI_ERROR_TRANSACTION:
1834  return "SPI_ERROR_TRANSACTION";
1835  case SPI_ERROR_NOATTRIBUTE:
1836  return "SPI_ERROR_NOATTRIBUTE";
1837  case SPI_ERROR_NOOUTFUNC:
1838  return "SPI_ERROR_NOOUTFUNC";
1839  case SPI_ERROR_TYPUNKNOWN:
1840  return "SPI_ERROR_TYPUNKNOWN";
1842  return "SPI_ERROR_REL_DUPLICATE";
1844  return "SPI_ERROR_REL_NOT_FOUND";
1845  case SPI_OK_CONNECT:
1846  return "SPI_OK_CONNECT";
1847  case SPI_OK_FINISH:
1848  return "SPI_OK_FINISH";
1849  case SPI_OK_FETCH:
1850  return "SPI_OK_FETCH";
1851  case SPI_OK_UTILITY:
1852  return "SPI_OK_UTILITY";
1853  case SPI_OK_SELECT:
1854  return "SPI_OK_SELECT";
1855  case SPI_OK_SELINTO:
1856  return "SPI_OK_SELINTO";
1857  case SPI_OK_INSERT:
1858  return "SPI_OK_INSERT";
1859  case SPI_OK_DELETE:
1860  return "SPI_OK_DELETE";
1861  case SPI_OK_UPDATE:
1862  return "SPI_OK_UPDATE";
1863  case SPI_OK_CURSOR:
1864  return "SPI_OK_CURSOR";
1866  return "SPI_OK_INSERT_RETURNING";
1868  return "SPI_OK_DELETE_RETURNING";
1870  return "SPI_OK_UPDATE_RETURNING";
1871  case SPI_OK_REWRITTEN:
1872  return "SPI_OK_REWRITTEN";
1873  case SPI_OK_REL_REGISTER:
1874  return "SPI_OK_REL_REGISTER";
1875  case SPI_OK_REL_UNREGISTER:
1876  return "SPI_OK_REL_UNREGISTER";
1877  }
1878  /* Unrecognized code ... return something useful ... */
1879  sprintf(buf, "Unrecognized SPI code %d", code);
1880  return buf;
1881 }
#define SPI_OK_CONNECT
Definition: spi.h:53
#define SPI_ERROR_PARAM
Definition: spi.h:45
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:51
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:50
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:42
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:64
#define SPI_OK_DELETE
Definition: spi.h:60
#define SPI_ERROR_COPY
Definition: spi.h:40
#define SPI_ERROR_OPUNKNOWN
Definition: spi.h:41
#define SPI_OK_REL_REGISTER
Definition: spi.h:67
#define SPI_ERROR_CONNECT
Definition: spi.h:39
#define SPI_OK_CURSOR
Definition: spi.h:62
#define sprintf
Definition: port.h:195
#define SPI_ERROR_NOOUTFUNC
Definition: spi.h:48
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:63
static char * buf
Definition: pg_test_fsync.c:67
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
#define SPI_OK_UTILITY
Definition: spi.h:56
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:65
#define SPI_OK_REWRITTEN
Definition: spi.h:66
#define SPI_ERROR_TRANSACTION
Definition: spi.h:46
#define SPI_OK_SELINTO
Definition: spi.h:58
#define SPI_OK_FETCH
Definition: spi.h:55
#define SPI_OK_SELECT
Definition: spi.h:57
#define SPI_OK_FINISH
Definition: spi.h:54
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:49
#define SPI_OK_UPDATE
Definition: spi.h:61
#define SPI_OK_INSERT
Definition: spi.h:59
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:68

◆ SPI_returntuple()

HeapTupleHeader SPI_returntuple ( HeapTuple  tuple,
TupleDesc  tupdesc 
)

Definition at line 923 of file spi.c.

References assign_record_type_typmod(), DatumGetHeapTupleHeader, heap_copy_tuple_as_datum(), MemoryContextSwitchTo(), _SPI_connection::savedcxt, SPI_ERROR_ARGUMENT, SPI_ERROR_UNCONNECTED, SPI_result, TupleDescData::tdtypeid, and TupleDescData::tdtypmod.

Referenced by coerce_function_result_tuple().

924 {
925  MemoryContext oldcxt;
926  HeapTupleHeader dtup;
927 
928  if (tuple == NULL || tupdesc == NULL)
929  {
931  return NULL;
932  }
933 
934  if (_SPI_current == NULL)
935  {
937  return NULL;
938  }
939 
940  /* For RECORD results, make sure a typmod has been assigned */
941  if (tupdesc->tdtypeid == RECORDOID &&
942  tupdesc->tdtypmod < 0)
943  assign_record_type_typmod(tupdesc);
944 
946 
947  dtup = DatumGetHeapTupleHeader(heap_copy_tuple_as_datum(tuple, tupdesc));
948 
949  MemoryContextSwitchTo(oldcxt);
950 
951  return dtup;
952 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:42
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:294
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1830
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
int32 tdtypmod
Definition: tupdesc.h:83
MemoryContext savedcxt
Definition: spi_priv.h:35
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:984
Oid tdtypeid
Definition: tupdesc.h:82

◆ SPI_rollback()

void SPI_rollback ( void  )

Definition at line 334 of file spi.c.

References _SPI_rollback().

Referenced by exec_stmt_rollback(), plperl_spi_rollback(), pltcl_rollback(), and PLy_rollback().

335 {
336  _SPI_rollback(false);
337 }
static void _SPI_rollback(bool chain)
Definition: spi.c:291

◆ SPI_rollback_and_chain()

void SPI_rollback_and_chain ( void  )

Definition at line 340 of file spi.c.

References _SPI_rollback().

Referenced by exec_stmt_rollback().

341 {
342  _SPI_rollback(true);
343 }
static void _SPI_rollback(bool chain)
Definition: spi.c:291

◆ SPI_saveplan()

SPIPlanPtr SPI_saveplan ( SPIPlanPtr  plan)

Definition at line 852 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_PLAN_MAGIC, _SPI_save_plan(), _SPI_plan::magic, SPI_ERROR_ARGUMENT, and SPI_result.

853 {
854  SPIPlanPtr newplan;
855 
856  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
857  {
859  return NULL;
860  }
861 
862  SPI_result = _SPI_begin_call(false); /* don't change context */
863  if (SPI_result < 0)
864  return NULL;
865 
866  newplan = _SPI_save_plan(plan);
867 
868  SPI_result = _SPI_end_call(false);
869 
870  return newplan;
871 }
#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:2778
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
static SPIPlanPtr _SPI_save_plan(SPIPlanPtr plan)
Definition: spi.c:2909
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802

◆ SPI_scroll_cursor_fetch()

void SPI_scroll_cursor_fetch ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1678 of file spi.c.

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

Referenced by exec_stmt_fetch().

1679 {
1680  _SPI_cursor_operation(portal,
1681  direction, count,
1683  /* we know that the DestSPI receiver doesn't need a destroy call */
1684 }
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:2708

◆ SPI_scroll_cursor_move()

void SPI_scroll_cursor_move ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1693 of file spi.c.

References _SPI_cursor_operation(), and None_Receiver.

Referenced by exec_stmt_fetch().

1694 {
1695  _SPI_cursor_operation(portal, direction, count, None_Receiver);
1696 }
DestReceiver * None_Receiver
Definition: dest.c:96
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2708

◆ SPI_start_transaction()

void SPI_start_transaction ( void  )

Definition at line 213 of file spi.c.

References CurrentMemoryContext, MemoryContextSwitchTo(), and StartTransactionCommand().

Referenced by exec_stmt_commit(), exec_stmt_rollback(), plperl_spi_commit(), plperl_spi_rollback(), pltcl_commit(), pltcl_rollback(), PLy_commit(), and PLy_rollback().

214 {
215  MemoryContext oldcontext = CurrentMemoryContext;
216 
218  MemoryContextSwitchTo(oldcontext);
219 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void StartTransactionCommand(void)
Definition: xact.c:2846

◆ SPI_unregister_relation()

int SPI_unregister_relation ( const char *  name)

Definition at line 3030 of file spi.c.

References _SPI_begin_call(), _SPI_end_call(), _SPI_find_ENR_by_name(), EphemeralNamedRelationData::md, EphemeralNamedRelationMetadataData::name, _SPI_connection::queryEnv, SPI_ERROR_ARGUMENT, SPI_ERROR_REL_NOT_FOUND, SPI_OK_REL_UNREGISTER, and unregister_ENR().

3031 {
3032  EphemeralNamedRelation match;
3033  int res;
3034 
3035  if (name == NULL)
3036  return SPI_ERROR_ARGUMENT;
3037 
3038  res = _SPI_begin_call(false); /* keep current memory context */
3039  if (res < 0)
3040  return res;
3041 
3042  match = _SPI_find_ENR_by_name(name);
3043  if (match)
3044  {
3046  res = SPI_OK_REL_UNREGISTER;
3047  }
3048  else
3050 
3051  _SPI_end_call(false);
3052 
3053  return res;
3054 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:51
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:2979
static _SPI_connection * _SPI_current
Definition: spi.c:50
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2778
#define SPI_ERROR_ARGUMENT
Definition: spi.h:44
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
void unregister_ENR(QueryEnvironment *queryEnv, const char *name)
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2802
const char * name
Definition: encode.c:561
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:68

◆ SPICleanup()

void SPICleanup ( void  )

Definition at line 350 of file spi.c.

References _SPI_connected, SPI_processed, and SPI_result.

Referenced by AtEOXact_SPI(), and PostgresMain().

351 {
352  _SPI_current = NULL;
353  _SPI_connected = -1;
354  /* Reset API global variables, too */
355  SPI_processed = 0;
356  SPI_tuptable = NULL;
357  SPI_result = 0;
358 }
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
int SPI_result
Definition: spi.c:47

Variable Documentation

◆ SPI_processed

◆ SPI_result

◆ SPI_tuptable