From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Niko Ware <nikowareinc@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Programmatic Trigger Create
Date: Sat, 20 Mar 2021 10:13:50 -0700
Message-ID: <5566ea3a-e0ae-8595-a13f-ec525e7c9eeb@aklaver.com> (raw)
In-Reply-To: <CACHGMtRojCxdrr0mGrr97M24KNQHA28o3rWXn2Q7v71SruM+1w@mail.gmail.com>
References: <CACHGMtRojCxdrr0mGrr97M24KNQHA28o3rWXn2Q7v71SruM+1w@mail.gmail.com>
On 3/20/21 10:03 AM, Niko Ware wrote:
> I would like to programmatically create audit trail functions which are
> called by triggers for custom user tables. This will be used for audit
> trail generation in our application. The user is able to define a custom
> table. Therefore, I need a custom audit trail function. The audit trail
> function outputs the row changes in human readable form (e.g., "process
> name changed from "my process" to "your process" by user xxxx on host xxx".
>
> The basic steps are as follows:
>
> 1. User defines the table
> 2. Create table via EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd
> 3. Programatically constructs the audit trail function for
> insert/update/delete.
> 4. Create the function via EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd
> 5. Create the trigger via EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd
>
> Here is an example in function source which is passed to "EXEC SQL
> EXECUTE IMMEDIATE" via char*:
In what program is:
EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd
being done?
>
>
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> 10
> 11
> 12
>
> CREATE OR REPLACE FUNCTION name_changes_log()
> RETURNS TRIGGER AS
> $BODY$
> BEGIN
> IF NEW.name <> OLD.name THEN
> INSERT INTO kids_audit(kids_id,kids_name,modified_on)
> VALUES(OLD.id,OLD.name,now());
> END IF;
>
> RETURN NEW;
> END;
> $BODY$ LANGUAGE plpgsql;
>
>
> The "execute immediate" succeeds, but the function is not created. The
> application is connected to the database as a user which has permission
> to create functions.
>
> I could output the function text to a file and then use "psql" to
> process the "create trigger", but this seems a bit hacky.
>
> Thanks in advance,
> Thomas
>
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, nikowareinc@gmail.com
Subject: Re: Programmatic Trigger Create
In-Reply-To: <5566ea3a-e0ae-8595-a13f-ec525e7c9eeb@aklaver.com>
References: <CACHGMtRojCxdrr0mGrr97M24KNQHA28o3rWXn2Q7v71SruM+1w@mail.gmail.com> <5566ea3a-e0ae-8595-a13f-ec525e7c9eeb@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox