473,836 Members | 1,554 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
13 13360
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 YourEmailAddres sHere" to ma*******@postg resql.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*****@mascar i.com>; "Bruce Momjian"
<pg***@candle.p ha.pa.us>; "generalpos t" <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_INF O_V1(setvalue);

Datum setvalue(PG_FUN CTION_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_INF O_V1(getvalue);

Datum getvalue(PG_FUN CTION_ARGS) {

text *result;
long len;

len = strlen(session_ data) + VARHDRSZ;
result = (text *) palloc(len);
VARATT_SIZEP(re sult) = 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_INF O_V1(setvalue);

Datum setvalue(PG_FUN CTION_ARGS) {

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

}

PG_FUNCTION_INF O_V1(getvalue);
Datum getvalue(PG_FUN CTION_ARGS) {

PG_RETURN_INT32 (session_data);
}

ANY COMMENTS ARE WELCOMED.

Najib.

----- Original Message -----
From: "Mike Mascari" <ma*****@mascar i.com>
To: "Nagib Abi Fadel" <na************ *@usj.edu.lb>
Cc: "Manfred Koizar" <mk*****@aon.at >; "Bruce Momjian"
<pg***@candle.p ha.pa.us>; "generalpos t" <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_INF O_V1(setvalue);

Datum setvalue(PG_FUN CTION_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_INF O_V1(getvalue);

Datum getvalue(PG_FUN CTION_ARGS) {

text *result;
long len;

len = strlen(session_ data) + VARHDRSZ;
result = (text *) palloc(len);
VARATT_SIZEP(re sult) = 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*******@postg resql.org

Nov 23 '05 #14

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

Similar topics

14
8869
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
3357
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
1402
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
1726
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
3541
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
14821
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
3967
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
2048
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
4694
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
9818
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
10843
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10589
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
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
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5648
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...
0
5825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4448
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
3
3112
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.