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_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_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_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)
 
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_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 AtEOXact_SPI (bool isCommit)
 
void AtEOSubXact_SPI (bool isCommit, SubTransactionId mySubid)
 

Variables

PGDLLIMPORT uint64 SPI_processed
 
PGDLLIMPORT Oid SPI_lastoid
 
PGDLLIMPORT SPITupleTableSPI_tuptable
 
PGDLLIMPORT int SPI_result
 

Macro Definition Documentation

◆ SPI_ERROR_ARGUMENT

◆ SPI_ERROR_CONNECT

#define SPI_ERROR_CONNECT   (-1)

Definition at line 36 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 40 of file spi.h.

◆ SPI_ERROR_NOATTRIBUTE

◆ SPI_ERROR_NOOUTFUNC

#define SPI_ERROR_NOOUTFUNC   (-10)

Definition at line 45 of file spi.h.

Referenced by SPI_result_code_string().

◆ SPI_ERROR_OPUNKNOWN

#define SPI_ERROR_OPUNKNOWN   (-3)

Definition at line 38 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 47 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 48 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 46 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 59 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 52 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 64 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 65 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 66 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_pop

#define SPI_pop ( )    ((void) 0)

Definition at line 70 of file spi.h.

◆ SPI_pop_conditional

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

Definition at line 72 of file spi.h.

◆ SPI_push

#define SPI_push ( )    ((void) 0)

Definition at line 69 of file spi.h.

◆ SPI_push_conditional

#define SPI_push_conditional ( )    false

Definition at line 71 of file spi.h.

◆ SPI_restore_connection

#define SPI_restore_connection ( )    ((void) 0)

Definition at line 73 of file spi.h.

Typedef Documentation

◆ SPIPlanPtr

Definition at line 34 of file spi.h.

◆ SPITupleTable

Function Documentation

◆ AtEOSubXact_SPI()

void AtEOSubXact_SPI ( bool  isCommit,
SubTransactionId  mySubid 
)

Definition at line 216 of file spi.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

217 {
218  bool found = false;
219 
220  while (_SPI_connected >= 0)
221  {
223 
224  if (connection->connectSubid != mySubid)
225  break; /* couldn't be any underneath it either */
226 
227  found = true;
228 
229  /*
230  * Release procedure memory explicitly (see note in SPI_connect)
231  */
232  if (connection->execCxt)
233  {
234  MemoryContextDelete(connection->execCxt);
235  connection->execCxt = NULL;
236  }
237  if (connection->procCxt)
238  {
239  MemoryContextDelete(connection->procCxt);
240  connection->procCxt = NULL;
241  }
242 
243  /*
244  * Pop the stack entry and reset global variables. Unlike
245  * SPI_finish(), we don't risk switching to memory contexts that might
246  * be already gone.
247  */
248  _SPI_connected--;
249  if (_SPI_connected < 0)
250  _SPI_current = NULL;
251  else
253  SPI_processed = 0;
255  SPI_tuptable = NULL;
256  }
257 
258  if (found && isCommit)
260  (errcode(ERRCODE_WARNING),
261  errmsg("subtransaction left non-empty SPI stack"),
262  errhint("Check for missing \"SPI_finish\" calls.")));
263 
264  /*
265  * If we are aborting a subtransaction and there is an open SPI context
266  * surrounding the subxact, clean up to prevent memory leakage.
267  */
268  if (_SPI_current && !isCommit)
269  {
270  slist_mutable_iter siter;
271 
272  /*
273  * Throw away executor state if current executor operation was started
274  * within current subxact (essentially, force a _SPI_end_call(true)).
275  */
276  if (_SPI_current->execSubid >= mySubid)
277  {
280  }
281 
282  /* throw away any tuple tables created within current subxact */
284  {
285  SPITupleTable *tuptable;
286 
287  tuptable = slist_container(SPITupleTable, next, siter.cur);
288  if (tuptable->subid >= mySubid)
289  {
290  /*
291  * If we used SPI_freetuptable() here, its internal search of
292  * the tuptables list would make this operation O(N^2).
293  * Instead, just free the tuptable manually. This should
294  * match what SPI_freetuptable() does.
295  */
296  slist_delete_current(&siter);
297  if (tuptable == _SPI_current->tuptable)
298  _SPI_current->tuptable = NULL;
299  if (tuptable == SPI_tuptable)
300  SPI_tuptable = NULL;
301  MemoryContextDelete(tuptable->tuptabcxt);
302  }
303  }
304  }
305 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
int errhint(const char *fmt,...)
Definition: elog.c:987
static int32 next
Definition: blutils.c:210
slist_node * cur
Definition: ilist.h:241
SPITupleTable * SPI_tuptable
Definition: spi.c:41
int errcode(int sqlerrcode)
Definition: elog.c:575
static int _SPI_connected
Definition: spi.c:47
SubTransactionId execSubid
Definition: spi_priv.h:30
static _SPI_connection * _SPI_current
Definition: spi.c:45
uint64 SPI_processed
Definition: spi.c:39
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
SubTransactionId connectSubid
Definition: spi_priv.h:37
SPITupleTable * tuptable
Definition: spi_priv.h:27
static _SPI_connection * _SPI_stack
Definition: spi.c:44
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
MemoryContext procCxt
Definition: spi_priv.h:34
Oid SPI_lastoid
Definition: spi.c:40
MemoryContext execCxt
Definition: spi_priv.h:35
#define InvalidOid
Definition: postgres_ext.h:36
#define InvalidSubTransactionId
Definition: c.h:451
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
SubTransactionId subid
Definition: spi.h:30
slist_head tuptables
Definition: spi_priv.h:33
MemoryContext tuptabcxt
Definition: spi.h:24

◆ AtEOXact_SPI()

void AtEOXact_SPI ( bool  isCommit)

Definition at line 188 of file spi.c.

References _SPI_connected, _SPI_stack_depth, ereport, errcode(), errhint(), errmsg(), InvalidOid, SPI_lastoid, SPI_processed, and WARNING.

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

189 {
190  /*
191  * Note that memory contexts belonging to SPI stack entries will be freed
192  * automatically, so we can ignore them here. We just need to restore our
193  * static variables to initial state.
194  */
195  if (isCommit && _SPI_connected != -1)
197  (errcode(ERRCODE_WARNING),
198  errmsg("transaction left non-empty SPI stack"),
199  errhint("Check for missing \"SPI_finish\" calls.")));
200 
201  _SPI_current = _SPI_stack = NULL;
202  _SPI_stack_depth = 0;
203  _SPI_connected = -1;
204  SPI_processed = 0;
206  SPI_tuptable = NULL;
207 }
int errhint(const char *fmt,...)
Definition: elog.c:987
static int _SPI_stack_depth
Definition: spi.c:46
SPITupleTable * SPI_tuptable
Definition: spi.c:41
int errcode(int sqlerrcode)
Definition: elog.c:575
static int _SPI_connected
Definition: spi.c:47
static _SPI_connection * _SPI_current
Definition: spi.c:45
uint64 SPI_processed
Definition: spi.c:39
static _SPI_connection * _SPI_stack
Definition: spi.c:44
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
Oid SPI_lastoid
Definition: spi.c:40
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ SPI_connect()

int SPI_connect ( void  )

