I have the following code in postgresql:
CREATE OR REPLACE FUNCTION update_category() RETURNS trigger AS $newProduct$
BEGIN
UPDATE category SET product_count = product_count + 1 WHERE cat_id = NEW.cat_id;
INSERT INTO notification (content, type, p_id) VALUES('new product', 1, NEW.p_id);
RETURN NEW;
END;
$newProduct$ LANGUAGE plpgsql;
CREATE TRIGGER update_cat AFTER INSERT ON product
EXECUTE PROCEDURE update_category();
And after inserting a record into product I get the error:
[2017-03-20 16:05:05] [55000] ERROR: record "new" is not assigned yet
[2017-03-20 16:05:05] Detail: The tuple structure of a not-yet-assigned record is indeterminate.
[2017-03-20 16:05:05] Where: SQL statement "UPDATE category SET product_count = product_count + 1 WHERE cat_id = NEW.cat_id"
[2017-03-20 16:05:05] PL/pgSQL function update_category() line 3 at SQL statement
I've looked around for a solution, but I only find cases where the error is because
FOR EACH STATEMENT
is being used instead of
FOR EACH ROW
Seeing as I'm simply executing the procedure once, the solution doesn't apply in my case.
Thanks for your help!
The solution was indeed to add FOR EACH ROW:
CREATE TRIGGER update_cat AFTER INSERT ON product
FOR EACH ROW EXECUTE PROCEDURE update_category();
I assumed FOR EACH ROW meant calling the procedure once for each row in product, not for each row inserted.
Related
I am using postgresql and pgadmin and I am new to this. I have a table "Users" that has the following columns (Username, Name, Email, Phone, Discount, Password, token, serial, created_on, updated_on, points, reference).
I was trying to create a trigger for the table so that everytime a new insert occurs with an existing username in the reference field, the points of the used username will be incremented by 50. So there are two operations - updating the existing table, inserting the new values.
I tried creating a trigger function like this:
create or replace function points()
returns trigger as
$BODY$
BEGIN
if
new."Users"."reference" in (old."Users"."username")
then
Insert into "Users"(Username,Name,Email,Phone,Discount,Password,token,serial,created_on,updated_on,points,reference)
values(new.Username,new.Name,new.Email,new.Phone,new.Discount,new.Password,new.token,new.serial,new.created_on,new.updated_on,new.points,new.reference);
update "Users"
set old."Users"."points" = old."Users"."points" + 50
where "Users"."username" = (select "Users"."username" from "Users" where new."Users"."reference" in (old."Users"."username"));
end if;
RETURN new;
END;
$BODY$
language plpgsql;
and a trigger like this:
create trigger referece_points
after insert
on "Users"
for each row
execute procedure points();
But when I try to insert a new values in the "Users" table, I get the following error:
ERROR: missing FROM-clause entry for table "Users" LINE 1: SELECT new."Users"."reference" in (old."Users"."username") ^ Query: SELECT new."Users"."reference" in (old."Users"."username") CONTEXT: PL/pgSQL function points() line 2 at IF
New is composite variable of type that is related to table joined with trigger.
The code new."Users"."reference" has not any sense. You should to write just new."reference".
I have a trigger AFTER INSERT ON mytable that calls a function
CREATE OR REPLACE FUNCTION myfunction() RETURNS trigger AS
$BODY$
DECLARE
index TEXT;
BEGIN
index := 'myIndex_' || NEW.id2::text;
IF to_regclass(index::cstring) IS NULL THEN
EXECUTE 'CREATE INDEX ' || index || ' ON mytable(id) WITH (FILLFACTOR=100) WHERE id2=' || NEW.id2|| ';';
RAISE NOTICE 'Created new index %',index;
END IF;
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
SECURITY DEFINER
COST 100;
ALTER FUNCTION myfunction()
OWNER TO theadmin;
This works wonderfully. For each distinct id2 I create an index. Speeds up relevant queries by a lot.
As mentioned above I trigger this AFTER INSERT ON. Before doing that however I had the trigger set to BEFORE INSERT ON. And the function did some strange things. (Yes, I had changed the RETURN NULL to RETURN NEW)
insert of a new row insert into mytable VALUES(1391, 868, 0.5, 0.5);
creates the corresponding index myIndex_868
the inserted row does not appear in mytable when doing a select :(
trying to insert the same row results in ERROR: duplicate key value violates unique constraint "mytable_pkey" because of course DETAIL: Key (id, id2)=(1391, 868) already exists.
inserting other rows for the same id2 works as expected :)
DELETE FROM mytable WHERE id = 1391 and id2 = 868 does nothing
DROP INDEX myIndex_868; drops the index. And suddenly the initial row that never appeared in the table is suddenly there!
Why does BEFORE INSERT ON behave so differently? Is this a bug in postgres 9.4 or did I overlook something?
Just for completeness' sake:
CREATE TRIGGER mytrigger
AFTER INSERT ON mytable
FOR EACH ROW EXECUTE PROCEDURE myfunction();
vs.
CREATE TRIGGER mytrigger
BEFORE INSERT ON mytable
FOR EACH ROW EXECUTE PROCEDURE myfunction();
I'd argue that this is a bug in PostgreSQL. I could reproduce it with 9.6.
It is clear that the row is not contained in the index as it is created in the BEFORE trigger, but the fact that the index is not updated when the row is inserted is a bug in my opinion.
I have written to pgsql-hackers to ask for an opinion.
But apart from that, I don't see the point of the whole exercise.
Better than creating a gazillion indexes would be to create a single one:
CREATE INDEX ON mytable(id2, id);
I have a table called rezultz with 1 row only containing valid SQL queries like: CREATE TRIGGER ... I'm trying to execute them with this function called get_all_rezultz() but it doesn't seem to work,any ideas why?
CREATE OR REPLACE FUNCTION get_all_rezultz() RETURNS SETOF rezultz AS
$BODY$
DECLARE
r rezultz%rowtype;
BEGIN
FOR r IN
SELECT * FROM rezultz
LOOP
-- can do some processing here
RETURN QUERY EXECUTE r; -- return current row of SELECT
END LOOP;
RETURN;
END
$BODY$
LANGUAGE plpgsql;
SELECT * FROM get_all_rezultz();
Here is the error I get :
NOTICE: identifier "CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON userman FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); " will be truncated to "CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE O"
CONTEXT: SQL statement "("CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON userman FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ")"
PL/pgSQL function get_all_rezultz() line 10 at RETURN QUERY
ERROR: syntax error at or near ""CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON userman FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ""
LINE 1: ("CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPD...
^
QUERY: ("CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON userman FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ")
CONTEXT: PL/pgSQL function get_all_rezultz() line 10 at RETURN QUERY
********** Error **********
ERROR: syntax error at or near ""CREATE TRIGGER userman_if_modified_trg AFTER INSERT OR UPDATE OR DELETE ON userman FOR EACH ROW EXECUTE PROCEDURE audit.if_modified_func(); ""
SQL state: 42601
Context: PL/pgSQL function get_all_rezultz() line 10 at RETURN QUERY
The variable r is a record containing multiple columns. It's not a scalar value (e.g. a string).
So you need to use the column name in the execute statement. Assuming the column in the table is called sql_statement you need to use:
RETURN QUERY EXECUTE r.sql_statement;
However, this will still not work because the select statement stored in that table will most definitely not return a results that is SETOF rezultz it returns a SETOF mytable if the query is select * from mytable.
You would need to specify RETURNS SETOF record but then you need to specify the column names and structure of the result when calling the function.
And even then this won't work because a function still only return a single result, not multiple results from multiple queries - which will happen if rezultzcontains more then one row.
If your SQL statements aren't actually SELECT statements as you have claimed, you will need to use EXECUTE you can't use RETURN QUERY for a CREATE TRIGGER statement.
I have this code, to add trigger for a View in my postgresql database
CREATE OR REPLACE FUNCTION changeCityProc() RETURNS TRIGGER AS $changeCity$
BEGIN
UPDATE vvs.tb_company SET city = "LAL" WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_company.city;
RETURN null;
END;
$changeCity$ LANGUAGE plpgsql;
CREATE TRIGGER mytrigger
INSTEAD OF UPDATE ON
cardpresso.cardinfo FOR EACH ROW EXECUTE PROCEDURE changeCityProc();
pgAdmin says "syntax error at or near "INSTEAD""
Let's simplify this. First, the two schemas.
CREATE SCHEMA cardpresso;
CREATE SCHEMA vvs;
Next, the simplest possible table.
CREATE TABLE vvs.tb_company
(
city character varying(25)
);
INSERT INTO vvs.tb_company VALUES ('foo');
And a fairly useless view.
CREATE VIEW cardpresso.cardinfo AS
SELECT 'test' as tb_company_city UNION ALL
SELECT 'Orem' UNION ALL
SELECT 'Wibble'
;
Simplify the function. Warning: This will destructively update the table. Be careful if you copy stuff from this function into something headed for production.
CREATE OR REPLACE FUNCTION changecityproc()
RETURNS trigger AS
$BODY$
BEGIN
UPDATE vvs.tb_company SET city = 'bar';
RETURN null;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
Finally, the trigger.
CREATE TRIGGER mytrigger
INSTEAD OF UPDATE
ON cardpresso.cardinfo
FOR EACH ROW
EXECUTE PROCEDURE changecityproc();
Now, if we update the view, we'd expect all the rows in vvs.tb_company to change to 'bar'.
select * from vvs.tb_company;
city
--
foo
update cardpresso.cardinfo
set tb_company_city = 'wibble';
select * from vvs.tb_company;
city
--
bar
So it looks like the problem is in the UPDATE statement of your function.
UPDATE vvs.tb_company SET city = "LAL"
WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_company.city;
I'm not certain what you intended that statement to do, but it's not valid in PostgreSQL, regardless of whether "LAL" is supposed to be a column name or a string literal. As a string literal, 'LAL', it will raise an error.
ERROR: missing FROM-clause entry for table "cardinfo"
LINE 2: WHERE cardpresso.cardinfo.tb_company_city = vvs.tb_compa...
I have to write a trigger to ensure unique entries in column account of table accounts:
create table accounts (id serial, account int4 default 0);
I tried to write my trigger like this:
create function x_6 () returns trigger as '
begin
IF row(new.account) is distinct from row(OLD.account) THEN
return NEW;
ELSE
raise notice '' Entries are not unique! '';
END IF;
end;
'
language 'plpgsql';
Or:
create function x_6 () returns trigger as '
begin
IF (new.account <> OLD.account) THEN
return NEW;
ELSE
raise notice '' Entries are not unique ! '';
END IF;
end;
'
language 'plpgsql';
And then
create trigger x_6t before insert on accounts for each row execute procedure x_6();
When I try to insert something:
insert into accounts(account) values (20);
I get an error in either case:
ERROR: record "old" is not assigned yet
DETAIL: The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT: PL/pgSQL function "x_6" line 3 at if
How can I fix it?
This is absolutely wrong way. You should not to use triggers for this purpose, you should to use unique indexes.
CREATE TABLE foo(a int PRIMARY KEY, b int);
-- column b has only unique values
CREATE UNIQUE INDEX ON foo(b);
Your code has more than one issue:
bad identifiers - konto instead account
it is table trigger - you has no any access to data there - PostgreSQL triggers are different than MSSQL
If you use row trigger, where there is possible access to record data, then OLD has different meaning than you expect. It is value of record before change - and this value is defined only for UPDATE or DELETE operations - and it is undefined for INSERT, because there previous value of record doesn't exist.