Get values from varying columns in a generic trigger

后端 未结 1 1483
夕颜
夕颜 2020-12-21 14:41

I am new to PostgreSQL and found a trigger which serves my purpose completely except for one little thing. The trigger is quite generic and runs across different tables and

相关标签:
1条回答
  • 2020-12-21 15:31

    Basically you need dynamic SQL for dynamic column names. format helps to format the DML command. Pass values from NEW and OLD with the USING clause.

    Given these tables:

    CREATE TABLE tbl (
      t_id serial PRIMARY KEY
     ,abc_cust_no text
    );
    
    CREATE TABLE log (
      id          int
     ,table_name  text
     ,column_name text
     ,old_value   text
     ,new_value   text
    );
    

    It could work like this:

    CREATE OR REPLACE FUNCTION trg_demo()
      RETURNS TRIGGER AS
    $func$
    BEGIN
    
    EXECUTE format('
       INSERT INTO log(id, table_name, column_name, old_value, new_value)
       SELECT ($2).t_id
             , $3
             , $4
             ,($1).%1$I
             ,($2).%1$I', TG_ARGV[0])
    USING OLD, NEW, TG_RELNAME, TG_ARGV[0];
    
    RETURN NEW;
    
    END
    $func$ LANGUAGE plpgsql;
    
    CREATE TRIGGER demo
    BEFORE UPDATE ON tbl
    FOR EACH ROW EXECUTE PROCEDURE trg_demo('abc_cust_no'); -- col name here.
    

    SQL Fiddle.

    Related answer on dba.SE:

    • How to access NEW or OLD field given only the field's name?

    List of special variables visible in plpgsql trigger functions in the manual.

    0 讨论(0)
提交回复
热议问题