SPI API and exceptions

Lists: pgsql-hackers
From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: pgsql-hackers(at)postgresql(dot)org
Subject: SPI API and exceptions
Date: 2012-12-28 08:07:05
Message-ID: 1356682025.20017.4.camel@vanquo.pezone.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

SPI was invented before there was proper exception handling, so it
communicates errors by return values. This makes programming with SPI
either prone to errors of omission, or very ugly (ultimately, the
standard reasons why exceptions were invented).

So I was pondering whether we could introduce exceptions to SPI in some
way. I'm not sure how. We could invent an entirely separate set of
functions, or do some tricks in the header that things before
differently depending on some #define. Any ideas?


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: SPI API and exceptions
Date: 2012-12-28 15:25:38
Message-ID: 9273.1356708338@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:
> SPI was invented before there was proper exception handling, so it
> communicates errors by return values. This makes programming with SPI
> either prone to errors of omission, or very ugly (ultimately, the
> standard reasons why exceptions were invented).

> So I was pondering whether we could introduce exceptions to SPI in some
> way. I'm not sure how. We could invent an entirely separate set of
> functions, or do some tricks in the header that things before
> differently depending on some #define. Any ideas?

For practically all the nontrivial SPI functions, callers already have
to expect that exceptions can be thrown; just not for the specific
error causes called out as SPI return codes. So I wonder whether we
could get away with just converting all the SPI errors to elogs as well.
It would change them from specifically-handled errors into general-case
errors, but for many call sites that's no problem.

Note though that there are some cases where handling of "expected"
errors would get a lot more annoying if we made them into elogs;
for instance SPI_ERROR_NOATTRIBUTE from SPI_fnumber() and friends.
That particular case might be solvable by defining SPI_ERROR_NOATTRIBUTE
as InvalidAttrNumber. I think you'd need to go around and look at all
uses of each SPI_ERROR_XXX code before deciding that it's just dead
weight and should be converted to a generic exception.

regards, tom lane