Definition at line 84 of file spi.c.

References _SPI_connected, _SPI_stack_depth, ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), Assert, _SPI_connection::connectSubid, elog, ERROR, _SPI_connection::execCxt, _SPI_connection::execSubid, GetCurrentSubTransactionId(), InvalidOid, InvalidSubTransactionId, _SPI_connection::lastoid, MemoryContextAlloc(), MemoryContextSwitchTo(), _SPI_connection::procCxt, _SPI_connection::processed, _SPI_connection::queryEnv, repalloc(), _SPI_connection::savedcxt, slist_init(), SPI_OK_CONNECT, TopTransactionContext, _SPI_connection::tuptable, and _SPI_connection::tuptables.

Referenced by check_foreign_key(), check_primary_key(), connectby(), crosstab(), cursor_to_xml(), cursor_to_xmlschema(), database_to_xml_internal(), database_to_xmlschema_internal(), funny_dup17(), 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(), plpython_call_handler(), plpython_inline_handler(), pltcl_event_trigger_handler(), pltcl_func_handler(), pltcl_trigger_handler(), 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_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), RI_FKey_setnull_upd(), RI_Initial_Check(), ri_restrict_del(), ri_restrict_upd(), schema_to_xml_internal(), schema_to_xmlschema_internal(), timetravel(), ts_stat1(), ts_stat2(), tsquery_rewrite_query(), ttdummy(), and xpath_table().

85 {
86  int newdepth;
87 
88  /* Enlarge stack if necessary */
89  if (_SPI_stack == NULL)
90  {
91  if (_SPI_connected != -1 || _SPI_stack_depth != 0)
92  elog(ERROR, "SPI stack corrupted");
93  newdepth = 16;
96  newdepth * sizeof(_SPI_connection));
97  _SPI_stack_depth = newdepth;
98  }
99  else
100  {
102  elog(ERROR, "SPI stack corrupted");
103  if (_SPI_stack_depth == _SPI_connected + 1)
104  {
105  newdepth = _SPI_stack_depth * 2;
108  newdepth * sizeof(_SPI_connection));
109  _SPI_stack_depth = newdepth;
110  }
111  }
112 
113  /* Enter new stack level */
114  _SPI_connected++;
116 
118  _SPI_current->processed = 0;
120  _SPI_current->tuptable = NULL;
123  _SPI_current->procCxt = NULL; /* in case we fail to create 'em */
124  _SPI_current->execCxt = NULL;
126  _SPI_current->queryEnv = NULL;
127 
128  /*
129  * Create memory contexts for this procedure
130  *
131  * XXX it would be better to use PortalContext as the parent context, but
132  * we may not be inside a portal (consider deferred-trigger execution).
133  * Perhaps CurTransactionContext would do? For now it doesn't matter
134  * because we clean up explicitly in AtEOSubXact_SPI().
135  */
137  "SPI Proc",
140  "SPI Exec",
142  /* ... and switch to procedure's context */
144 
145  return SPI_OK_CONNECT;
146 }
#define SPI_OK_CONNECT
Definition: spi.h:50
MemoryContext TopTransactionContext
Definition: mcxt.c:48
static int _SPI_stack_depth
Definition: spi.c:46
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static int _SPI_connected
Definition: spi.c:47
SubTransactionId execSubid
Definition: spi_priv.h:30
static _SPI_connection * _SPI_current
Definition: spi.c:45
SubTransactionId connectSubid
Definition: spi_priv.h:37
static void slist_init(slist_head *head)
Definition: ilist.h:554
#define ERROR
Definition: elog.h:43
QueryEnvironment * queryEnv
Definition: spi_priv.h:38
SPITupleTable * tuptable
Definition: spi_priv.h:27
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
static _SPI_connection * _SPI_stack
Definition: spi.c:44
MemoryContext savedcxt
Definition: spi_priv.h:36
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
MemoryContext procCxt
Definition: spi_priv.h:34
MemoryContext execCxt
Definition: spi_priv.h:35
#define InvalidOid
Definition: postgres_ext.h:36
uint64 processed
Definition: spi_priv.h:25
#define Assert(condition)
Definition: c.h:670
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:642
#define InvalidSubTransactionId
Definition: c.h:451
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define elog
Definition: elog.h:219
slist_head tuptables
Definition: spi_priv.h:33

◆ SPI_copytuple()

HeapTuple SPI_copytuple ( HeapTuple  tuple)

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

