Commit deac9488 authored by Tom Lane's avatar Tom Lane

Insert conditional SPI_push/SPI_pop calls into InputFunctionCall,

OutputFunctionCall, and friends.  This allows SPI-using functions to invoke
datatype I/O without concern for the possibility that a SPI-using function
will be called (which could be either the I/O function itself, or a function
used in a domain check constraint).  It's a tad ugly, but not nearly as ugly
as what'd be needed to make this work via retail insertion of push/pop
operations in all the PLs.

This reverts my patch of 2007-01-30 that inserted some retail SPI_push/pop
calls into plpgsql; that approach only fixed plpgsql, and not any other PLs.
But the other PLs have the issue too, as illustrated by a recent gripe from
Christian Schröder.

Back-patch to 8.2, which is as far back as this solution will work.  It's
also as far back as we need to worry about the domain-constraint case, since
earlier versions did not attempt to check domain constraints within datatype
input.  I'm not aware of any old I/O functions that use SPI themselves, so
this should be sufficient for a back-patch.
parent 6b883930
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.205 2009/01/07 13:44:36 tgl Exp $ * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.206 2009/01/07 20:38:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -304,6 +304,31 @@ SPI_pop(void) ...@@ -304,6 +304,31 @@ SPI_pop(void)
_SPI_curid--; _SPI_curid--;
} }
/* Conditional push: push only if we're inside a SPI procedure */
bool
SPI_push_conditional(void)
{
bool pushed = (_SPI_curid != _SPI_connected);
if (pushed)
{
_SPI_curid++;
/* We should now be in a state where SPI_connect would succeed */
Assert(_SPI_curid == _SPI_connected);
}
return pushed;
}
/* Conditional pop: pop only if SPI_push_conditional pushed */
void
SPI_pop_conditional(bool pushed)
{
/* We should be in a state where SPI_connect would succeed */
Assert(_SPI_curid == _SPI_connected);
if (pushed)
_SPI_curid--;
}
/* Restore state of SPI stack after aborting a subtransaction */ /* Restore state of SPI stack after aborting a subtransaction */
void void
SPI_restore_connection(void) SPI_restore_connection(void)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.124 2009/01/01 17:23:51 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.125 2009/01/07 20:38:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "catalog/pg_language.h" #include "catalog/pg_language.h"
#include "catalog/pg_proc.h" #include "catalog/pg_proc.h"
#include "executor/functions.h" #include "executor/functions.h"
#include "executor/spi.h"
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "nodes/nodeFuncs.h" #include "nodes/nodeFuncs.h"
...@@ -1846,16 +1847,25 @@ OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2, ...@@ -1846,16 +1847,25 @@ OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
* the caller should assume the result is NULL, but we'll call the input * the caller should assume the result is NULL, but we'll call the input
* function anyway if it's not strict. So this is almost but not quite * function anyway if it's not strict. So this is almost but not quite
* the same as FunctionCall3. * the same as FunctionCall3.
*
* One important difference from the bare function call is that we will
* push any active SPI context, allowing SPI-using I/O functions to be
* called from other SPI functions without extra notation. This is a hack,
* but the alternative of expecting all SPI functions to do SPI_push/SPI_pop
* around I/O calls seems worse.
*/ */
Datum Datum
InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod) InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
{ {
FunctionCallInfoData fcinfo; FunctionCallInfoData fcinfo;
Datum result; Datum result;
bool pushed;
if (str == NULL && flinfo->fn_strict) if (str == NULL && flinfo->fn_strict)
return (Datum) 0; /* just return null result */ return (Datum) 0; /* just return null result */
pushed = SPI_push_conditional();
InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL); InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
fcinfo.arg[0] = CStringGetDatum(str); fcinfo.arg[0] = CStringGetDatum(str);
...@@ -1881,6 +1891,8 @@ InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod) ...@@ -1881,6 +1891,8 @@ InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
fcinfo.flinfo->fn_oid); fcinfo.flinfo->fn_oid);
} }
SPI_pop_conditional(pushed);
return result; return result;
} }
...@@ -1889,13 +1901,22 @@ InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod) ...@@ -1889,13 +1901,22 @@ InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
* *
* Do not call this on NULL datums. * Do not call this on NULL datums.
* *
* This is mere window dressing for FunctionCall1, but its use is recommended * This is almost just window dressing for FunctionCall1, but it includes
* anyway so that code invoking output functions can be identified easily. * SPI context pushing for the same reasons as InputFunctionCall.
*/ */
char * char *
OutputFunctionCall(FmgrInfo *flinfo, Datum val) OutputFunctionCall(FmgrInfo *flinfo, Datum val)
{ {
return DatumGetCString(FunctionCall1(flinfo, val)); char *result;
bool pushed;
pushed = SPI_push_conditional();
result = DatumGetCString(FunctionCall1(flinfo, val));
SPI_pop_conditional(pushed);
return result;
} }
/* /*
...@@ -1904,7 +1925,8 @@ OutputFunctionCall(FmgrInfo *flinfo, Datum val) ...@@ -1904,7 +1925,8 @@ OutputFunctionCall(FmgrInfo *flinfo, Datum val)
* "buf" may be NULL to indicate we are reading a NULL. In this case * "buf" may be NULL to indicate we are reading a NULL. In this case
* the caller should assume the result is NULL, but we'll call the receive * the caller should assume the result is NULL, but we'll call the receive
* function anyway if it's not strict. So this is almost but not quite * function anyway if it's not strict. So this is almost but not quite
* the same as FunctionCall3. * the same as FunctionCall3. Also, this includes SPI context pushing for
* the same reasons as InputFunctionCall.
*/ */
Datum Datum
ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
...@@ -1912,10 +1934,13 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, ...@@ -1912,10 +1934,13 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
{ {
FunctionCallInfoData fcinfo; FunctionCallInfoData fcinfo;
Datum result; Datum result;
bool pushed;
if (buf == NULL && flinfo->fn_strict) if (buf == NULL && flinfo->fn_strict)
return (Datum) 0; /* just return null result */ return (Datum) 0; /* just return null result */
pushed = SPI_push_conditional();
InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL); InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
fcinfo.arg[0] = PointerGetDatum(buf); fcinfo.arg[0] = PointerGetDatum(buf);
...@@ -1941,6 +1966,8 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, ...@@ -1941,6 +1966,8 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
fcinfo.flinfo->fn_oid); fcinfo.flinfo->fn_oid);
} }
SPI_pop_conditional(pushed);
return result; return result;
} }
...@@ -1949,14 +1976,24 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf, ...@@ -1949,14 +1976,24 @@ ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
* *
* Do not call this on NULL datums. * Do not call this on NULL datums.
* *
* This is little more than window dressing for FunctionCall1, but its use is * This is little more than window dressing for FunctionCall1, but it does
* recommended anyway so that code invoking output functions can be identified * guarantee a non-toasted result, which strictly speaking the underlying
* easily. Note however that it does guarantee a non-toasted result. * function doesn't. Also, this includes SPI context pushing for the same
* reasons as InputFunctionCall.
*/ */
bytea * bytea *
SendFunctionCall(FmgrInfo *flinfo, Datum val) SendFunctionCall(FmgrInfo *flinfo, Datum val)
{ {
return DatumGetByteaP(FunctionCall1(flinfo, val)); bytea *result;
bool pushed;
pushed = SPI_push_conditional();
result = DatumGetByteaP(FunctionCall1(flinfo, val));
SPI_pop_conditional(pushed);
return result;
} }
/* /*
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/executor/spi.h,v 1.69 2009/01/07 13:44:37 tgl Exp $ * $PostgreSQL: pgsql/src/include/executor/spi.h,v 1.70 2009/01/07 20:38:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -66,6 +66,8 @@ extern int SPI_connect(void); ...@@ -66,6 +66,8 @@ extern int SPI_connect(void);
extern int SPI_finish(void); extern int SPI_finish(void);
extern void SPI_push(void); extern void SPI_push(void);
extern void SPI_pop(void); extern void SPI_pop(void);
extern bool SPI_push_conditional(void);
extern void SPI_pop_conditional(bool pushed);
extern void SPI_restore_connection(void); extern void SPI_restore_connection(void);
extern int SPI_execute(const char *src, bool read_only, long tcount); extern int SPI_execute(const char *src, bool read_only, long tcount);
extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, extern int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls,
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.227 2009/01/07 13:44:37 tgl Exp $ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.228 2009/01/07 20:38:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -4690,27 +4690,11 @@ make_tuple_from_row(PLpgSQL_execstate *estate, ...@@ -4690,27 +4690,11 @@ make_tuple_from_row(PLpgSQL_execstate *estate,
static char * static char *
convert_value_to_string(Datum value, Oid valtype) convert_value_to_string(Datum value, Oid valtype)
{ {
char *str;
Oid typoutput; Oid typoutput;
bool typIsVarlena; bool typIsVarlena;
getTypeOutputInfo(valtype, &typoutput, &typIsVarlena); getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
return OidOutputFunctionCall(typoutput, value);
/*
* We do SPI_push to allow the datatype output function to use SPI.
* However we do not mess around with CommandCounterIncrement or advancing
* the snapshot, which means that a stable output function would not see
* updates made so far by our own function. The use-case for such
* scenarios seems too narrow to justify the cycles that would be
* expended.
*/
SPI_push();
str = OidOutputFunctionCall(typoutput, value);
SPI_pop();
return str;
} }
/* ---------- /* ----------
...@@ -4736,25 +4720,14 @@ exec_cast_value(Datum value, Oid valtype, ...@@ -4736,25 +4720,14 @@ exec_cast_value(Datum value, Oid valtype,
char *extval; char *extval;
extval = convert_value_to_string(value, valtype); extval = convert_value_to_string(value, valtype);
/* Allow input function to use SPI ... see notes above */
SPI_push();
value = InputFunctionCall(reqinput, extval, value = InputFunctionCall(reqinput, extval,
reqtypioparam, reqtypmod); reqtypioparam, reqtypmod);
SPI_pop();
pfree(extval); pfree(extval);
} }
else else
{ {
SPI_push();
value = InputFunctionCall(reqinput, NULL, value = InputFunctionCall(reqinput, NULL,
reqtypioparam, reqtypmod); reqtypioparam, reqtypmod);
SPI_pop();
} }
} }
......
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