pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Example in plpgsql docs can lead to infinite loop
2+ messages / 2 participants
[nested] [flat]

* Example in plpgsql docs can lead to infinite loop
@ 2012-01-27 20:56  Phil Sorber <phil@omniti.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Phil Sorber @ 2012-01-27 20:56 UTC (permalink / raw)
  To: pgsql-bugs

http://www.postgresql.org/docs/devel/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE

This example can lead to an infinite loop if there is another column
that has a unique key constraint on it in addition to the primary key
and someone tries to execute the function with a unique primary key
but a duplicate value for the column with the unique constraint.

CREATE TABLE db (a INT PRIMARY KEY, b TEXT UNIQUE);

CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
    LOOP
        -- first try to update the key
        UPDATE db SET b = data WHERE a = key;
        IF found THEN
            RETURN;
        END IF;
        -- not there, so try to insert the key
        -- if someone else inserts the same key concurrently,
        -- we could get a unique-key failure
        BEGIN
            INSERT INTO db(a,b) VALUES (key, data);
            RETURN;
        EXCEPTION WHEN unique_violation THEN
            -- Do nothing, and loop to try the UPDATE again.
        END;
    END LOOP;
END;
$$
LANGUAGE plpgsql;

SELECT merge_db(1, 'david');
SELECT merge_db(2, 'david');

The update effects no rows because the primary key value doesn't exist
and the insert fails because the unique key constraint fails but the
exception handling ignores the error. It almost seems like there
should be a primary_key_violation exception type to distinguish, but
all I am suggesting right now is that we make a note of that case in
the docs so that fewer people get stung by this. I have attached a
patch with some suggested wording.

Attachments:

plpgsql-docs.patch (text/x-patch, 1.1K) download diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml new file mode 100644 index 5a1e33f..6c203b5 *** a/doc/src/sgml/plpgsql.sgml --- b/doc/src/sgml/plpgsql.sgml *************** SELECT merge_db(1, 'dennis'); *** 2561,2567 **** This coding assumes the <literal>unique_violation</> error is caused by the <command>INSERT</>, and not by, say, an <command>INSERT</> in a ! trigger function on the table. More safety could be had by using the features discussed next to check that the trapped error was the one expected. </para> --- 2561,2569 ---- This coding assumes the <literal>unique_violation</> error is caused by the <command>INSERT</>, and not by, say, an <command>INSERT</> in a ! trigger function on the table. It also assumes that the ! <literal>unique_violation</> error is caused by the primary key and not ! another unique constraint. More safety could be had by using the features discussed next to check that the trapped error was the one expected. </para>
^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: Example in plpgsql docs can lead to infinite loop
@ 2012-01-29 02:08  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Phil Sorber <phil@omniti.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2012-01-29 02:08 UTC (permalink / raw)
  To: Phil Sorber <phil@omniti.com>; +Cc: pgsql-bugs

Phil Sorber <phil@omniti.com> writes:
> http://www.postgresql.org/docs/devel/static/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE
> This example can lead to an infinite loop if there is another column
> that has a unique key constraint on it in addition to the primary key
> and someone tries to execute the function with a unique primary key
> but a duplicate value for the column with the unique constraint.

Hmm.  It might work, depending on what the table's various unique
indexes are for, but I agree that a caution about the case is
reasonable.  I adjusted your text a bit and committed this.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2012-01-29 02:08 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-01-27 20:56 Example in plpgsql docs can lead to infinite loop Phil Sorber <phil@omniti.com>
2012-01-29 02:08 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox