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-2017, 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 /* These used to be functions, now just no-ops for backwards compatibility */
69 #define SPI_push() ((void) 0)
70 #define SPI_pop() ((void) 0)
71 #define SPI_push_conditional() false
72 #define SPI_pop_conditional(pushed) ((void) 0)
73 #define SPI_restore_connection() ((void) 0)
74 
75 extern PGDLLIMPORT uint64 SPI_processed;
78 extern PGDLLIMPORT int SPI_result;
79 
80 extern int SPI_connect(void);
81 extern int SPI_finish(void);
82 extern int SPI_execute(const char *src, bool read_only, long tcount);
83 extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
84  bool read_only, long tcount);
85 extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan,
86  ParamListInfo params,
87  bool read_only, long tcount);
88 extern int SPI_exec(const char *src, long tcount);
89 extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls,
90  long tcount);
91 extern int SPI_execute_snapshot(SPIPlanPtr plan,
92  Datum *Values, const char *Nulls,
93  Snapshot snapshot,
94  Snapshot crosscheck_snapshot,
95  bool read_only, bool fire_triggers, long tcount);
96 extern int SPI_execute_with_args(const char *src,
97  int nargs, Oid *argtypes,
98  Datum *Values, const char *Nulls,
99  bool read_only, long tcount);
100 extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes);
101 extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes,
102  int cursorOptions);
103 extern SPIPlanPtr SPI_prepare_params(const char *src,
105  void *parserSetupArg,
106  int cursorOptions);
107 extern int SPI_keepplan(SPIPlanPtr plan);
108 extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan);
109 extern int SPI_freeplan(SPIPlanPtr plan);
110 
111 extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex);
112 extern int SPI_getargcount(SPIPlanPtr plan);
113 extern bool SPI_is_cursor_plan(SPIPlanPtr plan);
114 extern bool SPI_plan_is_valid(SPIPlanPtr plan);
115 extern const char *SPI_result_code_string(int code);
116 
117 extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan);
118 extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan);
119 
120 extern HeapTuple SPI_copytuple(HeapTuple tuple);
122 extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts,
123  int *attnum, Datum *Values, const char *Nulls);
124 extern int SPI_fnumber(TupleDesc tupdesc, const char *fname);
125 extern char *SPI_fname(TupleDesc tupdesc, int fnumber);
126 extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber);
127 extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull);
128 extern char *SPI_gettype(TupleDesc tupdesc, int fnumber);
129 extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber);
130 extern char *SPI_getrelname(Relation rel);
131 extern char *SPI_getnspname(Relation rel);
132 extern void *SPI_palloc(Size size);
133 extern void *SPI_repalloc(void *pointer, Size size);
134 extern void SPI_pfree(void *pointer);
135 extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen);
136 extern void SPI_freetuple(HeapTuple pointer);
137 extern void SPI_freetuptable(SPITupleTable *tuptable);
138 
139 extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan,
140  Datum *Values, const char *Nulls, bool read_only);
141 extern Portal SPI_cursor_open_with_args(const char *name,
142  const char *src,
143  int nargs, Oid *argtypes,
144  Datum *Values, const char *Nulls,
145  bool read_only, int cursorOptions);
146 extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan,
147  ParamListInfo params, bool read_only);
148 extern Portal SPI_cursor_find(const char *name);
149 extern void SPI_cursor_fetch(Portal portal, bool forward, long count);
150 extern void SPI_cursor_move(Portal portal, bool forward, long count);
151 extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count);
152 extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count);
153 extern void SPI_cursor_close(Portal portal);
154 
156 extern int SPI_unregister_relation(const char *name);
157 extern int SPI_register_trigger_data(TriggerData *tdata);
158 
159 extern void AtEOXact_SPI(bool isCommit);
160 extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid);
161 
162 #endif /* SPI_H */
PGDLLIMPORT SPITupleTable * SPI_tuptable
Definition: spi.c:41
char * SPI_fname(TupleDesc tupdesc, int fnumber)
Definition: spi.c:790
bool SPI_is_cursor_plan(SPIPlanPtr plan)
Definition: spi.c:1459
Oid * argtypes
Definition: spi_priv.h:89
const char * SPI_result_code_string(int code)
Definition: spi.c:1521
int SPI_register_relation(EphemeralNamedRelation enr)
Definition: spi.c:2685
int SPI_unregister_relation(const char *name)
Definition: spi.c:2719
Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber)
Definition: spi.c:900
FetchDirection
Definition: parsenodes.h:2671
HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, int *attnum, Datum *Values, const char *Nulls)
Definition: spi.c:696
char * SPI_getrelname(Relation rel)
Definition: spi.c:918
SPIPlanPtr SPI_prepare_params(const char *src, ParserSetupHook parserSetup, void *parserSetupArg, int cursorOptions)
Definition: spi.c:529
void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid)
Definition: spi.c:216
void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count)
Definition: spi.c:1399
int SPI_exec(const char *src, long tcount)
Definition: spi.c:338
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
Definition: spi.c:812
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:978
void * SPI_palloc(Size size)
Definition: spi.c:930
void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count)
Definition: spi.c:1384
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1037
SPIPlanPtr SPI_saveplan(SPIPlanPtr plan)
Definition: spi.c:593
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:1064
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:447
slist_node next
Definition: spi.h:29
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:1598
uint32 SubTransactionId
Definition: c.h:449
unsigned int Oid
Definition: postgres_ext.h:31
HeapTuple * vals
Definition: spi.h:28
#define PGDLLIMPORT
Definition: c.h:1050
SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, int cursorOptions)
Definition: spi.c:494
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:379
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:1116
void SPI_cursor_move(Portal portal, bool forward, long count)
Definition: spi.c:1370
struct SPITupleTable SPITupleTable
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:664
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1355
static struct @121 value
char * SPI_gettype(TupleDesc tupdesc, int fnumber)
Definition: spi.c:860
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:66
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:413
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2752
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:615
int SPI_finish(void)
Definition: spi.c:149
void SPI_pfree(void *pointer)
Definition: spi.c:946
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:310
PGDLLIMPORT Oid SPI_lastoid
Definition: spi.c:40
int SPI_getargcount(SPIPlanPtr plan)
Definition: spi.c:1439
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:1614
uintptr_t Datum
Definition: postgres.h:372
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:953
TupleDesc tupdesc
Definition: spi.h:27
int nargs
Definition: spi_priv.h:88
void SPI_freetuple(HeapTuple pointer)
Definition: spi.c:971
void SPI_cursor_close(Portal portal)
Definition: spi.c:1411
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:767
ParserSetupHook parserSetup
Definition: spi_priv.h:90
Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex)
Definition: spi.c:1424
size_t Size
Definition: c.h:404
void * parserSetupArg
Definition: spi_priv.h:91
const char * name
Definition: encode.c:521
void AtEOXact_SPI(bool isCommit)
Definition: spi.c:188
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:844
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1343
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:637
struct _SPI_plan * SPIPlanPtr
Definition: spi.h:34
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:488
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:372
PGDLLIMPORT int SPI_result
Definition: spi.c:42
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:566
void * SPI_repalloc(void *pointer, Size size)
Definition: spi.c:939
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:345
MemoryContext tuptabcxt
Definition: spi.h:24
char * SPI_getnspname(Relation rel)
Definition: spi.c:924
bool SPI_plan_is_valid(SPIPlanPtr plan)
Definition: spi.c:1497
static bool Nulls[MAXATTR]
Definition: bootstrap.c:165