![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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: |
|
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; |
#3
| |||
| |||
|
|
Suppose I have vacuum_values() function, which removes all "no longer referenced" by parent column. Kind of function 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. |

#4
| |||
| |||
|
|
Of course I can move all this logic outside of backend, and make the backend just 'do' the DELETEs, ignoring errors... But still, it should be doable in the procedural languages aswell..... |
#5
| |||
| |||
|
|
BEGIN DELETE FROM values WHERE value_id = r.value_id; EXCEPTION WHEN foreign_key_violation THEN NULL; END; |
The thing I didn't notice

![]() |
| Thread Tools | |
| Display Modes | |
| |