PostgreSQL Source Code  git master
plpy_spi.h File Reference
#include "plpython.h"
#include "utils/resowner.h"
Include dependency graph for plpy_spi.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PLyExceptionEntry
 

Typedefs

typedef struct PLyExceptionEntry PLyExceptionEntry
 

Functions

PyObject * PLy_spi_prepare (PyObject *self, PyObject *args)
 
PyObject * PLy_spi_execute (PyObject *self, PyObject *args)
 
PyObject * PLy_spi_execute_plan (PyObject *ob, PyObject *list, long limit)
 
void PLy_spi_subtransaction_begin (MemoryContext oldcontext, ResourceOwner oldowner)
 
void PLy_spi_subtransaction_commit (MemoryContext oldcontext, ResourceOwner oldowner)
 
void PLy_spi_subtransaction_abort (MemoryContext oldcontext, ResourceOwner oldowner)
 

Typedef Documentation

◆ PLyExceptionEntry

Function Documentation

◆ PLy_spi_execute()

PyObject* PLy_spi_execute ( PyObject *  self,
PyObject *  args 
)

Definition at line 156 of file plpy_spi.c.

References is_PLyPlanObject(), sort-test::list, PLy_exc_error, PLy_exception_set(), PLy_spi_execute_plan(), and PLy_spi_execute_query().

157 {
158  char *query;
159  PyObject *plan;
160  PyObject *list = NULL;
161  long limit = 0;
162 
163  if (PyArg_ParseTuple(args, "s|l", &query, &limit))
164  return PLy_spi_execute_query(query, limit);
165 
166  PyErr_Clear();
167 
168  if (PyArg_ParseTuple(args, "O|Ol", &plan, &list, &limit) &&
169  is_PLyPlanObject(plan))
170  return PLy_spi_execute_plan(plan, list, limit);
171 
172  PLy_exception_set(PLy_exc_error, "plpy.execute expected a query or a plan");
173  return NULL;
174 }
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:479
bool is_PLyPlanObject(PyObject *ob)
PyObject * PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
Definition: plpy_spi.c:177
static PyObject * PLy_spi_execute_query(char *query, long limit)
Definition: plpy_spi.c:308

◆ PLy_spi_execute_plan()

PyObject* PLy_spi_execute_plan ( PyObject *  ob,
PyObject *  list,
long  limit 
)

Definition at line 177 of file plpy_spi.c.

