Re: Audtiting, DDL and DML in same SQL Function - Mailing list pgsql-general

From Christian Ramseyer
Subject Re: Audtiting, DDL and DML in same SQL Function
Date
Msg-id 4F2AA171.1070107@networkz.ch
Whole thread Raw
In response to Re: Audtiting, DDL and DML in same SQL Function  (Scott Marlowe <scott.marlowe@gmail.com>)
List pgsql-general
On 2/2/12 12:39 AM, Scott Marlowe wrote:
> On Wed, Feb 1, 2012 at 4:27 PM, Scott Marlowe <scott.marlowe@gmail.com> wrote:
>> On Wed, Feb 1, 2012 at 3:29 PM, Christian Ramseyer <rc@networkz.ch> wrote:
>>> Optimally, I'd just have my applications perform a single
>>> call after connecting, e.g. "audit_init('USERNAME', 'Name of application')".
>>
>> I think if you build the query as a string and EXECUTE it it will
>> work.  But I'm not guaranteeing it.
>
> Note that you might have to build both queries and EXECUTE them to make it work.
>

Thanks Scott, executing it actually does the trick. I'm now using this:

create or replace function audit_start(text, text) returns void as $$
    declare
            username alias for $1;
            application alias for $2;
    begin

        execute 'drop table if exists audit_session ;
                create temporary table audit_session (
            username text, application text)';

        execute 'insert into audit_session
                          (username, application)
              values ($1, $2)'
            using username, application;
    end;
$$
language plpgsql;

Christian

pgsql-general by date:

Previous
From: Florian Weimer
Date:
Subject: Re: Is it possible to speed up addition of "not null"?
Next
From: Adrian Klaver
Date:
Subject: Re: [GENERA]: Postgresql-9.1.1 synchronous replication issue