>20:59:45.12147.03.0001. query: DO
> .12147.03.0001.BEGIN;
> .12147.03.0001.INSERT INTO config (name, value) VALUES (E'dbexp', E'10');
> .12147.03.0001.EXCEPTION WHEN unique_violation THEN
> .12147.03.0001.UPDATE config SET value=E'10' WHERE name=E'dbexp';
> .12147.03.0001.END error: fatal query: ERROR: syntax error at or near
>"DO"
> .12147.03.0001.LINE 1: DO
> .12147.03.0001. ^
>
>
>21:02:57.12672.03.0001. query: DO;
> .12672.03.0001.BEGIN;
> .12672.03.0001.INSERT INTO config (name, value) VALUES (E'dbexp', E'10');
> .12672.03.0001.EXCEPTION WHEN unique_violation THEN
> .12672.03.0001.UPDATE config SET value=E'10' WHERE name=E'dbexp';
> .12672.03.0001.END error: fatal query: ERROR: syntax error at or near
>"DO"
> .12672.03.0001.LINE 1: DO;
> .12672.03.0001. ^
>ERROR: syntax error at or near "DO" at character 1
You have to mark the beginning and the end of the code, usually with
dollar-quoting.
So this should be the right syntax:
DO $$
BEGIN
INSERT INTO config (name, value) VALUES (E'dbexp', E'10');
EXCEPTION WHEN unique_violation THEN
UPDATE config SET value=E'10' WHERE name=E'dbexp';
END
$$;
Note there's no semicolon after BEGIN inside PL/pgSQL code.
But if you want a fully reliable update-or-insert I think you need the
full loop, as shown in the documentation you linked to earlier (or else
use serialisable transactions with Postgresql 9.1).
I agree it's a shame that there isn't a more convenient way yet.
-M-