References arg, PLyPlanObject::args, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, DatumGetPointer, ERROR, PLyProcedure::fn_readonly, i, PLyPlanObject::nargs, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_FINALLY, PG_TRY, PLyPlanObject::plan, PLy_current_execution_context(), PLy_elog, PLy_exc_spi_error, PLy_exception_set(), PLy_exception_set_plural(), PLy_output_convert(), PLy_spi_execute_fetch_result(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PointerGetDatum, SPI_execute_plan(), SPI_processed, SPI_result_code_string(), SPI_tuptable, PLyObToDatum::typbyval, and PLyPlanObject::values.

Referenced by PLy_plan_execute(), and PLy_spi_execute().

178 {
179  volatile int nargs;
180  int i,
181  rv;
182  PLyPlanObject *plan;
183  volatile MemoryContext oldcontext;
184  volatile ResourceOwner oldowner;
185  PyObject *ret;
186 
187  if (list != NULL)
188  {
189  if (!PySequence_Check(list) || PyString_Check(list) || PyUnicode_Check(list))
190  {
191  PLy_exception_set(PyExc_TypeError, "plpy.execute takes a sequence as its second argument");
192  return NULL;
193  }
194  nargs = PySequence_Length(list);
195  }
196  else
197  nargs = 0;
198 
199  plan = (PLyPlanObject *) ob;
200 
201  if (nargs != plan->nargs)
202  {
203  char *sv;
204  PyObject *so = PyObject_Str(list);
205 
206  if (!so)
207  PLy_elog(ERROR, "could not execute plan");
208  sv = PyString_AsString(so);
209  PLy_exception_set_plural(PyExc_TypeError,
210  "Expected sequence of %d argument, got %d: %s",
211  "Expected sequence of %d arguments, got %d: %s",
212  plan->nargs,
213  plan->nargs, nargs, sv);
214  Py_DECREF(so);
215 
216  return NULL;
217  }
218 
219  oldcontext = CurrentMemoryContext;
220  oldowner = CurrentResourceOwner;
221 
222  PLy_spi_subtransaction_begin(oldcontext, oldowner);
223 
224  PG_TRY();
225  {
227  char *volatile nulls;
228  volatile int j;
229 
230  if (nargs > 0)
231  nulls = palloc(nargs * sizeof(char));
232  else
233  nulls = NULL;
234 
235  for (j = 0; j < nargs; j++)
236  {
237  PLyObToDatum *arg = &plan->args[j];
238  PyObject *elem;
239 
240  elem = PySequence_GetItem(list, j);
241  PG_TRY();
242  {
243  bool isnull;
244 
245  plan->values[j] = PLy_output_convert(arg, elem, &isnull);
246  nulls[j] = isnull ? 'n' : ' ';
247  }
248  PG_FINALLY();
249  {
250  Py_DECREF(elem);
251  }
252  PG_END_TRY();
253  }
254 
255  rv = SPI_execute_plan(plan->plan, plan->values, nulls,
256  exec_ctx->curr_proc->fn_readonly, limit);
258 
259  if (nargs > 0)
260  pfree(nulls);
261 
262  PLy_spi_subtransaction_commit(oldcontext, oldowner);
263  }
264  PG_CATCH();
265  {
266  int k;
267 
268  /*
269  * cleanup plan->values array
270  */
271  for (k = 0; k < nargs; k++)
272  {
273  if (!plan->args[k].typbyval &&
274  (plan->values[k] != PointerGetDatum(NULL)))
275  {
276  pfree(DatumGetPointer(plan->values[k]));
277  plan->values[k] = PointerGetDatum(NULL);
278  }
279  }
280 
281  PLy_spi_subtransaction_abort(oldcontext, oldowner);
282  return NULL;
283  }
284  PG_END_TRY();
285 
286  for (i = 0; i < nargs; i++)
287  {
288  if (!plan->args[i].typbyval &&
289  (plan->values[i] != PointerGetDatum(NULL)))
290  {
291  pfree(DatumGetPointer(plan->values[i]));
292  plan->values[i] = PointerGetDatum(NULL);
293  }
294  }
295 
296  if (rv < 0)
297  {
299  "SPI_execute_plan failed: %s",
301  return NULL;
302  }
303 
304  return ret;
305 }
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: plpy_elog.c:493
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:119
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:502
#define PointerGetDatum(X)
Definition: postgres.h:556
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
SPITupleTable * SPI_tuptable
Definition: spi.c:46
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:408
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:479
uint64 SPI_processed
Definition: spi.c:45
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:485
void pfree(void *pointer)
Definition: mcxt.c:1056
#define ERROR
Definition: elog.h:43
#define PLy_elog
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:532
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
PLyObToDatum * args
PyObject_HEAD SPIPlanPtr plan
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
PLyProcedure * curr_proc
Definition: plpy_main.h:20
static PyObject * PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
Definition: plpy_spi.c:350
#define PG_FINALLY()
Definition: elog.h:312
#define PG_CATCH()
Definition: elog.h:305
#define DatumGetPointer(X)
Definition: postgres.h:549
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:493
void * palloc(Size size)
Definition: mcxt.c:949
int i
void * arg
#define PG_TRY()
Definition: elog.h:295
PyObject * PLy_exc_spi_error
Definition: plpy_elog.c:17
#define PG_END_TRY()
Definition: elog.h:320

◆ PLy_spi_prepare()

PyObject* PLy_spi_prepare ( PyObject *  self,
PyObject *  args 
)

Definition at line 39 of file plpy_spi.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, PLyPlanObject::args, Assert, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, elog, ereport, errmsg(), ERROR, i, sort-test::list, PLyPlanObject::mcxt, MemoryContextSwitchTo(), PLyPlanObject::nargs, palloc0(), parseTypeString(), PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), PLyPlanObject::plan, PLy_current_execution_context(), PLy_exception_set(), PLy_output_setup_func(), PLy_plan_new(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PLyUnicode_AsString(), SPI_keepplan(), SPI_prepare(), SPI_result, SPI_result_code_string(), TopMemoryContext, PLyPlanObject::types, and PLyPlanObject::values.

40 {
41  PLyPlanObject *plan;
42  PyObject *list = NULL;
43  PyObject *volatile optr = NULL;
44  char *query;
46  volatile MemoryContext oldcontext;
47  volatile ResourceOwner oldowner;
48  volatile int nargs;
49 
50  if (!PyArg_ParseTuple(args, "s|O:prepare", &query, &list))
51  return NULL;
52 
53  if (list && (!PySequence_Check(list)))
54  {
55  PLy_exception_set(PyExc_TypeError,
56  "second argument of plpy.prepare must be a sequence");
57  return NULL;
58  }
59 
60  if ((plan = (PLyPlanObject *) PLy_plan_new()) == NULL)
61  return NULL;
62 
64  "PL/Python plan context",
66  oldcontext = MemoryContextSwitchTo(plan->mcxt);
67 
68  nargs = list ? PySequence_Length(list) : 0;
69 
70  plan->nargs = nargs;
71  plan->types = nargs ? palloc0(sizeof(Oid) * nargs) : NULL;
72  plan->values = nargs ? palloc0(sizeof(Datum) * nargs) : NULL;
73  plan->args = nargs ? palloc0(sizeof(PLyObToDatum) * nargs) : NULL;
74 
75  MemoryContextSwitchTo(oldcontext);
76 
77  oldcontext = CurrentMemoryContext;
78  oldowner = CurrentResourceOwner;
79 
80  PLy_spi_subtransaction_begin(oldcontext, oldowner);
81 
82  PG_TRY();
83  {
84  int i;
85 
86  for (i = 0; i < nargs; i++)
87  {
88  char *sptr;
89  Oid typeId;
90  int32 typmod;
91 
92  optr = PySequence_GetItem(list, i);
93  if (PyString_Check(optr))
94  sptr = PyString_AsString(optr);
95  else if (PyUnicode_Check(optr))
96  sptr = PLyUnicode_AsString(optr);
97  else
98  {
99  ereport(ERROR,
100  (errmsg("plpy.prepare: type name at ordinal position %d is not a string", i)));
101  sptr = NULL; /* keep compiler quiet */
102  }
103 
104  /********************************************************
105  * Resolve argument type names and then look them up by
106  * oid in the system cache, and remember the required
107  *information for input conversion.
108  ********************************************************/
109 
110  parseTypeString(sptr, &typeId, &typmod, false);
111 
112  Py_DECREF(optr);
113 
114  /*
115  * set optr to NULL, so we won't try to unref it again in case of
116  * an error
117  */
118  optr = NULL;
119 
120  plan->types[i] = typeId;
121  PLy_output_setup_func(&plan->args[i], plan->mcxt,
122  typeId, typmod,
123  exec_ctx->curr_proc);
124  }
125 
126  pg_verifymbstr(query, strlen(query), false);
127  plan->plan = SPI_prepare(query, plan->nargs, plan->types);
128  if (plan->plan == NULL)
129  elog(ERROR, "SPI_prepare failed: %s",
131 
132  /* transfer plan from procCxt to topCxt */
133  if (SPI_keepplan(plan->plan))
134  elog(ERROR, "SPI_keepplan failed");
135 
136  PLy_spi_subtransaction_commit(oldcontext, oldowner);
137  }
138  PG_CATCH();
139  {
140  Py_DECREF(plan);
141  Py_XDECREF(optr);
142 
143  PLy_spi_subtransaction_abort(oldcontext, oldowner);
144  return NULL;
145  }
146  PG_END_TRY();
147 
148  Assert(plan->plan != NULL);
149  return (PyObject *) plan;
150 }
#define AllocSetContextCreate
Definition: memutils.h:170
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:502
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:747
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
unsigned int Oid
Definition: postgres_ext.h:31
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:408
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:479
signed int int32
Definition: c.h:362
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:89
int SPI_result
Definition: spi.c:47
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:485
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:825
PLyObToDatum * args
PyObject_HEAD SPIPlanPtr plan
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
MemoryContext TopMemoryContext
Definition: mcxt.c:44
PLyProcedure * curr_proc
Definition: plpy_main.h:20
MemoryContext mcxt
void * palloc0(Size size)
Definition: mcxt.c:980
PyObject * PLy_plan_new(void)
uintptr_t Datum
Definition: postgres.h:367
void parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:832
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1446
#define ereport(elevel,...)
Definition: elog.h:144
#define PG_CATCH()
Definition: elog.h:305
#define Assert(condition)
Definition: c.h:745
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:295
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:493
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
#define PG_TRY()
Definition: elog.h:295
#define PG_END_TRY()
Definition: elog.h:320

◆ PLy_spi_subtransaction_abort()

void PLy_spi_subtransaction_abort ( MemoryContext  oldcontext,
ResourceOwner  oldowner 
)

Definition at line 502 of file plpy_spi.c.

References CopyErrorData(), CurrentResourceOwner, PLyExceptionEntry::exc, FlushErrorState(), FreeErrorData(), HASH_FIND, hash_search(), MemoryContextSwitchTo(), PLy_exc_spi_error, PLy_spi_exception_set(), PLy_spi_exceptions, RollbackAndReleaseCurrentSubTransaction(), and ErrorData::sqlerrcode.

Referenced by PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), and PLy_spi_prepare().

503 {
504  ErrorData *edata;
505  PLyExceptionEntry *entry;
506  PyObject *exc;
507 
508  /* Save error info */
509  MemoryContextSwitchTo(oldcontext);
510  edata = CopyErrorData();
511  FlushErrorState();
512 
513  /* Abort the inner transaction */
515  MemoryContextSwitchTo(oldcontext);
516  CurrentResourceOwner = oldowner;
517 
518  /* Look up the correct exception */
519  entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
520  HASH_FIND, NULL);
521 
522  /*
523  * This could be a custom error code, if that's the case fallback to
524  * SPIError
525  */
526  exc = entry ? entry->exc : PLy_exc_spi_error;
527  /* Make Python raise the exception */
528  PLy_spi_exception_set(exc, edata);
529  FreeErrorData(edata);
530 }
static void PLy_spi_exception_set(PyObject *excclass, ErrorData *edata)
Definition: plpy_spi.c:537
int sqlerrcode
Definition: elog.h:364
ErrorData * CopyErrorData(void)
Definition: elog.c:1474
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:927
void FlushErrorState(void)
Definition: elog.c:1568
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1530
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4513
HTAB * PLy_spi_exceptions
PyObject * exc
Definition: plpy_spi.h:18
PyObject * PLy_exc_spi_error
Definition: plpy_elog.c:17

◆ PLy_spi_subtransaction_begin()

void PLy_spi_subtransaction_begin ( MemoryContext  oldcontext,
ResourceOwner  oldowner 
)

Definition at line 485 of file plpy_spi.c.

References BeginInternalSubTransaction(), and MemoryContextSwitchTo().

Referenced by PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), and PLy_spi_prepare().

486 {
488  /* Want to run inside function's memory context */
489  MemoryContextSwitchTo(oldcontext);
490 }
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4408

◆ PLy_spi_subtransaction_commit()

void PLy_spi_subtransaction_commit ( MemoryContext  oldcontext,
ResourceOwner  oldowner 
)

Definition at line 493 of file plpy_spi.c.

References CurrentResourceOwner, MemoryContextSwitchTo(), and ReleaseCurrentSubTransaction().

Referenced by PLy_cursor_fetch(), PLy_cursor_iternext(), PLy_cursor_plan(), PLy_cursor_query(), PLy_spi_execute_plan(), PLy_spi_execute_query(), and PLy_spi_prepare().

494 {
495  /* Commit the inner transaction, return to outer xact context */
497  MemoryContextSwitchTo(oldcontext);
498  CurrentResourceOwner = oldowner;
499 }
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4479
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109