467,077 Members | 935 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

Error handling in stored functions/procedures

I am currently designing an application which should be accessible from
different interfaces. For this I like to be using stored procedures to
process the contents of form submissions and dialog screens.

After studying the PG database, it seems to me that I can fulfill the
following requirements.

a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table which
can be used to check field contents against field masks.

Together with a table reference, the key/value pairs from the submission
can be checked by a table-driven stored procedure which, depending on
the outcome of the test, can return a row which contains an exit status
and an exit message.

Since I do not really like to duplicate (or rewrite functionality), the
following steps should be carried out by the database itself, which has
the possibilities to define and use them. These steps are all kinds of
constraints which can be defined in the CREATE TABLE statement itself.

The big problem I am facing however, is that when an error is
encountered by the database itself, I do not have any influence anymore
on the error message and the flow of control.

Consider the following definition :

drop table testing;
create table testing (
key_nr serial primary key,
string_value text check (string_value != 'xx'),
int_value integer check (int_value <= 255 and int_value >= 0)
);

And the following SQL statement :

insert into testing (string_value, int_value) values ('xx', 100);

This will yield the following error :

ERROR: new row for relation "testing" violates check constraint
"testing_string_value"

interrupt further processing of the stored procedure, and return
immediately to the client.

Showing a user the previous message does not really give a clue about
what has happened. With some processing it is possible to retrieve the
name of the offending field from this message, but it is impossible to
tell the user what is wrong about the contents that he filled in.

To do this would force the programmer to test and catch all errors, and
translate them to meaningful feedback for the user. Since the raised
error returns to the client, this means that the client is forced to do
one of two things : keep a local translation repository, or call another
stored procedure.

The first solution is error prone due to keeping information about parts
of the database in two different places, the second is essentially a
waste of bandwidth and time, since the problem should have been solved
in the first stored procedure.

Another possibility is of course writing all constraints in stored
procedures and not using the built-in possibilities offered by PG, but
isn't that a waste of my time and a waste of all the work that has been
done by this team ?

What solutions do you use ?

Regards,

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
  • viewed: 1971
Share:
5 Replies
> a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table which
can be used to check field contents against field masks.

You can use column check constraints with regular expressions
for that. No need for an extra table I would assume.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
On Sun, 30 May 2004 20:04:50 +0200
Karsten Hilbert <Ka*************@gmx.net> wrote:
a) The basic contents of the internal data dictionary can be used to
check incoming fields from on their length and permitted contents.

b) With a little extra work, I should be able to define a table
which can be used to check field contents against field masks.

You can use column check constraints with regular expressions
for that. No need for an extra table I would assume.


Thanks for the tip.

This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #3
> This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

AFAICT 7.4 does much better error handling (no, you can't
easily control error handling inside a transaction, though). It
reports errors in a way that can be parsed a lot better thus
allowing for fairly easy translation into meaningful user
messages.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #4
On Sun, 30 May 2004 22:08:10 +0200
Karsten Hilbert <Ka*************@gmx.net> wrote:
This then, removes the first part of my explanation, and dumps me
completely in the second part, which is where the biggest problems
reside.

AFAICT 7.4 does much better error handling (no, you can't
easily control error handling inside a transaction, though). It
reports errors in a way that can be parsed a lot better thus
allowing for fairly easy translation into meaningful user
messages.


You mean that the default generated error messages contain some more
information I presume, like the table name and the constraint name ?

I think I noticed that already, and since I had time since yesterday
evening, I thought things over and came to the conclusion that this
mechanism at least gives an escape hatch, since it is possible to give a
name to each constraint, and then use this name as an index to get a
proper error message.

Regards,

Jurgen

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5
> You mean that the default generated error messages contain some more
information I presume, like the table name and the constraint name ? Even better, the information is *structured* afaik.
I think I noticed that already, and since I had time since yesterday
evening, I thought things over and came to the conclusion that this
mechanism at least gives an escape hatch, since it is possible to give a
name to each constraint, and then use this name as an index to get a
proper error message.

For check constraints you can even name them yourself during
table creation.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Wayno | last post: by
9 posts views Thread by dtwilliams@hotmail.com | last post: by
13 posts views Thread by Thelma Lubkin | last post: by
5 posts views Thread by Tim Marshall | last post: by
1 post views Thread by pob | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.