PostgreSQL Source Code  git master
spi.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * spi.h
4  * Server Programming Interface public declarations
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * src/include/executor/spi.h
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef SPI_H
14 #define SPI_H
15 
16 #include "commands/trigger.h"
17 #include "lib/ilist.h"
18 #include "nodes/parsenodes.h"
19 #include "utils/portal.h"
20 
21 
22 typedef struct SPITupleTable
23 {
24  /* Public members */
25  TupleDesc tupdesc; /* tuple descriptor */
26  HeapTuple *vals; /* array of tuples */
27  uint64 numvals; /* number of valid tuples */
28 
29  /* Private members, not intended for external callers */
30  uint64 alloced; /* allocated length of vals array */
31  MemoryContext tuptabcxt; /* memory context of result table */
32  slist_node next; /* link for internal bookkeeping */
33  SubTransactionId subid; /* subxact in which tuptable was created */
35 
36 /* Plans are opaque structs for standard users of SPI */
37 typedef struct _SPI_plan *SPIPlanPtr;
38 
39 #define SPI_ERROR_CONNECT (-1)
40 #define SPI_ERROR_COPY (-2)
41 #define SPI_ERROR_OPUNKNOWN (-3)
42 #define SPI_ERROR_UNCONNECTED (-4)
43 #define SPI_ERROR_CURSOR (-5) /* not used anymore */
44 #define SPI_ERROR_ARGUMENT (-6)
45 #define SPI_ERROR_PARAM (-7)
46 #define SPI_ERROR_TRANSACTION (-8)
47 #define SPI_ERROR_NOATTRIBUTE (-9)
48 #define SPI_ERROR_NOOUTFUNC (-10)
49 #define SPI_ERROR_TYPUNKNOWN (-11)
50 #define SPI_ERROR_REL_DUPLICATE (-12)
51 #define SPI_ERROR_REL_NOT_FOUND (-13)
52 
53 #define SPI_OK_CONNECT 1
54 #define SPI_OK_FINISH 2
55 #define SPI_OK_FETCH 3
56 #define SPI_OK_UTILITY 4
57 #define SPI_OK_SELECT 5
58 #define SPI_OK_SELINTO 6
59 #define SPI_OK_INSERT 7
60 #define SPI_OK_DELETE 8
61 #define SPI_OK_UPDATE 9
62 #define SPI_OK_CURSOR 10
63 #define SPI_OK_INSERT_RETURNING 11
64 #define SPI_OK_DELETE_RETURNING 12
65 #define SPI_OK_UPDATE_RETURNING 13
66 #define SPI_OK_REWRITTEN 14
67 #define SPI_OK_REL_REGISTER 15
68 #define SPI_OK_REL_UNREGISTER 16
69 #define SPI_OK_TD_REGISTER 17
70 
71 #define SPI_OPT_NONATOMIC (1 << 0)
72 
73 /* These used to be functions, now just no-ops for backwards compatibility */
74 #define SPI_push() ((void) 0)
75 #define SPI_pop() ((void) 0)
76 #define SPI_push_conditional() false
77 #define SPI_pop_conditional(pushed) ((void) 0)
78 #define SPI_restore_connection() ((void) 0)
79 
80 extern PGDLLIMPORT uint64 SPI_processed;
82 extern PGDLLIMPORT int SPI_result;
83 
84 extern int SPI_connect(void);
85 extern int SPI_connect_ext(int options);
86 extern int SPI_finish(void);
87 extern int SPI_execute(const char *src, bool read_only, long tcount);
88 extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
89  bool read_only, long tcount);
90 extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan,
91  ParamListInfo params,
92  bool read_only, long tcount);
93 extern int SPI_execute_plan_with_receiver(SPIPlanPtr plan,
94  ParamListInfo params,
95  bool read_only, long tcount,
97 extern int SPI_exec(const char *src, long tcount);
98 extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls,
99  long tcount);
100 extern int SPI_execute_snapshot(SPIPlanPtr plan,
101  Datum *Values, const char *Nulls,
102  Snapshot snapshot,
103  Snapshot crosscheck_snapshot,
104  bool read_only, bool fire_triggers, long tcount);
105 extern int SPI_execute_with_args(const char *src,
106  int nargs, Oid *argtypes,
107  Datum *Values, const char *Nulls,
108  bool read_only, long tcount);
109 extern int SPI_execute_with_receiver(const char *src,
110  ParamListInfo params,
111  bool read_only, long tcount,
112  DestReceiver *dest);
113 extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes);
114 extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
115  int cursorOptions);
116 extern SPIPlanPtr SPI_prepare_params(const char *src,
118  void *parserSetupArg,
119  int cursorOptions);
120 extern int SPI_keepplan(SPIPlanPtr plan);
121 extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan);
122 extern int SPI_freeplan(SPIPlanPtr plan);
123 
124 extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex);
125 extern int SPI_getargcount(SPIPlanPtr plan);
126 extern bool SPI_is_cursor_plan(SPIPlanPtr plan);
127 extern bool SPI_plan_is_valid(SPIPlanPtr plan);
128 extern const char *SPI_result_code_string(int code);
129 
130 extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan);
131 extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan);
132 
133 extern HeapTuple SPI_copytuple(HeapTuple tuple);
135 extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
136  int *attnum, Datum *Values, const char *Nulls);
137 extern int SPI_fnumber(TupleDesc tupdesc, const char *fname);
138 extern char *SPI_fname(TupleDesc tupdesc, int fnumber);
139 extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
140 extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull);
141 extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
142 extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber);
143 extern char *SPI_getrelname(Relation rel);
144 extern char *SPI_getnspname(Relation rel);
145 extern void *SPI_palloc(Size size);
146 extern void *SPI_repalloc(void *pointer, Size size);
147 extern void SPI_pfree(void *pointer);
148 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
149 extern void SPI_freetuple(HeapTuple pointer);
150 extern void SPI_freetuptable(SPITupleTable *tuptable);
151 
152 extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan,
153  Datum *Values, const char *Nulls, bool read_only);
154 extern Portal SPI_cursor_open_with_args(const char *name,
155  const char *src,
156  int nargs, Oid *argtypes,
157  Datum *Values, const char *Nulls,
158  bool read_only, int cursorOptions);
159 extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
160  ParamListInfo params, bool read_only);
162  const char *src,
163  ParamListInfo params,
164  bool read_only,
165  int cursorOptions);
166 extern Portal SPI_cursor_find(const char *name);
167 extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
168 extern void SPI_cursor_move(Portal portal, bool forward, long count);
169 extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count);
170 extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count);
171 extern void SPI_cursor_close(Portal portal);
172 
174 extern int SPI_unregister_relation(const char *name);
175 extern int SPI_register_trigger_data(TriggerData *tdata);
176 
177 extern void SPI_start_transaction(void);
178 extern void SPI_commit(void);
179 extern void SPI_commit_and_chain(void);
180 extern void SPI_rollback(void);
181 extern void SPI_rollback_and_chain(void);
182 
183 extern void SPICleanup(void);
184 extern void AtEOXact_SPI(bool isCommit);
185 extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
186 extern bool SPI_inside_nonatomic_context(void);
187 
188 #endif /* SPI_H */
PGDLLIMPORT SPITupleTable * SPI_tuptable
Definition: spi.c:46
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1047
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1753
Oid * argtypes
Definition: spi_priv.h:100
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
void SPI_rollback_and_chain(void)
Definition: spi.c:340
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:2996
int SPI_unregister_relation(const char *name)
Definition: spi.c:3030
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1157
FetchDirection
Definition: parsenodes.h:2742
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:955
char * SPI_getrelname(Relation rel)
Definition: spi.c:1175
SPIPlanPtr SPI_prepare_params(const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
Definition: spi.c:788
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:386
void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count)
Definition: spi.c:1693
int SPI_exec(const char *src, long tcount)
Definition: spi.c:525
bool SPI_inside_nonatomic_context(void)
Definition: spi.c:485
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1069
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1235
void * SPI_palloc(Size size)
Definition: spi.c:1187
void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count)
Definition: spi.c:1678
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1294
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
Definition: spi.c:852
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: spi.c:1321
PGDLLIMPORT uint64 SPI_processed
Definition: spi.c:45
int SPI_execute_with_args(const char *src, int nargs, Oid *argtypes, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:663
slist_node next
Definition: spi.h:32
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:1892
uint32 SubTransactionId
Definition: c.h:524
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:26
#define PGDLLIMPORT
Definition: c.h:1257
SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, int cursorOptions)
Definition: spi.c:753
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:566
int SPI_connect(void)
Definition: spi.c:90
uint64 numvals
Definition: spi.h:27
Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan, ParamListInfo params, bool read_only)
Definition: spi.c:1373
void SPI_cursor_move(Portal portal, bool forward, long count)
Definition: spi.c:1664
struct SPITupleTable SPITupleTable
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:923
void SPI_start_transaction(void)
Definition: spi.c:213
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1649
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1117
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
int SPI_connect_ext(int options)
Definition: spi.c:96
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: spi.c:629
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3063
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:874
int SPI_finish(void)
Definition: spi.c:177
void SPI_pfree(void *pointer)
Definition: spi.c:1203
int SPI_execute_with_receiver(const char *src, ParamListInfo params, bool read_only, long tcount, DestReceiver *dest)
Definition: spi.c:712
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:497
int SPI_getargcount(SPIPlanPtr plan)
Definition: spi.c:1733
int SPI_execute_plan_with_receiver(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount, DestReceiver *dest)
Definition: spi.c:593
void SPI_rollback(void)
Definition: spi.c:334
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:1908
uintptr_t Datum
Definition: postgres.h:367
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1210
TupleDesc tupdesc
Definition: spi.h:25
int nargs
Definition: spi_priv.h:99
int16 attnum
Definition: pg_attribute.h:79
static struct @143 value
void SPI_freetuple(HeapTuple pointer)
Definition: spi.c:1228
void SPI_cursor_close(Portal portal)
Definition: spi.c:1705
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1024
void SPI_commit(void)
Definition: spi.c:279
void SPI_commit_and_chain(void)
Definition: spi.c:285
ParserSetupHook parserSetup
Definition: spi_priv.h:101
Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
Definition: spi.c:1718
size_t Size
Definition: c.h:473
Portal SPI_cursor_parse_open_with_paramlist(const char *name, const char *src, ParamListInfo params, bool read_only, int cursorOptions)
Definition: spi.c:1386
void * parserSetupArg
Definition: spi_priv.h:102
const char * name
Definition: encode.c:561
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:364
uint64 alloced
Definition: spi.h:30
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1101
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1637
void SPICleanup(void)
Definition: spi.c:350
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:896
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:37
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:747
SubTransactionId subid
Definition: spi.h:33
Definition: pg_list.h:50
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
Definition: spi.c:559
PGDLLIMPORT int SPI_result
Definition: spi.c:47
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:825
void * SPI_repalloc(void *pointer, Size size)
Definition: spi.c:1196
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:532
MemoryContext tuptabcxt
Definition: spi.h:31
char * SPI_getnspname(Relation rel)
Definition: spi.c:1181
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1791
static bool Nulls[MAXATTR]
Definition: bootstrap.c:166