21 #include "utils/fmgrprotos.h"
57 PyObject *
volatile plargs = NULL;
58 PyObject *
volatile plrv = NULL;
95 if (srfstate == NULL || srfstate->
iter == NULL)
127 if (srfstate->
iter == NULL)
136 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
137 errmsg(
"unsupported set function return mode"),
138 errdetail(
"PL/Python set-returning functions only support returning one value per call.")));
143 srfstate->
iter = PyObject_GetIter(plrv);
148 if (srfstate->
iter == NULL)
150 (
errcode(ERRCODE_DATATYPE_MISMATCH),
151 errmsg(
"returned object cannot be iterated"),
152 errdetail(
"PL/Python set-returning functions must return an iterable object.")));
156 plrv = PyIter_Next(srfstate->
iter);
160 bool has_error = (PyErr_Occurred() != NULL);
162 Py_DECREF(srfstate->
iter);
163 srfstate->
iter = NULL;
208 (
errcode(ERRCODE_DATATYPE_MISMATCH),
209 errmsg(
"PL/Python procedure did not return None")));
212 (
errcode(ERRCODE_DATATYPE_MISMATCH),
213 errmsg(
"PL/Python function with return type \"void\" did not return None")));
219 else if (plrv == Py_None &&
220 srfstate && srfstate->
iter == NULL)
243 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
244 errmsg(
"function returning record called in context "
245 "that cannot accept type record")));
270 Py_XDECREF(srfstate->
iter);
271 srfstate->
iter = NULL;
293 if (srfstate->
iter == NULL)
322 PyObject *
volatile plargs = NULL;
323 PyObject *
volatile plrv = NULL;
384 if (PyUnicode_Check(plrv))
389 (
errcode(ERRCODE_DATA_EXCEPTION),
390 errmsg(
"unexpected return value from trigger procedure"),
391 errdetail(
"Expected None or a string.")));
404 (
errmsg(
"PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
413 (
errcode(ERRCODE_DATA_EXCEPTION),
414 errmsg(
"unexpected return value from trigger procedure"),
415 errdetail(
"Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
435 PyObject *
volatile arg = NULL;
465 if (PyList_SetItem(
args,
i,
arg) == -1)
466 PLy_elog(
ERROR,
"PyList_SetItem() failed, while setting up arguments");
470 PLy_elog(
ERROR,
"PyDict_SetItemString() failed, while setting up arguments");
504 proc->
nargs *
sizeof(PyObject *));
508 result->
args = PyDict_GetItemString(proc->
globals,
"args");
509 Py_XINCREF(result->
args);
514 result->
td = PyDict_GetItemString(proc->
globals,
"TD");
515 Py_XINCREF(result->
td);
549 for (
i = 0;
i < savedargs->
nargs;
i++)
563 PyDict_SetItemString(proc->
globals,
"args", savedargs->
args);
564 Py_DECREF(savedargs->
args);
570 PyDict_SetItemString(proc->
globals,
"TD", savedargs->
td);
571 Py_DECREF(savedargs->
td);
587 for (
i = 0;
i < savedargs->
nargs;
i++)
593 Py_XDECREF(savedargs->
args);
594 Py_XDECREF(savedargs->
td);
684 Py_XDECREF(srfstate->
iter);
685 srfstate->
iter = NULL;
725 pltdata = PyDict_New();
747 PyDict_SetItemString(pltdata,
"name", pltname);
753 PyDict_SetItemString(pltdata,
"relid", pltrelid);
759 PyDict_SetItemString(pltdata,
"table_name", plttablename);
760 Py_DECREF(plttablename);
765 PyDict_SetItemString(pltdata,
"table_schema", plttableschema);
766 Py_DECREF(plttableschema);
780 PyDict_SetItemString(pltdata,
"when", pltwhen);
786 PyDict_SetItemString(pltdata,
"level", pltlevel);
798 PyDict_SetItemString(pltdata,
"old", Py_None);
803 PyDict_SetItemString(pltdata,
"new", pytnew);
811 PyDict_SetItemString(pltdata,
"new", Py_None);
816 PyDict_SetItemString(pltdata,
"old", pytold);
828 PyDict_SetItemString(pltdata,
"new", pytnew);
834 PyDict_SetItemString(pltdata,
"old", pytold);
844 PyDict_SetItemString(pltdata,
"event", pltevent);
850 PyDict_SetItemString(pltdata,
"level", pltlevel);
853 PyDict_SetItemString(pltdata,
"old", Py_None);
854 PyDict_SetItemString(pltdata,
"new", Py_None);
871 PyDict_SetItemString(pltdata,
"event", pltevent);
886 Assert(pltargs && pltargs != Py_None);
895 PyList_SetItem(pltargs,
i, pltarg);
900 Assert(pltargs == Py_None);
902 PyDict_SetItemString(pltdata,
"args", pltargs);
924 PyObject *
volatile plntup;
925 PyObject *
volatile plkeys;
926 PyObject *
volatile plval;
927 Datum *
volatile modvalues;
928 bool *
volatile modnulls;
929 bool *
volatile modrepls;
936 plntup = plkeys = plval = NULL;
947 if ((plntup = PyDict_GetItemString(pltd,
"new")) == NULL)
949 (
errcode(ERRCODE_UNDEFINED_OBJECT),
950 errmsg(
"TD[\"new\"] deleted, cannot modify row")));
952 if (!PyDict_Check(plntup))
954 (
errcode(ERRCODE_DATATYPE_MISMATCH),
955 errmsg(
"TD[\"new\"] is not a dictionary")));
957 plkeys = PyDict_Keys(plntup);
958 nkeys = PyList_Size(plkeys);
963 modnulls = (
bool *)
palloc0(tupdesc->
natts *
sizeof(
bool));
964 modrepls = (
bool *)
palloc0(tupdesc->
natts *
sizeof(
bool));
966 for (
i = 0;
i < nkeys;
i++)
973 platt = PyList_GetItem(plkeys,
i);
974 if (PyUnicode_Check(platt))
979 (
errcode(ERRCODE_DATATYPE_MISMATCH),
980 errmsg(
"TD[\"new\"] dictionary key at ordinal position %d is not a string",
i)));
986 (
errcode(ERRCODE_UNDEFINED_COLUMN),
987 errmsg(
"key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
991 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
992 errmsg(
"cannot set system attribute \"%s\"",
996 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
997 errmsg(
"cannot set generated column \"%s\"",
1000 plval = PyDict_GetItem(plntup, platt);
1002 elog(
FATAL,
"Python interpreter is probably corrupted");
1011 &modnulls[attn - 1]);
1012 modrepls[attn - 1] =
true;
1062 PyObject *rv = NULL;
1065 PyDict_SetItemString(proc->
globals, kargs, vargs);
1069 #if PY_VERSION_HEX >= 0x03020000
1070 rv = PyEval_EvalCode(proc->
code,
1073 rv = PyEval_EvalCode((PyCodeObject *) proc->
code,
1104 Assert(save_subxact_level >= 0);
1113 (
errmsg(
"forcibly aborting a subtransaction that has not been exited")));
1122 pfree(subtransactiondata);
#define PG_USED_FOR_ASSERTS_ONLY
#define Assert(condition)
int errdetail(const char *fmt,...)
ErrorContextCallback * error_context_stack
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define DirectFunctionCall1(func, arg1)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
#define SRF_IS_FIRSTCALL()
#define SRF_RETURN_NEXT_NULL(_funcctx)
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
#define SRF_RETURN_DONE(_funcctx)
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
if(TABLE==NULL||TABLE_index==NULL)
List * list_delete_first(List *list)
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
void pfree(void *pointer)
void * palloc0(Size size)
void * MemoryContextAllocZero(MemoryContext context, Size size)
#define IsA(nodeptr, _type_)
Datum oidout(PG_FUNCTION_ARGS)
static int list_length(const List *l)
static void PLy_function_drop_args(PLySavedArgs *savedargs)
static void PLy_global_args_push(PLyProcedure *proc)
struct PLySRFState PLySRFState
Datum PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
static void plpython_trigger_error_callback(void *arg)
static void PLy_global_args_pop(PLyProcedure *proc)
static PyObject * PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
static void plpython_srf_cleanup_callback(void *arg)
static PLySavedArgs * PLy_function_save_args(PLyProcedure *proc)
static void PLy_abort_open_subtransactions(int save_subxact_level)
static void plpython_return_error_callback(void *arg)
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
PLyExecutionContext * PLy_current_execution_context(void)
List * explicit_subtransactions
PyObject * PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
PyObject * PLy_input_convert(PLyDatumToOb *arg, Datum val)
void PLy_output_setup_record(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
char * PLyUnicode_AsString(PyObject *unicode)
PyObject * PLyUnicode_FromString(const char *s)
int pg_strcasecmp(const char *s1, const char *s2)
static char * DatumGetCString(Datum X)
static Datum ObjectIdGetDatum(Oid X)
MemoryContextSwitchTo(old_ctx)
#define RelationGetDescr(relation)
ResourceOwner CurrentResourceOwner
char * SPI_getrelname(Relation rel)
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
int SPI_register_trigger_data(TriggerData *tdata)
char * SPI_getnspname(Relation rel)
#define SPI_ERROR_NOATTRIBUTE
struct ErrorContextCallback * previous
void(* callback)(void *arg)
MemoryContext multi_call_memory_ctx
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
MemoryContextCallbackFunction func
union PLyObToDatum::@186 u
MemoryContextCallback callback
struct PLySavedArgs * next
PyObject * namedargs[FLEXIBLE_ARRAY_MEMBER]
SetFunctionReturnMode returnMode
#define TRIGGER_FIRED_FOR_STATEMENT(event)
#define TRIGGER_FIRED_BY_DELETE(event)
#define TRIGGER_FIRED_BEFORE(event)
#define CALLED_AS_TRIGGER(fcinfo)
#define TRIGGER_FIRED_FOR_ROW(event)
#define TRIGGER_FIRED_AFTER(event)
#define TRIGGER_FIRED_BY_TRUNCATE(event)
#define TRIGGER_FIRED_BY_INSERT(event)
#define TRIGGER_FIRED_BY_UPDATE(event)
#define TRIGGER_FIRED_INSTEAD(event)
#define TupleDescAttr(tupdesc, i)
void RollbackAndReleaseCurrentSubTransaction(void)