Re: Creating a session variable in Postgres

From: Mike Mascari <mascarm(at)mascari(dot)com>
To: Manfred Koizar <mkoi-pg(at)aon(dot)at>
Cc: Nagib Abi Fadel <nagib(dot)abi-fadel(at)usj(dot)edu(dot)lb>, 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:55:18
Message-ID: 40BF3BD6.5050206@mascari.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Manfred Koizar wrote:
> 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');

Couldn't one also do (this is untested - may include syntax errors):

-- Create a wrapper function for View usage

CREATE FUNCTION getValue() RETURNS text AS '

DECLARE

result text;

BEGIN

SELECT INTO result session_value
FROM session_data;

RETURN result;

END;

LANGUAGE 'plpgsql';

-- Create our View using our function

CREATE VIEW t_view AS
SELECT *
FROM foo
WHERE foo.key = getValue();

-- Now, upon connecting, the app does:

CREATE TEMPORARY TABLE session_data (value text);
INSERT INTO session_data VALUES ('Hello');

In this way, the table needn't exist until the first invocation of
getValue() upon the first access of the view, since the code will be
recompiled during the first access, correct?

Mike Mascari

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Evan Rempel 2004-06-03 15:08:24 Re: PostgreSQL certifications?
Previous Message Dennis Gearon 2004-06-03 14:47:29 Re: Running Totals and stuff...