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-2019, 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_exec(const char *src, long tcount);
94 extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls,
95  long tcount);
96 extern int SPI_execute_snapshot(SPIPlanPtr plan,
97  Datum *Values, const char *Nulls,
98  Snapshot snapshot,
99  Snapshot crosscheck_snapshot,
100  bool read_only, bool fire_triggers, long tcount);
101 extern int SPI_execute_with_args(const char *src,
102  int nargs, Oid *argtypes,
103  Datum *Values, const char *Nulls,
104  bool read_only, long tcount);
105 extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes);
106 extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
107  int cursorOptions);
108 extern SPIPlanPtr SPI_prepare_params(const char *src,
110  void *parserSetupArg,
111  int cursorOptions);
112 extern int SPI_keepplan(SPIPlanPtr plan);
113 extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan);
114 extern int SPI_freeplan(SPIPlanPtr plan);
115 
116 extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex);
117 extern int SPI_getargcount(SPIPlanPtr plan);
118 extern bool SPI_is_cursor_plan(SPIPlanPtr plan);
119 extern bool SPI_plan_is_valid(SPIPlanPtr plan);
120 extern const char *SPI_result_code_string(int code);
121 
122 extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan);
123 extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan);
124 
125 extern HeapTuple SPI_copytuple(HeapTuple tuple);
127 extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
128  int *attnum, Datum *Values, const char *Nulls);
129 extern int SPI_fnumber(TupleDesc tupdesc, const char *fname);
130 extern char *SPI_fname(TupleDesc tupdesc, int fnumber);
131 extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
132 extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull);
133 extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
134 extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber);
135 extern char *SPI_getrelname(Relation rel);
136 extern char *SPI_getnspname(Relation rel);
137 extern void *SPI_palloc(Size size);
138 extern void *SPI_repalloc(void *pointer, Size size);
139 extern void SPI_pfree(void *pointer);
140 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
141 extern void SPI_freetuple(HeapTuple pointer);
142 extern void SPI_freetuptable(SPITupleTable *tuptable);
143 
144 extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan,
145  Datum *Values, const char *Nulls, bool read_only);
146 extern Portal SPI_cursor_open_with_args(const char *name,
147  const char *src,
148  int nargs, Oid *argtypes,
149  Datum *Values, const char *Nulls,
150  bool read_only, int cursorOptions);
151 extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
152  ParamListInfo params, bool read_only);
153 extern Portal SPI_cursor_find(const char *name);
154 extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
155 extern void SPI_cursor_move(Portal portal, bool forward, long count);
156 extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count);
157 extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count);
158 extern void SPI_cursor_close(Portal portal);
159 
161 extern int SPI_unregister_relation(const char *name);
162 extern int SPI_register_trigger_data(TriggerData *tdata);
163 
164 extern void SPI_start_transaction(void);
165 extern void SPI_commit(void);
166 extern void SPI_commit_and_chain(void);
167 extern void SPI_rollback(void);
168 extern void SPI_rollback_and_chain(void);
169 
170 extern void SPICleanup(void);
171 extern void AtEOXact_SPI(bool isCommit);
172 extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
173 extern bool SPI_inside_nonatomic_context(void);
174 
175 #endif /* SPI_H */
PGDLLIMPORT SPITupleTable * SPI_tuptable
Definition: spi.c:46
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:974
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1643
Oid * argtypes
Definition: spi_priv.h:100
const char * SPI_result_code_string(int code)
Definition: spi.c:1705
void SPI_rollback_and_chain(void)
Definition: spi.c:339
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:2867
int SPI_unregister_relation(const char *name)
Definition: spi.c:2901
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1084
FetchDirection
Definition: parsenodes.h:2717
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:882
char * SPI_getrelname(Relation rel)
Definition: spi.c:1102
SPIPlanPtr SPI_prepare_params(const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
Definition: spi.c:715
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:385
void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count)
Definition: spi.c:1583
int SPI_exec(const char *src, long tcount)
Definition: spi.c:524
bool SPI_inside_nonatomic_context(void)
Definition: spi.c:484
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:996
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1162
void * SPI_palloc(Size size)
Definition: spi.c:1114
void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count)
Definition: spi.c:1568
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1221
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
Definition: spi.c:779
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:1248
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:633
static struct @145 value
slist_node next
Definition: spi.h:32
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:1782
uint32 SubTransactionId
Definition: c.h:512
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:26
#define PGDLLIMPORT
Definition: c.h:1266
SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, int cursorOptions)
Definition: spi.c:680
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:565
int SPI_connect(void)
Definition: spi.c:89
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:1300
void SPI_cursor_move(Portal portal, bool forward, long count)
Definition: spi.c:1554
struct SPITupleTable SPITupleTable
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:850
void SPI_start_transaction(void)
Definition: spi.c:212
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1539
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:1044
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
int SPI_connect_ext(int options)
Definition: spi.c:95
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:599
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2934
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:801
int SPI_finish(void)
Definition: spi.c:176
void SPI_pfree(void *pointer)
Definition: spi.c:1130
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:496
int SPI_getargcount(SPIPlanPtr plan)
Definition: spi.c:1623
void SPI_rollback(void)
Definition: spi.c:333
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:1798
uintptr_t Datum
Definition: postgres.h:367
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1137
TupleDesc tupdesc
Definition: spi.h:25
int nargs
Definition: spi_priv.h:99
int16 attnum
Definition: pg_attribute.h:79
void SPI_freetuple(HeapTuple pointer)
Definition: spi.c:1155
void SPI_cursor_close(Portal portal)
Definition: spi.c:1595
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:951
void SPI_commit(void)
Definition: spi.c:278
void SPI_commit_and_chain(void)
Definition: spi.c:284
ParserSetupHook parserSetup
Definition: spi_priv.h:101
Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
Definition: spi.c:1608
size_t Size
Definition: c.h:467
void * parserSetupArg
Definition: spi_priv.h:102
const char * name
Definition: encode.c:521
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:363
uint64 alloced
Definition: spi.h:30
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1028
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1527
void SPICleanup(void)
Definition: spi.c:349
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:823
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:37
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:674
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:558
PGDLLIMPORT int SPI_result
Definition: spi.c:47
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:752
void * SPI_repalloc(void *pointer, Size size)
Definition: spi.c:1123
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:531
MemoryContext tuptabcxt
Definition: spi.h:31
char * SPI_getnspname(Relation rel)
Definition: spi.c:1108
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1681
static bool Nulls[MAXATTR]
Definition: bootstrap.c:168