472,805 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Creating a session variable in Postgres

Is it possible to create a session variable for each user in Postresql ??

Thx
Nov 23 '05 #1
13 13190
On Wed, 2 Jun 2004 16:00:18 +0200
"Nagib Abi Fadel" <na*************@usj.edu.lb> wrote:
Is it possible to create a session variable for each user in Postresql ??


I was just thinking that the term "session variable" is like the word "love" in
that it means something different to everyone.

That being said, I can't think of any definition of "session variable" that
Postgres isn't capable of handling. If you have a more specific meaning you
want to ask about, I suggest providing a scenerio, or otherwise more specific
question.

--
Bill Moran
Potential Technologies
http://www.potentialtech.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
Nagib Abi Fadel wrote:
Is it possible to create a session variable for each user in Postresql ??


No. The best you can do is create a temp table and put a value in
there.

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #3
Bruce Momjian wrote:
Nagib Abi Fadel wrote:
Is it possible to create a session variable for each user in Postresql ??

No. The best you can do is create a temp table and put a value in
there.


Would I be right in assuming that's going to be burning through OIDs
though (possibly an issue for a web-app with lots of short sessions).

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #4
Well i thought about that but i don't feel like it is a clean way.

Actually i need to create a dynamic view depending on the user choice of a
certain variable via a web application.

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.

i defined the following view: CREATE VIEW t_view as select * from SomeTable
where id = (select X from t_temp);
This didn't work first cause the temporary table does not exist.
So i created the temporary table then created the view "t_view" and then the
view was created (i kind of fooled the system).

Now every time a user access the web application he will choose a value for
X and the t_temp will be created and X inserted in it.

I solved my problem but it does not seem like a "clean way".

Any ideas ??

I have now a DYNAMIC view
----- Original Message -----
From: "Bruce Momjian" <pg***@candle.pha.pa.us>
To: "Nagib Abi Fadel" <na*************@usj.edu.lb>
Cc: "generalpost" <pg***********@postgresql.org>
Sent: Wednesday, June 02, 2004 04:53 PM
Subject: Re: [GENERAL] Creating a session variable in Postgres

Nagib Abi Fadel wrote:
Is it possible to create a session variable for each user in Postresql
??
No. The best you can do is create a temp table and put a value in
there.

--
Bruce Momjian | http://candle.pha.pa.us
pg***@candle.pha.pa.us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #5
On Thu, 3 Jun 2004 09:04:43 +0200, "Nagib Abi Fadel"
<na*************@usj.edu.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/s...ing-stats.html.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #6
Manfred Koizar wrote:
On Thu, 3 Jun 2004 09:04:43 +0200, "Nagib Abi Fadel"
<na*************@usj.edu.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



---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #7
> Would I be right in assuming that's going to be burning through OIDs
though (possibly an issue for a web-app with lots of short sessions).


:climbing on soapbox:
If pg had a 64-bit system record ID like Oracle does, that'd
take care of this issue, at least until you managed to write
18 quintilliion (1.8e19) rows. At a million rows a second, that'll
take you about 584,000 years.

Yeah, it costs some disk space, but disk space is cheap compared
to having to confront the OID rollover issue over and over again,
and not just because of bad database design.
:climbing off soapbox:
--
Mike Nolan

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #8
Thx guys Both Solutions works fine for me but which one is better (uses less
resources ?) ?
(Mike i tested your solution)

The use of sequence would require to clean up the table every N hour .
The use of temporary table wouldn't require any cleanup. Plus it won't use
any disk space (i suppose a temporary table wouldn't be written to disk
right ?).

Thx guys for your help.
----- Original Message -----
From: "Mike Mascari" <ma*****@mascari.com>
To: "Manfred Koizar" <mk*****@aon.at>
Cc: "Nagib Abi Fadel" <na*************@usj.edu.lb>; "Bruce Momjian"
<pg***@candle.pha.pa.us>; "generalpost" <pg***********@postgresql.org>
Sent: Thursday, June 03, 2004 04:55 PM
Subject: Re: [GENERAL] Creating a session variable in Postgres

Manfred Koizar wrote:
On Thu, 3 Jun 2004 09:04:43 +0200, "Nagib Abi Fadel"
<na*************@usj.edu.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 thevariable 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



---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #9
On Fri, 4 Jun 2004 08:25:38 +0200, "Nagib Abi Fadel"
<na*************@usj.edu.lb> wrote:
The use of sequence would require to clean up the table every N hour .
Right.
The use of temporary table wouldn't require any cleanup.


