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-2018, 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  MemoryContext tuptabcxt; /* memory context of result table */
25  uint64 alloced; /* # of alloced vals */
26  uint64 free; /* # of free vals */
27  TupleDesc tupdesc; /* tuple descriptor */
28  HeapTuple *vals; /* tuples */
29  slist_node next; /* link for internal bookkeeping */
30  SubTransactionId subid; /* subxact in which tuptable was created */
32 
33 /* Plans are opaque structs for standard users of SPI */
34 typedef struct _SPI_plan *SPIPlanPtr;
35 
36 #define SPI_ERROR_CONNECT (-1)
37 #define SPI_ERROR_COPY (-2)
38 #define SPI_ERROR_OPUNKNOWN (-3)
39 #define SPI_ERROR_UNCONNECTED (-4)
40 #define SPI_ERROR_CURSOR (-5) /* not used anymore */
41 #define SPI_ERROR_ARGUMENT (-6)
42 #define SPI_ERROR_PARAM (-7)
43 #define SPI_ERROR_TRANSACTION (-8)
44 #define SPI_ERROR_NOATTRIBUTE (-9)
45 #define SPI_ERROR_NOOUTFUNC (-10)
46 #define SPI_ERROR_TYPUNKNOWN (-11)
47 #define SPI_ERROR_REL_DUPLICATE (-12)
48 #define SPI_ERROR_REL_NOT_FOUND (-13)
49 
50 #define SPI_OK_CONNECT 1
51 #define SPI_OK_FINISH 2
52 #define SPI_OK_FETCH 3
53 #define SPI_OK_UTILITY 4
54 #define SPI_OK_SELECT 5
55 #define SPI_OK_SELINTO 6
56 #define SPI_OK_INSERT 7
57 #define SPI_OK_DELETE 8
58 #define SPI_OK_UPDATE 9
59 #define SPI_OK_CURSOR 10
60 #define SPI_OK_INSERT_RETURNING 11
61 #define SPI_OK_DELETE_RETURNING 12
62 #define SPI_OK_UPDATE_RETURNING 13
63 #define SPI_OK_REWRITTEN 14
64 #define SPI_OK_REL_REGISTER 15
65 #define SPI_OK_REL_UNREGISTER 16
66 #define SPI_OK_TD_REGISTER 17
67 
68 #define SPI_OPT_NONATOMIC (1 << 0)
69 
70 /* These used to be functions, now just no-ops for backwards compatibility */
71 #define SPI_push() ((void) 0)
72 #define SPI_pop() ((void) 0)
73 #define SPI_push_conditional() false
74 #define SPI_pop_conditional(pushed) ((void) 0)
75 #define SPI_restore_connection() ((void) 0)
76 
77 extern PGDLLIMPORT uint64 SPI_processed;
80 extern PGDLLIMPORT int SPI_result;
81 
82 extern int SPI_connect(void);
83 extern int SPI_connect_ext(int options);
84 extern int SPI_finish(void);
85 extern int SPI_execute(const char *src, bool read_only, long tcount);
86 extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
87  bool read_only, long tcount);
88 extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan,
89  ParamListInfo params,
90  bool read_only, long tcount);
91 extern int SPI_exec(const char *src, long tcount);
92 extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls,
93  long tcount);
94 extern int SPI_execute_snapshot(SPIPlanPtr plan,
95  Datum *Values, const char *Nulls,
96  Snapshot snapshot,
97  Snapshot crosscheck_snapshot,
98  bool read_only, bool fire_triggers, long tcount);
99 extern int SPI_execute_with_args(const char *src,
100  int nargs, Oid *argtypes,
101  Datum *Values, const char *Nulls,
102  bool read_only, long tcount);
103 extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes);
104 extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
105  int cursorOptions);
106 extern SPIPlanPtr SPI_prepare_params(const char *src,
108  void *parserSetupArg,
109  int cursorOptions);
110 extern int SPI_keepplan(SPIPlanPtr plan);
111 extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan);
112 extern int SPI_freeplan(SPIPlanPtr plan);
113 
114 extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex);
115 extern int SPI_getargcount(SPIPlanPtr plan);
116 extern bool SPI_is_cursor_plan(SPIPlanPtr plan);
117 extern bool SPI_plan_is_valid(SPIPlanPtr plan);
118 extern const char *SPI_result_code_string(int code);
119 
120 extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan);
121 extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan);
122 
123 extern HeapTuple SPI_copytuple(HeapTuple tuple);
125 extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
126  int *attnum, Datum *Values, const char *Nulls);
127 extern int SPI_fnumber(TupleDesc tupdesc, const char *fname);
128 extern char *SPI_fname(TupleDesc tupdesc, int fnumber);
129 extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
130 extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull);
131 extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
132 extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber);
133 extern char *SPI_getrelname(Relation rel);
134 extern char *SPI_getnspname(Relation rel);
135 extern void *SPI_palloc(Size size);
136 extern void *SPI_repalloc(void *pointer, Size size);
137 extern void SPI_pfree(void *pointer);
138 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
139 extern void SPI_freetuple(HeapTuple pointer);
140 extern void SPI_freetuptable(SPITupleTable *tuptable);
141 
142 extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan,
143  Datum *Values, const char *Nulls, bool read_only);
144 extern Portal SPI_cursor_open_with_args(const char *name,
145  const char *src,
146  int nargs, Oid *argtypes,
147  Datum *Values, const char *Nulls,
148  bool read_only, int cursorOptions);
149 extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
150  ParamListInfo params, bool read_only);
151 extern Portal SPI_cursor_find(const char *name);
152 extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
153 extern void SPI_cursor_move(Portal portal, bool forward, long count);
154 extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count);
155 extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count);
156 extern void SPI_cursor_close(Portal portal);
157 
159 extern int SPI_unregister_relation(const char *name);
160 extern int SPI_register_trigger_data(TriggerData *tdata);
161 
162 extern void SPI_start_transaction(void);
163 extern void SPI_commit(void);
164 extern void SPI_rollback(void);
165 
166 extern void SPICleanup(void);
167 extern void AtEOXact_SPI(bool isCommit);
168 extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
169 
170 #endif /* SPI_H */
PGDLLIMPORT SPITupleTable * SPI_tuptable
Definition: spi.c:41
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:880
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1549
Oid * argtypes
Definition: spi_priv.h:96
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:2789
int SPI_unregister_relation(const char *name)
Definition: spi.c:2823
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:990
FetchDirection
Definition: parsenodes.h:2678
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:786
char * SPI_getrelname(Relation rel)
Definition: spi.c:1008
SPIPlanPtr SPI_prepare_params(const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
Definition: spi.c:619
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:303
void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count)
Definition: spi.c:1489
int SPI_exec(const char *src, long tcount)
Definition: spi.c:428
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:902
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1068
void * SPI_palloc(Size size)
Definition: spi.c:1020
void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count)
Definition: spi.c:1474
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1127
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
Definition: spi.c:683
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:1154
PGDLLIMPORT uint64 SPI_processed
Definition: spi.c:39
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:537
slist_node next
Definition: spi.h:29
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:1688
uint32 SubTransactionId
Definition: c.h:478
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:28
#define PGDLLIMPORT
Definition: c.h:1140
SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, int cursorOptions)
Definition: spi.c:584
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:469
int SPI_connect(void)
Definition: spi.c:84
Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan, ParamListInfo params, bool read_only)
Definition: spi.c:1206
void SPI_cursor_move(Portal portal, bool forward, long count)
Definition: spi.c:1460
struct SPITupleTable SPITupleTable
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:754
void SPI_start_transaction(void)
Definition: spi.c:197
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1445
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:950
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
int SPI_connect_ext(int options)
Definition: spi.c:90
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:503
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2856
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:705
int SPI_finish(void)
Definition: spi.c:161
void SPI_pfree(void *pointer)
Definition: spi.c:1036
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:400
PGDLLIMPORT Oid SPI_lastoid
Definition: spi.c:40
int SPI_getargcount(SPIPlanPtr plan)
Definition: spi.c:1529
void SPI_rollback(void)
Definition: spi.c:240
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:1704
uintptr_t Datum
Definition: postgres.h:367
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1043
TupleDesc tupdesc
Definition: spi.h:27
static struct @131 value
int nargs
Definition: spi_priv.h:95
int16 attnum
Definition: pg_attribute.h:79
void SPI_freetuple(HeapTuple pointer)
Definition: spi.c:1061
void SPI_cursor_close(Portal portal)
Definition: spi.c:1501
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:857
void SPI_commit(void)
Definition: spi.c:206
ParserSetupHook parserSetup
Definition: spi_priv.h:97
Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
Definition: spi.c:1514
size_t Size
Definition: c.h:433
void * parserSetupArg
Definition: spi_priv.h:98
const char * name
Definition: encode.c:521
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:281
uint64 free
Definition: spi.h:26
uint64 alloced
Definition: spi.h:25
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:934
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1433
void SPICleanup(void)
Definition: spi.c:268
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:727
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:34
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:578
SubTransactionId subid
Definition: spi.h:30
Definition: pg_list.h:45
int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, long tcount)
Definition: spi.c:462
PGDLLIMPORT int SPI_result
Definition: spi.c:42
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:656
void * SPI_repalloc(void *pointer, Size size)
Definition: spi.c:1029
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:435
MemoryContext tuptabcxt
Definition: spi.h:24
char * SPI_getnspname(Relation rel)
Definition: spi.c:1014
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1587
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165