PostgreSQL Source Code  git master
plpy_cursorobject.c File Reference
#include "postgres.h"
#include <limits.h>
#include "catalog/pg_type.h"
#include "mb/pg_wchar.h"
#include "plpy_cursorobject.h"
#include "plpy_elog.h"
#include "plpy_main.h"
#include "plpy_planobject.h"
#include "plpy_resultobject.h"
#include "plpy_spi.h"
#include "plpython.h"
#include "utils/memutils.h"
Include dependency graph for plpy_cursorobject.c:

Go to the source code of this file.

Functions

static PyObject * PLy_cursor_query (const char *query)
 
static void PLy_cursor_dealloc (PyObject *arg)
 
static PyObject * PLy_cursor_iternext (PyObject *self)
 
static PyObject * PLy_cursor_fetch (PyObject *self, PyObject *args)
 
static PyObject * PLy_cursor_close (PyObject *self, PyObject *unused)
 
void PLy_cursor_init_type (void)
 
PyObject * PLy_cursor (PyObject *self, PyObject *args)
 
PyObject * PLy_cursor_plan (PyObject *ob, PyObject *args)
 

Variables

static const char PLy_cursor_doc [] = "Wrapper around a PostgreSQL cursor"
 
static PyMethodDef PLy_cursor_methods []
 
static PyTypeObject PLy_CursorType
 

Function Documentation

◆ PLy_cursor()

PyObject* PLy_cursor ( PyObject *  self,
PyObject *  args 
)

Definition at line 56 of file plpy_cursorobject.c.

57 {
58  char *query;
59  PyObject *plan;
60  PyObject *planargs = NULL;
61 
62  if (PyArg_ParseTuple(args, "s", &query))
63  return PLy_cursor_query(query);
64 
65  PyErr_Clear();
66 
67  if (PyArg_ParseTuple(args, "O|O", &plan, &planargs))
68  return PLy_cursor_plan(plan, planargs);
69 
70  PLy_exception_set(PLy_exc_error, "plpy.cursor expected a query or a plan");
71  return NULL;
72 }
#define plan(x)
Definition: pg_regress.c:161
PyObject * PLy_cursor_plan(PyObject *ob, PyObject *args)
static PyObject * PLy_cursor_query(const char *query)
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:472

References generate_unaccent_rules::args, plan, PLy_cursor_plan(), PLy_cursor_query(), PLy_exc_error, and PLy_exception_set().

◆ PLy_cursor_close()

static PyObject * PLy_cursor_close ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 469 of file plpy_cursorobject.c.

470 {
472 
473  if (!cursor->closed)
474  {
475  Portal portal = GetPortalByName(cursor->portalname);
476 
477  if (!PortalIsValid(portal))
478  {
479  PLy_exception_set(PyExc_ValueError,
480  "closing a cursor in an aborted subtransaction");
481  return NULL;
482  }
483 
484  UnpinPortal(portal);
485  SPI_cursor_close(portal);
486  cursor->closed = true;
487  }
488 
489  Py_RETURN_NONE;
490 }
#define PortalIsValid(p)
Definition: portal.h:211
void UnpinPortal(Portal portal)
Definition: portalmem.c:380
Portal GetPortalByName(const char *name)
Definition: portalmem.c:130
void SPI_cursor_close(Portal portal)
Definition: spi.c:1862
Definition: type.h:138

References GetPortalByName(), PLy_exception_set(), PortalIsValid, SPI_cursor_close(), and UnpinPortal().

◆ PLy_cursor_dealloc()

static void PLy_cursor_dealloc ( PyObject *  arg)
static

Definition at line 279 of file plpy_cursorobject.c.

