Commit 88dcdf90 authored by Peter Eisentraut's avatar Peter Eisentraut

Call PLy_spi_execute_fetch_result inside the try/catch block

This way errors from fetching tuples are correctly reported as errors
in the SPI call.  While at it, avoid palloc(0).

Jan Urbański
parent 52713d02
...@@ -2978,6 +2978,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -2978,6 +2978,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
rv; rv;
PLyPlanObject *plan; PLyPlanObject *plan;
volatile MemoryContext oldcontext; volatile MemoryContext oldcontext;
PyObject *ret;
if (list != NULL) if (list != NULL)
{ {
...@@ -3014,9 +3015,14 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -3014,9 +3015,14 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
oldcontext = CurrentMemoryContext; oldcontext = CurrentMemoryContext;
PG_TRY(); PG_TRY();
{ {
char *nulls = palloc(nargs * sizeof(char)); char *nulls;
volatile int j; volatile int j;
if (nargs > 0)
nulls = palloc(nargs * sizeof(char));
else
nulls = NULL;
for (j = 0; j < nargs; j++) for (j = 0; j < nargs; j++)
{ {
PyObject *elem; PyObject *elem;
...@@ -3055,8 +3061,10 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -3055,8 +3061,10 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
rv = SPI_execute_plan(plan->plan, plan->values, nulls, rv = SPI_execute_plan(plan->plan, plan->values, nulls,
PLy_curr_procedure->fn_readonly, limit); PLy_curr_procedure->fn_readonly, limit);
ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
pfree(nulls); if (nargs > 0)
pfree(nulls);
} }
PG_CATCH(); PG_CATCH();
{ {
...@@ -3099,7 +3107,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit) ...@@ -3099,7 +3107,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit)
} }
} }
return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv); return ret;
} }
static PyObject * static PyObject *
...@@ -3107,12 +3115,14 @@ PLy_spi_execute_query(char *query, long limit) ...@@ -3107,12 +3115,14 @@ PLy_spi_execute_query(char *query, long limit)
{ {
int rv; int rv;
volatile MemoryContext oldcontext; volatile MemoryContext oldcontext;
PyObject *ret;
oldcontext = CurrentMemoryContext; oldcontext = CurrentMemoryContext;
PG_TRY(); PG_TRY();
{ {
pg_verifymbstr(query, strlen(query), false); pg_verifymbstr(query, strlen(query), false);
rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit); rv = SPI_execute(query, PLy_curr_procedure->fn_readonly, limit);
ret = PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv);
} }
PG_CATCH(); PG_CATCH();
{ {
...@@ -3138,7 +3148,7 @@ PLy_spi_execute_query(char *query, long limit) ...@@ -3138,7 +3148,7 @@ PLy_spi_execute_query(char *query, long limit)
return NULL; return NULL;
} }
return PLy_spi_execute_fetch_result(SPI_tuptable, SPI_processed, rv); return ret;
} }
static PyObject * static PyObject *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment