Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit35f4994

Browse files
committed
Fix plperl and pltcl error handling per my previous proposal. SPI
operations are now run as subtransactions, so that errors in themcan be reported as ordinary Perl or Tcl errors and caught by thenormal error handling convention of those languages. Also do someminor code cleanup in pltcl.c: extract a large chunk of duplicatedcode in pltcl_SPI_execute and pltcl_SPI_execute_plan into a sharedsubroutine.
1 parenta3b663d commit35f4994

File tree

8 files changed

+409
-395
lines changed

8 files changed

+409
-395
lines changed

‎doc/src/sgml/plperl.sgml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.31 2004/11/19 23:22:54 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/plperl.sgml,v 2.32 2004/11/21 21:17:01 tgl Exp $
33
-->
44

55
<chapter id="plperl">
@@ -219,9 +219,13 @@ $nrows = $rv-&gt;{processed};
219219
Emit a log or error message. Possible levels are
220220
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
221221
<literal>NOTICE</>, <literal>WARNING</>, and <literal>ERROR</>.
222-
<literal>ERROR</> raises an error condition: further execution
223-
of the function is abandoned, and the current transaction is
224-
aborted.
222+
<literal>ERROR</>
223+
raises an error condition; if this is not trapped by the surrounding
224+
Perl code, the error propagates out to the calling query, causing
225+
the current transaction or subtransaction to be aborted. This
226+
is effectively the same as the Perl <literal>die</> command.
227+
The other levels simply report the message to the system log
228+
and/or client.
225229
</para>
226230
</listitem>
227231
</varlistentry>

‎doc/src/sgml/pltcl.sgml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.31 2004/09/20 22:48:25 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/pltcl.sgml,v 2.32 2004/11/21 21:17:02 tgl Exp $
33
-->
44

55
<chapter id="pltcl">
@@ -449,17 +449,19 @@ SELECT 'doesn''t' AS ret
449449
<term><function>elog</> <replaceable>level</replaceable> <replaceable>msg</replaceable></term>
450450
<listitem>
451451
<para>
452-
Emits a log or error message. Possible levels are
453-
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
454-
<literal>NOTICE</>, <literal>WARNING</>, <literal>ERROR</>, and
455-
<literal>FATAL</>. Most simply emit the given message just like
456-
the <literal>elog</> C function. <literal>ERROR</>
457-
raises an error condition: further execution of the function is
458-
abandoned, and the current transaction is aborted.
459-
<literal>FATAL</> aborts the transaction and causes the current
460-
session to shut down. (There is probably no good reason to use
461-
this error level in PL/Tcl functions, but it's provided for
462-
completeness.)
452+
Emits a log or error message. Possible levels are
453+
<literal>DEBUG</>, <literal>LOG</>, <literal>INFO</>,
454+
<literal>NOTICE</>, <literal>WARNING</>, <literal>ERROR</>, and
455+
<literal>FATAL</>. Most simply emit the given message just like
456+
the <literal>elog</> C function. <literal>ERROR</>
457+
raises an error condition; if this is not trapped by the surrounding
458+
Tcl code, the error propagates out to the calling query, causing
459+
the current transaction or subtransaction to be aborted. This
460+
is effectively the same as the Tcl <literal>error</> command.
461+
<literal>FATAL</> aborts the transaction and causes the current
462+
session to shut down. (There is probably no good reason to use
463+
this error level in PL/Tcl functions, but it's provided for
464+
completeness.)
463465
</para>
464466
</listitem>
465467
</varlistentry>

‎doc/src/sgml/release.sgml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.309 2004/11/20 21:44:24 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.310 2004/11/21 21:17:02 tgl Exp $
33
-->
44

55
<appendix id="release">
@@ -1686,6 +1686,15 @@ $PostgreSQL: pgsql/doc/src/sgml/release.sgml,v 1.309 2004/11/20 21:44:24 tgl Exp
16861686
</para>
16871687
</listitem>
16881688

1689+
<listitem>
1690+
<para>
1691+
In PL/Tcl, SPI commands are now run in subtransactions. If an error
1692+
occurs, the subtransaction is cleaned up and the error is reported
1693+
as an ordinary Tcl error, which can be trapped with <literal>catch</>.
1694+
Formerly, it was not possible to catch such errors.
1695+
</para>
1696+
</listitem>
1697+
16891698
</itemizedlist>
16901699
</sect3>
16911700

‎src/pl/plperl/plperl.c

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* ENHANCEMENTS, OR MODIFICATIONS.
3434
*
3535
* IDENTIFICATION
36-
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.59 2004/11/20 19:07:40 tgl Exp $
36+
* $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.60 2004/11/21 21:17:03 tgl Exp $
3737
*
3838
**********************************************************************/
3939

