473,774 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there an opposite to pg_get_userbyid () ?

Hi,
I'd like to store who changed records on some tables.
I'd prefer not to store the username but rather his/her ID.
Will I allways have to run
select usesysid from pg_user where usename=session _user;
or is there a complement to pg_get_userbyid () ?

Can I have this as a default-value for a created_by integer-collumn ?

---------------------------(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 #1
2 1863
On Thu, Oct 14, 2004 at 07:42:22PM +0200, Andreas wrote:

I'd like to store who changed records on some tables.
I'd prefer not to store the username but rather his/her ID.
Will I allways have to run
select usesysid from pg_user where usename=session _user;
or is there a complement to pg_get_userbyid () ?
If there is then I've overlooked it in the documentation. It's
easy enough to write:

CREATE FUNCTION get_userbyname( NAME) RETURNS INTEGER AS '
SELECT usesysid FROM pg_user WHERE usename = $1
' LANGUAGE SQL STABLE STRICT;
Can I have this as a default-value for a created_by integer-collumn ?


You should be able to use the above function in a column's DEFAULT
expression:

CREATE TABLE changelog (
logid SERIAL PRIMARY KEY,
logtime TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTA MP,
loguser INTEGER NOT NULL DEFAULT get_userbyname( CURRENT_USER),
logmsg TEXT NOT NULL
);

GRANT INSERT, SELECT ON changelog TO otheruser;
GRANT UPDATE ON changelog_logid _seq TO otheruser;

INSERT INTO changelog (logmsg) VALUES ('first message');
\c - otheruser
INSERT INTO changelog (logmsg) VALUES ('second message');
SELECT * FROM changelog;
logid | logtime | loguser | logmsg
-------+-------------------------------+---------+----------------
1 | 2004-10-14 18:43:20.581907-06 | 100 | first message
2 | 2004-10-14 18:43:35.541114-06 | 102 | second message
(2 rows)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

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

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

Nov 23 '05 #2
Michael Fuhr wrote:
On Thu, Oct 14, 2004 at 07:42:22PM +0200, Andreas wrote:

or is there a complement to pg_get_userbyid () ?


If there is then I've overlooked it in the documentation. It's
easy enough to write:


Thanks :)

I though it was consequent to expect such a function, since there is
pg_get_userbyid ().
I'm a bit reluctant to use a real lookup-function for what I have in mind.
My application connects to the server and hopefully can keep the
connection up until the user leaves his desk. During the whole session
every insert/update will trigger those user-id lookups for an
information that is constant for the session.
I hoped there were a function that could read the user-id directly from
the connection's properties.

Well, well ... that's life ...
Andreas ;)

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

Nov 23 '05 #3

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

Similar topics

4
1957
by: comp.lang.php | last post by:
Hi. Relative php newbie but longtime coder in other langs. Environment: PHP4. Looking for a language construct or function that gives the opposite of array(), example: function xyz($fmt, $argsarray) {
22
3946
by: achrist | last post by:
The yield statement looks to be a big step toward some kind of lightweight concurrency-oriented programming in python. Is there any similarly nice way to do the opposite of yield, just sit around (perhaps in the middle of a loop) and wait until some other routine (unknown to the waiting module) hurls a value in? If this is not implemented now, is there any chance that it's on the list of things to come?
15
4870
by: Greg Schmidt | last post by:
We all know that one trick in dealing with old browsers is to add extra bits of content with class="old", where old is defined as display:none in a style sheet that is @imported so that old browsers never see it and hence don't "none" the display of the content. I use this, for example, to put a heading on my navigation menu in NN4 et al, because they can't handle the CSS-P that I use to move the menu to a place where it's obvious and...
2
2206
by: ormy28 | last post by:
I really need some help with the following problem if anyone would be willing. I need a list box to list the opposite of what appears in a query. Heres the details: My database is for a doctors surgery, and this includes the tables tblPatients, tblDoctors and tblAppointments. tblAppointments has an Appointment ID and also includes the Patient ID (from tblPatient) and Doctor ID (from tblDoctor) plus the start and end time of the
5
2913
by: Tony | last post by:
Hi, I am looking for the opposite of wm_close, I assume it's possibly wm_open but no info comes up with that term. so, what is the opposite of WM_Close Any ideas welcome Tony
9
9237
by: pranav.choudhary | last post by:
Hi I am new to python. I wanted to know if there is an opposite of "import"
11
9050
by: igor.tatarinov | last post by:
Given a bunch of arrays, if I want to create tuples, there is zip(arrays). What if I want to do the opposite: break a tuple up and append the values to given arrays: map(append, arrays, tupl) except there is no unbound append() (List.append() does not exist, right?). Without append(), I am forced to write a (slow) explicit loop: for (a, v) in zip(arrays, tupl): a.append(v)
0
10267
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...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
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
9914
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...
1
7463
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
6717
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
5355
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
4012
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
2852
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.