PostgreSQL Source Code  git master
plpy_cursorobject.c File Reference
#include "postgres.h"
#include <limits.h>
#include "access/xact.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_procedure.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 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 58 of file plpy_cursorobject.c.

59 {
60  char *query;
61  PyObject *plan;
62  PyObject *planargs = NULL;
63 
64  if (PyArg_ParseTuple(args, "s", &query))
65  return PLy_cursor_query(query);
66 
67  PyErr_Clear();
68 
69  if (PyArg_ParseTuple(args, "O|O", &plan, &planargs))
70  return PLy_cursor_plan(plan, planargs);
71 
72  PLy_exception_set(PLy_exc_error, "plpy.cursor expected a query or a plan");
73  return NULL;
74 }
#define plan(x)
Definition: pg_regress.c:162
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 471 of file plpy_cursorobject.c.

472 {
474 
475  if (!cursor->closed)
476  {
477  Portal portal = GetPortalByName(cursor->portalname);
478 
479  if (!PortalIsValid(portal))
480  {
481  PLy_exception_set(PyExc_ValueError,
482  "closing a cursor in an aborted subtransaction");
483  return NULL;
484  }
485 
486  UnpinPortal(portal);
487  SPI_cursor_close(portal);
488  cursor->closed = true;
489  }
490 
491  Py_RETURN_NONE;
492 }
#define PortalIsValid(p)
Definition: portal.h:212
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:1859
Definition: type.h:137

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 281 of file plpy_cursorobject.c.

282 {
284  Portal portal;
285 
286  cursor = (PLyCursorObject *) arg;
287 
288  if (!cursor->closed)
289  {
290  portal = GetPortalByName(cursor->portalname);
291 
292  if (PortalIsValid(portal))
293  {
294  UnpinPortal(portal);
295  SPI_cursor_close(portal);
296  }
297  cursor->closed = true;
298  }
299  if (cursor->mcxt)
300  {
302  cursor->mcxt = NULL;
303  }
304  arg->ob_type->tp_free(arg);
305 }
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 370 of file plpy_cursorobject.c.

371 {
373  int count;
374  PLyResultObject *ret;
376  volatile MemoryContext oldcontext;
377  volatile ResourceOwner oldowner;
378  Portal portal;
379 
380  if (!PyArg_ParseTuple(args, "i:fetch", &count))
381  return NULL;
382 
383  cursor = (PLyCursorObject *) self;
384 
385  if (cursor->closed)
386  {
387  PLy_exception_set(PyExc_ValueError, "fetch from a closed cursor");
388  return NULL;
389  }
390 
391  portal = GetPortalByName(cursor->portalname);
392  if (!PortalIsValid(portal))
393  {
394  PLy_exception_set(PyExc_ValueError,
395  "iterating a cursor in an aborted subtransaction");
396  return NULL;
397  }
398 
399  ret = (PLyResultObject *) PLy_result_new();
400  if (ret == NULL)
401  return NULL;
402 
403  oldcontext = CurrentMemoryContext;
404  oldowner = CurrentResourceOwner;
405 
406  PLy_spi_subtransaction_begin(oldcontext, oldowner);
407 
408  PG_TRY();
409  {
410  SPI_cursor_fetch(portal, true, count);
411 
412  Py_DECREF(ret->status);
413  ret->status = PyLong_FromLong(SPI_OK_FETCH);
414 
415  Py_DECREF(ret->nrows);
416  ret->nrows = PyLong_FromUnsignedLongLong(SPI_processed);
417 
418  if (SPI_processed != 0)
419  {
420  uint64 i;
421 
422  /*
423  * PyList_New() and PyList_SetItem() use Py_ssize_t for list size
424  * and list indices; so we cannot support a result larger than
425  * PY_SSIZE_T_MAX.
426  */
427  if (SPI_processed > (uint64) PY_SSIZE_T_MAX)
428  ereport(ERROR,
429  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
430  errmsg("query result has too many rows to fit in a Python list")));
431 
432  Py_DECREF(ret->rows);
433  ret->rows = PyList_New(SPI_processed);
434  if (!ret->rows)
435  {
436  Py_DECREF(ret);
437  ret = NULL;
438  }
439  else
440  {
442  exec_ctx->curr_proc);
443 
444  for (i = 0; i < SPI_processed; i++)
445  {
446  PyObject *row = PLy_input_from_tuple(&cursor->result,
447  SPI_tuptable->vals[i],
449  true);
450 
451  PyList_SetItem(ret->rows, i, row);
452  }
453  }
454  }
455 
457 
458  PLy_spi_subtransaction_commit(oldcontext, oldowner);
459  }
460  PG_CATCH();
461  {
462  PLy_spi_subtransaction_abort(oldcontext, oldowner);
463  return NULL;
464  }
465  PG_END_TRY();
466 
467  return (PyObject *) ret;
468 }
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:380
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:73
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:367
PyObject * PLy_result_new(void)
void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:585
void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:594
void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
Definition: plpy_spi.c:577
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:1803
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1383
#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 51 of file plpy_cursorobject.c.

52 {
53  if (PyType_Ready(&PLy_CursorType) < 0)
54  elog(ERROR, "could not initialize PLy_CursorType");
55 }
#define elog(elevel,...)
Definition: elog.h:224
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 308 of file plpy_cursorobject.c.

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

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 141 of file plpy_cursorobject.c.

142 {
144  volatile int nargs;
145  int i;
148  volatile MemoryContext oldcontext;
149  volatile ResourceOwner oldowner;
150 
151  if (args)
152  {
153  if (!PySequence_Check(args) || PyUnicode_Check(args))
154  {
155  PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
156  return NULL;
157  }
158  nargs = PySequence_Length(args);
159  }
160  else
161  nargs = 0;
162 
163  plan = (PLyPlanObject *) ob;
164 
165  if (nargs != plan->nargs)
166  {
167  char *sv;
168  PyObject *so = PyObject_Str(args);
169 
170  if (!so)
171  PLy_elog(ERROR, "could not execute plan");
172  sv = PLyUnicode_AsString(so);
173  PLy_exception_set_plural(PyExc_TypeError,
174  "Expected sequence of %d argument, got %d: %s",
175  "Expected sequence of %d arguments, got %d: %s",
176  plan->nargs,
177  plan->nargs, nargs, sv);
178  Py_DECREF(so);
179 
180  return NULL;
181  }
182 
183  if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
184  return NULL;
185  cursor->portalname = NULL;
186  cursor->closed = false;
188  "PL/Python cursor context",
190 
191  /* Initialize for converting result tuples to Python */
192  PLy_input_setup_func(&cursor->result, cursor->mcxt,
193  RECORDOID, -1,
194  exec_ctx->curr_proc);
195 
196  oldcontext = CurrentMemoryContext;
197  oldowner = CurrentResourceOwner;
198 
199  PLy_spi_subtransaction_begin(oldcontext, oldowner);
200 
201  PG_TRY();
202  {
203  Portal portal;
204  char *volatile nulls;
205  volatile int j;
206 
207  if (nargs > 0)
208  nulls = palloc(nargs * sizeof(char));
209  else
210  nulls = NULL;
211 
212  for (j = 0; j < nargs; j++)
213  {
214  PLyObToDatum *arg = &plan->args[j];
215  PyObject *elem;
216 
217  elem = PySequence_GetItem(args, j);
218  PG_TRY(2);
219  {
220  bool isnull;
221 
222  plan->values[j] = PLy_output_convert(arg, elem, &isnull);
223  nulls[j] = isnull ? 'n' : ' ';
224  }
225  PG_FINALLY(2);
226  {
227  Py_DECREF(elem);
228  }
229  PG_END_TRY(2);
230  }
231 
232  portal = SPI_cursor_open(NULL, plan->plan, plan->values, nulls,
233  exec_ctx->curr_proc->fn_readonly);
234  if (portal == NULL)
235  elog(ERROR, "SPI_cursor_open() failed: %s",
237 
238  cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
239 
240  PinPortal(portal);
241 
242  PLy_spi_subtransaction_commit(oldcontext, oldowner);
243  }
244  PG_CATCH();
245  {
246  int k;
247 
248  /* cleanup plan->values array */
249  for (k = 0; k < nargs; k++)
250  {
251  if (!plan->args[k].typbyval &&
252  (plan->values[k] != PointerGetDatum(NULL)))
253  {
254  pfree(DatumGetPointer(plan->values[k]));
255  plan->values[k] = PointerGetDatum(NULL);
256  }
257  }
258 
259  Py_DECREF(cursor);
260 
261  PLy_spi_subtransaction_abort(oldcontext, oldowner);
262  return NULL;
263  }
264  PG_END_TRY();
265 
266  for (i = 0; i < nargs; i++)
267  {
268  if (!plan->args[i].typbyval &&
269  (plan->values[i] != PointerGetDatum(NULL)))
270  {
271  pfree(DatumGetPointer(plan->values[i]));
272  plan->values[i] = PointerGetDatum(NULL);
273  }
274  }
275 
276  Assert(cursor->portalname != NULL);
277  return (PyObject *) cursor;
278 }
#define Assert(condition)
Definition: c.h:858
#define PG_FINALLY(...)
Definition: elog.h:387
int j
Definition: isn.c:74
#define PLy_elog
void pfree(void *pointer)
Definition: mcxt.c:1520
MemoryContext TopMemoryContext
Definition: mcxt.c:149
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1682
void * palloc(Size size)
Definition: mcxt.c:1316
#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:83
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:1969
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1442
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 78 of file plpy_cursorobject.c.

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

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

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

Definition at line 30 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 32 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 char PLy_cursor_doc[]
struct PLyCursorObject PLyCursorObject

Definition at line 38 of file plpy_cursorobject.c.

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