Memory context in exception handler

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Memory context in exception handler
Date: 2007-01-13 22:18:59
Message-ID: 200701132319.00246.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I'm trying to use the PG_TRY/PG_CATCH exception handling:

bool
xml_is_document(xmltype *arg)
{
bool result;
xmlDocPtr doc;

PG_TRY();
{
doc = xml_parse((text *) arg, true, true);
result = true;
}
PG_CATCH();
{
ErrorData *errdata = CopyErrorData();
if (errdata->sqlerrcode == ERRCODE_INVALID_XML_DOCUMENT)
{
FlushErrorState();
result = false;
}
else
PG_RE_THROW();
}
PG_END_TRY();

if (doc)
xmlFreeDoc(doc);

return result;
}

But this fails because CopyErrorData() complains by way of assertion
that we're still in ErrorContext. A nearby comment suggests to switch
away to another context to preserve the data across FlushErrorState(),
but that doesn't seem necessary in this situation. Are there other
reasons why this rule is so rigorously enforced?

--
Peter Eisentraut
http://developer.postgresql.org/~petere/


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Peter Eisentraut <peter_e(at)gmx(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Memory context in exception handler
Date: 2007-01-14 00:14:52
Message-ID: 28302.1168733692@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Peter Eisentraut <peter_e(at)gmx(dot)net> writes:
> But this fails because CopyErrorData() complains by way of assertion
> that we're still in ErrorContext. A nearby comment suggests to switch
> away to another context to preserve the data across FlushErrorState(),
> but that doesn't seem necessary in this situation. Are there other
> reasons why this rule is so rigorously enforced?

I think it's a good error check because if you are trying to make a copy
of the current error data, doing so within the ErrorContext seems highly
unlikely to be safe.

As near as I can tell, you're using CopyErrorData not because you need
an actual copy but just because elog.c doesn't export any other API to
let you see the current sqlerrorcode. Perhaps adding a function to
return the top stack entry's sqlerrorcode would be a better API change?
(I'm a bit uncomfortable with handing out direct access to the struct,
but getting a peek at sqlerrorcode or other scalar values seems safe
enough.)

regards, tom lane


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Memory context in exception handler
Date: 2007-01-14 21:05:42
Message-ID: 200701142205.43063.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Tom Lane wrote:
> As near as I can tell, you're using CopyErrorData not because you
> need an actual copy but just because elog.c doesn't export any other
> API to let you see the current sqlerrorcode. Perhaps adding a
> function to return the top stack entry's sqlerrorcode would be a
> better API change?

Yes, something like that would be good to have. At the moment, there
are not a lot of users of this mechanism in our code, so I'm not in a
hurry to change this (and I think that I want to rewrite the XML
parsing code to do without the exceptions dance).

--
Peter Eisentraut
http://developer.postgresql.org/~petere/