Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to properly implement soft delete in postgresql?

Tags:

postgresql

in my postgres 9.3 database, I have the following combination of function and a trigger to implement soft delete functionality:

ALTER TABLE "LIBN02"."trigger_test_1"
ADD COLUMN delete_ind integer

CREATE OR REPLACE FUNCTION trigger_test_1_soft_delete()  
  RETURNS trigger AS $$
    DECLARE
      command text := ' SET delete_ind = 1 WHERE uuid_col = $1';
    BEGIN
      EXECUTE 'UPDATE ' || "LIBN02"."trigger_test_1" || command USING OLD.uuid_col;
      RETURN NULL;
    END;
  $$ LANGUAGE plpgsql;

CREATE TRIGGER test_1_soft_delete_trigger  
  BEFORE DELETE ON "LIBN02"."trigger_test_1"
  FOR EACH ROW EXECUTE PROCEDURE trigger_test_1_soft_delete();

After all of the above is done, I run the following delete statement:

DELETE FROM "LIBN02"."trigger_test_1"

I get the following error:

ERROR:  missing FROM-clause entry for table "LIBN02"
LINE 1: SELECT 'UPDATE ' || "LIBN02"."trigger_test_1" || command
                            ^
QUERY:  SELECT 'UPDATE ' || "LIBN02"."trigger_test_1" || command
CONTEXT:  PL/pgSQL function trigger_test_1_soft_delete() line 5 at EXECUTE

********** Error **********

ERROR: missing FROM-clause entry for table "LIBN02"
SQL state: 42P01
Context: PL/pgSQL function trigger_test_1_soft_delete() line 5 at EXECUTE

What should I change in order for this to work?

like image 936
Eugene Goldberg Avatar asked Sep 18 '25 02:09

Eugene Goldberg


1 Answers

The error you are receiving is because "LIBN02"."trigger_test_1" is not a string (these are quoted with single quotes), but an identifier. You should use

EXECUTE 'UPDATE "' || TG_TABLE_SCHEMA || '"."' || TG_TABLE_NAME || '" ' || command
   USING OLD.uuid_col;

You could also add AND NOT deleted to the WHERE clause to avoid unnecessary churn.

like image 107
Laurenz Albe Avatar answered Sep 20 '25 17:09

Laurenz Albe