postgresql trigger --> trigger function --> sub trigger function chain



  • I have a complex trigger function that I wish to break out into multiple sub functions. The trigger would call the master function and the master function would have logic in it to decide which sub function to call. This is as far as I've gotten:

    create table dbo.thing (thingid int);
    create table dbo.thingtwo (thingid int);
    

    create or replace function dbo.tf2(thingid int) returns void as $$
    insert into dbo.thingtwo values (thingid);
    $$ language sql;

    create or replace function dbo.tf1() returns trigger as $thinginsert$
    begin
    perform dbo.tf2(new.thingid);
    return null;
    end;
    $thinginsert$ language plpgsql;

    create trigger thinginsert
    after insert on dbo.thing
    for each row execute procedure dbo.tf1();

    The problem is I want to pass the entire new.* into the second trigger function, not just some columns. Ultimately there would be conditional logic in dbo.tf1() to decide whether to go dbo.tf2() or dbo.tf3() etc.

    How can I make this work?


    I'm using Postgres 12.8.



  • Inside a trigger on the table thing, NEW is of the composite type thing that has the same attribute names and data types as the table columns. So,

    create or replace function dbo.tf2(thing_row thing) returns void as $$
        insert into dbo.thingtwo values (thing_row.thingid);
    $$ language sql; 
    

    create or replace function dbo.tf1() returns trigger as $thinginsert$
    begin
    perform dbo.tf2(new);
    return null;
    end;
    $thinginsert$ language plpgsql;

    https://dbfiddle.uk/?rdbms=postgres_12&fiddle=6954795d825186f85db11ec2e8ee6096 .

    https://stackoverflow.com/questions/28110340/how-to-pass-a-record-to-a-pl-pgsql-function .




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2