Hi,
<URI:http://www.postgresql.org/docs/8.4/static/plpgsql-trigger.html>
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,
<URI:http://www.postgresql.org/docs/8.4/static/sql-expressions.html#SQL-SYNTAX-ROW-CONSTRUCTORS>
states that the syntax is correct. Is this a glitch in the
documentation, a bug or something else?
Tim