Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 如何在Postgres中实现清除操作系统软删除记录?_Postgresql - Fatal编程技术网

Postgresql 如何在Postgres中实现清除操作系统软删除记录?

Postgresql 如何在Postgres中实现清除操作系统软删除记录?,postgresql,Postgresql,在我的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

在我的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函数检查delete_ind字段的值,如果该字段已设置为1,则实际永久清除该行


正确的条件语法是什么,它可以设置delete\u ind.的值,或者根据delete\u 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\u 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\u ind
仅用作标志,则最好将其声明为
布尔非空
,而不是
整数