2011-12-18 21:14:16 +02:00
|
|
|
/*
|
|
|
|
* the PLyCursor class
|
|
|
|
*
|
|
|
|
* src/pl/plpython/plpy_cursorobject.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
Widen query numbers-of-tuples-processed counters to uint64.
This patch widens SPI_processed, EState's es_processed field, PortalData's
portalPos field, FuncCallContext's call_cntr and max_calls fields,
ExecutorRun's count argument, PortalRunFetch's result, and the max number
of rows in a SPITupleTable to uint64, and deals with (I hope) all the
ensuing fallout. Some of these values were declared uint32 before, and
others "long".
I also removed PortalData's posOverflow field, since that logic seems
pretty useless given that portalPos is now always 64 bits.
The user-visible results are that command tags for SELECT etc will
correctly report tuple counts larger than 4G, as will plpgsql's GET
GET DIAGNOSTICS ... ROW_COUNT command. Queries processing more tuples
than that are still not exactly the norm, but they're becoming more
common.
Most values associated with FETCH/MOVE distances, such as PortalRun's count
argument and the count argument of most SPI functions that have one, remain
declared as "long". It's not clear whether it would be worth promoting
those to int64; but it would definitely be a large dollop of additional
API churn on top of this, and it would only help 32-bit platforms which
seem relatively less likely to see any benefit.
Andreas Scherbaum, reviewed by Christian Ullrich, additional hacking by me
2016-03-12 16:05:10 -05:00
|
|
|
#include <limits.h>
|
|
|
|
|
2011-12-18 21:14:16 +02:00
|
|
|
#include "access/xact.h"
|
|
|
|
#include "mb/pg_wchar.h"
|
2015-11-05 13:52:30 -05:00
|
|
|
#include "utils/memutils.h"
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
#include "plpython.h"
|
|
|
|
|
|
|
|
#include "plpy_cursorobject.h"
|
|
|
|
|
|
|
|
#include "plpy_elog.h"
|
2012-03-13 13:19:06 -04:00
|
|
|
#include "plpy_main.h"
|
2011-12-18 21:14:16 +02:00
|
|
|
#include "plpy_planobject.h"
|
|
|
|
#include "plpy_procedure.h"
|
|
|
|
#include "plpy_resultobject.h"
|
|
|
|
#include "plpy_spi.h"
|
|
|
|
|
|
|
|
|
2011-12-29 22:55:49 +02:00
|
|
|
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);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
static char PLy_cursor_doc[] = {
|
|
|
|
"Wrapper around a PostgreSQL cursor"
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyMethodDef PLy_cursor_methods[] = {
|
|
|
|
{"fetch", PLy_cursor_fetch, METH_VARARGS, NULL},
|
|
|
|
{"close", PLy_cursor_close, METH_NOARGS, NULL},
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject PLy_CursorType = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2012-06-10 15:20:04 -04:00
|
|
|
"PLyCursor", /* tp_name */
|
2011-12-18 21:14:16 +02:00
|
|
|
sizeof(PLyCursorObject), /* tp_size */
|
|
|
|
0, /* tp_itemsize */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* methods
|
|
|
|
*/
|
|
|
|
PLy_cursor_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_ITER, /* tp_flags */
|
|
|
|
PLy_cursor_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
PyObject_SelfIter, /* tp_iter */
|
|
|
|
PLy_cursor_iternext, /* tp_iternext */
|
|
|
|
PLy_cursor_methods, /* tp_tpmethods */
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
PLy_cursor_init_type(void)
|
|
|
|
{
|
|
|
|
if (PyType_Ready(&PLy_CursorType) < 0)
|
|
|
|
elog(ERROR, "could not initialize PLy_CursorType");
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PLy_cursor(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
char *query;
|
|
|
|
PyObject *plan;
|
|
|
|
PyObject *planargs = NULL;
|
|
|
|
|
|
|
|
if (PyArg_ParseTuple(args, "s", &query))
|
|
|
|
return PLy_cursor_query(query);
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
if (PyArg_ParseTuple(args, "O|O", &plan, &planargs))
|
|
|
|
return PLy_cursor_plan(plan, planargs);
|
|
|
|
|
|
|
|
PLy_exception_set(PLy_exc_error, "plpy.cursor expected a query or a plan");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PLy_cursor_query(const char *query)
|
|
|
|
{
|
2012-06-10 15:20:04 -04:00
|
|
|
PLyCursorObject *cursor;
|
2011-12-18 21:14:16 +02:00
|
|
|
volatile MemoryContext oldcontext;
|
|
|
|
volatile ResourceOwner oldowner;
|
|
|
|
|
|
|
|
if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
cursor->portalname = NULL;
|
|
|
|
cursor->closed = false;
|
2015-11-05 13:52:30 -05:00
|
|
|
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"PL/Python cursor context",
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 17:50:38 -04:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
2015-11-05 13:52:30 -05:00
|
|
|
PLy_typeinfo_init(&cursor->result, cursor->mcxt);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
oldcontext = CurrentMemoryContext;
|
|
|
|
oldowner = CurrentResourceOwner;
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_begin(oldcontext, oldowner);
|
|
|
|
|
|
|
|
PG_TRY();
|
|
|
|
{
|
2012-06-10 15:20:04 -04:00
|
|
|
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
|
2011-12-18 21:14:16 +02:00
|
|
|
SPIPlanPtr plan;
|
|
|
|
Portal portal;
|
|
|
|
|
|
|
|
pg_verifymbstr(query, strlen(query), false);
|
|
|
|
|
|
|
|
plan = SPI_prepare(query, 0, NULL);
|
|
|
|
if (plan == NULL)
|
|
|
|
elog(ERROR, "SPI_prepare failed: %s",
|
|
|
|
SPI_result_code_string(SPI_result));
|
|
|
|
|
|
|
|
portal = SPI_cursor_open(NULL, plan, NULL, NULL,
|
2012-03-13 13:19:06 -04:00
|
|
|
exec_ctx->curr_proc->fn_readonly);
|
2011-12-18 21:14:16 +02:00
|
|
|
SPI_freeplan(plan);
|
|
|
|
|
|
|
|
if (portal == NULL)
|
2012-01-10 22:49:17 +02:00
|
|
|
elog(ERROR, "SPI_cursor_open() failed: %s",
|
2011-12-18 21:14:16 +02:00
|
|
|
SPI_result_code_string(SPI_result));
|
|
|
|
|
2015-11-05 13:52:30 -05:00
|
|
|
cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
PLy_spi_subtransaction_abort(oldcontext, oldowner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
|
|
|
Assert(cursor->portalname != NULL);
|
|
|
|
return (PyObject *) cursor;
|
|
|
|
}
|
|
|
|
|
2017-02-25 08:42:25 -05:00
|
|
|
PyObject *
|
2011-12-18 21:14:16 +02:00
|
|
|
PLy_cursor_plan(PyObject *ob, PyObject *args)
|
|
|
|
{
|
2012-06-10 15:20:04 -04:00
|
|
|
PLyCursorObject *cursor;
|
2011-12-18 21:14:16 +02:00
|
|
|
volatile int nargs;
|
|
|
|
int i;
|
|
|
|
PLyPlanObject *plan;
|
|
|
|
volatile MemoryContext oldcontext;
|
|
|
|
volatile ResourceOwner oldowner;
|
|
|
|
|
|
|
|
if (args)
|
|
|
|
{
|
|
|
|
if (!PySequence_Check(args) || PyString_Check(args) || PyUnicode_Check(args))
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_TypeError, "plpy.cursor takes a sequence as its second argument");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
nargs = PySequence_Length(args);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nargs = 0;
|
|
|
|
|
|
|
|
plan = (PLyPlanObject *) ob;
|
|
|
|
|
|
|
|
if (nargs != plan->nargs)
|
|
|
|
{
|
|
|
|
char *sv;
|
|
|
|
PyObject *so = PyObject_Str(args);
|
|
|
|
|
|
|
|
if (!so)
|
|
|
|
PLy_elog(ERROR, "could not execute plan");
|
|
|
|
sv = PyString_AsString(so);
|
|
|
|
PLy_exception_set_plural(PyExc_TypeError,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 15:35:54 -04:00
|
|
|
"Expected sequence of %d argument, got %d: %s",
|
|
|
|
"Expected sequence of %d arguments, got %d: %s",
|
2011-12-18 21:14:16 +02:00
|
|
|
plan->nargs,
|
|
|
|
plan->nargs, nargs, sv);
|
|
|
|
Py_DECREF(so);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cursor = PyObject_New(PLyCursorObject, &PLy_CursorType)) == NULL)
|
|
|
|
return NULL;
|
|
|
|
cursor->portalname = NULL;
|
|
|
|
cursor->closed = false;
|
2015-11-05 13:52:30 -05:00
|
|
|
cursor->mcxt = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"PL/Python cursor context",
|
Add macros to make AllocSetContextCreate() calls simpler and safer.
I found that half a dozen (nearly 5%) of our AllocSetContextCreate calls
had typos in the context-sizing parameters. While none of these led to
especially significant problems, they did create minor inefficiencies,
and it's now clear that expecting people to copy-and-paste those calls
accurately is not a great idea. Let's reduce the risk of future errors
by introducing single macros that encapsulate the common use-cases.
Three such macros are enough to cover all but two special-purpose contexts;
those two calls can be left as-is, I think.
While this patch doesn't in itself improve matters for third-party
extensions, it doesn't break anything for them either, and they can
gradually adopt the simplified notation over time.
In passing, change TopMemoryContext to use the default allocation
parameters. Formerly it could only be extended 8K at a time. That was
probably reasonable when this code was written; but nowadays we create
many more contexts than we did then, so that it's not unusual to have a
couple hundred K in TopMemoryContext, even without considering various
dubious code that sticks other things there. There seems no good reason
not to let it use growing blocks like most other contexts.
Back-patch to 9.6, mostly because that's still close enough to HEAD that
it's easy to do so, and keeping the branches in sync can be expected to
avoid some future back-patching pain. The bugs fixed by these changes
don't seem to be significant enough to justify fixing them further back.
Discussion: <21072.1472321324@sss.pgh.pa.us>
2016-08-27 17:50:38 -04:00
|
|
|
ALLOCSET_DEFAULT_SIZES);
|
2015-11-05 13:52:30 -05:00
|
|
|
PLy_typeinfo_init(&cursor->result, cursor->mcxt);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
oldcontext = CurrentMemoryContext;
|
|
|
|
oldowner = CurrentResourceOwner;
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_begin(oldcontext, oldowner);
|
|
|
|
|
|
|
|
PG_TRY();
|
|
|
|
{
|
2012-03-13 13:19:06 -04:00
|
|
|
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
|
2011-12-18 21:14:16 +02:00
|
|
|
Portal portal;
|
|
|
|
char *volatile nulls;
|
|
|
|
volatile int j;
|
|
|
|
|
|
|
|
if (nargs > 0)
|
|
|
|
nulls = palloc(nargs * sizeof(char));
|
|
|
|
else
|
|
|
|
nulls = NULL;
|
|
|
|
|
|
|
|
for (j = 0; j < nargs; j++)
|
|
|
|
{
|
|
|
|
PyObject *elem;
|
|
|
|
|
|
|
|
elem = PySequence_GetItem(args, j);
|
|
|
|
if (elem != Py_None)
|
|
|
|
{
|
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
plan->values[j] =
|
|
|
|
plan->args[j].out.d.func(&(plan->args[j].out.d),
|
|
|
|
-1,
|
2016-10-26 10:38:56 +03:00
|
|
|
elem,
|
|
|
|
false);
|
2011-12-18 21:14:16 +02:00
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
Py_DECREF(elem);
|
|
|
|
PG_RE_THROW();
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
|
|
|
Py_DECREF(elem);
|
|
|
|
nulls[j] = ' ';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Py_DECREF(elem);
|
|
|
|
plan->values[j] =
|
|
|
|
InputFunctionCall(&(plan->args[j].out.d.typfunc),
|
|
|
|
NULL,
|
|
|
|
plan->args[j].out.d.typioparam,
|
|
|
|
-1);
|
|
|
|
nulls[j] = 'n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
portal = SPI_cursor_open(NULL, plan->plan, plan->values, nulls,
|
2012-03-13 13:19:06 -04:00
|
|
|
exec_ctx->curr_proc->fn_readonly);
|
2011-12-18 21:14:16 +02:00
|
|
|
if (portal == NULL)
|
2012-01-10 22:49:17 +02:00
|
|
|
elog(ERROR, "SPI_cursor_open() failed: %s",
|
2011-12-18 21:14:16 +02:00
|
|
|
SPI_result_code_string(SPI_result));
|
|
|
|
|
2015-11-05 13:52:30 -05:00
|
|
|
cursor->portalname = MemoryContextStrdup(cursor->mcxt, portal->name);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
|
|
|
|
/* cleanup plan->values array */
|
|
|
|
for (k = 0; k < nargs; k++)
|
|
|
|
{
|
|
|
|
if (!plan->args[k].out.d.typbyval &&
|
|
|
|
(plan->values[k] != PointerGetDatum(NULL)))
|
|
|
|
{
|
|
|
|
pfree(DatumGetPointer(plan->values[k]));
|
|
|
|
plan->values[k] = PointerGetDatum(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(cursor);
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_abort(oldcontext, oldowner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
if (!plan->args[i].out.d.typbyval &&
|
|
|
|
(plan->values[i] != PointerGetDatum(NULL)))
|
|
|
|
{
|
|
|
|
pfree(DatumGetPointer(plan->values[i]));
|
|
|
|
plan->values[i] = PointerGetDatum(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(cursor->portalname != NULL);
|
|
|
|
return (PyObject *) cursor;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
PLy_cursor_dealloc(PyObject *arg)
|
|
|
|
{
|
|
|
|
PLyCursorObject *cursor;
|
2012-06-10 15:20:04 -04:00
|
|
|
Portal portal;
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
cursor = (PLyCursorObject *) arg;
|
|
|
|
|
|
|
|
if (!cursor->closed)
|
|
|
|
{
|
|
|
|
portal = GetPortalByName(cursor->portalname);
|
|
|
|
|
|
|
|
if (PortalIsValid(portal))
|
|
|
|
SPI_cursor_close(portal);
|
2015-11-05 13:52:30 -05:00
|
|
|
cursor->closed = true;
|
|
|
|
}
|
|
|
|
if (cursor->mcxt)
|
|
|
|
{
|
|
|
|
MemoryContextDelete(cursor->mcxt);
|
|
|
|
cursor->mcxt = NULL;
|
2011-12-18 21:14:16 +02:00
|
|
|
}
|
|
|
|
arg->ob_type->tp_free(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PLy_cursor_iternext(PyObject *self)
|
|
|
|
{
|
|
|
|
PLyCursorObject *cursor;
|
2012-06-10 15:20:04 -04:00
|
|
|
PyObject *ret;
|
2011-12-18 21:14:16 +02:00
|
|
|
volatile MemoryContext oldcontext;
|
|
|
|
volatile ResourceOwner oldowner;
|
2012-06-10 15:20:04 -04:00
|
|
|
Portal portal;
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
cursor = (PLyCursorObject *) self;
|
|
|
|
|
|
|
|
if (cursor->closed)
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_ValueError, "iterating a closed cursor");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
portal = GetPortalByName(cursor->portalname);
|
|
|
|
if (!PortalIsValid(portal))
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_ValueError,
|
|
|
|
"iterating a cursor in an aborted subtransaction");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldcontext = CurrentMemoryContext;
|
|
|
|
oldowner = CurrentResourceOwner;
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_begin(oldcontext, oldowner);
|
|
|
|
|
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
SPI_cursor_fetch(portal, true, 1);
|
|
|
|
if (SPI_processed == 0)
|
|
|
|
{
|
|
|
|
PyErr_SetNone(PyExc_StopIteration);
|
|
|
|
ret = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cursor->result.is_rowtype != 1)
|
|
|
|
PLy_input_tuple_funcs(&cursor->result, SPI_tuptable->tupdesc);
|
|
|
|
|
|
|
|
ret = PLyDict_FromTuple(&cursor->result, SPI_tuptable->vals[0],
|
|
|
|
SPI_tuptable->tupdesc);
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_freetuptable(SPI_tuptable);
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
PLy_spi_subtransaction_abort(oldcontext, oldowner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PLy_cursor_fetch(PyObject *self, PyObject *args)
|
|
|
|
{
|
|
|
|
PLyCursorObject *cursor;
|
2012-06-10 15:20:04 -04:00
|
|
|
int count;
|
|
|
|
PLyResultObject *ret;
|
2011-12-18 21:14:16 +02:00
|
|
|
volatile MemoryContext oldcontext;
|
|
|
|
volatile ResourceOwner oldowner;
|
2012-06-10 15:20:04 -04:00
|
|
|
Portal portal;
|
2011-12-18 21:14:16 +02:00
|
|
|
|
2016-10-27 12:00:00 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "i:fetch", &count))
|
2011-12-18 21:14:16 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cursor = (PLyCursorObject *) self;
|
|
|
|
|
|
|
|
if (cursor->closed)
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_ValueError, "fetch from a closed cursor");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
portal = GetPortalByName(cursor->portalname);
|
|
|
|
if (!PortalIsValid(portal))
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_ValueError,
|
|
|
|
"iterating a cursor in an aborted subtransaction");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (PLyResultObject *) PLy_result_new();
|
|
|
|
if (ret == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
oldcontext = CurrentMemoryContext;
|
|
|
|
oldowner = CurrentResourceOwner;
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_begin(oldcontext, oldowner);
|
|
|
|
|
|
|
|
PG_TRY();
|
|
|
|
{
|
|
|
|
SPI_cursor_fetch(portal, true, count);
|
|
|
|
|
|
|
|
if (cursor->result.is_rowtype != 1)
|
|
|
|
PLy_input_tuple_funcs(&cursor->result, SPI_tuptable->tupdesc);
|
|
|
|
|
|
|
|
Py_DECREF(ret->status);
|
|
|
|
ret->status = PyInt_FromLong(SPI_OK_FETCH);
|
|
|
|
|
|
|
|
Py_DECREF(ret->nrows);
|
Widen query numbers-of-tuples-processed counters to uint64.
This patch widens SPI_processed, EState's es_processed field, PortalData's
portalPos field, FuncCallContext's call_cntr and max_calls fields,
ExecutorRun's count argument, PortalRunFetch's result, and the max number
of rows in a SPITupleTable to uint64, and deals with (I hope) all the
ensuing fallout. Some of these values were declared uint32 before, and
others "long".
I also removed PortalData's posOverflow field, since that logic seems
pretty useless given that portalPos is now always 64 bits.
The user-visible results are that command tags for SELECT etc will
correctly report tuple counts larger than 4G, as will plpgsql's GET
GET DIAGNOSTICS ... ROW_COUNT command. Queries processing more tuples
than that are still not exactly the norm, but they're becoming more
common.
Most values associated with FETCH/MOVE distances, such as PortalRun's count
argument and the count argument of most SPI functions that have one, remain
declared as "long". It's not clear whether it would be worth promoting
those to int64; but it would definitely be a large dollop of additional
API churn on top of this, and it would only help 32-bit platforms which
seem relatively less likely to see any benefit.
Andreas Scherbaum, reviewed by Christian Ullrich, additional hacking by me
2016-03-12 16:05:10 -05:00
|
|
|
ret->nrows = (SPI_processed > (uint64) LONG_MAX) ?
|
|
|
|
PyFloat_FromDouble((double) SPI_processed) :
|
|
|
|
PyInt_FromLong((long) SPI_processed);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
if (SPI_processed != 0)
|
|
|
|
{
|
Widen query numbers-of-tuples-processed counters to uint64.
This patch widens SPI_processed, EState's es_processed field, PortalData's
portalPos field, FuncCallContext's call_cntr and max_calls fields,
ExecutorRun's count argument, PortalRunFetch's result, and the max number
of rows in a SPITupleTable to uint64, and deals with (I hope) all the
ensuing fallout. Some of these values were declared uint32 before, and
others "long".
I also removed PortalData's posOverflow field, since that logic seems
pretty useless given that portalPos is now always 64 bits.
The user-visible results are that command tags for SELECT etc will
correctly report tuple counts larger than 4G, as will plpgsql's GET
GET DIAGNOSTICS ... ROW_COUNT command. Queries processing more tuples
than that are still not exactly the norm, but they're becoming more
common.
Most values associated with FETCH/MOVE distances, such as PortalRun's count
argument and the count argument of most SPI functions that have one, remain
declared as "long". It's not clear whether it would be worth promoting
those to int64; but it would definitely be a large dollop of additional
API churn on top of this, and it would only help 32-bit platforms which
seem relatively less likely to see any benefit.
Andreas Scherbaum, reviewed by Christian Ullrich, additional hacking by me
2016-03-12 16:05:10 -05:00
|
|
|
uint64 i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PyList_New() and PyList_SetItem() use Py_ssize_t for list size
|
|
|
|
* and list indices; so we cannot support a result larger than
|
|
|
|
* PY_SSIZE_T_MAX.
|
|
|
|
*/
|
|
|
|
if (SPI_processed > (uint64) PY_SSIZE_T_MAX)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
|
|
|
|
errmsg("query result has too many rows to fit in a Python list")));
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
Py_DECREF(ret->rows);
|
|
|
|
ret->rows = PyList_New(SPI_processed);
|
|
|
|
|
|
|
|
for (i = 0; i < SPI_processed; i++)
|
|
|
|
{
|
|
|
|
PyObject *row = PLyDict_FromTuple(&cursor->result,
|
|
|
|
SPI_tuptable->vals[i],
|
|
|
|
SPI_tuptable->tupdesc);
|
2012-06-10 15:20:04 -04:00
|
|
|
|
2011-12-18 21:14:16 +02:00
|
|
|
PyList_SetItem(ret->rows, i, row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_freetuptable(SPI_tuptable);
|
|
|
|
|
|
|
|
PLy_spi_subtransaction_commit(oldcontext, oldowner);
|
|
|
|
}
|
|
|
|
PG_CATCH();
|
|
|
|
{
|
|
|
|
PLy_spi_subtransaction_abort(oldcontext, oldowner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PG_END_TRY();
|
|
|
|
|
|
|
|
return (PyObject *) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
PLy_cursor_close(PyObject *self, PyObject *unused)
|
|
|
|
{
|
|
|
|
PLyCursorObject *cursor = (PLyCursorObject *) self;
|
|
|
|
|
|
|
|
if (!cursor->closed)
|
|
|
|
{
|
2012-06-10 15:20:04 -04:00
|
|
|
Portal portal = GetPortalByName(cursor->portalname);
|
2011-12-18 21:14:16 +02:00
|
|
|
|
|
|
|
if (!PortalIsValid(portal))
|
|
|
|
{
|
|
|
|
PLy_exception_set(PyExc_ValueError,
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 15:35:54 -04:00
|
|
|
"closing a cursor in an aborted subtransaction");
|
2011-12-18 21:14:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SPI_cursor_close(portal);
|
|
|
|
cursor->closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|