A temporary table seems overkill when you could just use a session setting.
Up to version 9.1, custom session variables needed to be declared in postgresql.conf
through the
custom_variable_classes parameter, which made it a bit cumbersome, deployment-wise.
Since 9.2, it's no longer necessary. So you could just issue at connect time:
SET myapp.myusername='foobar';
and then in a trigger or in fact anywhere within the session, SELECT current_setting('myapp.myusername')
, or SHOW myapp.myusername
would get back the value.