@@ -1593,20 +1593,79 @@ plperl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc)
15931593
}
15941594

15951595

1596+
/*
1597+
* Implementation of spi_exec_query() Perl function
1598+
*/
15961599
HV*
15971600
plperl_spi_exec(char*query,intlimit)
15981601
{
15991602
HV*ret_hv;
1600-
intspi_rv;
16011603

1602-
spi_rv=SPI_execute(query,plperl_current_prodesc->fn_readonly,limit);
1603-
ret_hv=plperl_spi_execute_fetch_result(SPI_tuptable,SPI_processed,spi_rv);
1604+
/*
1605+
* Execute the query inside a sub-transaction, so we can cope with
1606+
* errors sanely
1607+
*/
1608+
MemoryContextoldcontext=CurrentMemoryContext;
1609+
ResourceOwneroldowner=CurrentResourceOwner;
1610+
1611+
BeginInternalSubTransaction(NULL);
1612+
/* Want to run inside function's memory context */
1613+
MemoryContextSwitchTo(oldcontext);
1614+
1615+
PG_TRY();
1616+
{
1617+
intspi_rv;
1618+
1619+
spi_rv=SPI_execute(query,plperl_current_prodesc->fn_readonly,
1620+
limit);
1621+
ret_hv=plperl_spi_execute_fetch_result(SPI_tuptable,SPI_processed,
1622+
spi_rv);
1623+
1624+
/* Commit the inner transaction, return to outer xact context */
1625+
ReleaseCurrentSubTransaction();
1626+
MemoryContextSwitchTo(oldcontext);
1627+
CurrentResourceOwner=oldowner;
1628+
/*
1629+
* AtEOSubXact_SPI() should not have popped any SPI context,
1630+
* but just in case it did, make sure we remain connected.
1631+
*/
1632+
SPI_restore_connection();
1633+
}
1634+
PG_CATCH();
1635+
{
1636+
ErrorData*edata;
1637+
1638+
/* Save error info */
1639+
MemoryContextSwitchTo(oldcontext);
1640+
edata=CopyErrorData();
1641+
FlushErrorState();
1642+
1643+
/* Abort the inner transaction */
1644+
RollbackAndReleaseCurrentSubTransaction();
1645+
MemoryContextSwitchTo(oldcontext);
1646+
CurrentResourceOwner=oldowner;
1647+
1648+
/*
1649+
* If AtEOSubXact_SPI() popped any SPI context of the subxact,
1650+
* it will have left us in a disconnected state. We need this
1651+
* hack to return to connected state.
1652+
*/
1653+
SPI_restore_connection();
1654+
1655+
/* Punt the error to Perl */
1656+
croak("%s",edata->message);
1657+
1658+
/* Can't get here, but keep compiler quiet */
1659+
returnNULL;
1660+
}
1661+
PG_END_TRY();
16041662

16051663
returnret_hv;
16061664
}
16071665

16081666
staticHV*
1609-
plperl_spi_execute_fetch_result(SPITupleTable*tuptable,intprocessed,intstatus)
1667+
plperl_spi_execute_fetch_result(SPITupleTable*tuptable,intprocessed,
1668+
intstatus)
16101669
{
16111670
HV*result;
16121671

@@ -1619,21 +1678,18 @@ plperl_spi_execute_fetch_result(SPITupleTable *tuptable, int processed, int stat
16191678

16201679
if (status==SPI_OK_SELECT)
16211680
{
1622-
if (processed)
1623-
{
1624-
AV*rows;
1625-
HV*row;
1626-
inti;
1681+
AV*rows;
1682+
HV*row;
1683+
inti;
16271684

1628-
rows=newAV();
1629-
for (i=0;i<processed;i++)
1630-
{
1631-
row=plperl_hash_from_tuple(tuptable->vals[i],tuptable->tupdesc);
1632-
av_push(rows,newRV_noinc((SV*)row));
1633-
}
1634-
hv_store(result,"rows",strlen("rows"),
1635-
newRV_noinc((SV*)rows),0);
1685+
rows=newAV();
1686+
for (i=0;i<processed;i++)
1687+
{
1688+
row=plperl_hash_from_tuple(tuptable->vals[i],tuptable->tupdesc);
1689+
av_push(rows,newRV_noinc((SV*)row));
16361690
}
1691+
hv_store(result,"rows",strlen("rows"),
1692+
newRV_noinc((SV*)rows),0);
16371693
}
16381694

16391695
SPI_freetuptable(tuptable);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp