PostgreSQL Source Code git master
plpy_subxactobject.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "plpy_elog.h"
#include "plpy_subxactobject.h"
#include "plpy_util.h"
#include "utils/memutils.h"
Include dependency graph for plpy_subxactobject.c:

Go to the source code of this file.

Functions

static PyObject * PLy_subtransaction_enter (PyObject *self, PyObject *unused)
 
static PyObject * PLy_subtransaction_exit (PyObject *self, PyObject *args)
 
void PLy_subtransaction_init_type (void)
 
PyObject * PLy_subtransaction_new (PyObject *self, PyObject *unused)
 

Variables

Listexplicit_subtransactions = NIL
 
static char PLy_subtransaction_doc []
 
static PyMethodDef PLy_subtransaction_methods []
 
static PyType_Slot PLySubtransaction_slots []
 
static PyType_Spec PLySubtransaction_spec
 
static PyTypeObject * PLy_SubtransactionType
 

Function Documentation

◆ PLy_subtransaction_enter()

static PyObject * PLy_subtransaction_enter ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 94 of file plpy_subxactobject.c.

95{
96 PLySubtransactionData *subxactdata;
97 MemoryContext oldcontext;
99
100 if (subxact->started)
101 {
102 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
103 return NULL;
104 }
105
106 if (subxact->exited)
107 {
108 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
109 return NULL;
110 }
111
112 subxact->started = true;
113 oldcontext = CurrentMemoryContext;
114
115 subxactdata = (PLySubtransactionData *)
117 sizeof(PLySubtransactionData));
118
119 subxactdata->oldcontext = oldcontext;
120 subxactdata->oldowner = CurrentResourceOwner;
121
123
124 /* Be sure that cells of explicit_subtransactions list are long-lived */
127
128 /* Caller wants to stay in original memory context */
129 MemoryContextSwitchTo(oldcontext);
130
131 Py_INCREF(self);
132 return self;
133}
List * lcons(void *datum, List *list)
Definition: list.c:495
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1260
MemoryContext TopTransactionContext
Definition: mcxt.c:170
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:472
List * explicit_subtransactions
ResourceOwner CurrentResourceOwner
Definition: resowner.c:173
PyObject_HEAD bool started
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4694

References BeginInternalSubTransaction(), CurrentMemoryContext, CurrentResourceOwner, PLySubtransactionObject::exited, explicit_subtransactions, lcons(), MemoryContextAlloc(), MemoryContextSwitchTo(), PLySubtransactionData::oldcontext, PLySubtransactionData::oldowner, PLy_exception_set(), PLySubtransactionObject::started, and TopTransactionContext.

◆ PLy_subtransaction_exit()

static PyObject * PLy_subtransaction_exit ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 147 of file plpy_subxactobject.c.

148{
149 PyObject *type;
150 PyObject *value;
151 PyObject *traceback;
152 PLySubtransactionData *subxactdata;
154
155 if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback))
156 return NULL;
157
158 if (!subxact->started)
159 {
160 PLy_exception_set(PyExc_ValueError, "this subtransaction has not been entered");
161 return NULL;
162 }
163
164 if (subxact->exited)
165 {
166 PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
167 return NULL;
168 }
169
171 {
172 PLy_exception_set(PyExc_ValueError, "there is no subtransaction to exit from");
173 return NULL;
174 }
175
176 subxact->exited = true;
177
178 if (type != Py_None)
179 {
180 /* Abort the inner transaction */
182 }
183 else
184 {
186 }
187
190
191 MemoryContextSwitchTo(subxactdata->oldcontext);
192 CurrentResourceOwner = subxactdata->oldowner;
193 pfree(subxactdata);
194
195 Py_RETURN_NONE;
196}
static struct @165 value
List * list_delete_first(List *list)
Definition: list.c:943
void pfree(void *pointer)
Definition: mcxt.c:2150
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
const char * type
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4796
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4768

References generate_unaccent_rules::args, CurrentResourceOwner, PLySubtransactionObject::exited, explicit_subtransactions, linitial, list_delete_first(), MemoryContextSwitchTo(), NIL, PLySubtransactionData::oldcontext, PLySubtransactionData::oldowner, pfree(), PLy_exception_set(), ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), PLySubtransactionObject::started, type, and value.

◆ PLy_subtransaction_init_type()

void PLy_subtransaction_init_type ( void  )

Definition at line 58 of file plpy_subxactobject.c.

59{
60 PLy_SubtransactionType = (PyTypeObject *) PyType_FromSpec(&PLySubtransaction_spec);
62 elog(ERROR, "could not initialize PLy_SubtransactionType");
63}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
static PyTypeObject * PLy_SubtransactionType
static PyType_Spec PLySubtransaction_spec

References elog, ERROR, PLy_SubtransactionType, and PLySubtransaction_spec.

Referenced by PLy_init_plpy().

◆ PLy_subtransaction_new()

PyObject * PLy_subtransaction_new ( PyObject *  self,
PyObject *  unused 
)

Definition at line 67 of file plpy_subxactobject.c.

68{
70
72 if (ob == NULL)
73 return NULL;
74#if PY_VERSION_HEX < 0x03080000
75 /* Workaround for Python issue 35810; no longer necessary in Python 3.8 */
76 Py_INCREF(PLy_SubtransactionType);
77#endif
78
79 ob->started = false;
80 ob->exited = false;
81
82 return (PyObject *) ob;
83}

References PLySubtransactionObject::exited, PLy_SubtransactionType, and PLySubtransactionObject::started.

Variable Documentation

◆ explicit_subtransactions

◆ PLy_subtransaction_doc

char PLy_subtransaction_doc[]
static
Initial value:
=
"PostgreSQL subtransaction context manager"

Definition at line 21 of file plpy_subxactobject.c.

◆ PLy_subtransaction_methods

PyMethodDef PLy_subtransaction_methods[]
static
Initial value:
= {
{"__enter__", PLy_subtransaction_enter, METH_VARARGS, NULL},
{"__exit__", PLy_subtransaction_exit, METH_VARARGS, NULL},
{"enter", PLy_subtransaction_enter, METH_VARARGS, NULL},
{"exit", PLy_subtransaction_exit, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
}
static PyObject * PLy_subtransaction_exit(PyObject *self, PyObject *args)
static PyObject * PLy_subtransaction_enter(PyObject *self, PyObject *unused)

Definition at line 24 of file plpy_subxactobject.c.

◆ PLy_SubtransactionType

PyTypeObject* PLy_SubtransactionType
static

Definition at line 54 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type(), and PLy_subtransaction_new().

◆ PLySubtransaction_slots

PyType_Slot PLySubtransaction_slots[]
static
Initial value:
=
{
{
Py_tp_doc, (char *) PLy_subtransaction_doc
},
{
},
{
0, NULL
}
}
static PyMethodDef PLy_subtransaction_methods[]
static char PLy_subtransaction_doc[]

Definition at line 33 of file plpy_subxactobject.c.

◆ PLySubtransaction_spec

PyType_Spec PLySubtransaction_spec
static
Initial value:
=
{
.name = "PLySubtransaction",
.basicsize = sizeof(PLySubtransactionObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
}
static PyType_Slot PLySubtransaction_slots[]
struct PLySubtransactionObject PLySubtransactionObject

Definition at line 46 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type().