280 {
282  Portal portal;
283 
284  cursor = (PLyCursorObject *) arg;
285 
286  if (!cursor->closed)
287  {
288  portal = GetPortalByName(cursor->portalname);
289 
290  if (PortalIsValid(portal))
291  {
292  UnpinPortal(portal);
293  SPI_cursor_close(portal);
294  }
295  cursor->closed = true;
296  }
297  if (cursor->mcxt)
298  {
300  cursor->mcxt = NULL;
301  }
302  arg->ob_type->tp_free(arg);
303 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
void * arg

References arg, GetPortalByName(), MemoryContextDelete(), PortalIsValid, SPI_cursor_close(), and UnpinPortal().

◆ PLy_cursor_fetch()

static PyObject * PLy_cursor_fetch ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 368 of file plpy_cursorobject.c.

369 {
371  int count;
372  PLyResultObject *ret;
374  volatile MemoryContext oldcontext;
375  volatile ResourceOwner oldowner;
376  Portal portal;
377 
378  if (!PyArg_ParseTuple(args, "i:fetch", &count))
379  return NULL;
380 
381  cursor = (PLyCursorObject *) self;
382 
383  if (cursor->closed)
384  {
385  PLy_exception_set(PyExc_ValueError, "fetch from a closed cursor");
386  return NULL;
387  }
388 
389  portal = GetPortalByName(cursor->portalname);
390  if (!PortalIsValid(portal))
391  {
392  PLy_exception_set(PyExc_ValueError,
393  "iterating a cursor in an aborted subtransaction");
394  return NULL;
395  }
396 
397  ret = (PLyResultObject *) PLy_result_new();
398  if (ret == NULL)
399  return NULL;
400 
401  oldcontext = CurrentMemoryContext;
402  oldowner = CurrentResourceOwner;
403 
404  PLy_spi_subtransaction_begin(oldcontext, oldowner);
405 
406  PG_TRY();
407  {
408  SPI_cursor_fetch(portal, true, count);
409 
410  Py_DECREF(ret->status);
411  ret->status = PyLong_FromLong(SPI_OK_FETCH);
412 
413  Py_DECREF(ret->nrows);
414  ret->nrows = PyLong_FromUnsignedLongLong(SPI_processed);
415 
416  if (SPI_processed != 0)
417  {
418  uint64 i;
419 
420  /*
421  * PyList_New() and PyList_SetItem() use Py_ssize_t for list size
422  * and list indices; so we cannot support a result larger than
423  * PY_SSIZE_T_MAX.
424  */
425  if (SPI_processed > (uint64) PY_SSIZE_T_MAX)
426  ereport(ERROR,
427  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
428  errmsg("query result has too many rows to fit in a Python list")));
429 
430  Py_DECREF(ret->rows);
431  ret->rows = PyList_New(SPI_processed);
432  if (!ret->rows)
433  {
434  Py_DECREF(ret);
435  ret = NULL;
436  }
437  else
438  {
440  exec_ctx->curr_proc);
441 
442  for (i = 0; i < SPI_processed; i++)
443  {
444  PyObject *row = PLy_input_from_tuple(&cursor->result,
445  SPI_tuptable->vals[i],
447  true);
448 
449  PyList_SetItem(ret->rows, i, row);
450  }
451  }
452  }
453 
455 
456  PLy_spi_subtransaction_commit(oldcontext, oldowner);
457  }
458  PG_CATCH();
459  {
460  PLy_spi_subtransaction_abort(oldcontext, oldowner);
461  return NULL;
462  }
463  PG_END_TRY();
464 
465  return (PyObject *) ret;
466 }
uint64_t uint64
Definition: c.h:486
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:381
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:72
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:365
PyObject * PLy_result_new(void)
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:582
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:591
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:574
PyObject * PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
Definition: plpy_typeio.c:134
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:165
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
uint64 SPI_processed
Definition: spi.c:44
SPITupleTable * SPI_tuptable
Definition: spi.c:45
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1806
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1386
#define SPI_OK_FETCH
Definition: spi.h:84
PLyProcedure * curr_proc
Definition: plpy_main.h:20
PyObject_HEAD PyObject * nrows
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26

