Skip to content

Commit 8c08894

Browse files
author
James William Pye
committed
Use XACTREF more and eliminate the an unnecessary context switch.
1 parent 4f9976a commit 8c08894

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/pl.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ srf_vpcinit(PG_FUNCTION_ARGS)
11301130
{
11311131
int r;
11321132

1133-
r = PySet_Add(TransactionScope, fn_info->fi_internal_state);
1133+
r = Py_XACTREF(fn_info->fi_internal_state);
11341134
Py_DECREF(fn_info->fi_internal_state);
11351135
if (r == -1)
11361136
{
@@ -1180,8 +1180,7 @@ srf_vpcnext(PG_FUNCTION_ARGS)
11801180
* This can cause a success to fail.
11811181
* If it fails on top of an existing exception, fine.
11821182
*/
1183-
PySet_Discard(TransactionScope, fn_info->fi_internal_state);
1184-
1183+
Py_DEXTREF(fn_info->fi_internal_state);
11851184
fn_info->fi_internal_state = NULL;
11861185
fcinfo->isnull = true;
11871186

@@ -1376,7 +1375,7 @@ initialize(PG_FUNCTION_ARGS)
13761375
module = run_PyPgFunction_module(func); /* ereport's on failure */
13771376
else
13781377
{
1379-
r = PySet_Add(TransactionScope, module);
1378+
r = Py_XACTREF(module);
13801379
Py_DECREF(module);
13811380
if (r == -1)
13821381
{
@@ -1389,7 +1388,7 @@ initialize(PG_FUNCTION_ARGS)
13891388
* Add func to the transaction scope now.
13901389
* func has been ACQUIRE'd by the handler, so don't DECREF.
13911390
*/
1392-
r = PySet_Add(TransactionScope, func);
1391+
r = Py_XACTREF(func);
13931392
if (r == -1)
13941393
{
13951394
PyErr_ThrowPostgresError(
@@ -1421,7 +1420,7 @@ initialize(PG_FUNCTION_ARGS)
14211420
if (td == NULL)
14221421
PyErr_ThrowPostgresError("could not create Postgres.TriggerData object");
14231422

1424-
r = PySet_Add(TransactionScope, td);
1423+
r = Py_XACTREF(td);
14251424
Py_DECREF(td);
14261425
if (r == -1)
14271426
PyErr_ThrowPostgresError("could not add trigger data to transaction scope");
@@ -1489,7 +1488,7 @@ initialize(PG_FUNCTION_ARGS)
14891488
*/
14901489
if (pinput != fn_info->fi_input)
14911490
{
1492-
r = PySet_Add(TransactionScope, pinput);
1491+
r = Py_XACTREF(pinput);
14931492
Py_DECREF(pinput);
14941493

14951494
if (r == -1)
@@ -1507,7 +1506,7 @@ initialize(PG_FUNCTION_ARGS)
15071506
*/
15081507
if (poutput != fn_info->fi_output)
15091508
{
1510-
r = PySet_Add(TransactionScope, poutput);
1509+
r = Py_XACTREF(poutput);
15111510
Py_DECREF(poutput);
15121511

15131512
if (r == -1)
@@ -1549,7 +1548,7 @@ initialize(PG_FUNCTION_ARGS)
15491548
PyErr_ThrowPostgresError(
15501549
"could not construct expected result type");
15511550

1552-
r = PySet_Add(TransactionScope, output);
1551+
r = Py_XACTREF(output);
15531552
Py_DECREF(output);
15541553
if (r == -1)
15551554
{
@@ -1705,10 +1704,15 @@ pl_handler(PG_FUNCTION_ARGS)
17051704

17061705
pl_execution_context = previous;
17071706

1707+
/*
1708+
* Restore the caller's memory context.
1709+
*/
1710+
MemoryContextSwitchTo(current_exec_state.return_memory_context);
1711+
17081712
/*
17091713
* Special case relays as there is a Python exception available
17101714
* for us to use. This failure case happens when DatumNew causes
1711-
* a Python exception.
1715+
* a *Python* exception.
17121716
*/
17131717
if (_PG_ERROR_IS_RELAY())
17141718
{
@@ -1721,7 +1725,6 @@ pl_handler(PG_FUNCTION_ARGS)
17211725
FlushErrorState();
17221726

17231727
/* Restore the caller's memory context. */
1724-
MemoryContextSwitchTo(current_exec_state.return_memory_context);
17251728
PyErr_ThrowPostgresErrorWithContext(
17261729
ERRCODE_PYTHON_EXCEPTION,
17271730
"function raised a Python exception",
@@ -1735,11 +1738,6 @@ pl_handler(PG_FUNCTION_ARGS)
17351738

17361739
_PYRO_DEALLOCATE(); /* release references held by owner and pop */
17371740

1738-
/*
1739-
* Restore the caller's memory context.
1740-
*/
1741-
MemoryContextSwitchTo(current_exec_state.return_memory_context);
1742-
17431741
/*
17441742
* Should have been converted by now.
17451743
*/

0 commit comments

Comments
 (0)