638 {
639  MemoryContext oldcxt;
640  HeapTuple ctuple;
641 
642  if (tuple == NULL)
643  {
645  return NULL;
646  }
647 
648  if (_SPI_current == NULL)
649  {
651  return NULL;
652  }
653 
655 
656  ctuple = heap_copytuple(tuple);
657 
658  MemoryContextSwitchTo(oldcxt);
659 
660  return ctuple;
661 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:611
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
MemoryContext savedcxt
Definition: spi_priv.h:36

◆ SPI_cursor_close()

void SPI_cursor_close ( Portal  portal)

Definition at line 1411 of file spi.c.

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

Referenced by exec_stmt_close(), exec_stmt_dynfors(), exec_stmt_forc(), exec_stmt_fors(), exec_stmt_return_query(), 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().

1412 {
1413  if (!PortalIsValid(portal))
1414  elog(ERROR, "invalid portal in SPI cursor operation");
1415 
1416  PortalDrop(portal, false);
1417 }
#define ERROR
Definition: elog.h:43
#define PortalIsValid(p)
Definition: portal.h:199
void PortalDrop(Portal portal, bool isTopCommit)
Definition: portalmem.c:461
#define elog
Definition: elog.h:219

◆ SPI_cursor_fetch()

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

Definition at line 1355 of file spi.c.

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

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

1356 {
1357  _SPI_cursor_operation(portal,
1358  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1360  /* we know that the DestSPI receiver doesn't need a destroy call */
1361 }
Definition: dest.h:93
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:109
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2391

◆ SPI_cursor_find()

Portal SPI_cursor_find ( const char *  name)

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

1344 {
1345  return GetPortalByName(name);
1346 }
Portal GetPortalByName(const char *name)
Definition: portalmem.c:129
const char * name
Definition: encode.c:521

◆ SPI_cursor_move()

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

Definition at line 1370 of file spi.c.

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

1371 {
1372  _SPI_cursor_operation(portal,
1373  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1374  None_Receiver);
1375 }
DestReceiver * None_Receiver
Definition: dest.c:91
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2391

◆ SPI_cursor_open()

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

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

1040 {
1041  Portal portal;
1042  ParamListInfo paramLI;
1043 
1044  /* build transient ParamListInfo in caller's context */
1045  paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
1046  Values, Nulls);
1047 
1048  portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
1049 
1050  /* done with the transient ParamListInfo */
1051  if (paramLI)
1052  pfree(paramLI);
1053 
1054  return portal;
1055 }
Oid * argtypes
Definition: spi_priv.h:89
void pfree(void *pointer)
Definition: mcxt.c:949
int nargs
Definition: spi_priv.h:88
const char * name
Definition: encode.c:521
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1129
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2248
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ 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 1064 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.

Referenced by exec_dynquery_with_params().

1069 {
1070  Portal result;
1071  _SPI_plan plan;
1072  ParamListInfo paramLI;
1073 
1074  if (src == NULL || nargs < 0)
1075  elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
1076 
1077  if (nargs > 0 && (argtypes == NULL || Values == NULL))
1078  elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
1079 
1080  SPI_result = _SPI_begin_call(true);
1081  if (SPI_result < 0)
1082  elog(ERROR, "SPI_cursor_open_with_args called while not connected");
1083 
1084  memset(&plan, 0, sizeof(_SPI_plan));
1085  plan.magic = _SPI_PLAN_MAGIC;
1086  plan.cursor_options = cursorOptions;
1087  plan.nargs = nargs;
1088  plan.argtypes = argtypes;
1089  plan.parserSetup = NULL;
1090  plan.parserSetupArg = NULL;
1091 
1092  /* build transient ParamListInfo in executor context */
1093  paramLI = _SPI_convert_params(nargs, argtypes,
1094  Values, Nulls);
1095 
1096  _SPI_prepare_plan(src, &plan);
1097 
1098  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1099 
1100  result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
1101 
1102  /* And clean up */
1103  _SPI_end_call(true);
1104 
1105  return result;
1106 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:1753
Oid * argtypes
Definition: spi_priv.h:89
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
int SPI_result
Definition: spi.c:42
#define ERROR
Definition: elog.h:43
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
int nargs
Definition: spi_priv.h:88
ParserSetupHook parserSetup
Definition: spi_priv.h:90
void * parserSetupArg
Definition: spi_priv.h:91
const char * name
Definition: encode.c:521
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1129
int cursor_options
Definition: spi_priv.h:87
#define elog
Definition: elog.h:219
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2248
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ SPI_cursor_open_with_paramlist()

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

Definition at line 1116 of file spi.c.

References SPI_cursor_open_internal().

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

1118 {
1119  return SPI_cursor_open_internal(name, plan, params, read_only);
1120 }
const char * name
Definition: encode.c:521
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1129

◆ SPI_datumTransfer()

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

Definition at line 953 of file spi.c.

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

Referenced by plpgsql_exec_function().

954 {
955  MemoryContext oldcxt;
956  Datum result;
957 
958  if (_SPI_current == NULL)
959  elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
960 
962 
963  result = datumTransfer(value, typByVal, typLen);
964 
965  MemoryContextSwitchTo(oldcxt);
966 
967  return result;
968 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
#define ERROR
Definition: elog.h:43
static struct @121 value
MemoryContext savedcxt
Definition: spi_priv.h:36
uintptr_t Datum
Definition: postgres.h:372
Datum datumTransfer(Datum value, bool typByVal, int typLen)
Definition: datum.c:190
#define elog
Definition: elog.h:219

◆ SPI_exec()

int SPI_exec ( const char *  src,
long  tcount 
)

Definition at line 338 of file spi.c.

References SPI_execute().

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

339 {
340  return SPI_execute(src, false, tcount);
341 }
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:310

◆ SPI_execp()

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

Definition at line 372 of file spi.c.

References SPI_execute_plan().

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

373 {
374  return SPI_execute_plan(plan, Values, Nulls, false, tcount);
375 }
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:345
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ SPI_execute()

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

Definition at line 310 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(), exec_stmt_dynexecute(), 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().

311 {
312  _SPI_plan plan;
313  int res;
314 
315  if (src == NULL || tcount < 0)
316  return SPI_ERROR_ARGUMENT;
317 
318  res = _SPI_begin_call(true);
319  if (res < 0)
320  return res;
321 
322  memset(&plan, 0, sizeof(_SPI_plan));
323  plan.magic = _SPI_PLAN_MAGIC;
325 
326  _SPI_prepare_oneshot_plan(src, &plan);
327 
328  res = _SPI_execute_plan(&plan, NULL,
330  read_only, true, tcount);
331 
332  _SPI_end_call(true);
333  return res;
334 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:1858
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount)
Definition: spi.c:1916
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
#define InvalidSnapshot
Definition: snapshot.h:25
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2646
int cursor_options
Definition: spi_priv.h:87

◆ SPI_execute_plan()

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

Definition at line 345 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 pg_get_ruledef_worker(), pg_get_viewdef_worker(), plperl_spi_exec_prepared(), pltcl_SPI_execute_plan(), PLy_spi_execute_plan(), and SPI_execp().

347 {
348  int res;
349 
350  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
351  return SPI_ERROR_ARGUMENT;
352 
353  if (plan->nargs > 0 && Values == NULL)
354  return SPI_ERROR_PARAM;
355 
356  res = _SPI_begin_call(true);
357  if (res < 0)
358  return res;
359 
360  res = _SPI_execute_plan(plan,
361  _SPI_convert_params(plan->nargs, plan->argtypes,
362  Values, Nulls),
364  read_only, true, tcount);
365 
366  _SPI_end_call(true);
367  return res;
368 }
Oid * argtypes
Definition: spi_priv.h:89
#define SPI_ERROR_PARAM
Definition: spi.h:42
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount)
Definition: spi.c:1916
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
#define InvalidSnapshot
Definition: snapshot.h:25
int nargs
Definition: spi_priv.h:88
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2248
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ SPI_execute_plan_with_paramlist()

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

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

381 {
382  int res;
383 
384  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
385  return SPI_ERROR_ARGUMENT;
386 
387  res = _SPI_begin_call(true);
388  if (res < 0)
389  return res;
390 
391  res = _SPI_execute_plan(plan, params,
393  read_only, true, tcount);
394 
395  _SPI_end_call(true);
396  return res;
397 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount)
Definition: spi.c:1916
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
#define InvalidSnapshot
Definition: snapshot.h:25

◆ 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 413 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(), and ri_PerformCheck().

417 {
418  int res;
419 
420  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
421  return SPI_ERROR_ARGUMENT;
422 
423  if (plan->nargs > 0 && Values == NULL)
424  return SPI_ERROR_PARAM;
425 
426  res = _SPI_begin_call(true);
427  if (res < 0)
428  return res;
429 
430  res = _SPI_execute_plan(plan,
431  _SPI_convert_params(plan->nargs, plan->argtypes,
432  Values, Nulls),
433  snapshot, crosscheck_snapshot,
434  read_only, fire_triggers, tcount);
435 
436  _SPI_end_call(true);
437  return res;
438 }
Oid * argtypes
Definition: spi_priv.h:89
#define SPI_ERROR_PARAM
Definition: spi.h:42
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount)
Definition: spi.c:1916
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
int nargs
Definition: spi_priv.h:88
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2248
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ 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 447 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.

Referenced by exec_stmt_dynexecute().

451 {
452  int res;
453  _SPI_plan plan;
454  ParamListInfo paramLI;
455 
456  if (src == NULL || nargs < 0 || tcount < 0)
457  return SPI_ERROR_ARGUMENT;
458 
459  if (nargs > 0 && (argtypes == NULL || Values == NULL))
460  return SPI_ERROR_PARAM;
461 
462  res = _SPI_begin_call(true);
463  if (res < 0)
464  return res;
465 
466  memset(&plan, 0, sizeof(_SPI_plan));
467  plan.magic = _SPI_PLAN_MAGIC;
469  plan.nargs = nargs;
470  plan.argtypes = argtypes;
471  plan.parserSetup = NULL;
472  plan.parserSetupArg = NULL;
473 
474  paramLI = _SPI_convert_params(nargs, argtypes,
475  Values, Nulls);
476 
477  _SPI_prepare_oneshot_plan(src, &plan);
478 
479  res = _SPI_execute_plan(&plan, paramLI,
481  read_only, true, tcount);
482 
483  _SPI_end_call(true);
484  return res;
485 }
Oid * argtypes
Definition: spi_priv.h:89
#define SPI_ERROR_PARAM
Definition: spi.h:42
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:1858
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool fire_triggers, uint64 tcount)
Definition: spi.c:1916
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
#define InvalidSnapshot
Definition: snapshot.h:25
int nargs
Definition: spi_priv.h:88
ParserSetupHook parserSetup
Definition: spi_priv.h:90
void * parserSetupArg
Definition: spi_priv.h:91
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2646
int cursor_options
Definition: spi_priv.h:87
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2248
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ SPI_finish()

