PG_TRY & PG_CATCH in FDW development

Started by Abbas Buttover 8 years ago3 messages
#1Abbas Butt
abbas.butt@enterprisedb.com

Hi Hackers,

I want to share a technical problem that I am facing while writing code for
an FDW.
The problem is as follows:
In the FDW call back functions it is recommended to use PG_TRY PG_CATCH
blocks along with PG_RE_THROW to disconnect from the foreign server.
I am using the same technique in IterateForeignScan function, and it is
supposed to work like this:

1 PG_TRY();
2 {
3 ... code that might throw ereport(ERROR) ...
4 }
5 PG_CATCH();
6 {
7 disconnect_from_foreign_server();
8 PG_RE_THROW();
9 }
10 PG_END_TRY();

PG_RE_THROW is supposed to throw the same error again and then take us out
of the function.

What is happening for me is that PG_RE_THROW takes me to PG_TRY in the same
function and then PG_TRY jumps to PG_CATCH where PG_RE_THROW again jumps to
PG_TRY in the same function resulting in an infinite loop. The query
therefore never returns. It is supposed to throw the error and quit.
My question is what could possibly cause this infinite loop?

Thanks in advance.

--
--
*Abbas*
Architect

Ph: 92.334.5100153
Skype ID: gabbasb
www.enterprisedb.co <http://www.enterprisedb.com/&gt;m
<http://www.enterprisedb.com/&gt;

*Follow us on Twitter*
@EnterpriseDB

Visit EnterpriseDB for tutorials, webinars, whitepapers
<http://www.enterprisedb.com/resources-community&gt; and more
<http://www.enterprisedb.com/resources-community&gt;

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Abbas Butt (#1)
Re: PG_TRY & PG_CATCH in FDW development

Abbas Butt <abbas.butt@enterprisedb.com> writes:

What is happening for me is that PG_RE_THROW takes me to PG_TRY in the same
function and then PG_TRY jumps to PG_CATCH where PG_RE_THROW again jumps to
PG_TRY in the same function resulting in an infinite loop. The query
therefore never returns. It is supposed to throw the error and quit.

Apparently PG_exception_stack isn't getting restored properly, but it's
sure hard to see why. I'm suspicious that you have something silly like
mismatched braces in the vicinity of the TRY/CATCH structure.

FWIW, doing things like disconnecting remote sessions might be better
handled in transaction-cleanup logic, anyway. What covers you for that
if the query aborts while control is not within your PG_TRY block?

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Abbas Butt
abbas.butt@enterprisedb.com
In reply to: Tom Lane (#2)
Re: PG_TRY & PG_CATCH in FDW development

Thanks for the reply.

On Tue, Apr 25, 2017 at 7:45 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Abbas Butt <abbas.butt@enterprisedb.com> writes:

What is happening for me is that PG_RE_THROW takes me to PG_TRY in the

same

function and then PG_TRY jumps to PG_CATCH where PG_RE_THROW again jumps

to

PG_TRY in the same function resulting in an infinite loop. The query
therefore never returns. It is supposed to throw the error and quit.

Apparently PG_exception_stack isn't getting restored properly, but it's
sure hard to see why. I'm suspicious that you have something silly like
mismatched braces in the vicinity of the TRY/CATCH structure.

I rechecked, braces are matching.

FWIW, doing things like disconnecting remote sessions might be better
handled in transaction-cleanup logic, anyway.

I see that postgres_fdw is using a similar login in pgfdw_xact_callback.
Let me try and use the same technique.

What covers you for that
if the query aborts while control is not within your PG_TRY block?

All the code that requires a connection to the foreign server is with in
the PG_TRY block, it is therefore not required any where else to close
connection before reporting any error.

regards, tom lane

--
--
*Abbas*
Architect

Ph: 92.334.5100153
Skype ID: gabbasb
www.enterprisedb.co <http://www.enterprisedb.com/&gt;m
<http://www.enterprisedb.com/&gt;

*Follow us on Twitter*
@EnterpriseDB

Visit EnterpriseDB for tutorials, webinars, whitepapers
<http://www.enterprisedb.com/resources-community&gt; and more
<http://www.enterprisedb.com/resources-community&gt;