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-2021, 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 "parser/parser.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 /* Optional arguments for SPI_prepare_extended */
37 typedef struct SPIPrepareOptions
38 {
44 
45 /* Optional arguments for SPI_execute[_plan]_extended */
46 typedef struct SPIExecuteOptions
47 {
49  bool read_only;
51  uint64 tcount;
55 
56 /* Optional arguments for SPI_cursor_parse_open */
57 typedef struct SPIParseOpenOptions
58 {
61  bool read_only;
63 
64 /* Plans are opaque structs for standard users of SPI */
65 typedef struct _SPI_plan *SPIPlanPtr;
66 
67 #define SPI_ERROR_CONNECT (-1)
68 #define SPI_ERROR_COPY (-2)
69 #define SPI_ERROR_OPUNKNOWN (-3)
70 #define SPI_ERROR_UNCONNECTED (-4)
71 #define SPI_ERROR_CURSOR (-5) /* not used anymore */
72 #define SPI_ERROR_ARGUMENT (-6)
73 #define SPI_ERROR_PARAM (-7)
74 #define SPI_ERROR_TRANSACTION (-8)
75 #define SPI_ERROR_NOATTRIBUTE (-9)
76 #define SPI_ERROR_NOOUTFUNC (-10)
77 #define SPI_ERROR_TYPUNKNOWN (-11)
78 #define SPI_ERROR_REL_DUPLICATE (-12)
79 #define SPI_ERROR_REL_NOT_FOUND (-13)
80 
81 #define SPI_OK_CONNECT 1
82 #define SPI_OK_FINISH 2
83 #define SPI_OK_FETCH 3
84 #define SPI_OK_UTILITY 4
85 #define SPI_OK_SELECT 5
86 #define SPI_OK_SELINTO 6
87 #define SPI_OK_INSERT 7
88 #define SPI_OK_DELETE 8
89 #define SPI_OK_UPDATE 9
90 #define SPI_OK_CURSOR 10
91 #define SPI_OK_INSERT_RETURNING 11
92 #define SPI_OK_DELETE_RETURNING 12
93 #define SPI_OK_UPDATE_RETURNING 13
94 #define SPI_OK_REWRITTEN 14
95 #define SPI_OK_REL_REGISTER 15
96 #define SPI_OK_REL_UNREGISTER 16
97 #define SPI_OK_TD_REGISTER 17
98 
99 #define SPI_OPT_NONATOMIC (1 << 0)
100 
101 /* These used to be functions, now just no-ops for backwards compatibility */
102 #define SPI_push() ((void) 0)
103 #define SPI_pop() ((void) 0)
104 #define SPI_push_conditional() false
105 #define SPI_pop_conditional(pushed) ((void) 0)
106 #define SPI_restore_connection() ((void) 0)
107 
108 extern PGDLLIMPORT uint64 SPI_processed;
110 extern PGDLLIMPORT int SPI_result;
111 
112 extern int SPI_connect(void);
113 extern int SPI_connect_ext(int options);
114 extern int SPI_finish(void);
115 extern int SPI_execute(const char *src, bool read_only, long tcount);
116 extern int SPI_execute_extended(const char *src,
117  const SPIExecuteOptions *options);
118 extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
119  bool read_only, long tcount);
120 extern int SPI_execute_plan_extended(SPIPlanPtr plan,
121  const SPIExecuteOptions *options);
122 extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan,
123  ParamListInfo params,
124  bool read_only, long tcount);
125 extern int SPI_exec(const char *src, long tcount);
126 extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls,
127  long tcount);
128 extern int SPI_execute_snapshot(SPIPlanPtr plan,
129  Datum *Values, const char *Nulls,
130  Snapshot snapshot,
131  Snapshot crosscheck_snapshot,
132  bool read_only, bool fire_triggers, long tcount);
133 extern int SPI_execute_with_args(const char *src,
134  int nargs, Oid *argtypes,
135  Datum *Values, const char *Nulls,
136  bool read_only, long tcount);
137 extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes);
138 extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
139  int cursorOptions);
140 extern SPIPlanPtr SPI_prepare_extended(const char *src,
141  const SPIPrepareOptions *options);
142 extern SPIPlanPtr SPI_prepare_params(const char *src,
144  void *parserSetupArg,
145  int cursorOptions);
146 extern int SPI_keepplan(SPIPlanPtr plan);
147 extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan);
148 extern int SPI_freeplan(SPIPlanPtr plan);
149 
150 extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex);
151 extern int SPI_getargcount(SPIPlanPtr plan);
152 extern bool SPI_is_cursor_plan(SPIPlanPtr plan);
153 extern bool SPI_plan_is_valid(SPIPlanPtr plan);
154 extern const char *SPI_result_code_string(int code);
155 
156 extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan);
157 extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan);
158 
159 extern HeapTuple SPI_copytuple(HeapTuple tuple);
161 extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
162  int *attnum, Datum *Values, const char *Nulls);
163 extern int SPI_fnumber(TupleDesc tupdesc, const char *fname);
164 extern char *SPI_fname(TupleDesc tupdesc, int fnumber);
165 extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
166 extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull);
167 extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
168 extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber);
169 extern char *SPI_getrelname(Relation rel);
170 extern char *SPI_getnspname(Relation rel);
171 extern void *SPI_palloc(Size size);
172 extern void *SPI_repalloc(void *pointer, Size size);
173 extern void SPI_pfree(void *pointer);
174 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
175 extern void SPI_freetuple(HeapTuple pointer);
176 extern void SPI_freetuptable(SPITupleTable *tuptable);
177 
178 extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan,
179  Datum *Values, const char *Nulls, bool read_only);
180 extern Portal SPI_cursor_open_with_args(const char *name,
181  const char *src,
182  int nargs, Oid *argtypes,
183  Datum *Values, const char *Nulls,
184  bool read_only, int cursorOptions);
185 extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
186  ParamListInfo params, bool read_only);
187 extern Portal SPI_cursor_parse_open(const char *name,
188  const char *src,
190 extern Portal SPI_cursor_find(const char *name);
191 extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
192 extern void SPI_cursor_move(Portal portal, bool forward, long count);
193 extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count);
194 extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count);
195 extern void SPI_cursor_close(Portal portal);
196 
198 extern int SPI_unregister_relation(const char *name);
199 extern int SPI_register_trigger_data(TriggerData *tdata);
200 
201 extern void SPI_start_transaction(void);
202 extern void SPI_commit(void);
203 extern void SPI_commit_and_chain(void);
204 extern void SPI_rollback(void);
205 extern void SPI_rollback_and_chain(void);
206 
207 extern void SPICleanup(void);
208 extern void AtEOXact_SPI(bool isCommit);
209 extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
210 extern bool SPI_inside_nonatomic_context(void);
211 
212 #endif /* SPI_H */
PGDLLIMPORT SPITupleTable * SPI_tuptable
Definition: spi.c:46
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1093
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1805
Oid * argtypes
Definition: spi_priv.h:100
const char * SPI_result_code_string(int code)
Definition: spi.c:1867
void SPI_rollback_and_chain(void)
Definition: spi.c:347
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:3125
int SPI_unregister_relation(const char *name)
Definition: spi.c:3159
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1203
FetchDirection
Definition: parsenodes.h:2853
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:1001
char * SPI_getrelname(Relation rel)
Definition: spi.c:1221
SPIPlanPtr SPI_prepare_params(const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
Definition: spi.c:833
struct SPIPrepareOptions SPIPrepareOptions
SPIPlanPtr SPI_prepare_extended(const char *src, const SPIPrepareOptions *options)
Definition: spi.c:797
ParamListInfo params
Definition: spi.h:59
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:393
void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count)
Definition: spi.c:1745
int SPI_exec(const char *src, long tcount)
Definition: spi.c:535
bool SPI_inside_nonatomic_context(void)
Definition: spi.c:492
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:1115
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1281
void * SPI_palloc(Size size)
Definition: spi.c:1233
void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count)
Definition: spi.c:1730
DestReceiver * dest
Definition: spi.h:52
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1340
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
Definition: spi.c:898
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:1367
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:711
slist_node next
Definition: spi.h:32
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:1944
uint64 tcount
Definition: spi.h:51
uint32 SubTransactionId
Definition: c.h:591
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:26
RawParseMode
Definition: parser.h:37
bool read_only
Definition: spi.h:61
#define PGDLLIMPORT
Definition: c.h:1317
SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, int cursorOptions)
Definition: spi.c:761
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:639
int SPI_connect(void)
Definition: spi.c:98
int SPI_execute_extended(const char *src, const SPIExecuteOptions *options)
Definition: spi.c:542
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:1420
void SPI_cursor_move(Portal portal, bool forward, long count)
Definition: spi.c:1716
struct SPITupleTable SPITupleTable
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:969
void SPI_start_transaction(void)
Definition: spi.c:221
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1701
int SPI_execute_plan_extended(SPIPlanPtr plan, const SPIExecuteOptions *options)
Definition: spi.c:615
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1163
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
ResourceOwner owner
Definition: spi.h:53
int SPI_connect_ext(int options)
Definition: spi.c:104
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:675
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3192
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:920
int cursorOptions
Definition: spi.h:60
int SPI_finish(void)
Definition: spi.c:185
ParserSetupHook parserSetup
Definition: spi.h:39
struct SPIExecuteOptions SPIExecuteOptions
void SPI_pfree(void *pointer)
Definition: spi.c:1249
struct SPIParseOpenOptions SPIParseOpenOptions
ParamListInfo params
Definition: spi.h:48
bool read_only
Definition: spi.h:49
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:504
int SPI_getargcount(SPIPlanPtr plan)
Definition: spi.c:1785
void SPI_rollback(void)
Definition: spi.c:341
Portal SPI_cursor_parse_open(const char *name, const char *src, const SPIParseOpenOptions *options)
Definition: spi.c:1428
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:1963
uintptr_t Datum
Definition: postgres.h:411
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1256
bool allow_nonatomic
Definition: spi.h:50
int cursorOptions
Definition: spi.h:42
TupleDesc tupdesc
Definition: spi.h:25
int nargs
Definition: spi_priv.h:99
int16 attnum
Definition: pg_attribute.h:83
static struct @143 value
void SPI_freetuple(HeapTuple pointer)
Definition: spi.c:1274
void SPI_cursor_close(Portal portal)
Definition: spi.c:1757
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1070
void SPI_commit(void)
Definition: spi.c:283
void SPI_commit_and_chain(void)
Definition: spi.c:289
ParserSetupHook parserSetup
Definition: spi_priv.h:101
Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
Definition: spi.c:1770
size_t Size
Definition: c.h:540
void * parserSetupArg
Definition: spi_priv.h:102
const char * name
Definition: encode.c:561
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:371
uint64 alloced
Definition: spi.h:30
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1147
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1689
void SPICleanup(void)
Definition: spi.c:357
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:942
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:65
void * parserSetupArg
Definition: spi.h:40
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:755
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:608
PGDLLIMPORT int SPI_result
Definition: spi.c:47
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:871
RawParseMode parseMode
Definition: spi.h:41
void * SPI_repalloc(void *pointer, Size size)
Definition: spi.c:1242
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:579
MemoryContext tuptabcxt
Definition: spi.h:31
char * SPI_getnspname(Relation rel)
Definition: spi.c:1227
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1843
static bool Nulls[MAXATTR]
Definition: bootstrap.c:157