References generate_unaccent_rules::args, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, ereport, errcode(), errmsg(), ERROR, GetPortalByName(), i, PLyResultObject::nrows, PG_CATCH, PG_END_TRY, PG_TRY, PLy_current_execution_context(), PLy_exception_set(), PLy_input_from_tuple(), PLy_input_setup_tuple(), PLy_result_new(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PortalIsValid, PLyResultObject::rows, SPI_cursor_fetch(), SPI_freetuptable(), SPI_OK_FETCH, SPI_processed, SPI_tuptable, PLyResultObject::status, SPITupleTable::tupdesc, and SPITupleTable::vals.

◆ PLy_cursor_init_type()

void PLy_cursor_init_type ( void  )

Definition at line 49 of file plpy_cursorobject.c.

50 {
51  if (PyType_Ready(&PLy_CursorType) < 0)
52  elog(ERROR, "could not initialize PLy_CursorType");
53 }
#define elog(elevel,...)
Definition: elog.h:225
static PyTypeObject PLy_CursorType

References elog, ERROR, and PLy_CursorType.

Referenced by PLy_init_plpy().

◆ PLy_cursor_iternext()

static PyObject * PLy_cursor_iternext ( PyObject *  self)
static

Definition at line 306 of file plpy_cursorobject.c.

307 {
309  PyObject *ret;
311  volatile MemoryContext oldcontext;
312  volatile ResourceOwner oldowner;
313  Portal portal;
314 
315  cursor = (PLyCursorObject *) self;
316 
317  if (cursor->closed)
318  {
319  PLy_exception_set(PyExc_ValueError, "iterating a closed cursor");
320  return NULL;
321  }
322 
323  portal = GetPortalByName(cursor->portalname);
324  if (!PortalIsValid(portal))
325  {
326  PLy_exception_set(PyExc_ValueError,
327  "iterating a cursor in an aborted subtransaction");
328  return NULL;
329  }
330 
331  oldcontext = CurrentMemoryContext;
332  oldowner = CurrentResourceOwner;
333 
334  PLy_spi_subtransaction_begin(oldcontext, oldowner);
335 
336  PG_TRY();
337  {
338  SPI_cursor_fetch(portal, true, 1);
339  if (SPI_processed == 0)
340  {
341  PyErr_SetNone(PyExc_StopIteration);
342  ret = NULL;
343  }
344  else
345  {
347  exec_ctx->curr_proc);
348 
349  ret = PLy_input_from_tuple(&cursor->result, SPI_tuptable->vals[0],
350  SPI_tuptable->tupdesc, true);
351  }
352 
354 
355  PLy_spi_subtransaction_commit(oldcontext, oldowner);
356  }
357  PG_CATCH();
358  {
359  PLy_spi_subtransaction_abort(oldcontext, oldowner);
360  return NULL;
361  }
362  PG_END_TRY();
363 
364  return ret;
365 }

References PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, GetPortalByName(), PG_CATCH, PG_END_TRY, PG_TRY, PLy_current_execution_context(), PLy_exception_set(), PLy_input_from_tuple(), PLy_input_setup_tuple(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PortalIsValid, SPI_cursor_fetch(), SPI_freetuptable(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, and SPITupleTable::vals.

◆ PLy_cursor_plan()

PyObject* PLy_cursor_plan ( PyObject *  ob,
PyObject *  args 
)

Definition at line 139 of file plpy_cursorobject.c.

140 {
142  volatile int nargs;
143  int i;
146  volatile MemoryContext oldcontext;
147  volatile ResourceOwner oldowner;
148 
149  if (args)
150  {
151  if (!PySequence_Check(args) || PyUnicode_Check(args))
152  {
153  PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
154  return NULL;
155  }
156  nargs = PySequence_Length(args);
157  }
158  else
159  nargs = 0;
160 
161  plan = (PLyPlanObject *) ob;
162 
163  if (nargs != plan->nargs)
164  {
165  char *sv;
166  PyObject *so = PyObject_Str(args);
167 
168  if (!so)
169  PLy_elog(ERROR, "could not execute plan");
170  sv = PLyUnicode_AsString(so);
171  PLy_exception_set_plural(PyExc_TypeError,
172  "Expected sequence of %d argument, got %d: %s",
173  "Expected sequence of %d arguments, got %d: %s",
174  plan->nargs,
175  plan->nargs, nargs, sv);
176  Py_DECREF(so);
177 
178  return NULL;
179  }
180 
181  if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
182  return NULL;
183  cursor->portalname = NULL;
184  cursor->closed = false;
186  "PL/Python cursor context",
188 
189  /* Initialize for converting result tuples to Python */
190  PLy_input_setup_func(&cursor->result, cursor->mcxt,
191  RECORDOID, -1,
192  exec_ctx->curr_proc);
193 
194  oldcontext = CurrentMemoryContext;
195  oldowner = CurrentResourceOwner;
196 
197  PLy_spi_subtransaction_begin(oldcontext, oldowner);
198 
199  PG_TRY();
200  {
201  Portal portal;
202  char *volatile nulls;
203  volatile int j;
204 
205  if (nargs > 0)
206  nulls = palloc(nargs * sizeof(char));
207  else
208  nulls = NULL;
209 
210  for (j = 0; j < nargs; j++)
211  {
212  PLyObToDatum *arg = &plan->args[j];
213  PyObject *elem;
214 
215  elem = PySequence_GetItem(args, j);
216  PG_TRY(2);
217  {
218  bool isnull;
219 
220  plan->values[j] = PLy_output_convert(arg, elem, &isnull);
221  nulls[j] = isnull ? 'n' : ' ';
222  }
223  PG_FINALLY(2);
224  {
225  Py_DECREF(elem);
226  }
227  PG_END_TRY(2);
228  }
229 
230  portal = SPI_cursor_open(NULL, plan->plan, plan->values, nulls,
231  exec_ctx->curr_proc->fn_readonly);
232  if (portal == NULL)
233  elog(ERROR, "SPI_cursor_open() failed: %s",
235 
236  cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
237 
238  PinPortal(portal);
239 
240  PLy_spi_subtransaction_commit(oldcontext, oldowner);
241  }
242  PG_CATCH();
243  {
244  int k;
245 
246  /* cleanup plan->values array */
247  for (k = 0; k < nargs; k++)
248  {
249  if (!plan->args[k].typbyval &&
250  (plan->values[k] != PointerGetDatum(NULL)))
251  {
252  pfree(DatumGetPointer(plan->values[k]));
253  plan->values[k] = PointerGetDatum(NULL);
254  }
255  }
256 
257  Py_DECREF(cursor);
258 
259  PLy_spi_subtransaction_abort(oldcontext, oldowner);
260  return NULL;
261  }
262  PG_END_TRY();
263 
264  for (i = 0; i < nargs; i++)
265  {
266  if (!plan->args[i].typbyval &&
267  (plan->values[i] != PointerGetDatum(NULL)))
268  {
269  pfree(DatumGetPointer(plan->values[i]));
270  plan->values[i] = PointerGetDatum(NULL);
271  }
272  }
273 
274  Assert(cursor->portalname != NULL);
275  return (PyObject *) cursor;
276 }
#define Assert(condition)
Definition: c.h:812
#define PG_FINALLY(...)
Definition: elog.h:388
int j
Definition: isn.c:73
#define PLy_elog
void pfree(void *pointer)
Definition: mcxt.c:1521
MemoryContext TopMemoryContext
Definition: mcxt.c:149
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
void * palloc(Size size)
Definition: mcxt.c:1317
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
void PLy_exception_set_plural(PyObject *exc, const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: plpy_elog.c:486
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:418
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:120
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:82
void PinPortal(Portal portal)
Definition: portalmem.c:371
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
int SPI_result
Definition: spi.c:46
const char * SPI_result_code_string(int code)
Definition: spi.c:1972
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1445
const char * name
Definition: portal.h:118

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, arg, generate_unaccent_rules::args, Assert, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, DatumGetPointer(), elog, ERROR, PLyProcedure::fn_readonly, i, j, MemoryContextStrdup(), PortalData::name, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_FINALLY, PG_TRY, PinPortal(), plan, PLy_current_execution_context(), PLy_CursorType, PLy_elog, PLy_exception_set(), PLy_exception_set_plural(), PLy_input_setup_func(), PLy_output_convert(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), PLyUnicode_AsString(), PointerGetDatum(), SPI_cursor_open(), SPI_result, SPI_result_code_string(), and TopMemoryContext.

Referenced by PLy_cursor(), and PLy_plan_cursor().

◆ PLy_cursor_query()

static PyObject * PLy_cursor_query ( const char *  query)
static

Definition at line 76 of file plpy_cursorobject.c.

77 {
80  volatile MemoryContext oldcontext;
81  volatile ResourceOwner oldowner;
82 
83  if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
84  return NULL;
85  cursor->portalname = NULL;
86  cursor->closed = false;
88  "PL/Python cursor context",
90 
91  /* Initialize for converting result tuples to Python */
92  PLy_input_setup_func(&cursor->result, cursor->mcxt,
93  RECORDOID, -1,
94  exec_ctx->curr_proc);
95 
96  oldcontext = CurrentMemoryContext;
97  oldowner = CurrentResourceOwner;
98 
99  PLy_spi_subtransaction_begin(oldcontext, oldowner);
100 
101  PG_TRY();
102  {
104  Portal portal;
105 
106  pg_verifymbstr(query, strlen(query), false);
107 
108  plan = SPI_prepare(query, 0, NULL);
109  if (plan == NULL)
110  elog(ERROR, "SPI_prepare failed: %s",
112 
113  portal = SPI_cursor_open(NULL, plan, NULL, NULL,
114  exec_ctx->curr_proc->fn_readonly);
116 
117  if (portal == NULL)
118  elog(ERROR, "SPI_cursor_open() failed: %s",
120 
121  cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
122 
123  PinPortal(portal);
124 
125  PLy_spi_subtransaction_commit(oldcontext, oldowner);
126  }
127  PG_CATCH();
128  {
129  PLy_spi_subtransaction_abort(oldcontext, oldowner);
130  return NULL;
131  }
132  PG_END_TRY();
133 
134  Assert(cursor->portalname != NULL);
135  return (PyObject *) cursor;
136 }
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1556
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:1025
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:860

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, PLyExecutionContext::curr_proc, CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, PLyProcedure::fn_readonly, MemoryContextStrdup(), PortalData::name, PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), PinPortal(), plan, PLy_current_execution_context(), PLy_CursorType, PLy_input_setup_func(), PLy_spi_subtransaction_abort(), PLy_spi_subtransaction_begin(), PLy_spi_subtransaction_commit(), SPI_cursor_open(), SPI_freeplan(), SPI_prepare(), SPI_result, SPI_result_code_string(), and TopMemoryContext.

Referenced by PLy_cursor().

Variable Documentation

◆ PLy_cursor_doc

const char PLy_cursor_doc[] = "Wrapper around a PostgreSQL cursor"
static

Definition at line 28 of file plpy_cursorobject.c.

◆ PLy_cursor_methods

PyMethodDef PLy_cursor_methods[]
static
Initial value:
= {
{"fetch", PLy_cursor_fetch, METH_VARARGS, NULL},
{"close", PLy_cursor_close, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
}
static PyObject * PLy_cursor_fetch(PyObject *self, PyObject *args)
static PyObject * PLy_cursor_close(PyObject *self, PyObject *unused)

Definition at line 30 of file plpy_cursorobject.c.

◆ PLy_CursorType

PyTypeObject PLy_CursorType
static
Initial value:
= {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "PLyCursor",
.tp_basicsize = sizeof(PLyCursorObject),
.tp_dealloc = PLy_cursor_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = PLy_cursor_doc,
.tp_iter = PyObject_SelfIter,
.tp_iternext = PLy_cursor_iternext,
.tp_methods = PLy_cursor_methods,
}
static PyMethodDef PLy_cursor_methods[]
static PyObject * PLy_cursor_iternext(PyObject *self)
static void PLy_cursor_dealloc(PyObject *arg)
static const char PLy_cursor_doc[]
struct PLyCursorObject PLyCursorObject

Definition at line 36 of file plpy_cursorobject.c.

Referenced by PLy_cursor_init_type(), PLy_cursor_plan(), and PLy_cursor_query().