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 "plpython.h"
#include "utils/memutils.h"
Include dependency graph for plpy_subxactobject.c:

Go to the source code of this file.

Functions

static void PLy_subtransaction_dealloc (PyObject *subxact)
 
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 PyTypeObject PLy_SubtransactionType
 

Function Documentation

◆ PLy_subtransaction_dealloc()

static void PLy_subtransaction_dealloc ( PyObject *  subxact)
static

Definition at line 72 of file plpy_subxactobject.c.

73 {
74 }

◆ PLy_subtransaction_enter()

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

Definition at line 85 of file plpy_subxactobject.c.

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

86 {
87  PLySubtransactionData *subxactdata;
88  MemoryContext oldcontext;
90 
91  if (subxact->started)
92  {
93  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
94  return NULL;
95  }
96 
97  if (subxact->exited)
98  {
99  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
100  return NULL;
101  }
102 
103  subxact->started = true;
104  oldcontext = CurrentMemoryContext;
105 
106  subxactdata = (PLySubtransactionData *)
108  sizeof(PLySubtransactionData));
109 
110  subxactdata->oldcontext = oldcontext;
111  subxactdata->oldowner = CurrentResourceOwner;
112 
114 
115  /* Be sure that cells of explicit_subtransactions list are long-lived */
118 
119  /* Caller wants to stay in original memory context */
120  MemoryContextSwitchTo(oldcontext);
121 
122  Py_INCREF(self);
123  return self;
124 }
MemoryContext TopTransactionContext
Definition: mcxt.c:49
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:487
List * explicit_subtransactions
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
List * lcons(void *datum, List *list)
Definition: list.c:454
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4359
PyObject_HEAD bool started
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796

◆ PLy_subtransaction_exit()

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

Definition at line 138 of file plpy_subxactobject.c.

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

139 {
140  PyObject *type;
141  PyObject *value;
142  PyObject *traceback;
143  PLySubtransactionData *subxactdata;
145 
146  if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback))
147  return NULL;
148 
149  if (!subxact->started)
150  {
151  PLy_exception_set(PyExc_ValueError, "this subtransaction has not been entered");
152  return NULL;
153  }
154 
155  if (subxact->exited)
156  {
157  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
158  return NULL;
159  }
160 
162  {
163  PLy_exception_set(PyExc_ValueError, "there is no subtransaction to exit from");
164  return NULL;
165  }
166 
167  subxact->exited = true;
168 
169  if (type != Py_None)
170  {
171  /* Abort the inner transaction */
173  }
174  else
175  {
177  }
178 
181 
182  MemoryContextSwitchTo(subxactdata->oldcontext);
183  CurrentResourceOwner = subxactdata->oldowner;
184  pfree(subxactdata);
185 
186  Py_RETURN_NONE;
187 }
#define NIL
Definition: pg_list.h:65
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4430
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static struct @145 value
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:487
void pfree(void *pointer)
Definition: mcxt.c:1056
#define linitial(l)
Definition: pg_list.h:195
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4464
List * explicit_subtransactions
PyObject_HEAD bool started
List * list_delete_first(List *list)
Definition: list.c:861

◆ PLy_subtransaction_init_type()

void PLy_subtransaction_init_type ( void  )

Definition at line 47 of file plpy_subxactobject.c.

References elog, ERROR, and PLy_SubtransactionType.

Referenced by PLy_init_plpy().

48 {
49  if (PyType_Ready(&PLy_SubtransactionType) < 0)
50  elog(ERROR, "could not initialize PLy_SubtransactionType");
51 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228
static PyTypeObject PLy_SubtransactionType

◆ PLy_subtransaction_new()

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

Definition at line 55 of file plpy_subxactobject.c.

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

56 {
58 
60 
61  if (ob == NULL)
62  return NULL;
63 
64  ob->started = false;
65  ob->exited = false;
66 
67  return (PyObject *) ob;
68 }
PyObject_HEAD bool started
static PyTypeObject PLy_SubtransactionType

Variable Documentation

◆ explicit_subtransactions

List* explicit_subtransactions = NIL

◆ PLy_subtransaction_doc

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

Definition at line 22 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_enter(PyObject *self, PyObject *unused)
static PyObject * PLy_subtransaction_exit(PyObject *self, PyObject *args)

Definition at line 26 of file plpy_subxactobject.c.

◆ PLy_SubtransactionType

PyTypeObject PLy_SubtransactionType
static
Initial value:
= {
.tp_name = "PLySubtransaction",
.tp_basicsize = sizeof(PLySubtransactionObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
}
static char PLy_subtransaction_doc[]
#define PyVarObject_HEAD_INIT(type, size)
Definition: plpython.h:113
static void PLy_subtransaction_dealloc(PyObject *subxact)
struct PLySubtransactionObject PLySubtransactionObject
static PyMethodDef PLy_subtransaction_methods[]

Definition at line 35 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type(), and PLy_subtransaction_new().