pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedProgrammatic Trigger Create
3+ messages / 3 participants
[nested] [flat]
* Programmatic Trigger Create
@ 2021-03-20 17:03 Niko Ware <nikowareinc@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Niko Ware @ 2021-03-20 17:03 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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*:
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Programmatic Trigger Create
@ 2021-03-20 17:13 Adrian Klaver <adrian.klaver@aklaver.com>
parent: Niko Ware <nikowareinc@gmail.com>
0 siblings, 1 reply; 3+ messages in thread
From: Adrian Klaver @ 2021-03-20 17:13 UTC (permalink / raw)
To: Niko Ware <nikowareinc@gmail.com>; pgsql-general@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Programmatic Trigger Create
@ 2021-03-20 17:23 Tom Lane <tgl@sss.pgh.pa.us>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2021-03-20 17:23 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: Niko Ware <nikowareinc@gmail.com>; pgsql-general@lists.postgresql.org
Adrian Klaver <adrian.klaver@aklaver.com> writes:
> In what program is:
> EXEC SQL EXECUTE IMMEDIATE :my_sql_cmd
> being done?
Presumably ECPG, which if memory serves defaults to not-auto-commit.
Maybe Niko is failing to commit the transaction?
If all else fails, turning on log_statements on the server and
examining the log might help debug what the program is doing wrong.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2021-03-20 17:23 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-20 17:03 Programmatic Trigger Create Niko Ware <nikowareinc@gmail.com>
2021-03-20 17:13 ` Adrian Klaver <adrian.klaver@aklaver.com>
2021-03-20 17:23 ` Tom Lane <tgl@sss.pgh.pa.us>
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