如何在 Postgres 中实现清除 os 软删除记录



在我的Postgres 9.4数据库中,我有以下触发器/函数,它实现了"软删除"功能:

ALTER TABLE my_schema.my_table
ADD COLUMN delete_ind integer

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

CREATE TRIGGER my_table_soft_delete_trigger  
  BEFORE DELETE ON "my_schema"."my_table"
  FOR EACH ROW EXECUTE PROCEDURE trigger_mytable_soft_delete();

上面的代码为我提供了"软删除"功能,但是,它也阻止我实际删除/清除那些已经标记为已删除的行。

新的期望行为是使用此删除函数来检查delete_ind字段的值,如果它已设置为 1,则实际永久清除该行。

正确的条件语法是什么,它会根据delete_ind列的当前值设置 delete_ind. 的值或实际删除有问题的行?

可以通过对函数进行相对较小的修改来完成:

CREATE OR REPLACE FUNCTION trigger_mytable_soft_delete()  
RETURNS trigger AS
$$  
BEGIN
    if OLD.delete_ind = 1 then
        /* Delete proceeds, you just needs to *do nothing* 
           except for returning the OLD row as it were */
        RETURN OLD ;  
    else
        /* instead of deleting, set a flag */
        UPDATE my_schema.my_table 
           SET deleted_ind = 1
         WHERE uuid_col = old.uuid_col ;
        /* This will skip the process of this row.
           It will also avoid subsequent triggers to be fired, and the row will
           not be counted on the rows-affected count. If more triggers need
           to be processed, make sure this is the last in the chain.
        */
        RETURN NULL ;
    end if ;
END;
$$ 
LANGUAGE plpgsql;

(如果你的函数是逐个表使用的,你可以硬编码,不需要动态 SQL)


旁注:如果列delete_ind仅用作标志,则最好通过将其声明为boolean not null而不是integer来传达其含义。

最新更新