Thread: session id and global storage

session id and global storage

From
David Hoksza
Date:
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.

And second thing - it would be great if I could save pointer in some
global storage, because I would like to access my dynamically created
tree in more subsequent scans, which is impossible with void pointer
stored just in opaque scan structure:(

Thnaks,      David Hoksza



Re: session id and global storage

From
David Hoksza
Date:
Something like this would be maybe possible, but this select can
return more rows, when the user is connected with more instances...

David Hoksza

________________________________

>> 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.

AL> How about

AL> select procpid||' '||backend_start from pg_stat_activity;

AL> Yours,
AL> Laurenz Albe



Re: session id and global storage

From
David Hoksza
Date:
It seems MyProcID is what I was searching for...

David Hoksza
________________________________

DH> Something like this would be maybe possible, but this select can
DH> return more rows, when the user is connected with more instances...

DH> David Hoksza

DH> ________________________________

>>> 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.

AL>> How about

AL>> select procpid||' '||backend_start from pg_stat_activity;

AL>> Yours,
AL>> Laurenz Albe


DH> ---------------------------(end of
DH> broadcast)---------------------------
DH> TIP 5: don't forget to increase your free space map settings



Re: session id and global storage

From
Hannu Krosing
Date:
Ühel kenal päeval, N, 2006-06-01 kell 10:10, kirjutas David Hoksza:
> It seems MyProcID is what I was searching for...
> 

On a buzy server with lots of connects, procID will repeat quite often.

-- 
----------------
Hannu Krosing
Database Architect
Skype Technologies OÜ
Akadeemia tee 21 F, Tallinn, 12618, Estonia

Skype me:  callto:hkrosing
Get Skype for free:  http://www.skype.com




Re: session id and global storage

From
"Andrew Dunstan"
Date:
Hannu Krosing said:
> Ühel kenal päeval, N, 2006-06-01 kell 10:10, kirjutas David Hoksza:
>> It seems MyProcID is what I was searching for...
>>
>
> On a buzy server with lots of connects, procID will repeat quite often.
>

log_line-prefix has a sessionid gadget:

Session ID: A unique identifier for each session. It is 2 4-byte hexadecimal
numbers (without leading zeros) separated by a dot. The numbers are the
session start time and the process ID

Assuming PIDs are not reused within one second that will be unique.

cheers

andrew







Re: session id and global storage

From
"Rodrigo De Leon"
Date:
>>> 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_startFROM pg_stat_activityWHERE datname = current_database()AND usename = session_userAND
client_addr= inet_client_addr()AND client_port = inet_client_port();
 

Regards,

Rodrigo


Re: session id and global storage

From
Andrew Dunstan
Date:
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



Re: session id and global storage

From
"Rodrigo De Leon"
Date:
> 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.


Re: session id and global storage

From
Tom Lane
Date:
Andrew Dunstan <andrew@dunslane.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