Re: Creating a session variable in Postgres

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: "Nagib Abi Fadel" <nagib(dot)abi-fadel(at)usj(dot)edu(dot)lb>
Cc: "Bruce Momjian" <pgman(at)candle(dot)pha(dot)pa(dot)us>, "generalpost" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Creating a session variable in Postgres
Date: 2004-06-03 14:01:11
Message-ID: 5g9ub0dr9jfqjtourj4obafb0h67jkc6pm@email.aon.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 3 Jun 2004 09:04:43 +0200, "Nagib Abi Fadel"
<nagib(dot)abi-fadel(at)usj(dot)edu(dot)lb> wrote:
>Let's say for example the variable is called "X". The view is called
>"t_view" and the temporary table is called "t_temp".
>Each time a user connects to the web, the application will initialize the
>variable X and it will be inserted into the temporary table t_temp.

Sequence values are session-specific which is exactly the property
you're looking for.

CREATE TABLE session (
id SERIAL PRIMARY KEY,
x text
);

CREATE VIEW mysession (x) AS
SELECT x FROM session WHERE id=currval('session_id_seq');

CREATE VIEW t_view AS
SELECT *
FROM SomeTable st INNER JOIN mysession s
ON st.id = s.x;

At the start of a session you just

INSERT INTO session (x) VALUES ('whatever');

From time to time you have to clean up the session table (delete old
entries, VACUUM ANALYSE).

If the value of your session variable X has no special meaning apart
from being unique, you don't need the session table, you just create a
sequence and use nextval/currval directly.

Or you might want to use pg_backend_pid(). It is documented here:
http://www.postgresql.org/docs/7.4/static/monitoring-stats.html.

Servus
Manfred

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Derek Chen-Becker 2004-06-03 14:05:34 Re: JDBC: what exactly does nullsAreSortedHigh() return?
Previous Message Martijn van Oosterhout 2004-06-03 13:50:22 Re: A "linking" Question