Wrong. You would have to clean up the meta data, at least pg_class and
pg_attribute, maybe pg_index also. For the price of one temp table you
can have several rows in a permanent table.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #10
On Thu, 3 Jun 2004 14:59:40 -0500 (CDT), Mike Nolan <no***@gw.tssi.com>
wrote:
disk space is cheap


Yes, but very often I/O is the bottleneck.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #11
So considering those facts, it would be better to use the Sequence Method,
since it would only require cleaning up one table ....
Or is there anything else i am missing ???
----- Original Message -----
From: "Manfred Koizar" <mk*****@aon.at>
To: "Nagib Abi Fadel" <na*************@usj.edu.lb>
Cc: "Mike Mascari" <ma*****@mascari.com>; "Bruce Momjian"
<pg***@candle.pha.pa.us>; "generalpost" <pg***********@postgresql.org>
Sent: Friday, June 04, 2004 08:32 AM
Subject: Re: [GENERAL] Creating a session variable in Postgres

On Fri, 4 Jun 2004 08:25:38 +0200, "Nagib Abi Fadel"
<na*************@usj.edu.lb> wrote:
The use of sequence would require to clean up the table every N hour .


Right.
The use of temporary table wouldn't require any cleanup.


Wrong. You would have to clean up the meta data, at least pg_class and
pg_attribute, maybe pg_index also. For the price of one temp table you
can have several rows in a permanent table.

Servus
Manfred

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #12
Nagib Abi Fadel wrote:
So considering those facts, it would be better to use the Sequence Method,
since it would only require cleaning up one table ....
Or is there anything else i am missing ???


It is becoming more of a toss-op. Prior to 7.4, the system indexes
would grow until a manual REINDEX was issued in a stand-alone
backend. In 7.4, the dead tuples remain, but at least can be re-used
once they've been marked that way by the occassional vacuum.
autovacuum will tend to make dead-tuple reclaimation transparent,
like Oracle.

The absolutely cheapest method is to write a pair of functions in
'C' that sets/gets a global variable:

#include "postgres.h"
#include "fmgr.h"

#define MAX_DATA 64

char session_data[MAX_DATA] = "";

PG_FUNCTION_INFO_V1(setvalue);

Datum setvalue(PG_FUNCTION_ARGS) {

text *value;
long len;

value = PG_GETARG_TEXT_P(0);
len = VARSIZE(value) - VARHDRSZ;
if (len >= MAX_DATA) {
elog(ERROR, "setvalue: value too long: %li", len);
}
memcpy(session_data, VARDATA(value), len);
session_data[len] = 0;

PG_RETURN_BOOL(true);

}

PG_FUNCTION_INFO_V1(getvalue);

Datum getvalue(PG_FUNCTION_ARGS) {

text *result;
long len;

len = strlen(session_data) + VARHDRSZ;
result = (text *) palloc(len);
VARATT_SIZEP(result) = len;
memcpy(VARDATA(result), session_data, len - VARHDRSZ);

PG_RETURN_TEXT_P(result);

}

-- Compile

gcc -c example.c -I/usr/include/pgsql/server
gcc -shared -o pgexample.so pgexample.o

-- Install somewhere PostgreSQL can get at it

cp pgexample.so /usr/local/mypglibs

-- Create the functions where path-to-lib is the path to
-- the shared library.

CREATE OR REPLACE FUNCTION setvalue(text) RETURNS boolean
AS '/usr/local/mypglibs/pgexample.so'
LANGUAGE 'C' WITH (isStrict);

CREATE OR REPLACE FUNCTION getvalue() RETURNS text
AS '/usr/local/mypglibs/pgexample.so'
LANGUAGE 'C' WITH (isStrict);

Now all you need to to is invoke setvalue() at the start of the
session, and build views around getvalue():

CREATE VIEW v_foo AS
SELECT *
FROM foo
WHERE foo.key = getvalue();

At the start of a session:

SELECT setvalue('Mike Mascari was here');

Hope that helps,

Mike Mascari
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #13
Hi Thx Mike, it's the best solution i think.

But i did some modifications to the code since i need to store an integer I
wrote the following:

#include "postgres.h"
#include "fmgr.h"

int32 session_data;

PG_FUNCTION_INFO_V1(setvalue);

Datum setvalue(PG_FUNCTION_ARGS) {

session_data = PG_GETARG_INT32(0);
PG_RETURN_BOOL(true);

}

PG_FUNCTION_INFO_V1(getvalue);
Datum getvalue(PG_FUNCTION_ARGS) {

PG_RETURN_INT32(session_data);
}

ANY COMMENTS ARE WELCOMED.

Najib.

----- Original Message -----
From: "Mike Mascari" <ma*****@mascari.com>
To: "Nagib Abi Fadel" <na*************@usj.edu.lb>
Cc: "Manfred Koizar" <mk*****@aon.at>; "Bruce Momjian"
<pg***@candle.pha.pa.us>; "generalpost" <pg***********@postgresql.org>
Sent: Friday, June 04, 2004 11:21 AM
Subject: Re: [GENERAL] Creating a session variable in Postgres

Nagib Abi Fadel wrote:
So considering those facts, it would be better to use the Sequence Method, since it would only require cleaning up one table ....
Or is there anything else i am missing ???


It is becoming more of a toss-op. Prior to 7.4, the system indexes
would grow until a manual REINDEX was issued in a stand-alone
backend. In 7.4, the dead tuples remain, but at least can be re-used
once they've been marked that way by the occassional vacuum.
autovacuum will tend to make dead-tuple reclaimation transparent,
like Oracle.

The absolutely cheapest method is to write a pair of functions in
'C' that sets/gets a global variable:

#include "postgres.h"
#include "fmgr.h"

#define MAX_DATA 64

char session_data[MAX_DATA] = "";

PG_FUNCTION_INFO_V1(setvalue);

Datum setvalue(PG_FUNCTION_ARGS) {

text *value;
long len;

value = PG_GETARG_TEXT_P(0);
len = VARSIZE(value) - VARHDRSZ;
if (len >= MAX_DATA) {
elog(ERROR, "setvalue: value too long: %li", len);
}
memcpy(session_data, VARDATA(value), len);
session_data[len] = 0;

PG_RETURN_BOOL(true);

}

PG_FUNCTION_INFO_V1(getvalue);

Datum getvalue(PG_FUNCTION_ARGS) {

text *result;
long len;

len = strlen(session_data) + VARHDRSZ;
result = (text *) palloc(len);
VARATT_SIZEP(result) = len;
memcpy(VARDATA(result), session_data, len - VARHDRSZ);

PG_RETURN_TEXT_P(result);

}

-- Compile

gcc -c example.c -I/usr/include/pgsql/server
gcc -shared -o pgexample.so pgexample.o

-- Install somewhere PostgreSQL can get at it

cp pgexample.so /usr/local/mypglibs

-- Create the functions where path-to-lib is the path to
-- the shared library.

CREATE OR REPLACE FUNCTION setvalue(text) RETURNS boolean
AS '/usr/local/mypglibs/pgexample.so'
LANGUAGE 'C' WITH (isStrict);

CREATE OR REPLACE FUNCTION getvalue() RETURNS text
AS '/usr/local/mypglibs/pgexample.so'
LANGUAGE 'C' WITH (isStrict);

Now all you need to to is invoke setvalue() at the start of the
session, and build views around getvalue():

CREATE VIEW v_foo AS
SELECT *
FROM foo
WHERE foo.key = getvalue();

At the start of a session:

SELECT setvalue('Mike Mascari was here');

Hope that helps,

Mike Mascari
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: RooLoo | last post by:
Hey all In my GLOBAL.ASA file I'm trying to create a session variable for reference in the various webpages of my site.... Sub Session_OnStart Session("LoggedOn")="Y" End Sub When...
11
by: doltharz | last post by:
Please Help me i'm doing something i though was to be REALLY EASY but it drives me crazy The complete code is at the end of the email (i mean newsgroup article), i always use Option...
0
by: Nick Jushchyshyn | last post by:
Having a really strange issue with IIS 6 where the session variable does not stay consistant. The first symptom was that database connections (which use a user entered name/PW stored in the...
1
by: KathyB | last post by:
Hi, sorry to be greedy with all my posts lately, but can you tell I'm doing new things this week? I've just done my first datalist (a simple one), that give me one column of records. I would...
1
by: Vidyadhar Joshi | last post by:
I have the following scenario in a true load balanced environment (without sticky sessions): There are 2 ASPX pages. I want to pass an object from the first page to the second page. On the...
4
by: sconeek | last post by:
hi all, i am working on a java and HTML based web application. now i am storing session variables. is there a way for me to pass on these variables to javascript. what i am getting at is, i need...
4
by: MrBiggles | last post by:
Here's the sitch: I read in a csv file with 60000 lines (20 fields per record), and store the data to a local array. The file is read in and stored just fine and pretty quick. Now, if I try to...
3
by: RSH | last post by:
Hi, I have a situation where I have created an object that contains fields,properties and functions. After creating the object I attempted to assign it to a session variable so i could retrieve...
7
by: ADN | last post by:
Hi, I am creating a custom HTTPModule to intercept the request of when the user is attempting to retrieve a session variable. For instance, if I set a session variable in my code like so: ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.