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