BUG #19064: Trigger allows creation with invalid column references but fails at runtime - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #19064: Trigger allows creation with invalid column references but fails at runtime
Date
Msg-id 19064-b896cfc12d4c7e75@postgresql.org
Whole thread Raw
Responses Re: BUG #19064: Trigger allows creation with invalid column references but fails at runtime
Re: BUG #19064: Trigger allows creation with invalid column references but fails at runtime
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      19064
Logged by:          Ketan Bhatiya
Email address:      ketan.bhatiya@lenditt.com
PostgreSQL version: Unsupported/Unknown
Operating system:   Windows
Description:

Example Function & Trigger

CREATE OR REPLACE FUNCTION update_order_total()
RETURNS TRIGGER AS $$
BEGIN
   -- Wrong column reference: "wrong_column" does not exist in "Orders"
   UPDATE Orders
   SET total = NEW.wrong_column
   WHERE id = NEW.id;
   RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE TRIGGER trg_update_order_total
AFTER UPDATE ON Orders
FOR EACH ROW
EXECUTE FUNCTION update_order_total();


Steps to Reproduce

Run the above function and trigger creation.
✅ They are created successfully.

Update a row in Orders.

Expected Result
If the trigger contains a reference to a non-existing column, PostgreSQL
should throw an error at creation time.

Actual Result

The function and trigger creation succeed.

❌ At runtime, when an update happens, it fails with:

ERROR: record "new" has no field "wrong_column"

Suggestion / Proposed Improvement
PostgreSQL should validate column references at trigger creation time, not
just at runtime.
If a column does not exist in the target table (Orders in this example),
trigger creation should fail immediately with a clear error message.


pgsql-bugs by date:

Previous
From: Pavel Hushcha
Date:
Subject: Issue with PostgreSQL 18.0 Docker image volume mount (/var/lib/postgresql/data symlink)
Next
From: Magnus Hagander
Date:
Subject: Re: Issue with PostgreSQL 18.0 Docker image volume mount (/var/lib/postgresql/data symlink)