-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Every scenario is executed with a new connection/session
SCENARIO 1 (regular table)
Created a regular table (within the pgtt_schema 🤷♂️) with a trigger just to verify everything works as expected:
CREATE TABLE IF NOT EXISTS pgtt_schema.persistant
( id_temp serial,
a_code character(3),
descr text
);
-- the trigger implementation
CREATE OR REPLACE FUNCTION pgtt_schema.copy()
RETURNS trigger
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
new.descr := new.a_code;
return new;
END;
$BODY$;
-- the trigger itself
CREATE OR REPLACE TRIGGER copy
BEFORE INSERT
ON pgtt_schema.persistant
FOR EACH ROW
EXECUTE FUNCTION pgtt_schema.copy();
-- An insert
INSERT INTO pgtt_schema.persistant (a_code) VALUES ('PQR');
-- and verify the output
select * from pgtt_schema.persistant;
| id_temp | a_code | descr |
|---|---|---|
| 1 | PQR | PQR |
So far so good.
SCENARIO 2 (postgres temp-table)
Trying with regular postgres-temp-table:
First disable the extension:
set pgtt.enabled=off;
Then do similar as above for a postgres-temp-table
DROP TABLE IF EXISTS pg_tmp;
CREATE GLOBAL TEMPORARY TABLE IF NOT EXISTS pg_tmp
( id_temp serial,
a_code character(3),
descr text
)
ON COMMIT PRESERVE ROWS;
-- Reuse the trigger implementation for the create of the trigger
CREATE OR REPLACE TRIGGER copy
BEFORE INSERT
ON pg_tmp
FOR EACH ROW
EXECUTE FUNCTION pgtt_schema.copy();
-- An insert in the postgres-temp-table
INSERT INTO pg_tmp (a_code) VALUES ('PQR');
-- and verify the output
select * from pg_tmp;
| id_temp | a_code | descr |
|---|---|---|
| 1 | PQR | PQR |
Works as expected 👍
SCENARIO 3 (pgtt temp-table)
So now the scenario with the pgtt-temp-table:
Do not forget to re-enable the extension (and just to be sure):
set pgtt.enabled=on;
Then do similar as above for a pgtt-temp-table
DROP TABLE IF EXISTS pgtt_schema.pg_tmp;
CREATE GLOBAL TEMPORARY TABLE pgtt_schema.pg_tmp
( id_temp serial NOT NULL,
a_code character(3),
descr text
)
ON COMMIT PRESERVE ROWS
;
CREATE OR REPLACE TRIGGER copy
BEFORE INSERT
ON pgtt_schema.pg_tmp
FOR EACH ROW
EXECUTE FUNCTION pgtt_schema.copy();
INSERT INTO pgtt_schema.pg_tmp (a_code) VALUES ('PQR');
select * from pgtt_schema.pg_tmp;
Such a pitty 😨
| id_temp | a_code | descr |
|---|---|---|
| 1 | PQR | [null] |
After some investigation the pgtt-temp-table was found in one of the schemas: "pg_temp_..."
However the trigger was absent. Manually added it and then it worked as I expected it.
Used version 3.2.0 according the extension