[GENERAL] How to capture and handle failed INSERT

2007-03-04 Thread Postgres User
Hi, I'm using this code to increment a counter table: IF Exists (SELECT keyfld FROM counter_tbl WHERE key_fld = 'key_val') THEN UPDATE counter_tbl SET counter_fld = counter_fld + 1 WHERE key_fld = 'key_val'; ELSE INSERT INTO counter_tbl(key_fld, counter_fld) VALUES('key_val',

Re: [GENERAL] How to capture and handle failed INSERT

2007-03-04 Thread Berend Tober
Postgres User wrote: I'm using this code to increment a counter table: IF Exists (SELECT keyfld FROM counter_tbl WHERE key_fld = 'key_val') THEN UPDATE counter_tbl SET counter_fld = counter_fld + 1 WHERE key_fld = 'key_val'; ELSE INSERT INTO counter_tbl(key_fld, counter_fld)

Re: [GENERAL] How to capture and handle failed INSERT

2007-03-04 Thread Postgres User
The best answer: RTFM! I found this example in the docs, although it should really exit the Loop after 10 failed attempts instead of going indefinitely: CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS $$ BEGIN LOOP UPDATE db SET b = data WHERE a = key; IF found