Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search for
  Advanced Search

Re: Audit-trail engine: getting the application's layer user_id


  • From: Tilmann Singer <tils-pgsql(at)tils(dot)net>
  • To: pgsql-general(at)postgresql(dot)org
  • Subject: Re: Audit-trail engine: getting the application's layer user_id
  • Date: Wed, 25 Apr 2007 18:40:14 +0200
  • Message-id: <20070425164014(dot)GX26299(at)tils(dot)net>

* Manuel Sugawara <masm(at)fciencias(dot)unam(dot)mx> [20070425 17:57]:
> I think you can use a plpgsql function with execute. For instance, if
> the name of your temp table is current_user_id the function will be
> something like:
> 
> create function get_current_user_id() returns int as $$
> declare
>   v_rec record;
>   v_user int;
>   v_query text;
> begin
>   v_query := 'SELECT user_id FROM current_user_id';
>   for v_rec in execute v_query loop
>     v_user := v_rec.user_id;
>   end loop;
>   return v_user;
> end;   
> $$ language plpgsql;
> 
> Untested but that's the idea, you need to use execute to avoid the
> caching of the plan. You might also want to control what happens when
> the table does not exist and that can be done handling the
> corresponding exception. Check the docs for the details.

Excellent, that works exactly as needed! I will rely on the permanent
table being there always to provide the default value if no temporary
table has been created.


Thanks! Til



Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group