59 PyObject *
volatile plargs = NULL;
60 PyObject *
volatile plrv = NULL;
97 if (srfstate == NULL || srfstate->
iter == NULL)
129 if (srfstate->
iter == NULL)
138 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
139 errmsg(
"unsupported set function return mode"),
140 errdetail(
"PL/Python set-returning functions only support returning one value per call.")));
145 srfstate->
iter = PyObject_GetIter(plrv);
150 if (srfstate->
iter == NULL)
152 (
errcode(ERRCODE_DATATYPE_MISMATCH),
153 errmsg(
"returned object cannot be iterated"),
154 errdetail(
"PL/Python set-returning functions must return an iterable object.")));
158 plrv = PyIter_Next(srfstate->
iter);
162 bool has_error = (PyErr_Occurred() != NULL);
164 Py_DECREF(srfstate->
iter);
165 srfstate->
iter = NULL;
210 (
errcode(ERRCODE_DATATYPE_MISMATCH),
211 errmsg(
"PL/Python procedure did not return None")));
214 (
errcode(ERRCODE_DATATYPE_MISMATCH),
215 errmsg(
"PL/Python function with return type \"void\" did not return None")));
221 else if (plrv == Py_None &&
222 srfstate && srfstate->
iter == NULL)
245 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
246 errmsg(
"function returning record called in context "
247 "that cannot accept type record")));
272 Py_XDECREF(srfstate->
iter);
273 srfstate->
iter = NULL;
295 if (srfstate->
iter == NULL)
324 PyObject *
volatile plargs = NULL;
325 PyObject *
volatile plrv = NULL;
386 if (PyUnicode_Check(plrv))
391 (
errcode(ERRCODE_DATA_EXCEPTION),
392 errmsg(
"unexpected return value from trigger procedure"),
393 errdetail(
"Expected None or a string.")));
406 (
errmsg(
"PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
415 (
errcode(ERRCODE_DATA_EXCEPTION),
416 errmsg(
"unexpected return value from trigger procedure"),
417 errdetail(
"Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
437 PyObject *
volatile arg = NULL;
467 if (PyList_SetItem(
args,
i,
arg) == -1)
468 PLy_elog(
ERROR,
"PyList_SetItem() failed, while setting up arguments");
472 PLy_elog(
ERROR,
"PyDict_SetItemString() failed, while setting up arguments");
506 proc->
nargs *
sizeof(PyObject *));
510 result->
args = PyDict_GetItemString(proc->
globals,
"args");
511 Py_XINCREF(result->
args);
516 result->
td = PyDict_GetItemString(proc->
globals,
"TD");
517 Py_XINCREF(result->
td);
551 for (
i = 0;
i < savedargs->
nargs;
i++)
565 PyDict_SetItemString(proc->
globals,
"args", savedargs->
args);
566 Py_DECREF(savedargs->
args);
572 PyDict_SetItemString(proc->
globals,
"TD", savedargs->
td);
573 Py_DECREF(savedargs->
td);
589 for (
i = 0;
i < savedargs->
nargs;
i++)
595 Py_XDECREF(savedargs->
args);
596 Py_XDECREF(savedargs->
td);
686 Py_XDECREF(srfstate->
iter);
687 srfstate->
iter = NULL;
727 pltdata = PyDict_New();
749 PyDict_SetItemString(pltdata,
"name", pltname);
755 PyDict_SetItemString(pltdata,
"relid", pltrelid);
761 PyDict_SetItemString(pltdata,
"table_name", plttablename);
762 Py_DECREF(plttablename);
767 PyDict_SetItemString(pltdata,
"table_schema", plttableschema);
768 Py_DECREF(plttableschema);
782 PyDict_SetItemString(pltdata,
"when", pltwhen);
788 PyDict_SetItemString(pltdata,
"level", pltlevel);
800 PyDict_SetItemString(pltdata,
"old", Py_None);
805 PyDict_SetItemString(pltdata,
"new", pytnew);
813 PyDict_SetItemString(pltdata,
"new", Py_None);
818 PyDict_SetItemString(pltdata,
"old", pytold);
830 PyDict_SetItemString(pltdata,
"new", pytnew);
836 PyDict_SetItemString(pltdata,
"old", pytold);
846 PyDict_SetItemString(pltdata,
"event", pltevent);
852 PyDict_SetItemString(pltdata,
"level", pltlevel);
855 PyDict_SetItemString(pltdata,
"old", Py_None);
856 PyDict_SetItemString(pltdata,
"new", Py_None);
873 PyDict_SetItemString(pltdata,
"event", pltevent);
888 Assert(pltargs && pltargs != Py_None);
897 PyList_SetItem(pltargs,
i, pltarg);
902 Assert(pltargs == Py_None);
904 PyDict_SetItemString(pltdata,
"args", pltargs);
926 PyObject *
volatile plntup;
927 PyObject *
volatile plkeys;
928 PyObject *
volatile plval;
929 Datum *
volatile modvalues;
930 bool *
volatile modnulls;
931 bool *
volatile modrepls;
938 plntup = plkeys = plval = NULL;
949 if ((plntup = PyDict_GetItemString(pltd,
"new")) == NULL)
951 (
errcode(ERRCODE_UNDEFINED_OBJECT),
952 errmsg(
"TD[\"new\"] deleted, cannot modify row")));
954 if (!PyDict_Check(plntup))
956 (
errcode(ERRCODE_DATATYPE_MISMATCH),
957 errmsg(
"TD[\"new\"] is not a dictionary")));
959 plkeys = PyDict_Keys(plntup);
960 nkeys = PyList_Size(plkeys);
965 modnulls = (
bool *)
palloc0(tupdesc->
natts *
sizeof(
bool));
966 modrepls = (
bool *)
palloc0(tupdesc->
natts *
sizeof(
bool));
968 for (
i = 0;
i < nkeys;
i++)
975 platt = PyList_GetItem(plkeys,
i);
976 if (PyUnicode_Check(platt))
981 (
errcode(ERRCODE_DATATYPE_MISMATCH),
982 errmsg(
"TD[\"new\"] dictionary key at ordinal position %d is not a string",
i)));
988 (
errcode(ERRCODE_UNDEFINED_COLUMN),
989 errmsg(
"key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
993 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
994 errmsg(
"cannot set system attribute \"%s\"",
998 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
999 errmsg(
"cannot set generated column \"%s\"",
1002 plval = PyDict_GetItem(plntup, platt);
1004 elog(
FATAL,
"Python interpreter is probably corrupted");
1013 &modnulls[attn - 1]);
1014 modrepls[attn - 1] =
true;
1064 PyObject *rv = NULL;
1067 PyDict_SetItemString(proc->
globals, kargs, vargs);
1071 #if PY_VERSION_HEX >= 0x03020000
1072 rv = PyEval_EvalCode(proc->
code,
1075 rv = PyEval_EvalCode((PyCodeObject *) proc->
code,
1106 Assert(save_subxact_level >= 0);
1115 (
errmsg(
"forcibly aborting a subtransaction that has not been exited")));
1124 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::@183 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)