PostgreSQL Source Code git master
plpy_plpymodule.c
Go to the documentation of this file.
1/*
2 * the plpy module
3 *
4 * src/pl/plpython/plpy_plpymodule.c
5 */
6
7#include "postgres.h"
8
9#include "mb/pg_wchar.h"
10#include "plpy_cursorobject.h"
11#include "plpy_elog.h"
12#include "plpy_planobject.h"
13#include "plpy_plpymodule.h"
14#include "plpy_resultobject.h"
15#include "plpy_spi.h"
16#include "plpy_subxactobject.h"
17#include "plpython.h"
18#include "utils/builtins.h"
19
21
22
23static void PLy_add_exceptions(PyObject *plpy);
24static PyObject *PLy_create_exception(char *name,
25 PyObject *base, PyObject *dict,
26 const char *modname, PyObject *mod);
27static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base);
28
29/* module functions */
30static PyObject *PLy_debug(PyObject *self, PyObject *args, PyObject *kw);
31static PyObject *PLy_log(PyObject *self, PyObject *args, PyObject *kw);
32static PyObject *PLy_info(PyObject *self, PyObject *args, PyObject *kw);
33static PyObject *PLy_notice(PyObject *self, PyObject *args, PyObject *kw);
34static PyObject *PLy_warning(PyObject *self, PyObject *args, PyObject *kw);
35static PyObject *PLy_error(PyObject *self, PyObject *args, PyObject *kw);
36static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
37static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
38static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
39static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
40
41
42/* A list of all known exceptions, generated from backend/utils/errcodes.txt */
43typedef struct ExceptionMap
44{
45 char *name;
46 char *classname;
49
50static const ExceptionMap exception_map[] = {
51#include "spiexceptions.h"
52 {NULL, NULL, 0}
53};
54
55static PyMethodDef PLy_methods[] = {
56 /*
57 * logging methods
58 */
59 {"debug", (PyCFunction) (pg_funcptr_t) PLy_debug, METH_VARARGS | METH_KEYWORDS, NULL},
60 {"log", (PyCFunction) (pg_funcptr_t) PLy_log, METH_VARARGS | METH_KEYWORDS, NULL},
61 {"info", (PyCFunction) (pg_funcptr_t) PLy_info, METH_VARARGS | METH_KEYWORDS, NULL},
62 {"notice", (PyCFunction) (pg_funcptr_t) PLy_notice, METH_VARARGS | METH_KEYWORDS, NULL},
63 {"warning", (PyCFunction) (pg_funcptr_t) PLy_warning, METH_VARARGS | METH_KEYWORDS, NULL},
64 {"error", (PyCFunction) (pg_funcptr_t) PLy_error, METH_VARARGS | METH_KEYWORDS, NULL},
65 {"fatal", (PyCFunction) (pg_funcptr_t) PLy_fatal, METH_VARARGS | METH_KEYWORDS, NULL},
66
67 /*
68 * create a stored plan
69 */
70 {"prepare", PLy_spi_prepare, METH_VARARGS, NULL},
71
72 /*
73 * execute a plan or query
74 */
75 {"execute", PLy_spi_execute, METH_VARARGS, NULL},
76
77 /*
78 * escaping strings
79 */
80 {"quote_literal", PLy_quote_literal, METH_VARARGS, NULL},
81 {"quote_nullable", PLy_quote_nullable, METH_VARARGS, NULL},
82 {"quote_ident", PLy_quote_ident, METH_VARARGS, NULL},
83
84 /*
85 * create the subtransaction context manager
86 */
87 {"subtransaction", PLy_subtransaction_new, METH_NOARGS, NULL},
88
89 /*
90 * create a cursor
91 */
92 {"cursor", PLy_cursor, METH_VARARGS, NULL},
93
94 /*
95 * transaction control
96 */
97 {"commit", PLy_commit, METH_NOARGS, NULL},
98 {"rollback", PLy_rollback, METH_NOARGS, NULL},
99
100 {NULL, NULL, 0, NULL}
101};
102
103static PyMethodDef PLy_exc_methods[] = {
104 {NULL, NULL, 0, NULL}
105};
106
107static PyModuleDef PLy_module = {
108 PyModuleDef_HEAD_INIT,
109 .m_name = "plpy",
110 .m_size = -1,
111 .m_methods = PLy_methods,
112};
113
114static PyModuleDef PLy_exc_module = {
115 PyModuleDef_HEAD_INIT,
116 .m_name = "spiexceptions",
117 .m_size = -1,
118 .m_methods = PLy_exc_methods,
119};
120
121/*
122 * Must have external linkage, because PyMODINIT_FUNC does dllexport on
123 * Windows-like platforms.
124 */
125PyMODINIT_FUNC
127{
128 PyObject *m;
129
130 m = PyModule_Create(&PLy_module);
131 if (m == NULL)
132 return NULL;
133
135
136 return m;
137}
138
139void
141{
142 PyObject *main_mod,
143 *main_dict,
144 *plpy_mod;
145
146 /*
147 * initialize plpy module
148 */
153
154 PyModule_Create(&PLy_module);
155
156 /* PyDict_SetItemString(plpy, "PlanType", (PyObject *) &PLy_PlanType); */
157
158 /*
159 * initialize main module, and add plpy
160 */
161 main_mod = PyImport_AddModule("__main__");
162 main_dict = PyModule_GetDict(main_mod);
163 plpy_mod = PyImport_AddModule("plpy");
164 if (plpy_mod == NULL)
165 PLy_elog(ERROR, "could not import \"plpy\" module");
166 PyDict_SetItemString(main_dict, "plpy", plpy_mod);
167 if (PyErr_Occurred())
168 PLy_elog(ERROR, "could not import \"plpy\" module");
169}
170
171static void
172PLy_add_exceptions(PyObject *plpy)
173{
174 PyObject *excmod;
175 HASHCTL hash_ctl;
176
177 excmod = PyModule_Create(&PLy_exc_module);
178 if (excmod == NULL)
179 PLy_elog(ERROR, "could not create the spiexceptions module");
180
181 /*
182 * PyModule_AddObject does not add a refcount to the object, for some odd
183 * reason; we must do that.
184 */
185 Py_INCREF(excmod);
186 if (PyModule_AddObject(plpy, "spiexceptions", excmod) < 0)
187 PLy_elog(ERROR, "could not add the spiexceptions module");
188
189 PLy_exc_error = PLy_create_exception("plpy.Error", NULL, NULL,
190 "Error", plpy);
191 PLy_exc_fatal = PLy_create_exception("plpy.Fatal", NULL, NULL,
192 "Fatal", plpy);
193 PLy_exc_spi_error = PLy_create_exception("plpy.SPIError", NULL, NULL,
194 "SPIError", plpy);
195
196 hash_ctl.keysize = sizeof(int);
197 hash_ctl.entrysize = sizeof(PLyExceptionEntry);
198 PLy_spi_exceptions = hash_create("PL/Python SPI exceptions", 256,
199 &hash_ctl, HASH_ELEM | HASH_BLOBS);
200
202}
203
204/*
205 * Create an exception object and add it to the module
206 */
207static PyObject *
208PLy_create_exception(char *name, PyObject *base, PyObject *dict,
209 const char *modname, PyObject *mod)
210{
211 PyObject *exc;
212
213 exc = PyErr_NewException(name, base, dict);
214 if (exc == NULL)
215 PLy_elog(ERROR, NULL);
216
217 /*
218 * PyModule_AddObject does not add a refcount to the object, for some odd
219 * reason; we must do that.
220 */
221 Py_INCREF(exc);
222 PyModule_AddObject(mod, modname, exc);
223
224 /*
225 * The caller will also store a pointer to the exception object in some
226 * permanent variable, so add another ref to account for that. This is
227 * probably excessively paranoid, but let's be sure.
228 */
229 Py_INCREF(exc);
230 return exc;
231}
232
233/*
234 * Add all the autogenerated exceptions as subclasses of SPIError
235 */
236static void
237PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
238{
239 int i;
240
241 for (i = 0; exception_map[i].name != NULL; i++)
242 {
243 bool found;
244 PyObject *exc;
245 PLyExceptionEntry *entry;
246 PyObject *sqlstate;
247 PyObject *dict = PyDict_New();
248
249 if (dict == NULL)
250 PLy_elog(ERROR, NULL);
251
253 if (sqlstate == NULL)
254 PLy_elog(ERROR, "could not generate SPI exceptions");
255
256 PyDict_SetItemString(dict, "sqlstate", sqlstate);
257 Py_DECREF(sqlstate);
258
259 exc = PLy_create_exception(exception_map[i].name, base, dict,
260 exception_map[i].classname, mod);
261
262 entry = hash_search(PLy_spi_exceptions, &exception_map[i].sqlstate,
263 HASH_ENTER, &found);
264 Assert(!found);
265 entry->exc = exc;
266 }
267}
268
269
270/*
271 * the python interface to the elog function
272 * don't confuse these with PLy_elog
273 */
274static PyObject *PLy_output(volatile int level, PyObject *self,
275 PyObject *args, PyObject *kw);
276
277static PyObject *
278PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
279{
280 return PLy_output(DEBUG2, self, args, kw);
281}
282
283static PyObject *
284PLy_log(PyObject *self, PyObject *args, PyObject *kw)
285{
286 return PLy_output(LOG, self, args, kw);
287}
288
289static PyObject *
290PLy_info(PyObject *self, PyObject *args, PyObject *kw)
291{
292 return PLy_output(INFO, self, args, kw);
293}
294
295static PyObject *
296PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
297{
298 return PLy_output(NOTICE, self, args, kw);
299}
300
301static PyObject *
302PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
303{
304 return PLy_output(WARNING, self, args, kw);
305}
306
307static PyObject *
308PLy_error(PyObject *self, PyObject *args, PyObject *kw)
309{
310 return PLy_output(ERROR, self, args, kw);
311}
312
313static PyObject *
314PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
315{
316 return PLy_output(FATAL, self, args, kw);
317}
318
319static PyObject *
320PLy_quote_literal(PyObject *self, PyObject *args)
321{
322 const char *str;
323 char *quoted;
324 PyObject *ret;
325
326 if (!PyArg_ParseTuple(args, "s:quote_literal", &str))
327 return NULL;
328
329 quoted = quote_literal_cstr(str);
330 ret = PLyUnicode_FromString(quoted);
331 pfree(quoted);
332
333 return ret;
334}
335
336static PyObject *
337PLy_quote_nullable(PyObject *self, PyObject *args)
338{
339 const char *str;
340 char *quoted;
341 PyObject *ret;
342
343 if (!PyArg_ParseTuple(args, "z:quote_nullable", &str))
344 return NULL;
345
346 if (str == NULL)
347 return PLyUnicode_FromString("NULL");
348
349 quoted = quote_literal_cstr(str);
350 ret = PLyUnicode_FromString(quoted);
351 pfree(quoted);
352
353 return ret;
354}
355
356static PyObject *
357PLy_quote_ident(PyObject *self, PyObject *args)
358{
359 const char *str;
360 const char *quoted;
361 PyObject *ret;
362
363 if (!PyArg_ParseTuple(args, "s:quote_ident", &str))
364 return NULL;
365
366 quoted = quote_identifier(str);
367 ret = PLyUnicode_FromString(quoted);
368
369 return ret;
370}
371
372/* enforce cast of object to string */
373static char *
374object_to_string(PyObject *obj)
375{
376 if (obj)
377 {
378 PyObject *so = PyObject_Str(obj);
379
380 if (so != NULL)
381 {
382 char *str;
383
385 Py_DECREF(so);
386
387 return str;
388 }
389 }
390
391 return NULL;
392}
393
394static PyObject *
395PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
396{
397 int sqlstate = 0;
398 char *volatile sqlstatestr = NULL;
399 char *volatile message = NULL;
400 char *volatile detail = NULL;
401 char *volatile hint = NULL;
402 char *volatile column_name = NULL;
403 char *volatile constraint_name = NULL;
404 char *volatile datatype_name = NULL;
405 char *volatile table_name = NULL;
406 char *volatile schema_name = NULL;
407 volatile MemoryContext oldcontext;
408 PyObject *key,
409 *value;
410 PyObject *volatile so;
411 Py_ssize_t pos = 0;
412
413 if (PyTuple_Size(args) == 1)
414 {
415 /*
416 * Treat single argument specially to avoid undesirable ('tuple',)
417 * decoration.
418 */
419 PyObject *o;
420
421 if (!PyArg_UnpackTuple(args, "plpy.elog", 1, 1, &o))
422 PLy_elog(ERROR, "could not unpack arguments in plpy.elog");
423 so = PyObject_Str(o);
424 }
425 else
426 so = PyObject_Str(args);
427
428 if (so == NULL || ((message = PLyUnicode_AsString(so)) == NULL))
429 {
430 level = ERROR;
431 message = dgettext(TEXTDOMAIN, "could not parse error message in plpy.elog");
432 }
433 message = pstrdup(message);
434
435 Py_XDECREF(so);
436
437 if (kw != NULL)
438 {
439 while (PyDict_Next(kw, &pos, &key, &value))
440 {
441 char *keyword = PLyUnicode_AsString(key);
442
443 if (strcmp(keyword, "message") == 0)
444 {
445 /* the message should not be overwritten */
446 if (PyTuple_Size(args) != 0)
447 {
448 PLy_exception_set(PyExc_TypeError, "argument 'message' given by name and position");
449 return NULL;
450 }
451
452 if (message)
453 pfree(message);
454 message = object_to_string(value);
455 }
456 else if (strcmp(keyword, "detail") == 0)
457 detail = object_to_string(value);
458 else if (strcmp(keyword, "hint") == 0)
459 hint = object_to_string(value);
460 else if (strcmp(keyword, "sqlstate") == 0)
461 sqlstatestr = object_to_string(value);
462 else if (strcmp(keyword, "schema_name") == 0)
463 schema_name = object_to_string(value);
464 else if (strcmp(keyword, "table_name") == 0)
465 table_name = object_to_string(value);
466 else if (strcmp(keyword, "column_name") == 0)
467 column_name = object_to_string(value);
468 else if (strcmp(keyword, "datatype_name") == 0)
469 datatype_name = object_to_string(value);
470 else if (strcmp(keyword, "constraint_name") == 0)
471 constraint_name = object_to_string(value);
472 else
473 {
474 PLy_exception_set(PyExc_TypeError,
475 "'%s' is an invalid keyword argument for this function",
476 keyword);
477 return NULL;
478 }
479 }
480 }
481
482 if (sqlstatestr != NULL)
483 {
484 if (strlen(sqlstatestr) != 5)
485 {
486 PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
487 return NULL;
488 }
489
490 if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
491 {
492 PLy_exception_set(PyExc_ValueError, "invalid SQLSTATE code");
493 return NULL;
494 }
495
496 sqlstate = MAKE_SQLSTATE(sqlstatestr[0],
497 sqlstatestr[1],
498 sqlstatestr[2],
499 sqlstatestr[3],
500 sqlstatestr[4]);
501 }
502
503 oldcontext = CurrentMemoryContext;
504 PG_TRY();
505 {
506 if (message != NULL)
507 pg_verifymbstr(message, strlen(message), false);
508 if (detail != NULL)
509 pg_verifymbstr(detail, strlen(detail), false);
510 if (hint != NULL)
511 pg_verifymbstr(hint, strlen(hint), false);
512 if (schema_name != NULL)
513 pg_verifymbstr(schema_name, strlen(schema_name), false);
514 if (table_name != NULL)
515 pg_verifymbstr(table_name, strlen(table_name), false);
516 if (column_name != NULL)
517 pg_verifymbstr(column_name, strlen(column_name), false);
518 if (datatype_name != NULL)
519 pg_verifymbstr(datatype_name, strlen(datatype_name), false);
520 if (constraint_name != NULL)
521 pg_verifymbstr(constraint_name, strlen(constraint_name), false);
522
523 ereport(level,
524 ((sqlstate != 0) ? errcode(sqlstate) : 0,
525 (message != NULL) ? errmsg_internal("%s", message) : 0,
526 (detail != NULL) ? errdetail_internal("%s", detail) : 0,
527 (hint != NULL) ? errhint("%s", hint) : 0,
528 (column_name != NULL) ?
529 err_generic_string(PG_DIAG_COLUMN_NAME, column_name) : 0,
530 (constraint_name != NULL) ?
531 err_generic_string(PG_DIAG_CONSTRAINT_NAME, constraint_name) : 0,
532 (datatype_name != NULL) ?
533 err_generic_string(PG_DIAG_DATATYPE_NAME, datatype_name) : 0,
534 (table_name != NULL) ?
535 err_generic_string(PG_DIAG_TABLE_NAME, table_name) : 0,
536 (schema_name != NULL) ?
537 err_generic_string(PG_DIAG_SCHEMA_NAME, schema_name) : 0));
538 }
539 PG_CATCH();
540 {
541 ErrorData *edata;
542
543 MemoryContextSwitchTo(oldcontext);
544 edata = CopyErrorData();
546
548 FreeErrorData(edata);
549
550 return NULL;
551 }
552 PG_END_TRY();
553
554 /*
555 * return a legal object so the interpreter will continue on its merry way
556 */
557 Py_RETURN_NONE;
558}
#define dgettext(d, x)
Definition: c.h:1151
void(* pg_funcptr_t)(void)
Definition: c.h:424
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
int err_generic_string(int field, const char *str)
Definition: elog.c:1512
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1818
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1230
ErrorData * CopyErrorData(void)
Definition: elog.c:1746
void FlushErrorState(void)
Definition: elog.c:1867
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errcode(int sqlerrcode)
Definition: elog.c:853
char * unpack_sql_state(int sql_state)
Definition: elog.c:3169
#define LOG
Definition: elog.h:31
#define FATAL
Definition: elog.h:41
#define PG_TRY(...)
Definition: elog.h:371
#define WARNING
Definition: elog.h:36
#define DEBUG2
Definition: elog.h:29
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:381
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition: elog.h:56
#define TEXTDOMAIN
Definition: elog.h:152
#define NOTICE
Definition: elog.h:35
#define INFO
Definition: elog.h:34
#define ereport(elevel,...)
Definition: elog.h:149
Assert(PointerIsAligned(start, uint64))
const char * str
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
static struct @165 value
int i
Definition: isn.c:74
#define PLy_elog
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1556
char * pstrdup(const char *in)
Definition: mcxt.c:1699
void pfree(void *pointer)
Definition: mcxt.c:1524
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
PyObject * PLy_cursor(PyObject *self, PyObject *args)
void PLy_cursor_init_type(void)
PyObject * PLy_exc_error
Definition: plpy_elog.c:15
PyObject * PLy_exc_spi_error
Definition: plpy_elog.c:17
void PLy_exception_set_with_details(PyObject *excclass, ErrorData *edata)
Definition: plpy_elog.c:504
void PLy_exception_set(PyObject *exc, const char *fmt,...)
Definition: plpy_elog.c:472
PyObject * PLy_exc_fatal
Definition: plpy_elog.c:16
void PLy_plan_init_type(void)
PyMODINIT_FUNC PyInit_plpy(void)
static PyObject * PLy_notice(PyObject *self, PyObject *args, PyObject *kw)
static PyMethodDef PLy_exc_methods[]
static PyObject * PLy_quote_ident(PyObject *self, PyObject *args)
static char * object_to_string(PyObject *obj)
static PyObject * PLy_debug(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_quote_nullable(PyObject *self, PyObject *args)
static void PLy_generate_spi_exceptions(PyObject *mod, PyObject *base)
static PyModuleDef PLy_module
static PyObject * PLy_error(PyObject *self, PyObject *args, PyObject *kw)
void PLy_init_plpy(void)
static PyObject * PLy_log(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_warning(PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_create_exception(char *name, PyObject *base, PyObject *dict, const char *modname, PyObject *mod)
static PyObject * PLy_quote_literal(PyObject *self, PyObject *args)
struct ExceptionMap ExceptionMap
static PyMethodDef PLy_methods[]
static PyModuleDef PLy_exc_module
static PyObject * PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
static PyObject * PLy_info(PyObject *self, PyObject *args, PyObject *kw)
HTAB * PLy_spi_exceptions
static PyObject * PLy_fatal(PyObject *self, PyObject *args, PyObject *kw)
static const ExceptionMap exception_map[]
static void PLy_add_exceptions(PyObject *plpy)
void PLy_result_init_type(void)
PyObject * PLy_spi_prepare(PyObject *self, PyObject *args)
Definition: plpy_spi.c:36
PyObject * PLy_commit(PyObject *self, PyObject *args)
Definition: plpy_spi.c:446
PyObject * PLy_rollback(PyObject *self, PyObject *args)
Definition: plpy_spi.c:493
PyObject * PLy_spi_execute(PyObject *self, PyObject *args)
Definition: plpy_spi.c:150
struct PLyExceptionEntry PLyExceptionEntry
PyObject * PLy_subtransaction_new(PyObject *self, PyObject *unused)
void PLy_subtransaction_init_type(void)
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:82
PyObject * PLyUnicode_FromString(const char *s)
Definition: plpy_util.c:117
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:65
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:69
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:68
#define PG_DIAG_TABLE_NAME
Definition: postgres_ext.h:66
#define PG_DIAG_COLUMN_NAME
Definition: postgres_ext.h:67
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:13019
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
Definition: dynahash.c:220
PyObject * exc
Definition: plpy_spi.h:21
const char * name