PostgreSQL Source Code  git master
spi.h File Reference
#include "commands/trigger.h"
#include "lib/ilist.h"
#include "parser/parser.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
 
struct  SPIPrepareOptions
 
struct  SPIExecuteOptions
 
struct  SPIParseOpenOptions
 

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 SPIPrepareOptions SPIPrepareOptions
 
typedef struct SPIExecuteOptions SPIExecuteOptions
 
typedef struct SPIParseOpenOptions SPIParseOpenOptions
 
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_extended (const char *src, const SPIExecuteOptions *options)
 
int SPI_execute_plan (SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
 
int SPI_execute_plan_extended (SPIPlanPtr plan, const SPIExecuteOptions *options)
 
int SPI_execute_plan_with_paramlist (SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
 
int SPI_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_extended (const char *src, const SPIPrepareOptions *options)
 
SPIPlanPtr SPI_prepare_params (const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
 
int SPI_keepplan (SPIPlanPtr plan)
 
SPIPlanPtr SPI_saveplan (SPIPlanPtr plan)
 
int SPI_freeplan (SPIPlanPtr plan)
 
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 (const char *name, const char *src, const SPIParseOpenOptions *options)
 
Portal SPI_cursor_find (const char *name)
 
void SPI_cursor_fetch (Portal portal, bool forward, long count)
 
void SPI_cursor_move (Portal portal, bool forward, long count)
 
void SPI_scroll_cursor_fetch (Portal, 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 67 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 71 of file spi.h.

◆ SPI_ERROR_NOATTRIBUTE

◆ SPI_ERROR_NOOUTFUNC

#define SPI_ERROR_NOOUTFUNC   (-10)

Definition at line 76 of file spi.h.

Referenced by SPI_result_code_string().

◆ SPI_ERROR_OPUNKNOWN

#define SPI_ERROR_OPUNKNOWN   (-3)

Definition at line 69 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 78 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 79 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 77 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 90 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 83 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 95 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 96 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

◆ SPI_OK_TD_REGISTER

#define SPI_OK_TD_REGISTER   17

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

◆ SPI_pop_conditional

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

Definition at line 105 of file spi.h.

◆ SPI_push

#define SPI_push ( )    ((void) 0)

Definition at line 102 of file spi.h.

◆ SPI_push_conditional

#define SPI_push_conditional ( )    false

Definition at line 104 of file spi.h.

◆ SPI_restore_connection

#define SPI_restore_connection ( )    ((void) 0)

Definition at line 106 of file spi.h.

Typedef Documentation

◆ SPIExecuteOptions

◆ SPIParseOpenOptions

◆ SPIPlanPtr

typedef struct _SPI_plan* SPIPlanPtr

Definition at line 65 of file spi.h.

◆ SPIPrepareOptions

◆ SPITupleTable

typedef struct SPITupleTable SPITupleTable

Function Documentation

◆ AtEOSubXact_SPI()

void AtEOSubXact_SPI ( bool  isCommit,
SubTransactionId  mySubid 
)

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

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

◆ AtEOXact_SPI()

void AtEOXact_SPI ( bool  isCommit)

Definition at line 371 of file spi.c.

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

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

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

◆ SPI_commit()

void SPI_commit ( void  )

Definition at line 283 of file spi.c.

References _SPI_commit().

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

284 {
285  _SPI_commit(false);
286 }
static void _SPI_commit(bool chain)
Definition: spi.c:230

◆ SPI_commit_and_chain()

void SPI_commit_and_chain ( void  )

Definition at line 289 of file spi.c.

References _SPI_commit().

Referenced by exec_stmt_commit().

290 {
291  _SPI_commit(true);
292 }
static void _SPI_commit(bool chain)
Definition: spi.c:230

◆ SPI_connect()

◆ SPI_connect_ext()

int SPI_connect_ext ( int  options)

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

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

◆ SPI_copytuple()

HeapTuple SPI_copytuple ( HeapTuple  tuple)

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

943 {
944  MemoryContext oldcxt;
945  HeapTuple ctuple;
946 
947  if (tuple == NULL)
948  {
950  return NULL;
951  }
952 
953  if (_SPI_current == NULL)
954  {
956  return NULL;
957  }
958 
960 
961  ctuple = heap_copytuple(tuple);
962 
963  MemoryContextSwitchTo(oldcxt);
964 
965  return ctuple;
966 }
HeapTuple heap_copytuple(HeapTuple tuple)
Definition: heaptuple.c:680
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:70
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:72
MemoryContext savedcxt
Definition: spi_priv.h:35

◆ SPI_cursor_close()

void SPI_cursor_close ( Portal  portal)

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

1758 {
1759  if (!PortalIsValid(portal))
1760  elog(ERROR, "invalid portal in SPI cursor operation");
1761 
1762  PortalDrop(portal, false);
1763 }
#define ERROR
Definition: elog.h:46
#define PortalIsValid(p)
Definition: portal.h:211
void PortalDrop(Portal portal, bool isTopCommit)
Definition: portalmem.c:467
#define elog(elevel,...)
Definition: elog.h:232

◆ SPI_cursor_fetch()

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

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

1702 {
1703  _SPI_cursor_operation(portal,
1704  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1706  /* we know that the DestSPI receiver doesn't need a destroy call */
1707 }
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:2835

◆ SPI_cursor_find()

Portal SPI_cursor_find ( const char *  name)

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

1690 {
1691  return GetPortalByName(name);
1692 }
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 1716 of file spi.c.

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

1717 {
1718  _SPI_cursor_operation(portal,
1719  forward ? FETCH_FORWARD : FETCH_BACKWARD, count,
1720  None_Receiver);
1721 }
DestReceiver * None_Receiver
Definition: dest.c:96
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2835

◆ SPI_cursor_open()

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

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

1343 {
1344  Portal portal;
1345  ParamListInfo paramLI;
1346 
1347  /* build transient ParamListInfo in caller's context */
1348  paramLI = _SPI_convert_params(plan->nargs, plan->argtypes,
1349  Values, Nulls);
1350 
1351  portal = SPI_cursor_open_internal(name, plan, paramLI, read_only);
1352 
1353  /* done with the transient ParamListInfo */
1354  if (paramLI)
1355  pfree(paramLI);
1356 
1357  return portal;
1358 }
Oid * argtypes
Definition: spi_priv.h:100
void pfree(void *pointer)
Definition: mcxt.c:1169
int nargs
Definition: spi_priv.h:99
const char * name
Definition: encode.c:561
static Portal SPI_cursor_open_internal(const char *name, SPIPlanPtr plan, ParamListInfo paramLI, bool read_only)
Definition: spi.c:1472
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2683
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ 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 1367 of file spi.c.

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

1372 {
1373  Portal result;
1374  _SPI_plan plan;
1375  ParamListInfo paramLI;
1376 
1377  if (src == NULL || nargs < 0)
1378  elog(ERROR, "SPI_cursor_open_with_args called with invalid arguments");
1379 
1380  if (nargs > 0 && (argtypes == NULL || Values == NULL))
1381  elog(ERROR, "SPI_cursor_open_with_args called with missing parameters");
1382 
1383  SPI_result = _SPI_begin_call(true);
1384  if (SPI_result < 0)
1385  elog(ERROR, "SPI_cursor_open_with_args called while not connected");
1386 
1387  memset(&plan, 0, sizeof(_SPI_plan));
1388  plan.magic = _SPI_PLAN_MAGIC;
1390  plan.cursor_options = cursorOptions;
1391  plan.nargs = nargs;
1392  plan.argtypes = argtypes;
1393  plan.parserSetup = NULL;
1394  plan.parserSetupArg = NULL;
1395 
1396  /* build transient ParamListInfo in executor context */
1397  paramLI = _SPI_convert_params(nargs, argtypes,
1398  Values, Nulls);
1399 
1400  _SPI_prepare_plan(src, &plan);
1401 
1402  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1403 
1404  result = SPI_cursor_open_internal(name, &plan, paramLI, read_only);
1405 
1406  /* And clean up */
1407  _SPI_end_call(true);
1408 
1409  return result;
1410 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2108
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:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:46
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
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:1472
#define elog(elevel,...)
Definition: elog.h:232
int cursor_options
Definition: spi_priv.h:98
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2683
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_cursor_open_with_paramlist()

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

Definition at line 1420 of file spi.c.

References SPI_cursor_open_internal().

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

1422 {
1423  return SPI_cursor_open_internal(name, plan, params, read_only);
1424 }
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:1472

◆ SPI_cursor_parse_open()

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

Definition at line 1428 of file spi.c.

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

Referenced by exec_dynquery_with_params().

1431 {
1432  Portal result;
1433  _SPI_plan plan;
1434 
1435  if (src == NULL || options == NULL)
1436  elog(ERROR, "SPI_cursor_parse_open called with invalid arguments");
1437 
1438  SPI_result = _SPI_begin_call(true);
1439  if (SPI_result < 0)
1440  elog(ERROR, "SPI_cursor_parse_open called while not connected");
1441 
1442  memset(&plan, 0, sizeof(_SPI_plan));
1443  plan.magic = _SPI_PLAN_MAGIC;
1445  plan.cursor_options = options->cursorOptions;
1446  if (options->params)
1447  {
1448  plan.parserSetup = options->params->parserSetup;
1449  plan.parserSetupArg = options->params->parserSetupArg;
1450  }
1451 
1452  _SPI_prepare_plan(src, &plan);
1453 
1454  /* We needn't copy the plan; SPI_cursor_open_internal will do so */
1455 
1456  result = SPI_cursor_open_internal(name, &plan,
1457  options->params, options->read_only);
1458 
1459  /* And clean up */
1460  _SPI_end_call(true);
1461 
1462  return result;
1463 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2108
void * parserSetupArg
Definition: params.h:117
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
ParamListInfo params
Definition: spi.h:59
int magic
Definition: spi_priv.h:92
ParserSetupHook parserSetup
Definition: params.h:116
bool read_only
Definition: spi.h:61
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define ERROR
Definition: elog.h:46
int cursorOptions
Definition: spi.h:60
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
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:1472
#define elog(elevel,...)
Definition: elog.h:232
int cursor_options
Definition: spi_priv.h:98

◆ SPI_datumTransfer()

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

Definition at line 1256 of file spi.c.

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

Referenced by coerce_function_result_tuple(), and plpgsql_exec_function().

1257 {
1258  MemoryContext oldcxt;
1259  Datum result;
1260 
1261  if (_SPI_current == NULL)
1262  elog(ERROR, "SPI_datumTransfer called while not connected to SPI");
1263 
1265 
1266  result = datumTransfer(value, typByVal, typLen);
1267 
1268  MemoryContextSwitchTo(oldcxt);
1269 
1270  return result;
1271 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define ERROR
Definition: elog.h:46
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:411
static struct @143 value
Datum datumTransfer(Datum value, bool typByVal, int typLen)
Definition: datum.c:193
#define elog(elevel,...)
Definition: elog.h:232

◆ SPI_exec()

int SPI_exec ( const char *  src,
long  tcount 
)

Definition at line 535 of file spi.c.

References SPI_execute().

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

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

◆ SPI_execp()

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

Definition at line 608 of file spi.c.

References SPI_execute_plan().

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

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

◆ SPI_execute()

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

Definition at line 504 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::parse_mode, RAW_PARSE_DEFAULT, 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().

505 {
506  _SPI_plan plan;
507  int res;
508 
509  if (src == NULL || tcount < 0)
510  return SPI_ERROR_ARGUMENT;
511 
512  res = _SPI_begin_call(true);
513  if (res < 0)
514  return res;
515 
516  memset(&plan, 0, sizeof(_SPI_plan));
517  plan.magic = _SPI_PLAN_MAGIC;
520 
521  _SPI_prepare_oneshot_plan(src, &plan);
522 
523  res = _SPI_execute_plan(&plan, NULL,
525  read_only, false,
526  true, tcount,
527  NULL, NULL);
528 
529  _SPI_end_call(true);
530  return res;
531 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2216
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2828
int cursor_options
Definition: spi_priv.h:98

◆ SPI_execute_extended()

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

Definition at line 542 of file spi.c.

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

Referenced by exec_stmt_dynexecute(), and exec_stmt_return_query().

544 {
545  int res;
546  _SPI_plan plan;
547 
548  if (src == NULL || options == NULL)
549  return SPI_ERROR_ARGUMENT;
550 
551  res = _SPI_begin_call(true);
552  if (res < 0)
553  return res;
554 
555  memset(&plan, 0, sizeof(_SPI_plan));
556  plan.magic = _SPI_PLAN_MAGIC;
559  if (options->params)
560  {
561  plan.parserSetup = options->params->parserSetup;
562  plan.parserSetupArg = options->params->parserSetupArg;
563  }
564 
565  _SPI_prepare_oneshot_plan(src, &plan);
566 
567  res = _SPI_execute_plan(&plan, options->params,
569  options->read_only, options->allow_nonatomic,
570  true, options->tcount,
571  options->dest, options->owner);
572 
573  _SPI_end_call(true);
574  return res;
575 }
void * parserSetupArg
Definition: params.h:117
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
DestReceiver * dest
Definition: spi.h:52
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2216
uint64 tcount
Definition: spi.h:51
ParserSetupHook parserSetup
Definition: params.h:116
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
ResourceOwner owner
Definition: spi.h:53
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123
bool allow_nonatomic
Definition: spi.h:50
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2828
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 579 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(), SPI_execp(), and test_predtest().

581 {
582  int res;
583 
584  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
585  return SPI_ERROR_ARGUMENT;
586 
587  if (plan->nargs > 0 && Values == NULL)
588  return SPI_ERROR_PARAM;
589 
590  res = _SPI_begin_call(true);
591  if (res < 0)
592  return res;
593 
594  res = _SPI_execute_plan(plan,
595  _SPI_convert_params(plan->nargs, plan->argtypes,
596  Values, Nulls),
598  read_only, false,
599  true, tcount,
600  NULL, NULL);
601 
602  _SPI_end_call(true);
603  return res;
604 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:73
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123
int nargs
Definition: spi_priv.h:99
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2683
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_execute_plan_extended()

int SPI_execute_plan_extended ( SPIPlanPtr  plan,
const SPIExecuteOptions options 
)

Definition at line 615 of file spi.c.

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

Referenced by exec_stmt_call(), and exec_stmt_return_query().

617 {
618  int res;
619 
620  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || options == NULL)
621  return SPI_ERROR_ARGUMENT;
622 
623  res = _SPI_begin_call(true);
624  if (res < 0)
625  return res;
626 
627  res = _SPI_execute_plan(plan, options->params,
629  options->read_only, options->allow_nonatomic,
630  true, options->tcount,
631  options->dest, options->owner);
632 
633  _SPI_end_call(true);
634  return res;
635 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
DestReceiver * dest
Definition: spi.h:52
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
uint64 tcount
Definition: spi.h:51
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
ResourceOwner owner
Definition: spi.h:53
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123
bool allow_nonatomic
Definition: spi.h:50

◆ SPI_execute_plan_with_paramlist()

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

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

641 {
642  int res;
643 
644  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
645  return SPI_ERROR_ARGUMENT;
646 
647  res = _SPI_begin_call(true);
648  if (res < 0)
649  return res;
650 
651  res = _SPI_execute_plan(plan, params,
653  read_only, false,
654  true, tcount,
655  NULL, NULL);
656 
657  _SPI_end_call(true);
658  return res;
659 }
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123

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

679 {
680  int res;
681 
682  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC || tcount < 0)
683  return SPI_ERROR_ARGUMENT;
684 
685  if (plan->nargs > 0 && Values == NULL)
686  return SPI_ERROR_PARAM;
687 
688  res = _SPI_begin_call(true);
689  if (res < 0)
690  return res;
691 
692  res = _SPI_execute_plan(plan,
693  _SPI_convert_params(plan->nargs, plan->argtypes,
694  Values, Nulls),
695  snapshot, crosscheck_snapshot,
696  read_only, false,
697  fire_triggers, tcount,
698  NULL, NULL);
699 
700  _SPI_end_call(true);
701  return res;
702 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:73
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
int nargs
Definition: spi_priv.h:99
static ParamListInfo _SPI_convert_params(int nargs, Oid *argtypes, Datum *Values, const char *Nulls)
Definition: spi.c:2683
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_execute_with_args()

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

Definition at line 711 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::parse_mode, _SPI_plan::parserSetup, _SPI_plan::parserSetupArg, RAW_PARSE_DEFAULT, SPI_ERROR_ARGUMENT, and SPI_ERROR_PARAM.

715 {
716  int res;
717  _SPI_plan plan;
718  ParamListInfo paramLI;
719 
720  if (src == NULL || nargs < 0 || tcount < 0)
721  return SPI_ERROR_ARGUMENT;
722 
723  if (nargs > 0 && (argtypes == NULL || Values == NULL))
724  return SPI_ERROR_PARAM;
725 
726  res = _SPI_begin_call(true);
727  if (res < 0)
728  return res;
729 
730  memset(&plan, 0, sizeof(_SPI_plan));
731  plan.magic = _SPI_PLAN_MAGIC;
734  plan.nargs = nargs;
735  plan.argtypes = argtypes;
736  plan.parserSetup = NULL;
737  plan.parserSetupArg = NULL;
738 
739  paramLI = _SPI_convert_params(nargs, argtypes,
740  Values, Nulls);
741 
742  _SPI_prepare_oneshot_plan(src, &plan);
743 
744  res = _SPI_execute_plan(&plan, paramLI,
746  read_only, false,
747  true, tcount,
748  NULL, NULL);
749 
750  _SPI_end_call(true);
751  return res;
752 }
Oid * argtypes
Definition: spi_priv.h:100
#define SPI_ERROR_PARAM
Definition: spi.h:73
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
static int _SPI_execute_plan(SPIPlanPtr plan, ParamListInfo paramLI, Snapshot snapshot, Snapshot crosscheck_snapshot, bool read_only, bool allow_nonatomic, bool fire_triggers, uint64 tcount, DestReceiver *caller_dest, ResourceOwner plan_owner)
Definition: spi.c:2281
static void _SPI_prepare_oneshot_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2216
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
#define InvalidSnapshot
Definition: snapshot.h:123
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:2828
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:2683
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_finish()

int SPI_finish ( void  )

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

186 {
187  int res;
188 
189  res = _SPI_begin_call(false); /* just check we're connected */
190  if (res < 0)
191  return res;
192 
193  /* Restore memory context as it was before procedure call */
195 
196  /* Release memory used in procedure call (including tuptables) */
198  _SPI_current->execCxt = NULL;
200  _SPI_current->procCxt = NULL;
201 
202  /*
203  * Restore outer API variables, especially SPI_tuptable which is probably
204  * pointing at a just-deleted tuptable
205  */
209 
210  /* Exit stack level */
211  _SPI_connected--;
212  if (_SPI_connected < 0)
213  _SPI_current = NULL;
214  else
216 
217  return SPI_OK_FINISH;
218 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static int _SPI_connected
Definition: spi.c:52
static _SPI_connection * _SPI_current
Definition: spi.c:50
uint64 SPI_processed
Definition: spi.c:45
SPITupleTable * outer_tuptable
Definition: spi_priv.h:47
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
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:82

◆ SPI_fname()

char* SPI_fname ( TupleDesc  tupdesc,
int  fnumber 
)

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

1094 {
1095  const FormData_pg_attribute *att;
1096 
1097  SPI_result = 0;
1098 
1099  if (fnumber > tupdesc->natts || fnumber == 0 ||
1101  {
1103  return NULL;
1104  }
1105 
1106  if (fnumber > 0)
1107  att = TupleDescAttr(tupdesc, fnumber - 1);
1108  else
1109  att = SystemAttributeDefinition(fnumber);
1110 
1111  return pstrdup(NameStr(att->attname));
1112 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
FormData_pg_attribute
Definition: pg_attribute.h:191
#define NameStr(name)
Definition: c.h:681

◆ SPI_fnumber()

int SPI_fnumber ( TupleDesc  tupdesc,
const char *  fname 
)

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

1071 {
1072  int res;
1073  const FormData_pg_attribute *sysatt;
1074 
1075  for (res = 0; res < tupdesc->natts; res++)
1076  {
1077  Form_pg_attribute attr = TupleDescAttr(tupdesc, res);
1078 
1079  if (namestrcmp(&attr->attname, fname) == 0 &&
1080  !attr->attisdropped)
1081  return res + 1;
1082  }
1083 
1084  sysatt = SystemAttributeByName(fname);
1085  if (sysatt != NULL)
1086  return sysatt->attnum;
1087 
1088  /* SPI_ERROR_NOATTRIBUTE is different from all sys column numbers */
1089  return SPI_ERROR_NOATTRIBUTE;
1090 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int namestrcmp(Name name, const char *str)
Definition: name.c:247
const FormData_pg_attribute * SystemAttributeByName(const char *attname)
Definition: heap.c:263
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
FormData_pg_attribute
Definition: pg_attribute.h:191

◆ SPI_freeplan()

int SPI_freeplan ( SPIPlanPtr  plan)

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

921 {
922  ListCell *lc;
923 
924  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
925  return SPI_ERROR_ARGUMENT;
926 
927  /* Release the plancache entries */
928  foreach(lc, plan->plancache_list)
929  {
930  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
931 
932  DropCachedPlan(plansource);
933  }
934 
935  /* Now get rid of the _SPI_plan and subsidiary data in its plancxt */
937 
938  return 0;
939 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
MemoryContext plancxt
Definition: spi_priv.h:96
#define lfirst(lc)
Definition: pg_list.h:169
void DropCachedPlan(CachedPlanSource *plansource)
Definition: plancache.c:498

◆ SPI_freetuple()

void SPI_freetuple ( HeapTuple  pointer)

Definition at line 1274 of file spi.c.

References heap_freetuple().

1275 {
1276  /* No longer need to worry which context tuple was in... */
1277  heap_freetuple(tuple);
1278 }
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338

◆ SPI_freetuptable()

void SPI_freetuptable ( SPITupleTable tuptable)

Definition at line 1281 of file spi.c.

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

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

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

◆ SPI_getargcount()

int SPI_getargcount ( SPIPlanPtr  plan)

Definition at line 1785 of file spi.c.

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

1786 {
1787  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
1788  {
1790  return -1;
1791  }
1792  return plan->nargs;
1793 }
#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:72
int nargs
Definition: spi_priv.h:99

◆ SPI_getargtypeid()

Oid SPI_getargtypeid ( SPIPlanPtr  plan,
int  argIndex 
)

Definition at line 1770 of file spi.c.

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

1771 {
1772  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC ||
1773  argIndex < 0 || argIndex >= plan->nargs)
1774  {
1776  return InvalidOid;
1777  }
1778  return plan->argtypes[argIndex];
1779 }
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:72
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 1147 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().

1148 {
1149  SPI_result = 0;
1150 
1151  if (fnumber > tupdesc->natts || fnumber == 0 ||
1153  {
1155  *isnull = true;
1156  return (Datum) NULL;
1157  }
1158 
1159  return heap_getattr(tuple, fnumber, tupdesc, isnull);
1160 }
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411

◆ SPI_getnspname()

char* SPI_getnspname ( Relation  rel)

Definition at line 1227 of file spi.c.

References get_namespace_name(), and RelationGetNamespace.

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

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

◆ SPI_getrelname()

char* SPI_getrelname ( Relation  rel)

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

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

◆ SPI_gettype()

char* SPI_gettype ( TupleDesc  tupdesc,
int  fnumber 
)

Definition at line 1163 of file spi.c.

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

Referenced by check_foreign_key().

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

◆ SPI_gettypeid()

Oid SPI_gettypeid ( TupleDesc  tupdesc,
int  fnumber 
)

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

1204 {
1205  SPI_result = 0;
1206 
1207  if (fnumber > tupdesc->natts || fnumber == 0 ||
1209  {
1211  return InvalidOid;
1212  }
1213 
1214  if (fnumber > 0)
1215  return TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1216  else
1217  return (SystemAttributeDefinition(fnumber))->atttypid;
1218 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
#define InvalidOid
Definition: postgres_ext.h:36

◆ SPI_getvalue()

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

Definition at line 1115 of file spi.c.

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

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

1116 {
1117  Datum val;
1118  bool isnull;
1119  Oid typoid,
1120  foutoid;
1121  bool typisvarlena;
1122 
1123  SPI_result = 0;
1124 
1125  if (fnumber > tupdesc->natts || fnumber == 0 ||
1127  {
1129  return NULL;
1130  }
1131 
1132  val = heap_getattr(tuple, fnumber, tupdesc, &isnull);
1133  if (isnull)
1134  return NULL;
1135 
1136  if (fnumber > 0)
1137  typoid = TupleDescAttr(tupdesc, fnumber - 1)->atttypid;
1138  else
1139  typoid = (SystemAttributeDefinition(fnumber))->atttypid;
1140 
1141  getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
1142 
1143  return OidOutputFunctionCall(foutoid, val);
1144 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2854
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27
unsigned int Oid
Definition: postgres_ext.h:31
const FormData_pg_attribute * SystemAttributeDefinition(AttrNumber attno)
Definition: heap.c:251
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:761
uintptr_t Datum
Definition: postgres.h:411
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1653
long val
Definition: informix.c:664

◆ SPI_inside_nonatomic_context()

bool SPI_inside_nonatomic_context ( void  )

Definition at line 492 of file spi.c.

References _SPI_connection::atomic.

Referenced by StartTransaction().

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

◆ SPI_is_cursor_plan()

bool SPI_is_cursor_plan ( SPIPlanPtr  plan)

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

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

◆ SPI_keepplan()

int SPI_keepplan ( SPIPlanPtr  plan)

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

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

◆ SPI_modifytuple()

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

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

1003 {
1004  MemoryContext oldcxt;
1005  HeapTuple mtuple;
1006  int numberOfAttributes;
1007  Datum *v;
1008  bool *n;
1009  int i;
1010 
1011  if (rel == NULL || tuple == NULL || natts < 0 || attnum == NULL || Values == NULL)
1012  {
1014  return NULL;
1015  }
1016 
1017  if (_SPI_current == NULL)
1018  {
1020  return NULL;
1021  }
1022 
1024 
1025  SPI_result = 0;
1026 
1027  numberOfAttributes = rel->rd_att->natts;
1028  v = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
1029  n = (bool *) palloc(numberOfAttributes * sizeof(bool));
1030 
1031  /* fetch old values and nulls */
1032  heap_deform_tuple(tuple, rel->rd_att, v, n);
1033 
1034  /* replace values and nulls */
1035  for (i = 0; i < natts; i++)
1036  {
1037  if (attnum[i] <= 0 || attnum[i] > numberOfAttributes)
1038  break;
1039  v[attnum[i] - 1] = Values[i];
1040  n[attnum[i] - 1] = (Nulls && Nulls[i] == 'n');
1041  }
1042 
1043  if (i == natts) /* no errors in *attnum */
1044  {
1045  mtuple = heap_form_tuple(rel->rd_att, v, n);
1046 
1047  /*
1048  * copy the identification info of the old tuple: t_ctid, t_self, and
1049  * OID (if any)
1050  */
1051  mtuple->t_data->t_ctid = tuple->t_data->t_ctid;
1052  mtuple->t_self = tuple->t_self;
1053  mtuple->t_tableOid = tuple->t_tableOid;
1054  }
1055  else
1056  {
1057  mtuple = NULL;
1059  }
1060 
1061  pfree(v);
1062  pfree(n);
1063 
1064  MemoryContextSwitchTo(oldcxt);
1065 
1066  return mtuple;
1067 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:70
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:72
void pfree(void *pointer)
Definition: mcxt.c:1169
ItemPointerData t_ctid
Definition: htup_details.h:160
ItemPointerData t_self
Definition: htup.h:65
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
Oid t_tableOid
Definition: htup.h:66
MemoryContext savedcxt
Definition: spi_priv.h:35
uintptr_t Datum
Definition: postgres.h:411
TupleDesc rd_att
Definition: rel.h:110
int16 attnum
Definition: pg_attribute.h:83
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1249
void * palloc(Size size)
Definition: mcxt.c:1062
int i
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157

◆ SPI_palloc()

void* SPI_palloc ( Size  size)

Definition at line 1233 of file spi.c.

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

Referenced by _SPI_strdup(), and coerce_function_result_tuple().

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

◆ SPI_pfree()

void SPI_pfree ( void *  pointer)

Definition at line 1249 of file spi.c.

References pfree().

1250 {
1251  /* No longer need to worry which context chunk was in... */
1252  pfree(pointer);
1253 }
void pfree(void *pointer)
Definition: mcxt.c:1169

◆ SPI_plan_get_cached_plan()

CachedPlan* SPI_plan_get_cached_plan ( SPIPlanPtr  plan)

Definition at line 1963 of file spi.c.

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

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

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

◆ SPI_plan_get_plan_sources()

List* SPI_plan_get_plan_sources ( SPIPlanPtr  plan)

Definition at line 1944 of file spi.c.

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

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

1945 {
1946  Assert(plan->magic == _SPI_PLAN_MAGIC);
1947  return plan->plancache_list;
1948 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:804

◆ SPI_plan_is_valid()

bool SPI_plan_is_valid ( SPIPlanPtr  plan)

Definition at line 1843 of file spi.c.

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

Referenced by ri_FetchPreparedPlan().

1844 {
1845  ListCell *lc;
1846 
1847  Assert(plan->magic == _SPI_PLAN_MAGIC);
1848 
1849  foreach(lc, plan->plancache_list)
1850  {
1851  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(lc);
1852 
1853  if (!CachedPlanIsValid(plansource))
1854  return false;
1855  }
1856  return true;
1857 }
List * plancache_list
Definition: spi_priv.h:95
#define _SPI_PLAN_MAGIC
Definition: spi_priv.h:20
int magic
Definition: spi_priv.h:92
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
bool CachedPlanIsValid(CachedPlanSource *plansource)
Definition: plancache.c:1598

◆ SPI_prepare()

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

◆ SPI_prepare_cursor()

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

Definition at line 761 of file spi.c.

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

Referenced by SPI_prepare().

763 {
764  _SPI_plan plan;
765  SPIPlanPtr result;
766 
767  if (src == NULL || nargs < 0 || (nargs > 0 && argtypes == NULL))
768  {
770  return NULL;
771  }
772 
773  SPI_result = _SPI_begin_call(true);
774  if (SPI_result < 0)
775  return NULL;
776 
777  memset(&plan, 0, sizeof(_SPI_plan));
778  plan.magic = _SPI_PLAN_MAGIC;
780  plan.cursor_options = cursorOptions;
781  plan.nargs = nargs;
782  plan.argtypes = argtypes;
783  plan.parserSetup = NULL;
784  plan.parserSetupArg = NULL;
785 
786  _SPI_prepare_plan(src, &plan);
787 
788  /* copy plan to procedure context */
789  result = _SPI_make_plan_non_temp(&plan);
790 
791  _SPI_end_call(true);
792 
793  return result;
794 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2108
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:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2969
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
int cursor_options
Definition: spi_priv.h:98

◆ SPI_prepare_extended()

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

Definition at line 797 of file spi.c.

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

Referenced by exec_prepare_plan().

799 {
800  _SPI_plan plan;
801  SPIPlanPtr result;
802 
803  if (src == NULL || options == NULL)
804  {
806  return NULL;
807  }
808 
809  SPI_result = _SPI_begin_call(true);
810  if (SPI_result < 0)
811  return NULL;
812 
813  memset(&plan, 0, sizeof(_SPI_plan));
814  plan.magic = _SPI_PLAN_MAGIC;
815  plan.parse_mode = options->parseMode;
816  plan.cursor_options = options->cursorOptions;
817  plan.nargs = 0;
818  plan.argtypes = NULL;
819  plan.parserSetup = options->parserSetup;
820  plan.parserSetupArg = options->parserSetupArg;
821 
822  _SPI_prepare_plan(src, &plan);
823 
824  /* copy plan to procedure context */
825  result = _SPI_make_plan_non_temp(&plan);
826 
827  _SPI_end_call(true);
828 
829  return result;
830 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2108
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:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2969
ParserSetupHook parserSetup
Definition: spi.h:39
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
int cursorOptions
Definition: spi.h:42
int nargs
Definition: spi_priv.h:99
ParserSetupHook parserSetup
Definition: spi_priv.h:101
void * parserSetupArg
Definition: spi_priv.h:102
int cursor_options
Definition: spi_priv.h:98
void * parserSetupArg
Definition: spi.h:40
RawParseMode parseMode
Definition: spi.h:41

◆ SPI_prepare_params()

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

Definition at line 833 of file spi.c.

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

837 {
838  _SPI_plan plan;
839  SPIPlanPtr result;
840 
841  if (src == NULL)
842  {
844  return NULL;
845  }
846 
847  SPI_result = _SPI_begin_call(true);
848  if (SPI_result < 0)
849  return NULL;
850 
851  memset(&plan, 0, sizeof(_SPI_plan));
852  plan.magic = _SPI_PLAN_MAGIC;
854  plan.cursor_options = cursorOptions;
855  plan.nargs = 0;
856  plan.argtypes = NULL;
857  plan.parserSetup = parserSetup;
858  plan.parserSetupArg = parserSetupArg;
859 
860  _SPI_prepare_plan(src, &plan);
861 
862  /* copy plan to procedure context */
863  result = _SPI_make_plan_non_temp(&plan);
864 
865  _SPI_end_call(true);
866 
867  return result;
868 }
static void _SPI_prepare_plan(const char *src, SPIPlanPtr plan)
Definition: spi.c:2108
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:2905
RawParseMode parse_mode
Definition: spi_priv.h:97
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static SPIPlanPtr _SPI_make_plan_non_temp(SPIPlanPtr plan)
Definition: spi.c:2969
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
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 3125 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().

3126 {
3127  EphemeralNamedRelation match;
3128  int res;
3129 
3130  if (enr == NULL || enr->md.name == NULL)
3131  return SPI_ERROR_ARGUMENT;
3132 
3133  res = _SPI_begin_call(false); /* keep current memory context */
3134  if (res < 0)
3135  return res;
3136 
3137  match = _SPI_find_ENR_by_name(enr->md.name);
3138  if (match)
3140  else
3141  {
3142  if (_SPI_current->queryEnv == NULL)
3144 
3146  res = SPI_OK_REL_REGISTER;
3147  }
3148 
3149  _SPI_end_call(false);
3150 
3151  return res;
3152 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:78
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:3108
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define SPI_OK_REL_REGISTER
Definition: spi.h:95
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
QueryEnvironment * queryEnv
Definition: spi_priv.h:37
QueryEnvironment * create_queryEnv(void)
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929
void register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)

◆ SPI_register_trigger_data()

int SPI_register_trigger_data ( TriggerData tdata)

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

3193 {
3194  if (tdata == NULL)
3195  return SPI_ERROR_ARGUMENT;
3196 
3197  if (tdata->tg_newtable)
3198  {
3201  int rc;
3202 
3203  enr->md.name = tdata->tg_trigger->tgnewtable;
3204  enr->md.reliddesc = tdata->tg_relation->rd_id;
3205  enr->md.tupdesc = NULL;
3208  enr->reldata = tdata->tg_newtable;
3209  rc = SPI_register_relation(enr);
3210  if (rc != SPI_OK_REL_REGISTER)
3211  return rc;
3212  }
3213 
3214  if (tdata->tg_oldtable)
3215  {
3218  int rc;
3219 
3220  enr->md.name = tdata->tg_trigger->tgoldtable;
3221  enr->md.reliddesc = tdata->tg_relation->rd_id;
3222  enr->md.tupdesc = NULL;
3225  enr->reldata = tdata->tg_oldtable;
3226  rc = SPI_register_relation(enr);
3227  if (rc != SPI_OK_REL_REGISTER)
3228  return rc;
3229  }
3230 
3231  return SPI_OK_TD_REGISTER;
3232 }
EphemeralNamedRelationMetadataData md
#define SPI_OK_REL_REGISTER
Definition: spi.h:95
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
#define SPI_OK_TD_REGISTER
Definition: spi.h:97
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:3125
void * palloc(Size size)
Definition: mcxt.c:1062
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 1242 of file spi.c.

References repalloc().

1243 {
1244  /* No longer need to worry which context chunk was in... */
1245  return repalloc(pointer, size);
1246 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182

◆ SPI_result_code_string()

const char* SPI_result_code_string ( int  code)

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

1868 {
1869  static char buf[64];
1870 
1871  switch (code)
1872  {
1873  case SPI_ERROR_CONNECT:
1874  return "SPI_ERROR_CONNECT";
1875  case SPI_ERROR_COPY:
1876  return "SPI_ERROR_COPY";
1877  case SPI_ERROR_OPUNKNOWN:
1878  return "SPI_ERROR_OPUNKNOWN";
1879  case SPI_ERROR_UNCONNECTED:
1880  return "SPI_ERROR_UNCONNECTED";
1881  case SPI_ERROR_ARGUMENT:
1882  return "SPI_ERROR_ARGUMENT";
1883  case SPI_ERROR_PARAM:
1884  return "SPI_ERROR_PARAM";
1885  case SPI_ERROR_TRANSACTION:
1886  return "SPI_ERROR_TRANSACTION";
1887  case SPI_ERROR_NOATTRIBUTE:
1888  return "SPI_ERROR_NOATTRIBUTE";
1889  case SPI_ERROR_NOOUTFUNC:
1890  return "SPI_ERROR_NOOUTFUNC";
1891  case SPI_ERROR_TYPUNKNOWN:
1892  return "SPI_ERROR_TYPUNKNOWN";
1894  return "SPI_ERROR_REL_DUPLICATE";
1896  return "SPI_ERROR_REL_NOT_FOUND";
1897  case SPI_OK_CONNECT:
1898  return "SPI_OK_CONNECT";
1899  case SPI_OK_FINISH:
1900  return "SPI_OK_FINISH";
1901  case SPI_OK_FETCH:
1902  return "SPI_OK_FETCH";
1903  case SPI_OK_UTILITY:
1904  return "SPI_OK_UTILITY";
1905  case SPI_OK_SELECT:
1906  return "SPI_OK_SELECT";
1907  case SPI_OK_SELINTO:
1908  return "SPI_OK_SELINTO";
1909  case SPI_OK_INSERT:
1910  return "SPI_OK_INSERT";
1911  case SPI_OK_DELETE:
1912  return "SPI_OK_DELETE";
1913  case SPI_OK_UPDATE:
1914  return "SPI_OK_UPDATE";
1915  case SPI_OK_CURSOR:
1916  return "SPI_OK_CURSOR";
1918  return "SPI_OK_INSERT_RETURNING";
1920  return "SPI_OK_DELETE_RETURNING";
1922  return "SPI_OK_UPDATE_RETURNING";
1923  case SPI_OK_REWRITTEN:
1924  return "SPI_OK_REWRITTEN";
1925  case SPI_OK_REL_REGISTER:
1926  return "SPI_OK_REL_REGISTER";
1927  case SPI_OK_REL_UNREGISTER:
1928  return "SPI_OK_REL_UNREGISTER";
1929  }
1930  /* Unrecognized code ... return something useful ... */
1931  sprintf(buf, "Unrecognized SPI code %d", code);
1932  return buf;
1933 }
#define SPI_OK_CONNECT
Definition: spi.h:81
#define SPI_ERROR_PARAM
Definition: spi.h:73
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:79
#define SPI_ERROR_REL_DUPLICATE
Definition: spi.h:78
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:70
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:92
#define SPI_OK_DELETE
Definition: spi.h:88
#define SPI_ERROR_COPY
Definition: spi.h:68
#define SPI_ERROR_OPUNKNOWN
Definition: spi.h:69
#define SPI_OK_REL_REGISTER
Definition: spi.h:95
#define SPI_ERROR_CONNECT
Definition: spi.h:67
#define SPI_OK_CURSOR
Definition: spi.h:90
#define sprintf
Definition: port.h:218
#define SPI_ERROR_NOOUTFUNC
Definition: spi.h:76
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:91
static char * buf
Definition: pg_test_fsync.c:68
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:75
#define SPI_OK_UTILITY
Definition: spi.h:84
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:93
#define SPI_OK_REWRITTEN
Definition: spi.h:94
#define SPI_ERROR_TRANSACTION
Definition: spi.h:74
#define SPI_OK_SELINTO
Definition: spi.h:86
#define SPI_OK_FETCH
Definition: spi.h:83
#define SPI_OK_SELECT
Definition: spi.h:85
#define SPI_OK_FINISH
Definition: spi.h:82
#define SPI_ERROR_TYPUNKNOWN
Definition: spi.h:77
#define SPI_OK_UPDATE
Definition: spi.h:89
#define SPI_OK_INSERT
Definition: spi.h:87
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:96

◆ SPI_returntuple()

HeapTupleHeader SPI_returntuple ( HeapTuple  tuple,
TupleDesc  tupdesc 
)

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

970 {
971  MemoryContext oldcxt;
972  HeapTupleHeader dtup;
973 
974  if (tuple == NULL || tupdesc == NULL)
975  {
977  return NULL;
978  }
979 
980  if (_SPI_current == NULL)
981  {
983  return NULL;
984  }
985 
986  /* For RECORD results, make sure a typmod has been assigned */
987  if (tupdesc->tdtypeid == RECORDOID &&
988  tupdesc->tdtypmod < 0)
989  assign_record_type_typmod(tupdesc);
990 
992 
993  dtup = DatumGetHeapTupleHeader(heap_copy_tuple_as_datum(tuple, tupdesc));
994 
995  MemoryContextSwitchTo(oldcxt);
996 
997  return dtup;
998 }
#define SPI_ERROR_UNCONNECTED
Definition: spi.h:70
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static _SPI_connection * _SPI_current
Definition: spi.c:50
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
void assign_record_type_typmod(TupleDesc tupDesc)
Definition: typcache.c:1947
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
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 341 of file spi.c.

References _SPI_rollback().

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

342 {
343  _SPI_rollback(false);
344 }
static void _SPI_rollback(bool chain)
Definition: spi.c:295

◆ SPI_rollback_and_chain()

void SPI_rollback_and_chain ( void  )

Definition at line 347 of file spi.c.

References _SPI_rollback().

Referenced by exec_stmt_rollback().

348 {
349  _SPI_rollback(true);
350 }
static void _SPI_rollback(bool chain)
Definition: spi.c:295

◆ SPI_saveplan()

SPIPlanPtr SPI_saveplan ( SPIPlanPtr  plan)

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

899 {
900  SPIPlanPtr newplan;
901 
902  if (plan == NULL || plan->magic != _SPI_PLAN_MAGIC)
903  {
905  return NULL;
906  }
907 
908  SPI_result = _SPI_begin_call(false); /* don't change context */
909  if (SPI_result < 0)
910  return NULL;
911 
912  newplan = _SPI_save_plan(plan);
913 
914  SPI_result = _SPI_end_call(false);
915 
916  return newplan;
917 }
#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:2905
int SPI_result
Definition: spi.c:47
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
static SPIPlanPtr _SPI_save_plan(SPIPlanPtr plan)
Definition: spi.c:3037
static int _SPI_end_call(bool use_exec)
Definition: spi.c:2929

◆ SPI_scroll_cursor_fetch()

void SPI_scroll_cursor_fetch ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1730 of file spi.c.

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

Referenced by exec_stmt_fetch().

1731 {
1732  _SPI_cursor_operation(portal,
1733  direction, count,
1735  /* we know that the DestSPI receiver doesn't need a destroy call */
1736 }
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:2835

◆ SPI_scroll_cursor_move()

void SPI_scroll_cursor_move ( Portal  ,
FetchDirection  direction,
long  count 
)

Definition at line 1745 of file spi.c.

References _SPI_cursor_operation(), and None_Receiver.

Referenced by exec_stmt_fetch().

1746 {
1747  _SPI_cursor_operation(portal, direction, count, None_Receiver);
1748 }
DestReceiver * None_Receiver
Definition: dest.c:96
static void _SPI_cursor_operation(Portal portal, FetchDirection direction, long count, DestReceiver *dest)
Definition: spi.c:2835

◆ SPI_start_transaction()

void SPI_start_transaction ( void  )

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

222 {
223  MemoryContext oldcontext = CurrentMemoryContext;
224 
226  MemoryContextSwitchTo(oldcontext);
227 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void StartTransactionCommand(void)
Definition: xact.c:2848

◆ SPI_unregister_relation()

int SPI_unregister_relation ( const char *  name)

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

3160 {
3161  EphemeralNamedRelation match;
3162  int res;
3163 
3164  if (name == NULL)
3165  return SPI_ERROR_ARGUMENT;
3166 
3167  res = _SPI_begin_call(false); /* keep current memory context */
3168  if (res < 0)
3169  return res;
3170 
3171  match = _SPI_find_ENR_by_name(name);
3172  if (match)
3173  {
3175  res = SPI_OK_REL_UNREGISTER;
3176  }
3177  else
3179 
3180  _SPI_end_call(false);
3181 
3182  return res;
3183 }
EphemeralNamedRelationMetadataData md
#define SPI_ERROR_REL_NOT_FOUND
Definition: spi.h:79
static EphemeralNamedRelation _SPI_find_ENR_by_name(const char *name)
Definition: spi.c:3108
static _SPI_connection * _SPI_current
Definition: spi.c:50
static int _SPI_begin_call(bool use_exec)
Definition: spi.c:2905
#define SPI_ERROR_ARGUMENT
Definition: spi.h:72
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:2929
const char * name
Definition: encode.c:561
#define SPI_OK_REL_UNREGISTER
Definition: spi.h:96

◆ SPICleanup()

void SPICleanup ( void  )

Definition at line 357 of file spi.c.

References _SPI_connected, SPI_processed, and SPI_result.

Referenced by AtEOXact_SPI(), and PostgresMain().

358 {
359  _SPI_current = NULL;
360  _SPI_connected = -1;
361  /* Reset API global variables, too */
362  SPI_processed = 0;
363  SPI_tuptable = NULL;
364  SPI_result = 0;
365 }
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