PostgreSQL Syntax error at or near FUNCTION on create trigger
Asked Answered
S

2

8

I installed postgresql and postgresql-plpython-10 (using apt) on a new machine running Ubuntu server 18.04 and I have reinstated a Postgresql database (extracted using pg_dumpall) from another machine onto a new machine (Linux).

I checked that the trackspreadsheetnztplpy function exists in the database and the table nztgsheet exists. I reran the function on the old machine and it worked perfectly. I checked plpythonu is installed on the new machine using psql command: \dL.

SQL error:

ERROR: syntax error at or near "FUNCTION" LINE 1: ...H ROW WHEN (OLD.* IS DISTINCT FROM NEW.*) EXECUTE FUNCTION t... ^

In statement: CREATE TRIGGER trackspreadsheetnzt AFTER UPDATE ON nztgsheet FOR EACH ROW WHEN (OLD.* IS DISTINCT FROM NEW.*) EXECUTE FUNCTION trackspreadsheetnztplpy();

I expected the trigger function to work, but it throws a syntax error instead.

Salo answered 8/8, 2019 at 23:17 Comment(0)
F
24

The EXECUTE FUNCTION syntax for CREATE TRIGGER statements was introduced in Postgres 11.

In Postgres 10, you need to say EXECUTE PROCEDURE instead.

This syntax was deprecated in Postgres 11 with the introduction of procedures, which are distinct from functions and cannot be used to implement a trigger.

Flirtation answered 8/8, 2019 at 23:25 Comment(1)
Thanks. Yes, the old machine was running version 11. Upgraded the new machine to version 11 and now it works!. Thanks.Salo
M
0

If you are trying to add trigger function from pgadmin UI, In the code tab you need to specify function from BEGIN to END, as postgres implicitly specifies in the code " CREATE OR REPLACE FUNCTION ****() RETURNS TRIGGER AS $$"

Minnie answered 15/9, 2021 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.