473,890 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13367
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.ph a.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.p ha.pa.us>
To: "Nagib Abi Fadel" <na************ *@usj.edu.lb>
Cc: "generalpos t" <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.ph a.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 YourEmailAddres sHere" to ma*******@postg resql.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('ses sion_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('ses sion_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 YourEmailAddres sHere" to ma*******@postg resql.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*****@mascar i.com>
To: "Manfred Koizar" <mk*****@aon.at >
Cc: "Nagib Abi Fadel" <na************ *@usj.edu.lb>; "Bruce Momjian"
<pg***@candle.p ha.pa.us>; "generalpos t" <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('ses sion_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

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

Similar topics

14
8872
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 referring to Session("LoggedOn") on my various ASP pages, it is coming
11
3360
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 Explicit and Response.Expires=-1,
0
1403
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 Session) in ASP pages were failing. Finally got to (what I think is) the root issue by creating two ASP pages, one that sets Session("test") = "Hello", and the other simply displays "Test word: .." and the content of Session("test"). After visiting the...
1
1731
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 like to put the total count of the rows into the control's footer. I couldn't find any references for doing this and ended up trying several things, one of which was assigning a variable within the sub creating the datalist control, and when that...
1
3542
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 btnContinue_Click event of Page1.aspx, I create the object and store it in a session variable. The next statement would be Response.Redirect("Page2.aspx"). The code appears like this: private void btnContinue_Click(object sender, EventArgs e) {...
4
14824
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 JS to do something based on the user variable. can somebody help me out on this one. thanks.
4
3970
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 assign that array to a session variable it chokes. e.g. create array and load each element with a row from the file (btw, each row is an array as well, using fgetcsv()). When local array is loaded, I assign to session var as so: $_SESSION =...
3
2051
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 the information it contained on another page. This was significant because I am initially loading the data from the database, then storing relevent information in the object, I am allowing users to change the data then preview the modifications...
7
4697
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: session = "Hello World"; When the request for that session variable "myString" is attempted to be retrieved, I would like to intercept that request like so: if (Request is for session variable "myString")
0
9980
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9829
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5856
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4683
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4278
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.