#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "executor/functions.h"
+#include "executor/spi.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
* 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
* 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
InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
{
FunctionCallInfoData fcinfo;
Datum result;
+ bool pushed;
if (str == NULL && flinfo->fn_strict)
return (Datum) 0; /* just return null result */
+ pushed = SPI_push_conditional();
+
InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
fcinfo.arg[0] = CStringGetDatum(str);
fcinfo.flinfo->fn_oid);
}
+ SPI_pop_conditional(pushed);
+
return result;
}
*
* Do not call this on NULL datums.
*
- * This is mere window dressing for FunctionCall1, but its use is recommended
- * anyway so that code invoking output functions can be identified easily.
+ * This is almost just window dressing for FunctionCall1, but it includes
+ * SPI context pushing for the same reasons as InputFunctionCall.
*/
char *
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;
}
/*
* "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
* 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
ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
{
FunctionCallInfoData fcinfo;
Datum result;
+ bool pushed;
if (buf == NULL && flinfo->fn_strict)
return (Datum) 0; /* just return null result */
+ pushed = SPI_push_conditional();
+
InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
fcinfo.arg[0] = PointerGetDatum(buf);
fcinfo.flinfo->fn_oid);
}
+ SPI_pop_conditional(pushed);
+
return result;
}
*
* Do not call this on NULL datums.
*
- * This is little more than window dressing for FunctionCall1, but its use is
- * recommended anyway so that code invoking output functions can be identified
- * easily. Note however that it does guarantee a non-toasted result.
+ * This is little more than window dressing for FunctionCall1, but it does
+ * guarantee a non-toasted result, which strictly speaking the underlying
+ * function doesn't. Also, this includes SPI context pushing for the same
+ * reasons as InputFunctionCall.
*/
bytea *
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;
}
/*
static char *
convert_value_to_string(Datum value, Oid valtype)
{
- char *str;
Oid typoutput;
bool typIsVarlena;
getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
-
- /*
- * 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;
+ return OidOutputFunctionCall(typoutput, value);
}
/* ----------
char *extval;
extval = convert_value_to_string(value, valtype);
-
- /* Allow input function to use SPI ... see notes above */
- SPI_push();
-
value = InputFunctionCall(reqinput, extval,
reqtypioparam, reqtypmod);
-
- SPI_pop();
-
pfree(extval);
}
else
{
- SPI_push();
-
value = InputFunctionCall(reqinput, NULL,
reqtypioparam, reqtypmod);
-
- SPI_pop();
}
}