I have a shared trigger function used by multiple tables.
Each tables's primary key uses the format pk_ + tablename i.e. pk_account or pk_order
How can I dynamically reference the NEW object column in a trigger using a variable?
CREATE OR REPLACE FUNCTION shared_test_trigger_fn()
RETURNS trigger AS
$$
DECLARE
primary_key TEXT DEFAULT CONCAT('pk_', TG_TABLE_NAME);
primary_value TEXT DEFAULT CONCAT('NEW.pk_', TG_TABLE_NAME);
BEGIN
RAISE NOTICE 'The PK field is called %', primary_key;
RAISE NOTICE 'The PK value is %', primary_value;
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
DROP TRIGGER IF EXISTS account_test_trigger ON account;
CREATE TRIGGER account_test_trigger BEFORE UPDATE
ON account
FOR EACH ROW
EXECUTE PROCEDURE shared_test_trigger_fn();
UPDATE account SET created = created WHERE pk_account = 1;
EDIT1:
This trigger is simply an example to demonstrate the issue. The scenario is, we have a number of tables sharing a similar format with the PK field including the table name. Rather than code & maintain multiple similar trigger functions we are looking to have a single trigger function that can work with multiple tables.
It can be done fairly easy in plpython3u:
CREATE OR REPLACE FUNCTION public.pk_tablename()
RETURNS trigger
LANGUAGE plpython3u
AS $function$
pk = "pk_" + TD["table_name"]
pk_val = TD["new"][pk]
plpy.notice("PK value is " + str(pk_val))
$function$
CREATE TRIGGER trg_pk_tablename BEFORE UPDATE
ON animals
FOR EACH ROW
EXECUTE PROCEDURE pk_tablename();
animals
Table "public.animals"
Column | Type | Collation | Nullable | Default
------------+------------------------+-----------+----------+---------
pk_animals | integer | | not null |
cond | character varying(200) | | not null |
animal | character varying(200) | | not null |
Indexes:
"animals_pkey" PRIMARY KEY, btree (pk_animals)
Triggers:
trg_pk_tablename BEFORE UPDATE ON animals FOR EACH ROW EXECUTE FUNCTION pk_tablename()
update animals set cond = 'good' where pk_animals = 200;
NOTICE: PK value is 200
UPDATE 1
I have made attempts using plpgsql but have not found the magic sauce yet.
Related
As PostgreSQL doesn't dump object creation date so I want to manually dump user creation date by using trigger and functions. I have created trigger and functions but it's not working.
CREATE TABLE user_audits (
usesysid INT GENERATED ALWAYS AS IDENTITY,
usename varchar NOT NULL,
created_on TIMESTAMP(6) NOT NULL
);
============================
CREATE OR REPLACE FUNCTION user_creation()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
IF NEW.usename <> OLD.usename THEN
INSERT INTO user_audits(usesysid,usename,created_on)
VALUES(usesysid,usename,now());
END IF;
RETURN NEW;
END;
$$
=================================
CREATE TRIGGER user_creation
BEFORE UPDATE
ON user
FOR EACH ROW
EXECUTE PROCEDURE user_creation();
This is important for audit purpose, for now I am using log file to check creation date but it will rotate after sometime.
Please suggest the better way to dump user creation date in table so that I can retrieve the information anytime.
Thanks
I created a similar excercise with the following tables:
The user_tbl table having only a identity column usersysid and the username
CREATE TABLE user_tbl (
usersysid INT GENERATED ALWAYS AS IDENTITY,
username varchar NOT NULL
);
The user_audits table, slightly modified version of yours: where i added an id identity field. I removed the identity from the usersysid field (since it'll be populated with the one coming from user_tbl)
CREATE TABLE user_audits (
id INT GENERATED ALWAYS AS IDENTITY,
usersysid INT,
username varchar NOT NULL,
created_on TIMESTAMP(6) NOT NULL
);
Now the function, I check if the OLD.username is null, this means that is an insert, if NEW.username <> OLD.username then is an update.
CREATE OR REPLACE FUNCTION user_creation()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
BEGIN
IF OLD.username is null OR NEW.username <> OLD.username THEN
INSERT INTO user_audits(usersysid,username,created_on)
VALUES(NEW.usersysid,NEW.username,now());
END IF;
RETURN NEW;
END;
$$
;
And finally the trigger, which is fired both on INSERT or UPDATE
CREATE TRIGGER user_creation
BEFORE INSERT OR UPDATE
ON user_tbl
FOR EACH ROW
EXECUTE PROCEDURE user_creation();
Now if I create two new rows and update one with the following
insert into user_tbl (username) values('Carlo');
insert into user_tbl (username) values('Gianni');
update user_tbl set username='Giorgio' where usersysid=1;
I end up with the user_tbl containing the 2 expected rows
defaultdb=> select * from user_tbl;
usersysid | username
-----------+----------
2 | Gianni
1 | Giorgio
(2 rows)
and the user_audits tables containing 3 rows (2 for the insert + 1 for the update)
defaultdb=> select * from user_audits;
id | usersysid | username | created_on
----+-----------+----------+----------------------------
1 | 1 | Carlo | 2021-06-04 13:57:44.810889
2 | 2 | Gianni | 2021-06-04 13:58:14.680878
3 | 1 | Giorgio | 2021-06-04 13:58:44.702364
(3 rows)
Description of the problem:
Hi everyone, i have this table:
CREATE TABLE COMPORDINE (
CodProdotto CHAR(5) NOT NULL CHECK(CodProdotto ~* '^[0-9]+$'),
CodOrdine CHAR(5) CHECK(CodOrdine ~* '^[0-9]+$'),
Prezzo REAL NOT NULL, CHECK(Prezzo >= 0.0)
CONSTRAINT OrdineFK FOREIGN KEY(CodOrdine) REFERENCES ORDINE(CodOrdine)
CONSTRAINT ProdottoFK FOREIGN KEY(CodProdotto) REFERENCES PRODOTTO(CodProdotto)
);
on which I declared a TRIGGER:
CREATE TRIGGER updatePrezzoOrdine
AFTER INSERT ON COMPORDINE
FOR EACH ROW
EXECUTE PROCEDURE updatePrezzoOrdine();
and a TRIGGER FUNCTION:
CREATE OR REPLACE FUNCTION updatePrezzoOrdine()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
DECLARE
Totale REAL;
BEGIN
SELECT SUM(Prezzo) INTO Totale
FROM COMPORDINE AS CO
WHERE CO.CodOrdine = NEW.CodOrdine
GROUP BY CodOrdine;
UPDATE ORDINE SET PrezzoTotale = PrezzoTotale + Totale
WHERE CodOrdine = NEW.CodOrdine;
RETURN NEW;
END
$$
Everything works and the trigger just fires fine, but no UPDATE is done on table ORDINE (on line 12).
What I experience (and tried):
I've tried to put some print instructions to see if the query was working:
RAISE NOTICE 'NEW.CodOrdine = %', NEW.CodOrdine;
RAISE NOTICE 'NEW.CodProdotto = %', NEW.CodProdotto;
RAISE NOTICE 'NEW.Prezzo = %', NEW.Prezzo;
but the output returns that new row inserted:
INSERT INTO COMPORDINE VALUES ('12345', '11111', 1.80)
has null values:
NOTICE: NEW.CodOrdine = <NULL>
NOTICE: NEW.CodProdotto = <NULL>
NOTICE: NEW.Prezzo = <NULL>
So the UPDATE "fails" because no row in ORDINE matches NULL values:
CodOrdine
PrezzoTotale
DataAcquisto
CodCliente
CodDipendente
12345
default (0.0)
2020-12-12
00000
99999
Apparently the problem could be related to the inserted row, may it?
What I expect:
After INSERT I would the ORDINE.PrezzoTotale column to be updated with the correct total price of every item in COMPORDINE that matches COMPORDINE.CodOrdine = ORDINE.CodOrdine in this way:
INSERT INTO COMPORDINE VALUES ('12345', '11111', 1.80)
CodOrdine
PrezzoTotale
DataAcquisto
CodCliente
CodDipendente
12345
1.80
2020-12-12
00000
99999
Big thanks to anyone in advance
[EDIT #1]:
As asked, here's the output of \d compordine:
**"public.compordine" table:**
| Colonna | Tipo | Ordinamento | Pu‗ essere null | Default|
|------------|--------------|-------------|-----------------|--------|
|codprodotto | character(5) | | not null | |
|codordine | character(5) | | not null | |
|prezzo | real | | not null | |
**Indexes**:
"compordine_codprodotto_key" UNIQUE CONSTRAINT, btree (codprodotto)
**Check constraints**:
"compordine_codordine_check" CHECK (codordine ~* '^[0-9]+$'::text)
"compordine_codprodotto_check" CHECK (codprodotto ~* '^[0-9]+$'::text)
"compordine_prezzo_check" CHECK (prezzo > 0::double precision)
**Referential integrity constraints**:
"ordinefk" FOREIGN KEY (codordine) REFERENCES ordine(codordine) ON UPDATE CASCADE
"prodottofk" FOREIGN KEY (codprodotto) REFERENCES prodotto(codprodotto) ON UPDATE CASCADE
**Triggers**:
updateprezzoordine AFTER INSERT ON compordine FOR EACH STATEMENT EXECUTE FUNCTION updateprezzoordine()
**Disabled triggers**:
updatescorte BEFORE INSERT ON compordine FOR EACH ROW EXECUTE FUNCTION updatescorte()
[EDIT #2]:
So, I did several other attempts to find a solution (SPOILER ALERT: no solution found); what I did, was to create another function and trigger that simply prints the NEW.<column> value after an INSERT statement on a different table called CLIENTE (my suspect was that all this issue was releated only to COMPORDINE table but I was wrong), here's the code:
CREATE TABLE CLIENTE (
CodCliente CHAR(5) PRIMARY KEY, CHECK(CodCliente ~* '^[0-9]+$'),
Nome VARCHAR(255) NOT NULL, CHECK(Nome ~* '^[A-Za-z ]+$'),
Cognome VARCHAR(255) NOT NULL, CHECK(Cognome ~* '^[A-Za-z ]+$'),
CodiceFiscale CHAR(16) NOT NULL, CHECK(CodiceFiscale ~* '^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$'),
Indirizzo VARCHAR(300) NOT NULL,
Email VARCHAR(255) NOT NULL, CHECK(Email ~* '^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+[.][A-za-z]+$'),
UNIQUE(CodiceFiscale, Email)
);
CREATE TRIGGER print
BEFORE INSERT ON Cliente
EXECUTE PROCEDURE print();
CREATE FUNCTION print()
RETURNS TRIGGER
LANGUAGE plpgsql
AS
$$
BEGIN
RAISE NOTICE 'CodCliente = %', NEW.CodCliente;
RETURN NEW;
END
$$
INSERT INTO CLIENTE VALUES ('34567', 'Gianfranco', 'Rana', 'GNFRNA87F24E123A', 'Via Delle Rane 23', 'franco.rane#ravioli.it')
Output returns:
NEW.CodCliente = <NULL>
While it should be:
NEW.CodCliente = <34567>
I simply can't understand why, shouldn't NEW in an AFTER INSERT TRIGGER returns the value this way? :
Event
OLD
NEW
INSERT
NULL
New Record
DELETE
Old Record
NULL
UPDATE
Original Record
Updated Record
Had the same issue. For me the issue was the way I created the trigger:
create trigger pending_balance_add after insert on pending_balance
execute function update_pending_balance();
I forgot to tell that "for each row" and perhaps "procedure" instead of function:
create trigger pending_balance_add before insert on pending_balance
for each row
execute procedure update_pending_balance();
Now inside of update_pending_balance I have correct new.amount, etc.
Regarding your issue, it shows that
Triggers:
updateprezzoordine AFTER INSERT ON compordine FOR EACH STATEMENT EXECUTE FUNCTION updateprezzoordine()
Disabled triggers:
updatescorte BEFORE INSERT ON compordine FOR EACH ROW EXECUTE FUNCTION updatescorte()
When trigger is executed for statement it has NEW.FIELD values as NULL
PostgreSQL Can't change values in AFTER trigger. Change the trigger to BEFORE.
Does not matter if you change the variable record NEW, the data was already changed in the table record. AFTER triggers ignore NEW changes. You can read and write but only read matters.
A very similar question here but not quite the same as this one.
I have a function that uses IF statements to determine what type of SELECT query to return.
How can I declare what a CREATE FUNCTION statment should return when I will never know the exact columns a SELECT query within it might return? That is, I can't setup a RETURNS TABLE declaration with a list of columns because I don't know which columns might come back. All I know is that I definitely will want a table of results to be returned.
Here is my function (uncompleted, pseudo):
CREATE OR REPLACE FUNCTION functiona(_url character varying DEFAULT NULL)
RETURNS -- what type? if TABLE how do I know what columns to specify
LANGUAGE plpgsql
AS
$$
DECLARE
_urltypeid int;
BEGIN
IF _url IS NOT NULL
THEN
_urltypeid := reference.urltype(_url);
IF _urltypeid = 1
THEN
RETURN QUERY
SELECT location, auxiliary, response FROM tablea -- unique columns from one table
END IF;
IF _urltypeid = 2
THEN
RETURN QUERY
SELECT ip, location, host, authority FROM tableb -- unique columns from another table
END IF;
END IF;
END;
$$;
I come from a MS SQL Server background where I don't have to specify in the CREATE FUNCTIONstatement what I'm returning, hence this is very confusing for me.
Not an answer, but an explanation of why answer from #JonathanJacobson will not work using a simple example:
\d animals
Table "public.animals"
Column | Type | Collation | Nullable | Default
--------+------------------------+-----------+----------+---------
id | integer | | not null |
cond | character varying(200) | | not null |
animal | character varying(200) | | not null |
CREATE OR REPLACE FUNCTION public.animal(a_type character varying)
RETURNS record
LANGUAGE plpgsql
AS $function$
BEGIN
SELECT row(id, cond, animal) FROM animals where animal = a_type;
END;
$function$
select * from animal('cat');
ERROR: a column definition list is required for functions returning "record"
LINE 1: select * from animal('cat');
CREATE OR REPLACE FUNCTION public.animal(a_type character varying)
RETURNS SETOF record
LANGUAGE plpgsql
AS $function$
BEGIN
RETURN QUERY
SELECT id, cond, animal FROM animals where animal = a_type;
END;
$function$
;
select * from animal('cat') as t(i integer, c varchar, a varchar);
i | c | a
---+------+-----
1 | fat | cat
2 | slim | cat
6 | big | cat
In order to use the output of a function returning a record or setof record you need to declare the output fields and types when you run the function.
You could use the record type. Not tested.
CREATE OR REPLACE FUNCTION functiona(_url character varying DEFAULT NULL)
RETURNS record
LANGUAGE plpgsql
AS
$$
DECLARE
_broadcasttypeid int;
BEGIN
IF _url IS NOT NULL
THEN
_urltypeid := reference.urltype(_url);
IF _urltypeid = 1
THEN
RETURN
(SELECT row(location, auxiliary, response) FROM tablea);
END IF;
IF _urltypeid = 2
THEN
RETURN
(SELECT row(ip, location, host, authority) FROM tableb);
END IF;
END IF;
END;
$$;
Other composite types, such as jsonb and hstore are also a solution.
Let's say we have a table like:
Table "someschema.todo"
Column | Type | Nullable | Default
--------------+--------------+----------+----------------------------------
id | integer | not null | nextval('todo_id_seq'::regclass)
contract_id | integer | not null |
title | text | |
description | text | |
status | todo_status | | 'INCOMPLETE'::todo_status
Foreign-key constraints:
"todo_contract_id_fkey" FOREIGN KEY (contract_id) REFERENCES contract(id)
Row Level Security is enabled, and we'll assume that the rules have been set up appropriately with one exception: a user type who would not normally be allowed to UPDATE rows in this table is required to be able to bump the status column from one enum value to another. Say, from INCOMPLETE to PENDING. That user type also needs to be able to UPDATE the table at other times (depending on conditions related to the contract_id fkey) so we can't just use a blanket column grant.
Arguably, this could make the column a candidate for inclusion in a new todo_status table, but let's just rule that out for the moment. Now we could write a trigger to check every column by name to see if it had been modified and only allow those queries which modify status and nothing else... but that seems fragile (what if we later add another column?) and painful.
Is there a way within a trigger to allow a modification of "no column except status"? In other words, "deny access unless the only column modified is status".
Supplemental: is there a way to accomplish this using a check_expression or using_expression within CREATE POLICY that I haven't considered? I've been assuming that because we don't have the NEW values in using_expression or the OLD values in check_expression, I can't use RLS to achieve what we need.
A trigger would be relatively robust
CREATE OR REPLACE FUNCTION validate_update()
RETURNS trigger AS
$BODY$
DECLARE
v_key TEXT;
v_value TEXT;
valid_update boolean := true;
BEGIN
FOR v_key, v_value IN select key, value from each(hstore(NEW)) each LOOP
if (coalesce(v_value,'') != coalesce((hstore(OLD) -> v_key),'')) then
if (v_key != 'status') then
valid_update := false;
end if;
end if;
END LOOP;
if (valid_update) then
raise info 'good update';
return NEW;
else
raise info 'bad update';
return null;
end if;
END;
$BODY$
LANGUAGE plpgsql;
create trigger validate_update before update on someschema.todo
for each row execute procedure validate_update();
We want to use pgsql_fdw to select a table of remote postgresql database. When we
select the table in a session it is okay, but when we use the foreign table in a funciton
it turns out "ERROR: cache lookup failed for type 0". Does anybody know why?
1. base information
skytf=> \d ft_test;
Foreign table "skytf.ft_test"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(32) |
Server: pgsql_srv
skytf=> \des+ pgsql_srv
List of foreign servers
Name | Owner | Foreign-data wrapper | Access privileges | Type | Version | Options
-----------+-------+----------------------+-------------------+------+---------+----------------------------------------
pgsql_srv | skytf | pgsql_fdw | | | | {host=127.0.0.1,port=1923,dbname=mydb}
(1 row)
2. destination table
mydb=> \d test
Table "mydb.test"
Column | Type | Modifiers
--------+-----------------------+-----------
id | integer |
name | character varying(32) |
Indexes:
"idx_test_1" btree (id)
3. function
CREATE or replace FUNCTION func_sync_bill() RETURNS INTEGER AS $$
BEGIN
begin
insert into test_tf (id,name) select id,name from ft_test;
return 1;
end;
END;
$$ LANGUAGE 'plpgsql';
4. it works in a session
skytf=> create table test_tf(id integer,name varchar(32));
CREATE TABLE
skytf=> insert into test_tf select * from ft_test;
INSERT 0 1990000
5. function call error
skytf=> truncate table test_tf;
TRUNCATE TABLE
skytf=> select func_sync_bill();
ERROR: cache lookup failed for type 0
CONTEXT: SQL statement "insert into test_tf (id,name) select id,name from ft_test"
PL/pgSQL function "func_sync_bill" line 5 at SQL statement
When I call the function func_sync_bill() which will select a foreign table, it generates the the error.
Is this a bug of pgsql_fdw?
Looks like a bug in the foreign data wrapper or plpgsql or something. Given that the \d commands are working, I would expect that the catalogs are fine, which suggests that the real problem is data corruption although the fact that it works outside the function suggests that maybe that's not the case.
things to do to troubleshoot would be to try to rewrite your function as an SQL language function instead of plpgsql and see if that fixes the problem. if it does, you have a workaround. Also try on more recent minor versions of PostgreSQL. If the problem is fixed, then great. But if not, this is a case where a bug report to the project may result in getting the problem fixed.
Finaly I use dynamic statements avoid the problem.
--dynamic statements
skytf=> CREATE or replace FUNCTION func_sync_bill() RETURNS INTEGER AS $$
skytf$> BEGIN
skytf$> begin
skytf$> EXECUTE 'insert into test_tf (id,name) select id,name from ft_test';
skytf$> return 0;
skytf$> end;
skytf$> END;
skytf$> $$ LANGUAGE 'plpgsql';
CREATE FUNCTION
--try again
skytf=> truncate table test_tf;
TRUNCATE TABLE
skytf=> select func_sync_bill();
func_sync_bill
----------------
0
(1 row)
skytf=> select count(*) from test_tf;
count
---------
1990000
(1 row)