int SPI_finish ( void  )

Definition at line 149 of file spi.c.

References _SPI_begin_call(), _SPI_connected, _SPI_connection::execCxt, InvalidOid, MemoryContextDelete(), MemoryContextSwitchTo(), _SPI_connection::procCxt, _SPI_connection::savedcxt, SPI_lastoid, SPI_OK_FINISH, and SPI_processed.

Referenced by check_foreign_key(), check_primary_key(), connectby(), crosstab(), cursor_to_xml(), cursor_to_xmlschema(), database_to_xml_internal(), database_to_xmlschema_internal(), funny_dup17(), 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_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), RI_FKey_setnull_upd(), RI_Initial_Check(), ri_restrict_del(), ri_restrict_upd(), schema_to_xml_internal(), schema_to_xmlschema_internal(), timetravel(), ts_stat1(), ts_stat2(), tsquery_rewrite_query(), ttdummy(), and xpath_table().

150 {
151  int res;
152 
153  res = _SPI_begin_call(false); /* just check we're connected */
154  if (res < 0)
155  return res;
156 
157  /* Restore memory context as it was before procedure call */
159 
160  /* Release memory used in procedure call (including tuptables) */
162  _SPI_current->execCxt = NULL;
164  _SPI_current->procCxt = NULL;
165 
166  /*
167  * Reset result variables, especially SPI_tuptable which is probably
168  * pointing at a just-deleted tuptable
169  */
170  SPI_processed = 0;
172  SPI_tuptable = NULL;
173 
174  /* Exit stack level */
175  _SPI_connected--;
176  if (_SPI_connected < 0)
177  _SPI_current = NULL;
178  else
180 
181  return SPI_OK_FINISH;
182 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static int _SPI_connected
Definition: spi.c:47
static _SPI_connection * _SPI_current
Definition: spi.c:45
uint64 SPI_processed
Definition: spi.c:39
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
static _SPI_connection * _SPI_stack
Definition: spi.c:44
MemoryContext savedcxt
Definition: spi_priv.h:36
MemoryContext procCxt
Definition: spi_priv.h:34
Oid SPI_lastoid
Definition: spi.c:40
MemoryContext execCxt
Definition: spi_priv.h:35
#define InvalidOid
Definition: postgres_ext.h:36
#define SPI_OK_FINISH
Definition: spi.h:51

◆ SPI_fname()

char* SPI_fname ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 790 of file spi.c.

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

Referenced by funny_dup17(), get_pkey_attnames(), ri_ReportViolation(), and SPI_sql_row_to_xmlelement().

791 {
792  Form_pg_attribute att;
793 
794  SPI_result = 0;
795 
796  if (fnumber > tupdesc->natts || fnumber == 0 ||
798  {
800  return NULL;
801  }
802 
803  if (fnumber > 0)
804  att = TupleDescAttr(tupdesc, fnumber - 1);
805  else
806  att = SystemAttributeDefinition(fnumber, true);
807 
808  return pstrdup(NameStr(att->attname));
809 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
int natts
Definition: tupdesc.h:79
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define NameStr(name)
Definition: c.h:547

◆ SPI_fnumber()

int SPI_fnumber ( TupleDesc  tupdesc,
const char *  fname 
)

Definition at line 767 of file spi.c.

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

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

768 {
769  int res;
770  Form_pg_attribute sysatt;
771 
772  for (res = 0; res < tupdesc->natts; res++)
773  {
774  Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
775 
776  if (namestrcmp(&attr->attname, fname) == 0 &&
777  !attr->attisdropped)
778  return res + 1;
779  }
780 
781  sysatt = SystemAttributeByName(fname, true /* "oid" will be accepted */ );
782  if (sysatt != NULL)
783  return sysatt->attnum;
784 
785  /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
786  return SPI_ERROR_NOATTRIBUTE;
787 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
Form_pg_attribute SystemAttributeByName(const char *attname, bool relhasoids)
Definition: heap.c:214
int namestrcmp(Name name, const char *str)
Definition: name.c:247
int natts
Definition: tupdesc.h:79
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187

◆ SPI_freeplan()

int SPI_freeplan ( SPIPlanPtr  plan)

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

616 {
617  ListCell *lc;
618 
619  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
620  return SPI_ERROR_ARGUMENT;
621 
622  /* Release the plancache entries */
623  foreach(lc, plan->plancache_list)
624  {
625  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
626 
627  DropCachedPlan(plansource);
628  }
629 
630  /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
632 
633  return 0;
634 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
List * plancache_list
Definition: spi_priv.h:85
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
MemoryContext plancxt
Definition: spi_priv.h:86
#define lfirst(lc)
Definition: pg_list.h:106
void DropCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:486

◆ SPI_freetuple()

void SPI_freetuple ( HeapTuple  pointer)

Definition at line 971 of file spi.c.

References heap_freetuple().

972 {
973  /* No longer need to worry which context tuple was in... */
974  heap_freetuple(tuple);
975 }
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373

◆ SPI_freetuptable()

void SPI_freetuptable ( SPITupleTable tuptable)

Definition at line 978 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_dynexecute(), exec_stmt_execsql(), exec_stmt_fetch(), exec_stmt_return_query(), 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().

979 {
980  bool found = false;
981 
982  /* ignore call if NULL pointer */
983  if (tuptable == NULL)
984  return;
985 
986  /*
987  * Search only the topmost SPI context for a matching tuple table.
988  */
989  if (_SPI_current != NULL)
990  {
991  slist_mutable_iter siter;
992 
993  /* find tuptable in active list, then remove it */
995  {
996  SPITupleTable *tt;
997 
998  tt = slist_container(SPITupleTable, next, siter.cur);
999  if (tt == tuptable)
1000  {
1001  slist_delete_current(&siter);
1002  found = true;
1003  break;
1004  }
1005  }
1006  }
1007 
1008  /*
1009  * Refuse the deletion if we didn't find it in the topmost SPI context.
1010  * This is primarily a guard against double deletion, but might prevent
1011  * other errors as well. Since the worst consequence of not deleting a
1012  * tuptable would be a transient memory leak, this is just a WARNING.
1013  */
1014  if (!found)
1015  {
1016  elog(WARNING, "attempt to delete invalid SPITupleTable %p", tuptable);
1017  return;
1018  }
1019 
1020  /* for safety, reset global variables that might point at tuptable */
1021  if (tuptable == _SPI_current->tuptable)
1022  _SPI_current->tuptable = NULL;
1023  if (tuptable == SPI_tuptable)
1024  SPI_tuptable = NULL;
1025 
1026  /* release all memory belonging to tuptable */
1027  MemoryContextDelete(tuptable->tuptabcxt);
1028 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static int32 next
Definition: blutils.c:210
slist_node * cur
Definition: ilist.h:241
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static _SPI_connection * _SPI_current
Definition: spi.c:45
#define slist_foreach_modify(iter, lhead)
Definition: ilist.h:716
SPITupleTable * tuptable
Definition: spi_priv.h:27
#define WARNING
Definition: elog.h:40
#define slist_container(type, membername, ptr)
Definition: ilist.h:674
static void slist_delete_current(slist_mutable_iter *iter)
Definition: ilist.h:652
#define elog
Definition: elog.h:219
slist_head tuptables
Definition: spi_priv.h:33
MemoryContext tuptabcxt
Definition: spi.h:24

◆ SPI_getargcount()

int SPI_getargcount ( SPIPlanPtr  plan)

Definition at line 1439 of file spi.c.

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

1440 {
1441  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1442  {
1444  return -1;
1445  }
1446  return plan->nargs;
1447 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
int nargs
Definition: spi_priv.h:88

◆ SPI_getargtypeid()

Oid SPI_getargtypeid ( SPIPlanPtr  plan,
int  argIndex 
)

Definition at line 1424 of file spi.c.

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

1425 {
1426  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
1427  argIndex < 0 || argIndex >= plan->nargs)
1428  {
1430  return InvalidOid;
1431  }
1432  return plan->argtypes[argIndex];
1433 }
Oid * argtypes
Definition: spi_priv.h:89
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
int nargs
Definition: spi_priv.h:88
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getbinval()

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

Definition at line 844 of file spi.c.

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

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

845 {
846  SPI_result = 0;
847 
848  if (fnumber > tupdesc->natts || fnumber == 0 ||
850  {
852  *isnull = true;
853  return (Datum) NULL;
854  }
855 
856  return heap_getattr(tuple, fnumber, tupdesc, isnull);
857 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
int natts
Definition: tupdesc.h:79
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:774
uintptr_t Datum
Definition: postgres.h:372

◆ SPI_getnspname()

char* SPI_getnspname ( Relation  rel)

Definition at line 924 of file spi.c.

References get_namespace_name(), and RelationGetNamespace.

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

925 {
927 }
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3047
#define RelationGetNamespace(relation)
Definition: rel.h:443

◆ SPI_getrelname()

char* SPI_getrelname ( Relation  rel)

Definition at line 918 of file spi.c.

References pstrdup(), and RelationGetRelationName.

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

919 {
920  return pstrdup(RelationGetRelationName(rel));
921 }
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define RelationGetRelationName(relation)
Definition: rel.h:436

◆ SPI_gettype()

char* SPI_gettype ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 860 of file spi.c.

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

Referenced by check_foreign_key(), and funny_dup17().

861 {
862  Oid typoid;
863  HeapTuple typeTuple;
864  char *result;
865 
866  SPI_result = 0;
867 
868  if (fnumber > tupdesc->natts || fnumber == 0 ||
870  {
872  return NULL;
873  }
874 
875  if (fnumber > 0)
876  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
877  else
878  typoid = (SystemAttributeDefinition(fnumber, true))->atttypid;
879 
880  typeTuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
881 
882  if (!HeapTupleIsValid(typeTuple))
883  {
885  return NULL;
886  }
887 
888  result = pstrdup(NameStr(((Form_pg_type) GETSTRUCT(typeTuple))->typname));
889  ReleaseSysCache(typeTuple);
890  return result;
891 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
char * pstrdup(const char *in)
Definition: mcxt.c:1076
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
int SPI_result
Definition: spi.c:42
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:46
#define NameStr(name)
Definition: c.h:547

◆ SPI_gettypeid()

Oid SPI_gettypeid ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 900 of file spi.c.

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

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

901 {
902  SPI_result = 0;
903 
904  if (fnumber > tupdesc->natts || fnumber == 0 ||
906  {
908  return InvalidOid;
909  }
910 
911  if (fnumber > 0)
912  return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
913  else
914  return (SystemAttributeDefinition(fnumber, true))->atttypid;
915 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
int natts
Definition: tupdesc.h:79
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getvalue()

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

Definition at line 812 of file spi.c.

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

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

813 {
814  Datum val;
815  bool isnull;
816  Oid typoid,
817  foutoid;
818  bool typisvarlena;
819 
820  SPI_result = 0;
821 
822  if (fnumber > tupdesc->natts || fnumber == 0 ||
824  {
826  return NULL;
827  }
828 
829  val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
830  if (isnull)
831  return NULL;
832 
833  if (fnumber > 0)
834  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
835  else
836  typoid = (SystemAttributeDefinition(fnumber, true))->atttypid;
837 
838  getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
839 
840  return OidOutputFunctionCall(foutoid, val);
841 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2646
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:28
unsigned int Oid
Definition: postgres_ext.h:31
int natts
Definition: tupdesc.h:79
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:774
uintptr_t Datum
Definition: postgres.h:372
Form_pg_attribute SystemAttributeDefinition(AttrNumber attno, bool relhasoids)
Definition: heap.c:200
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1742
long val
Definition: informix.c:689

◆ SPI_is_cursor_plan()

bool SPI_is_cursor_plan ( SPIPlanPtr  plan)

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

1460 {
1461  CachedPlanSource *plansource;
1462 
1463  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1464  {
1466  return false;
1467  }
1468 
1469  if (list_length(plan->plancache_list) != 1)
1470  {
1471  SPI_result = 0;
1472  return false; /* not exactly 1 pre-rewrite command */
1473  }
1474  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1475 
1476  /*
1477  * We used to force revalidation of the cached plan here, but that seems
1478  * unnecessary: invalidation could mean a change in the rowtype of the
1479  * tuples returned by a plan, but not whether it returns tuples at all.
1480  */
1481  SPI_result = 0;
1482 
1483  /* Does it return tuples? */
1484  if (plansource->resultDesc)
1485  return true;
1486 
1487  return false;
1488 }
List * plancache_list
Definition: spi_priv.h:85
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
TupleDesc resultDesc
Definition: plancache.h:92
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
#define linitial(l)
Definition: pg_list.h:111
static int list_length(const List *l)
Definition: pg_list.h:89

◆ SPI_keepplan()

int SPI_keepplan ( SPIPlanPtr  plan)

Definition at line 566 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(), timetravel(), and ttdummy().

567 {
568  ListCell *lc;
569 
570  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
571  plan->saved || plan->oneshot)
572  return SPI_ERROR_ARGUMENT;
573 
574  /*
575  * Mark it saved, reparent it under CacheMemoryContext, and mark all the
576  * component CachedPlanSources as saved. This sequence cannot fail
577  * partway through, so there's no risk of long-term memory leakage.
578  */
579  plan->saved = true;
581 
582  foreach(lc, plan->plancache_list)
583  {
584  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
585 
586  SaveCachedPlan(plansource);
587  }
588 
589  return 0;
590 }
List * plancache_list
Definition: spi_priv.h:85
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent)
Definition: mcxt.c:317
int magic
Definition: spi_priv.h:82
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
MemoryContext plancxt
Definition: spi_priv.h:86
bool saved
Definition: spi_priv.h:83
void SaveCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:441
#define lfirst(lc)
Definition: pg_list.h:106
bool oneshot
Definition: spi_priv.h:84
MemoryContext CacheMemoryContext
Definition: mcxt.c:46

◆ SPI_modifytuple()

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

Definition at line 696 of file spi.c.

References heap_deform_tuple(), heap_form_tuple(), HeapTupleGetOid, HeapTupleSetOid, i, MemoryContextSwitchTo(), tupleDesc::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, HeapTupleData::t_tableOid, and tupleDesc::tdhasoid.

Referenced by timetravel(), and ttdummy().

698 {
699  MemoryContext oldcxt;
700  HeapTuple mtuple;
701  int numberOfAttributes;
702  Datum *v;
703  bool *n;
704  int i;
705 
706  if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
707  {
709  return NULL;
710  }
711 
712  if (_SPI_current == NULL)
713  {
715  return NULL;
716  }
717 
719 
720  SPI_result = 0;
721 
722  numberOfAttributes = rel->rd_att->natts;
723  v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
724  n = (bool *) palloc(numberOfAttributes * sizeof(bool));
725 
726  /* fetch old values and nulls */
727  heap_deform_tuple(tuple, rel->rd_att, v, n);
728 
729  /* replace values and nulls */
730  for (i = 0; i < natts; i++)
731  {
732  if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
733  break;
734  v[attnum[i] - 1] = Values[i];
735  n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n') ? true : false;
736  }
737 
738  if (i == natts) /* no errors in *attnum */
739  {
740  mtuple = heap_form_tuple(rel->rd_att, v, n);
741 
742  /*
743  * copy the identification info of the old tuple: t_ctid, t_self, and
744  * OID (if any)
745  */
746  mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
747  mtuple->t_self = tuple->t_self;
748  mtuple->t_tableOid = tuple->t_tableOid;
749  if (rel->rd_att->tdhasoid)
750  HeapTupleSetOid(mtuple, HeapTupleGetOid(tuple));
751  }
752  else
753  {
754  mtuple = NULL;
756  }
757 
758  pfree(v);
759  pfree(n);
760 
761  MemoryContextSwitchTo(oldcxt);
762 
763  return mtuple;
764 }
bool tdhasoid
Definition: tupdesc.h:82
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
int natts
Definition: tupdesc.h:79
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleSetOid(tuple, oid)
Definition: htup_details.h:703
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
void pfree(void *pointer)
Definition: mcxt.c:949
ItemPointerData t_ctid
Definition: htup_details.h:155
ItemPointerData t_self
Definition: htup.h:65
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
Oid t_tableOid
Definition: htup.h:66
MemoryContext savedcxt
Definition: spi_priv.h:36
uintptr_t Datum
Definition: postgres.h:372
TupleDesc rd_att
Definition: rel.h:115
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:936
void * palloc(Size size)
Definition: mcxt.c:848
int i
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:700
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165

◆ SPI_palloc()

void* SPI_palloc ( Size  size)

Definition at line 930 of file spi.c.

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

Referenced by _SPI_strdup().

931 {
932  if (_SPI_current == NULL)
933  elog(ERROR, "SPI_palloc called while not connected to SPI");
934 
936 }
static _SPI_connection * _SPI_current
Definition: spi.c:45
#define ERROR
Definition: elog.h:43
MemoryContext savedcxt
Definition: spi_priv.h:36
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:706
#define elog
Definition: elog.h:219

◆ SPI_pfree()

void SPI_pfree ( void *  pointer)

Definition at line 946 of file spi.c.

References pfree().

947 {
948  /* No longer need to worry which context chunk was in... */
949  pfree(pointer);
950 }
void pfree(void *pointer)
Definition: mcxt.c:949

◆ SPI_plan_get_cached_plan()

CachedPlan* SPI_plan_get_cached_plan ( SPIPlanPtr  plan)

Definition at line 1614 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, and _SPI_plan::saved.

Referenced by exec_eval_simple_expr(), and exec_simple_check_plan().

1615 {
1616  CachedPlanSource *plansource;
1617  CachedPlan *cplan;
1618  ErrorContextCallback spierrcontext;
1619 
1620  Assert(plan->magic == _SPI_PLAN_MAGIC);
1621 
1622  /* Can't support one-shot plans here */
1623  if (plan->oneshot)
1624  return NULL;
1625 
1626  /* Must have exactly one CachedPlanSource */
1627  if (list_length(plan->plancache_list) != 1)
1628  return NULL;
1629  plansource = (CachedPlanSource *) linitial(plan->plancache_list);
1630 
1631  /* Setup error traceback support for ereport() */
1632  spierrcontext.callback = _SPI_error_callback;
1633  spierrcontext.arg = (void *) plansource->query_string;
1634  spierrcontext.previous = error_context_stack;
1635  error_context_stack = &spierrcontext;
1636 
1637  /* Get the generic plan for the query */
1638  cplan = GetCachedPlan(plansource, NULL, plan->saved,
1640  Assert(cplan == plansource->gplan);
1641 
1642  /* Pop the error context stack */
1643  error_context_stack = spierrcontext.previous;
1644 
1645  return cplan;
1646 }
List * plancache_list
Definition: spi_priv.h:85
CachedPlan * GetCachedPlan(CachedPlanSource *plansource, ParamListInfo boundParams, bool useResOwner, QueryEnvironment *queryEnv)
Definition: plancache.c:1135
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static _SPI_connection * _SPI_current
Definition: spi.c:45
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
struct CachedPlan * gplan
Definition: plancache.h:105
ErrorContextCallback * error_context_stack
Definition: elog.c:88
static void _SPI_error_callback(void *arg)
Definition: spi.c:2365
#define linitial(l)
Definition: pg_list.h:111
QueryEnvironment * queryEnv
Definition: spi_priv.h:38
bool saved
Definition: spi_priv.h:83
#define Assert(condition)
Definition: c.h:670
bool oneshot
Definition: spi_priv.h:84
const char * query_string
Definition: plancache.h:84
static int list_length(const List *l)
Definition: pg_list.h:89

◆ SPI_plan_get_plan_sources()

List* SPI_plan_get_plan_sources ( SPIPlanPtr  plan)

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

1599 {
1600  Assert(plan->magic == _SPI_PLAN_MAGIC);
1601  return plan->plancache_list;
1602 }
List * plancache_list
Definition: spi_priv.h:85
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
#define Assert(condition)
Definition: c.h:670

◆ SPI_plan_is_valid()

bool SPI_plan_is_valid ( SPIPlanPtr  plan)

Definition at line 1497 of file spi.c.

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

Referenced by ri_FetchPreparedPlan().

1498 {
1499  ListCell *lc;
1500 
1501  Assert(plan->magic == _SPI_PLAN_MAGIC);
1502 
1503  foreach(lc, plan->plancache_list)
1504  {
1505  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
1506 
1507  if (!CachedPlanIsValid(plansource))
1508  return false;
1509  }
1510  return true;
1511 }
List * plancache_list
Definition: spi_priv.h:85
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
#define Assert(condition)
Definition: c.h:670
#define lfirst(lc)
Definition: pg_list.h:106
bool CachedPlanIsValid(CachedPlanSource *plansource)
Definition: plancache.c:1412

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

496 {
497  _SPI_plan plan;
498  SPIPlanPtr result;
499 
500  if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
501  {
503  return NULL;
504  }
505 
506  SPI_result = _SPI_begin_call(true);
507  if (SPI_result < 0)
508  return NULL;
509 
510  memset(&plan, 0, sizeof(_SPI_plan));
511  plan.magic = _SPI_PLAN_MAGIC;
512  plan.cursor_options = cursorOptions;
513  plan.nargs = nargs;
514  plan.argtypes = argtypes;
515  plan.parserSetup = NULL;
516  plan.parserSetupArg = NULL;
517 
518  _SPI_prepare_plan(src, &plan);
519 
520  /* copy plan to procedure context */
521  result = _SPI_make_plan_non_temp(&plan);
522 
523  _SPI_end_call(true);
524 
525  return result;
526 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:1753
Oid * argtypes
Definition: spi_priv.h:89
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2525
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
int nargs
Definition: spi_priv.h:88
ParserSetupHook parserSetup
Definition: spi_priv.h:90
void * parserSetupArg
Definition: spi_priv.h:91
int cursor_options
Definition: spi_priv.h:87

◆ SPI_prepare_params()

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

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

533 {
534  _SPI_plan plan;
535  SPIPlanPtr result;
536 
537  if (src == NULL)
538  {
540  return NULL;
541  }
542 
543  SPI_result = _SPI_begin_call(true);
544  if (SPI_result < 0)
545  return NULL;
546 
547  memset(&plan, 0, sizeof(_SPI_plan));
548  plan.magic = _SPI_PLAN_MAGIC;
549  plan.cursor_options = cursorOptions;
550  plan.nargs = 0;
551  plan.argtypes = NULL;
552  plan.parserSetup = parserSetup;
553  plan.parserSetupArg = parserSetupArg;
554 
555  _SPI_prepare_plan(src, &plan);
556 
557  /* copy plan to procedure context */
558  result = _SPI_make_plan_non_temp(&plan);
559 
560  _SPI_end_call(true);
561 
562  return result;
563 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:1753
Oid * argtypes
Definition: spi_priv.h:89
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2525
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
int nargs
Definition: spi_priv.h:88
ParserSetupHook parserSetup
Definition: spi_priv.h:90
void * parserSetupArg
Definition: spi_priv.h:91
int cursor_options
Definition: spi_priv.h:87

◆ SPI_register_relation()

int SPI_register_relation ( EphemeralNamedRelation  enr)

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

2686 {
2687  EphemeralNamedRelation match;
2688  int res;
2689 
2690  if (enr == NULL || enr->md.name == NULL)
2691  return SPI_ERROR_ARGUMENT;
2692 
2693  res = _SPI_begin_call(false); /* keep current memory context */
2694  if (res < 0)
2695  return res;
2696 
2697  match = _SPI_find_ENR_by_name(enr->md.name);
2698  if (match)
2700  else
2701  {
2702  if (_SPI_current->queryEnv == NULL)
2704 
2706  res = SPI_OK_REL_REGISTER;
2707  }
2708 
2709  _SPI_end_call(false);
2710 
2711  return res;
2712 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:47
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:2668
static _SPI_connection * _SPI_current
Definition: spi.c:45
#define SPI_OK_REL_REGISTER
Definition: spi.h:64
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
QueryEnvironment * queryEnv
Definition: spi_priv.h:38
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
void register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)
QueryEnvironment * create_queryEnv()

◆ SPI_register_trigger_data()

int SPI_register_trigger_data ( TriggerData tdata)

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

2753 {
2754  if (tdata == NULL)
2755  return SPI_ERROR_ARGUMENT;
2756 
2757  if (tdata->tg_newtable)
2758  {
2761  int rc;
2762 
2763  enr->md.name = tdata->tg_trigger->tgnewtable;
2764  enr->md.reliddesc = tdata->tg_relation->rd_id;
2765  enr->md.tupdesc = NULL;
2768  enr->reldata = tdata->tg_newtable;
2769  rc = SPI_register_relation(enr);
2770  if (rc != SPI_OK_REL_REGISTER)
2771  return rc;
2772  }
2773 
2774  if (tdata->tg_oldtable)
2775  {
2778  int rc;
2779 
2780  enr->md.name = tdata->tg_trigger->tgoldtable;
2781  enr->md.reliddesc = tdata->tg_relation->rd_id;
2782  enr->md.tupdesc = NULL;
2785  enr->reldata = tdata->tg_oldtable;
2786  rc = SPI_register_relation(enr);
2787  if (rc != SPI_OK_REL_REGISTER)
2788  return rc;
2789  }
2790 
2791  return SPI_OK_TD_REGISTER;
2792 }
EphemeralNamedRelationMetadataData md
#define SPI_OK_REL_REGISTER
Definition: spi.h:64
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
#define SPI_OK_TD_REGISTER
Definition: spi.h:66
Oid rd_id
Definition: rel.h:116
char * tgnewtable
Definition: reltrigger.h:43
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:2685
void * palloc(Size size)
Definition: mcxt.c:848
Tuplestorestate * tg_newtable
Definition: trigger.h:41
EphemeralNameRelationType enrtype
char * tgoldtable
Definition: reltrigger.h:42
Relation tg_relation
Definition: trigger.h:34

◆ SPI_repalloc()

void* SPI_repalloc ( void *  pointer,
Size  size 
)

Definition at line 939 of file spi.c.

References repalloc().

940 {
941  /* No longer need to worry which context chunk was in... */
942  return repalloc(pointer, size);
943 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962

◆ SPI_result_code_string()

const char* SPI_result_code_string ( int  code)

Definition at line 1521 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, and SPI_OK_UTILITY.

Referenced by check_foreign_key(), check_primary_key(), exec_dynquery_with_params(), exec_prepare_plan(), exec_run_select(), exec_stmt_dynexecute(), exec_stmt_execsql(), exec_stmt_forc(), exec_stmt_open(), 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(), RI_Initial_Check(), ri_PerformCheck(), ri_PlanCheck(), timetravel(), and ttdummy().

1522 {
1523  static char buf[64];
1524 
1525  switch (code)
1526  {
1527  case SPI_ERROR_CONNECT:
1528  return "SPI_ERROR_CONNECT";
1529  case SPI_ERROR_COPY:
1530  return "SPI_ERROR_COPY";
1531  case SPI_ERROR_OPUNKNOWN:
1532  return "SPI_ERROR_OPUNKNOWN";
1533  case SPI_ERROR_UNCONNECTED:
1534  return "SPI_ERROR_UNCONNECTED";
1535  case SPI_ERROR_ARGUMENT:
1536  return "SPI_ERROR_ARGUMENT";
1537  case SPI_ERROR_PARAM:
1538  return "SPI_ERROR_PARAM";
1539  case SPI_ERROR_TRANSACTION:
1540  return "SPI_ERROR_TRANSACTION";
1541  case SPI_ERROR_NOATTRIBUTE:
1542  return "SPI_ERROR_NOATTRIBUTE";
1543  case SPI_ERROR_NOOUTFUNC:
1544  return "SPI_ERROR_NOOUTFUNC";
1545  case SPI_ERROR_TYPUNKNOWN:
1546  return "SPI_ERROR_TYPUNKNOWN";
1548  return "SPI_ERROR_REL_DUPLICATE";
1550  return "SPI_ERROR_REL_NOT_FOUND";
1551  case SPI_OK_CONNECT:
1552  return "SPI_OK_CONNECT";
1553  case SPI_OK_FINISH:
1554  return "SPI_OK_FINISH";
1555  case SPI_OK_FETCH:
1556  return "SPI_OK_FETCH";
1557  case SPI_OK_UTILITY:
1558  return "SPI_OK_UTILITY";
1559  case SPI_OK_SELECT:
1560  return "SPI_OK_SELECT";
1561  case SPI_OK_SELINTO:
1562  return "SPI_OK_SELINTO";
1563  case SPI_OK_INSERT:
1564  return "SPI_OK_INSERT";
1565  case SPI_OK_DELETE:
1566  return "SPI_OK_DELETE";
1567  case SPI_OK_UPDATE:
1568  return "SPI_OK_UPDATE";
1569  case SPI_OK_CURSOR:
1570  return "SPI_OK_CURSOR";
1572  return "SPI_OK_INSERT_RETURNING";
1574  return "SPI_OK_DELETE_RETURNING";
1576  return "SPI_OK_UPDATE_RETURNING";
1577  case SPI_OK_REWRITTEN:
1578  return "SPI_OK_REWRITTEN";
1579  case SPI_OK_REL_REGISTER:
1580  return "SPI_OK_REL_REGISTER";
1581  case SPI_OK_REL_UNREGISTER:
1582  return "SPI_OK_REL_UNREGISTER";
1583  }
1584  /* Unrecognized code ... return something useful ... */
1585  sprintf(buf, "Unrecognized SPI code %d", code);
1586  return buf;
1587 }
#define SPI_OK_CONNECT
Definition: spi.h:50
#define SPI_ERROR_PARAM
Definition: spi.h:42
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:48
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:47
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:39
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:61
#define SPI_OK_DELETE
Definition: spi.h:57
#define SPI_ERROR_COPY
Definition: spi.h:37
#define SPI_ERROR_OPUNKNOWN
Definition: spi.h:38
#define SPI_OK_REL_REGISTER
Definition: spi.h:64
#define SPI_ERROR_CONNECT
Definition: spi.h:36
#define SPI_OK_CURSOR
Definition: spi.h:59
#define SPI_ERROR_NOOUTFUNC
Definition: spi.h:45
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:60
static char * buf
Definition: pg_test_fsync.c:67
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
#define SPI_OK_UTILITY
Definition: spi.h:53
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:62
#define SPI_OK_REWRITTEN
Definition: spi.h:63
#define SPI_ERROR_TRANSACTION
Definition: spi.h:43
#define SPI_OK_SELINTO
Definition: spi.h:55
#define SPI_OK_FETCH
Definition: spi.h:52
#define SPI_OK_SELECT
Definition: spi.h:54
#define SPI_OK_FINISH
Definition: spi.h:51
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:46
#define SPI_OK_UPDATE
Definition: spi.h:58
#define SPI_OK_INSERT
Definition: spi.h:56
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:65

◆ SPI_returntuple()

HeapTupleHeader SPI_returntuple ( HeapTuple  tuple,
TupleDesc  tupdesc 
)

Definition at line 664 of file spi.c.

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

Referenced by plpgsql_exec_function().

665 {
666  MemoryContext oldcxt;
667  HeapTupleHeader dtup;
668 
669  if (tuple == NULL || tupdesc == NULL)
670  {
672  return NULL;
673  }
674 
675  if (_SPI_current == NULL)
676  {
678  return NULL;
679  }
680 
681  /* For RECORD results, make sure a typmod has been assigned */
682  if (tupdesc->tdtypeid == RECORDOID &&
683  tupdesc->tdtypmod < 0)
684  assign_record_type_typmod(tupdesc);
685 
687 
688  dtup = DatumGetHeapTupleHeader(heap_copy_tuple_as_datum(tuple, tupdesc));
689 
690  MemoryContextSwitchTo(oldcxt);
691 
692  return dtup;
693 }
Oid tdtypeid
Definition: tupdesc.h:80
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:45
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:259
int32 tdtypmod
Definition: tupdesc.h:81
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1738
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
#define RECORDOID
Definition: pg_type.h:680
MemoryContext savedcxt
Definition: spi_priv.h:36
Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
Definition: heaptuple.c:659

◆ SPI_saveplan()

SPIPlanPtr SPI_saveplan ( SPIPlanPtr  plan)

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

594 {
595  SPIPlanPtr newplan;
596 
597  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
598  {
600  return NULL;
601  }
602 
603  SPI_result = _SPI_begin_call(false); /* don't change context */
604  if (SPI_result < 0)
605  return NULL;
606 
607  newplan = _SPI_save_plan(plan);
608 
609  SPI_result = _SPI_end_call(false);
610 
611  return newplan;
612 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:82
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
int SPI_result
Definition: spi.c:42
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
static SPIPlanPtr _SPI_save_plan(SPIPlanPtr plan)
Definition: spi.c:2595
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485

◆ SPI_scroll_cursor_fetch()

void SPI_scroll_cursor_fetch ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1384 of file spi.c.

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

Referenced by exec_stmt_fetch().

1385 {
1386  _SPI_cursor_operation(portal,
1387  direction, count,
1389  /* we know that the DestSPI receiver doesn't need a destroy call */
1390 }
Definition: dest.h:93
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:109
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2391

◆ SPI_scroll_cursor_move()

void SPI_scroll_cursor_move ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1399 of file spi.c.

References _SPI_cursor_operation(), and None_Receiver.

Referenced by exec_stmt_fetch().

1400 {
1401  _SPI_cursor_operation(portal, direction, count, None_Receiver);
1402 }
DestReceiver * None_Receiver
Definition: dest.c:91
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2391

◆ SPI_unregister_relation()

int SPI_unregister_relation ( const char *  name)

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

2720 {
2721  EphemeralNamedRelation match;
2722  int res;
2723 
2724  if (name == NULL)
2725  return SPI_ERROR_ARGUMENT;
2726 
2727  res = _SPI_begin_call(false); /* keep current memory context */
2728  if (res < 0)
2729  return res;
2730 
2731  match = _SPI_find_ENR_by_name(name);
2732  if (match)
2733  {
2735  res = SPI_OK_REL_UNREGISTER;
2736  }
2737  else
2739 
2740  _SPI_end_call(false);
2741 
2742  return res;
2743 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:48
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:2668
static _SPI_connection * _SPI_current
Definition: spi.c:45
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2461
#define SPI_ERROR_ARGUMENT
Definition: spi.h:41
QueryEnvironment * queryEnv
Definition: spi_priv.h:38
void unregister_ENR(QueryEnvironment *queryEnv, const char *name)
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2485
const char * name
Definition: encode.c:521
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:65

Variable Documentation

◆ SPI_lastoid

◆ SPI_processed

◆ SPI_result

◆ SPI_tuptable