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 71 of file plpy_subxactobject.c.

72 {
73 }

◆ PLy_subtransaction_enter()

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

Definition at line 84 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.

85 {
86  PLySubtransactionData *subxactdata;
87  MemoryContext oldcontext;
89 
90  if (subxact->started)
91  {
92  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
93  return NULL;
94  }
95 
96  if (subxact->exited)
97  {
98  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
99  return NULL;
100  }
101 
102  subxact->started = true;
103  oldcontext = CurrentMemoryContext;
104 
105  subxactdata = (PLySubtransactionData *)
107  sizeof(PLySubtransactionData));
108 
109  subxactdata->oldcontext = oldcontext;
110  subxactdata->oldowner = CurrentResourceOwner;
111 
113 
114  /* Be sure that cells of explicit_subtransactions list are long-lived */
117 
118  /* Caller wants to stay in original memory context */
119  MemoryContextSwitchTo(oldcontext);
120 
121  Py_INCREF(self);
122  return self;
123 }
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:479
List * explicit_subtransactions
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
List * lcons(void *datum, List *list)
Definition: list.c:453
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4408
PyObject_HEAD bool started
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797

◆ PLy_subtransaction_exit()

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

Definition at line 137 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.

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

◆ PLy_subtransaction_init_type()

void PLy_subtransaction_init_type ( void  )

Definition at line 46 of file plpy_subxactobject.c.

References elog, ERROR, and PLy_SubtransactionType.

Referenced by PLy_init_plpy().

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

◆ PLy_subtransaction_new()

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

Definition at line 54 of file plpy_subxactobject.c.

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

55 {
57 
59 
60  if (ob == NULL)
61  return NULL;
62 
63  ob->started = false;
64  ob->exited = false;
65 
66  return (PyObject *) ob;
67 }
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 25 of file plpy_subxactobject.c.

◆ PLy_SubtransactionType

PyTypeObject PLy_SubtransactionType
static
Initial value:
= {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "PLySubtransaction",
.tp_basicsize = sizeof(PLySubtransactionObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
}
static char PLy_subtransaction_doc[]
static void PLy_subtransaction_dealloc(PyObject *subxact)
struct PLySubtransactionObject PLySubtransactionObject
static PyMethodDef PLy_subtransaction_methods[]

Definition at line 34 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type(), and PLy_subtransaction_new().