I have these two tables in a PostgreSQL 12 database:
CREATE TABLE public.test (
entity_id uuid PRIMARY KEY DEFAULT uuid_generate_v4()
);
CREATE TABLE public.test_registration (
entity_id uuid REFERENCES test(entity_id),
row_id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
entry_name text,
code text
);
entry_name is populated by a trigger AFTER INSERT:
CREATE OR REPLACE FUNCTION test_registration_entry_name()
RETURNS trigger AS
$$
BEGIN
UPDATE test_registration
SET entry_name = new_inserts.code
FROM new_inserts
WHERE new_inserts.row_id = test_registration.row_id;
RETURN NULL;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER test_insert_trigger
AFTER INSERT ON test_registration
REFERENCING NEW TABLE AS new_inserts
FOR EACH STATEMENT
WHEN (pg_trigger_depth() = 0)
EXECUTE PROCEDURE test_registration_entry_name();
The trigger function differs from table to table.
My current issue is when I insert a row into test_registration as such:
INSERT INTO test DEFAULT VALUES
RETURNING entity_id; -- let's say: 'a17e66c5-1049-4ba9-bed3-90bbc823e064'
And then insert a registration and return all row values for the inserted row:
INSERT INTO test_registration (entity_id,code)
VALUES ('a17e66c5-1049-4ba9-bed3-90bbc823e064'::uuid,'EB')
RETURNING *
I get all column values correctly, except for the entry_name which is null. Which makes sense because entry_name is being set after the insert.
So I tried to expose this by altering my INSERT using the row_id as such:
WITH create_query AS (INSERT INTO test_registration (entity_id,code)
VALUES ('a17e66c5-1049-4ba9-bed3-90bbc823e064'::uuid,'13')
RETURNING *) SELECT v.entity_id, v.row_id, b.entity_id,b.entry_name, b.row_id
from create_query AS v, test_registration AS b WHERE V.Row_id = b.Row_id
This then turns out to not return anything. create_query.row_id seems to differ from the one in test_registration - which makes no sense? Why would they be different?
How can I extract all column values for the inserted row, after the triggers has been triggered.
for each statementfor the trigger as some of the inserts can consist of doing inserts of multiple rows, hence making this performance inefficient if the the trigger is executed for foreach row.I tried changing my
– kafka Mar 21 '21 at 07:04AFTERinsert toBEFOREbut got into some issues with not able to reference table?AFTERtriggers. – Erwin Brandstetter Mar 21 '21 at 12:48