Re: Dynamic pgplsql triggers

From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: "Worky Workerson" <worky(dot)workerson(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Dynamic pgplsql triggers
Date: 2006-08-01 13:21:03
Message-ID: b42b73150608010621n43ca61dbl7478980b03c711fd@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/31/06, Worky Workerson <worky(dot)workerson(at)gmail(dot)com> wrote:
> I'm trying to trigger a whole bunch of partitions at once (initial DB
> setup) using the same plpgsql trigger. The trigger is basically the
> merge trigger in the docs (i.e. UPDATE IF NOT FOUND RETURN NEW ...).
>
> I need to use the TG_RELNAME variable within the "UPDATE" in the
> trigger so that I can use the same function to trigger all of the
> partitions (correct?), the problem is that I can't quite figure out
> how. I figure that I will have to use EXECUTE on a string that I
> build up, right? The problem that I'm having with this approach is
> that some of the columns of NEW don't have a text conversion, and I'm
> getting an error whenever the trigger fires. Is there a way around
> this and/or a better way to trigger a bunch of partitions with the
> same function?

I don't think it's possible. however, what is possible and achieves
roughly the same affect is to query the system catalogs (or
information schema) and via dynamic sql cut trigger
funtions/procedures by looping the results of your query. non-dynamic
sql will usually be a bit faster than dynamic as a bonus, the only
downsie is you are creating a lot of functions, albeit in easy to
manage fashion. If you are really clever, you can put your trigger
functions in a special schema for organizational purposes.

to do this the 'functional' way:

create or replace function create_trigger_for_table(table_name text,
schema_name text) returns void as
$$
begin
excecute 'create or replace function ' -- and so forth
end;
$$;

and to invoke the function:

select create_trigger_for_table(table_name , schema_name ) from
information_schema.tables -- and so forth

regards,
merlin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Worky Workerson 2006-08-01 14:02:07 Re: Dynamic pgplsql triggers
Previous Message Martijn van Oosterhout 2006-08-01 12:42:51 Re: Unicode sorting problem