Suppose I have vacuum_values() function, which removes all
"no longer referenced" by parent column.  Kind of function
to be run from time to time to clean table from crud.
It looks like this:

I suppose you have a good reason to not use a foreign key with "ON DELETE CASCADE" ?


                FOR r IN SELECT value_id FROM values NATURAL LEFT JOIN
other_tab WHERE other_tab.value_id IS NULL FOR UPDATE OF values LOOP
                        DELETE FROM values WHERE value_id = r.value_id;
                END LOOP;
                RETURN;

I don't remember the exact syntax (look in the DELETE docs) but you can certainly put a left join inside a delete and do it all at once with only one query, and it'll be faster to boot.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
     joining column's datatypes do not match

Reply via email to