Re: session id and global storage

Lists: pgsql-hackers
From: "Rodrigo De Leon" <rdeleonp(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: session id and global storage
Date: 2006-06-29 13:12:20
Message-ID: a55915760606290612t78415910xc0a91046192e9eed@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

>>> Hi, I cant find any function, which tells me something like session
>>> id. Is there something like that? I need it in my AM, because I need
>>> to know, if something which I wrote in file was written in this
>>> current session or previously.
>>
>> How about
>> select procpid||' '||backend_start from pg_stat_activity;
>> Yours,
>> Laurenz Albe
>
>Something like this would be maybe possible, but this select can
>return more rows, when the user is connected with more instances...
>
>David Hoksza

You could do this:

SELECT procpid||' '||backend_start
FROM pg_stat_activity
WHERE datname = current_database()
AND usename = session_user
AND client_addr = inet_client_addr()
AND client_port = inet_client_port();

Regards,

Rodrigo


From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: rdeleonp(at)gmail(dot)com, rdeleonp(at)yahoo(dot)com
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: session id and global storage
Date: 2006-06-29 13:44:13
Message-ID: 44A3D92D.8020502@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Rodrigo De Leon wrote:

>>>> Hi, I cant find any function, which tells me something like session
>>>> id. Is there something like that? I need it in my AM, because I need
>>>> to know, if something which I wrote in file was written in this
>>>> current session or previously.
>>>
>>>
>>> How about
>>> select procpid||' '||backend_start from pg_stat_activity;
>>> Yours,
>>> Laurenz Albe
>>
>>
>> Something like this would be maybe possible, but this select can
>> return more rows, when the user is connected with more instances...
>>
>
> You could do this:
>
> SELECT procpid||' '||backend_start
> FROM pg_stat_activity
> WHERE datname = current_database()
> AND usename = session_user
> AND client_addr = inet_client_addr()
> AND client_port = inet_client_port();
>

That's pretty roundabout. We already expose (hex coded) pid.starttime as
a session identifier in log_line_prefix (it's the %c escape) so I don't
see any reason not to provide either the same thing directly in a
function, or at least to expose the backend pid.

If you need it in backend C code, the data can be fetched from MyProcPid
and MyProcPort->session_start.tv_sec

cheers

andrew


From: "Rodrigo De Leon" <rdeleonp(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: session id and global storage
Date: 2006-06-29 13:48:21
Message-ID: a55915760606290648n4693aa1fr9c801cea6e609f22@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

> That's pretty roundabout. We already expose (hex coded) pid.starttime as
> a session identifier in log_line_prefix (it's the %c escape) so I don't
> see any reason not to provide either the same thing directly in a
> function, or at least to expose the backend pid.

That would be nice.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Dunstan <andrew(at)dunslane(dot)net>
Cc: rdeleonp(at)gmail(dot)com, rdeleonp(at)yahoo(dot)com, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: session id and global storage
Date: 2006-06-29 14:10:54
Message-ID: 20345.1151590254@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Andrew Dunstan <andrew(at)dunslane(dot)net> writes:
> Rodrigo De Leon wrote:
>> You could do this:
>>
>> SELECT procpid||' '||backend_start
>> FROM pg_stat_activity
>> WHERE datname = current_database()
>> AND usename = session_user
>> AND client_addr = inet_client_addr()
>> AND client_port = inet_client_port();

> That's pretty roundabout.

Indeed. Use pg_backend_pid() instead:

SELECT whatever FROM pg_stat_activity WHERE procpid = pg_backend_pid();

A difficulty with this in existing releases is that pg_stat_activity
lags behind reality, so that you won't see your session listed in it
until you've been connected at least half a second or so. 8.2 won't
have that problem.

regards, tom lane