I am trying to delete rows in a specific order. I tried the suggestions in the post How do I delete a fixed number of rows with sorting in PostgreSQL?, but nothing worked. I know they did not work because I had a trigger on delete and seeing which one is deleted and it is never doing it in the order that I want it to.
The tests that I wrote are like this:
create table test1 (
th text,
co text,
ty text);
CREATE OR REPLACE FUNCTION testfunc() RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
DECLARE modified_row test1%ROWTYPE;
BEGIN
IF TG_OP = 'DELETE' THEN
modified_row := OLD;
ELSE
modified_row := NEW;
END IF;
raise notice 'opearation: %, co: %', TG_OP, modified_row.co;
RETURN modified_row;
END;
$$;
CREATE TRIGGER trigger_test1
BEFORE DELETE OR INSERT on test1
FOR EACH ROW
EXECUTE PROCEDURE testfunc();
insert into test1 (th, co, ty) values ('t1', 'c1', 'p'), ('t2', 'c2','p'), ('t2', 'c3','p'), ('t2', 'c4','p');
delete from test1 where ctid in (select ctid from test1 order by co desc);
The delete statement above is the highest voted and accepted answer in the above post. But for me, the output has always been:
NOTICE: opearation: DELETE, co: c1
NOTICE: opearation: DELETE, co: c2
NOTICE: opearation: DELETE, co: c3
NOTICE: opearation: DELETE, co: c4
which indicates that the deletes are not done in desc order of 'co' that I am trying to do. Am I missing something here?
NOTE: forget about the insert order, I am only concerned about the delete order. And I could not ask this question in the above post because I do not have enough reputation to put a comment.