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 | 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 */ |
31 | } SPITupleTable; |
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; |
78 | extern PGDLLIMPORT SPITupleTable *SPI_tuptable; |
79 | extern PGDLLIMPORT int SPI_result; |
80 | |
81 | extern int SPI_connect(void); |
82 | extern int SPI_connect_ext(int options); |
83 | extern int SPI_finish(void); |
84 | extern int SPI_execute(const char *src, bool read_only, long tcount); |
85 | extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, |
86 | bool read_only, long tcount); |
87 | extern int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, |
88 | ParamListInfo params, |
89 | bool read_only, long tcount); |
90 | extern int SPI_exec(const char *src, long tcount); |
91 | extern int SPI_execp(SPIPlanPtr plan, Datum *Values, const char *Nulls, |
92 | long tcount); |
93 | extern int SPI_execute_snapshot(SPIPlanPtr plan, |
94 | Datum *Values, const char *Nulls, |
95 | Snapshot snapshot, |
96 | Snapshot crosscheck_snapshot, |
97 | bool read_only, bool fire_triggers, long tcount); |
98 | extern int SPI_execute_with_args(const char *src, |
99 | int nargs, Oid *argtypes, |
100 | Datum *Values, const char *Nulls, |
101 | bool read_only, long tcount); |
102 | extern SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes); |
103 | extern SPIPlanPtr SPI_prepare_cursor(const char *src, int nargs, Oid *argtypes, |
104 | int cursorOptions); |
105 | extern SPIPlanPtr SPI_prepare_params(const char *src, |
106 | ParserSetupHook parserSetup, |
107 | void *parserSetupArg, |
108 | int cursorOptions); |
109 | extern int SPI_keepplan(SPIPlanPtr plan); |
110 | extern SPIPlanPtr SPI_saveplan(SPIPlanPtr plan); |
111 | extern int SPI_freeplan(SPIPlanPtr plan); |
112 | |
113 | extern Oid SPI_getargtypeid(SPIPlanPtr plan, int argIndex); |
114 | extern int SPI_getargcount(SPIPlanPtr plan); |
115 | extern bool SPI_is_cursor_plan(SPIPlanPtr plan); |
116 | extern bool SPI_plan_is_valid(SPIPlanPtr plan); |
117 | extern const char *SPI_result_code_string(int code); |
118 | |
119 | extern List *SPI_plan_get_plan_sources(SPIPlanPtr plan); |
120 | extern CachedPlan *SPI_plan_get_cached_plan(SPIPlanPtr plan); |
121 | |
122 | extern HeapTuple SPI_copytuple(HeapTuple tuple); |
123 | extern HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc); |
124 | extern HeapTuple SPI_modifytuple(Relation rel, HeapTuple tuple, int natts, |
125 | int *attnum, Datum *Values, const char *Nulls); |
126 | extern int SPI_fnumber(TupleDesc tupdesc, const char *fname); |
127 | extern char *SPI_fname(TupleDesc tupdesc, int fnumber); |
128 | extern char *SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber); |
129 | extern Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull); |
130 | extern char *SPI_gettype(TupleDesc tupdesc, int fnumber); |
131 | extern Oid SPI_gettypeid(TupleDesc tupdesc, int fnumber); |
132 | extern char *SPI_getrelname(Relation rel); |
133 | extern char *SPI_getnspname(Relation rel); |
134 | extern void *SPI_palloc(Size size); |
135 | extern void *SPI_repalloc(void *pointer, Size size); |
136 | extern void SPI_pfree(void *pointer); |
137 | extern Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen); |
138 | extern void SPI_freetuple(HeapTuple pointer); |
139 | extern void SPI_freetuptable(SPITupleTable *tuptable); |
140 | |
141 | extern Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, |
142 | Datum *Values, const char *Nulls, bool read_only); |
143 | extern Portal SPI_cursor_open_with_args(const char *name, |
144 | const char *src, |
145 | int nargs, Oid *argtypes, |
146 | Datum *Values, const char *Nulls, |
147 | bool read_only, int cursorOptions); |
148 | extern Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan, |
149 | ParamListInfo params, bool read_only); |
150 | extern Portal SPI_cursor_find(const char *name); |
151 | extern void SPI_cursor_fetch(Portal portal, bool forward, long count); |
152 | extern void SPI_cursor_move(Portal portal, bool forward, long count); |
153 | extern void SPI_scroll_cursor_fetch(Portal, FetchDirection direction, long count); |
154 | extern void SPI_scroll_cursor_move(Portal, FetchDirection direction, long count); |
155 | extern void SPI_cursor_close(Portal portal); |
156 | |
157 | extern int SPI_register_relation(EphemeralNamedRelation enr); |
158 | extern int SPI_unregister_relation(const char *name); |
159 | extern int SPI_register_trigger_data(TriggerData *tdata); |
160 | |
161 | extern void SPI_start_transaction(void); |
162 | extern void SPI_commit(void); |
163 | extern void SPI_commit_and_chain(void); |
164 | extern void SPI_rollback(void); |
165 | extern void SPI_rollback_and_chain(void); |
166 | |
167 | extern void SPICleanup(void); |
168 | extern void AtEOXact_SPI(bool isCommit); |
169 | extern void AtEOSubXact_SPI(bool isCommit, SubTransactionId mySubid); |
170 | extern bool SPI_inside_nonatomic_context(void); |
171 | |
172 | #endif /* SPI_H */ |
173 | |