Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 94FAA63EC04 for ; Sun, 14 Mar 2010 15:20:13 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 15343-01-6 for ; Sun, 14 Mar 2010 18:20:00 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id AC2A263E830 for ; Sun, 14 Mar 2010 15:19:33 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1NqsPT-0003XK-VM for pgsql-docs@postgresql.org; Sun, 14 Mar 2010 19:19:31 +0100 Received: from e177125208.adsl.alicedsl.de ([85.177.125.208]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 14 Mar 2010 19:19:31 +0100 Received: from tim by e177125208.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 14 Mar 2010 19:19:31 +0100 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: pgsql-docs@postgresql.org To: pgsql-docs@postgresql.org From: Tim Landscheidt Subject: OLD and NEW in PL/pgSQL triggers Date: Sun, 14 Mar 2010 18:19:20 +0000 Organization: Lines: 47 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: e177125208.adsl.alicedsl.de Mail-Copies-To: never User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:22xznBr1AZcHo1DVJ8JgtRUg5R0= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/6 X-Sequence-Number: 5371 Hi, says: | [...] | When a PL/pgSQL function is called as a trigger, several | special variables are created automatically in the top-level | block. They are: | NEW | Data type RECORD; variable holding the new database row | for INSERT/UPDATE operations in row-level triggers. This | variable is NULL in statement-level triggers and for DE- | LETE operations. | OLD | Data type RECORD; variable holding the old database row | for UPDATE/DELETE operations in row-level triggers. This | variable is NULL in statement-level triggers and for IN- | SERT operations. | [...] Yet, if I try to use that promise: | tim=# CREATE FUNCTION UniversalTrigger () RETURNS TRIGGER | tim-# AS $$BEGIN IF OLD IS NULL THEN RAISE NOTICE 'OLD IS NULL'; END IF; END;$$ | tim-# LANGUAGE PLPGSQL; | CREATE FUNCTION | tim=# CREATE TABLE TriggerTest (ID SERIAL PRIMARY KEY); | NOTICE: CREATE TABLE will create implicit sequence "triggertest_id_seq" for serial column "triggertest.id" | NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "triggertest_pkey" for table "triggertest" | CREATE TABLE | tim=# CREATE TRIGGER UniversalTrigger BEFORE DELETE OR INSERT OR UPDATE ON TriggerTest FOR EACH ROW EXECUTE PROCEDURE UniversalTrigger (); | CREATE TRIGGER | tim=# INSERT INTO TriggerTest (ID) VALUES (1); | ERROR: record "old" is not assigned yet | DETAIL: The tuple structure of a not-yet-assigned record is indeterminate. | CONTEXT: PL/pgSQL function "universaltrigger" line 1 at IF | tim=# In my reading, states that the syntax is correct. Is this a glitch in the documentation, a bug or something else? Tim