Inserting NEW.* from a generic trigger using EXECUTE in PL/pgsql

后端 未结 3 1906
醉酒成梦
醉酒成梦 2020-12-17 14:40

I have a number of tables that use the Postgres \"Partitioning\" feature. I want to define a common BEFORE INSERT OF ROW trigger on each table that will 1) dynamically crea

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 15:21

    You can use EXECUTE USING to pass NEW to it. Your example would be

    EXECUTE 'INSERT INTO ' || TG_RELID || '::regclass SELECT $1' USING NEW;
    

    (Note that I use TG_RELID casted to regclass instead of fiddling with TG_TABLE_SCHEMA and TABLE_NAME because it is easier to use, if nonstandard. But then, plpgsql is nonstandard anyway.)

提交回复
热议问题