PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
plpy_subxactobject.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "utils/memutils.h"
#include "plpython.h"
#include "plpy_subxactobject.h"
#include "plpy_elog.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

static void PLy_subtransaction_dealloc ( PyObject *  subxact)
static

Definition at line 101 of file plpy_subxactobject.c.

102 {
103 }
static PyObject * PLy_subtransaction_enter ( PyObject *  self,
PyObject *  unused 
)
static

Definition at line 114 of file plpy_subxactobject.c.

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

115 {
116  PLySubtransactionData *subxactdata;
117  MemoryContext oldcontext;
119 
120  if (subxact->started)
121  {
122  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
123  return NULL;
124  }
125 
126  if (subxact->exited)
127  {
128  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
129  return NULL;
130  }
131 
132  subxact->started = true;
133  oldcontext = CurrentMemoryContext;
134 
135  subxactdata = (PLySubtransactionData *)
137  sizeof(PLySubtransactionData));
138 
139  subxactdata->oldcontext = oldcontext;
140  subxactdata->oldowner = CurrentResourceOwner;
141 
143 
144  /* Be sure that cells of explicit_subtransactions list are long-lived */
147 
148  /* Caller wants to stay in original memory context */
149  MemoryContextSwitchTo(oldcontext);
150 
151  Py_INCREF(self);
152  return self;
153 }
MemoryContext TopTransactionContext
Definition: mcxt.c:48
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
List * explicit_subtransactions
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
List * lcons(void *datum, List *list)
Definition: list.c:259
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
PyObject_HEAD bool started
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
static PyObject * PLy_subtransaction_exit ( PyObject *  self,
PyObject *  args 
)
static

Definition at line 167 of file plpy_subxactobject.c.

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

168 {
169  PyObject *type;
170  PyObject *value;
171  PyObject *traceback;
172  PLySubtransactionData *subxactdata;
174 
175  if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback))
176  return NULL;
177 
178  if (!subxact->started)
179  {
180  PLy_exception_set(PyExc_ValueError, "this subtransaction has not been entered");
181  return NULL;
182  }
183 
184  if (subxact->exited)
185  {
186  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
187  return NULL;
188  }
189 
191  {
192  PLy_exception_set(PyExc_ValueError, "there is no subtransaction to exit from");
193  return NULL;
194  }
195 
196  subxact->exited = true;
197 
198  if (type != Py_None)
199  {
200  /* Abort the inner transaction */
202  }
203  else
204  {
206  }
207 
210 
211  MemoryContextSwitchTo(subxactdata->oldcontext);
212  CurrentResourceOwner = subxactdata->oldowner;
213  pfree(subxactdata);
214 
215  Py_INCREF(Py_None);
216  return Py_None;
217 }
#define NIL
Definition: pg_list.h:69
static struct @76 value
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:500
void pfree(void *pointer)
Definition: mcxt.c:992
#define linitial(l)
Definition: pg_list.h:110
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
List * explicit_subtransactions
#define NULL
Definition: c.h:226
PyObject_HEAD bool started
List * list_delete_first(List *list)
Definition: list.c:666
void PLy_subtransaction_init_type ( void  )

Definition at line 76 of file plpy_subxactobject.c.

References elog, ERROR, and PLy_SubtransactionType.

Referenced by PLy_init_plpy().

77 {
78  if (PyType_Ready(&PLy_SubtransactionType) < 0)
79  elog(ERROR, "could not initialize PLy_SubtransactionType");
80 }
#define ERROR
Definition: elog.h:43
#define elog
Definition: elog.h:219
static PyTypeObject PLy_SubtransactionType
PyObject* PLy_subtransaction_new ( PyObject *  self,
PyObject *  unused 
)

Definition at line 84 of file plpy_subxactobject.c.

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

85 {
87 
89 
90  if (ob == NULL)
91  return NULL;
92 
93  ob->started = false;
94  ob->exited = false;
95 
96  return (PyObject *) ob;
97 }
#define NULL
Definition: c.h:226
PyObject_HEAD bool started
static PyTypeObject PLy_SubtransactionType

Variable Documentation

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

Definition at line 26 of file plpy_subxactobject.c.

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)
#define NULL
Definition: c.h:226
static PyObject * PLy_subtransaction_exit(PyObject *self, PyObject *args)

Definition at line 30 of file plpy_subxactobject.c.

PyTypeObject PLy_SubtransactionType
static

Definition at line 39 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type(), and PLy_subtransaction_new().