PostgreSQL Source Code  git master
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

◆ PLy_subtransaction_dealloc()

static void PLy_subtransaction_dealloc ( PyObject *  subxact)
static

Definition at line 76 of file plpy_subxactobject.c.

77 {
78 }

◆ PLy_subtransaction_enter()

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

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

90 {
91  PLySubtransactionData *subxactdata;
92  MemoryContext oldcontext;
94 
95  if (subxact->started)
96  {
97  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been entered");
98  return NULL;
99  }
100 
101  if (subxact->exited)
102  {
103  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
104  return NULL;
105  }
106 
107  subxact->started = true;
108  oldcontext = CurrentMemoryContext;
109 
110  subxactdata = (PLySubtransactionData *)
112  sizeof(PLySubtransactionData));
113 
114  subxactdata->oldcontext = oldcontext;
115  subxactdata->oldowner = CurrentResourceOwner;
116 
118 
119  /* Be sure that cells of explicit_subtransactions list are long-lived */
122 
123  /* Caller wants to stay in original memory context */
124  MemoryContextSwitchTo(oldcontext);
125 
126  Py_INCREF(self);
127  return self;
128 }
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:502
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:4357
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 142 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.

143 {
144  PyObject *type;
145  PyObject *value;
146  PyObject *traceback;
147  PLySubtransactionData *subxactdata;
149 
150  if (!PyArg_ParseTuple(args, "OOO", &type, &value, &traceback))
151  return NULL;
152 
153  if (!subxact->started)
154  {
155  PLy_exception_set(PyExc_ValueError, "this subtransaction has not been entered");
156  return NULL;
157  }
158 
159  if (subxact->exited)
160  {
161  PLy_exception_set(PyExc_ValueError, "this subtransaction has already been exited");
162  return NULL;
163  }
164 
166  {
167  PLy_exception_set(PyExc_ValueError, "there is no subtransaction to exit from");
168  return NULL;
169  }
170 
171  subxact->exited = true;
172 
173  if (type != Py_None)
174  {
175  /* Abort the inner transaction */
177  }
178  else
179  {
181  }
182 
185 
186  MemoryContextSwitchTo(subxactdata->oldcontext);
187  CurrentResourceOwner = subxactdata->oldowner;
188  pfree(subxactdata);
189 
190  Py_RETURN_NONE;
191 }
#define NIL
Definition: pg_list.h:65
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4428
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:502
void pfree(void *pointer)
Definition: mcxt.c:1056
#define linitial(l)
Definition: pg_list.h:195
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4462
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 51 of file plpy_subxactobject.c.

References elog, ERROR, and PLy_SubtransactionType.

Referenced by PLy_init_plpy().

52 {
53  if (PyType_Ready(&PLy_SubtransactionType) < 0)
54  elog(ERROR, "could not initialize PLy_SubtransactionType");
55 }
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:226
static PyTypeObject PLy_SubtransactionType

◆ PLy_subtransaction_new()

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

Definition at line 59 of file plpy_subxactobject.c.

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

60 {
62 
64 
65  if (ob == NULL)
66  return NULL;
67 
68  ob->started = false;
69  ob->exited = false;
70 
71  return (PyObject *) ob;
72 }
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 26 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 30 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 39 of file plpy_subxactobject.c.

Referenced by PLy_subtransaction_init_type(), and PLy_subtransaction_new().