473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Data change logs

Hello all,

I have a particular application sitting on postgres that requires a
complete log of all inserts, updates and deletes, along with the
application user who made the change. There is a functional solution in
place, but it is a bit ugly. I was hoping that some of the
pgsql-general readers would have encountered this problem before and
have some insight to offer.

Here's how the current solution works. We have five tables for logging,
with the following basic structure:

insert_log ( id, userid, time, tablename, number )
insert_log_col ( id, insert_log, col, value )

update_log ( id, userid, time, tablename, number )
update_log_col ( id, update_log, col, old, new )

delete_log ( id, userid, time, tablename, number )

So, as you can see, each "insert log" has many "insert log columns",
which document the values inserted into each column. "update log" works
in much the same way, as well as recording the value which was replaced
by the update.

Whenever the frontend of the app (in PHP) needs to do an insert, update,
or delete, it passes the tablename, primary key number, and field /
value pairs (except for deletes) to a wrapper function. The wrapper
function performs whatever validations are necessary, assembles the SQL
command to carry out the action, and if the action was successful, then
inserts the information into the log tables.

Because the wrapper functions reside on the front-end, this is a fairly
costly process. The database is growing rapidly, and will continue to
do so, and thus performance is an increasingly serious issue. Moving
the wrappers to PL/pgSQL functions is something we're looking into right
now, but I'm open to the possibility that this entire solution is a poor
approach.

Bear in mind the overal purpose of the logging is for traceability - we
want to be able to track down who made what changes and when, for any
and all data in the system. Point-in-time recovery, while it would be
cool, is not a serious concern.

So, if anyone out there has an effective alternative, I would love to
hear about it.

Regards,

Brendan Jurd

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

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

Nov 23 '05 #1
2 1571
> So, if anyone out there has an effective alternative, I would love to
hear about it.


The way I do it is to create a copy of the table I want to track and add
a text column for the user name and a timestamp column.

I then set up an on update trigger on the original table that does the
following:

insert into xxxx_log select * from xxxx where keyfield = NEW.keyfield;

I also set up an on insert trigger on the log table that adds the timestamp
and the user ID (from session_user).

The big negative is that if you add columns to the original table, you
have to fiddle with the log table to make sure you keep the username and
timestamp columns after all the columns in the original table and keep the
columns in sync with the original table as to both size and order in which
they appear, or you'll get errors.

Pulling the data out of the log table can be a bit more, because it has
the OLD data but not the NEW data. But you know what the values were,
who changed them and when, and you can check the original table to see
what the current value is. (If there are multiple changes, you
have to check the next one in timestamp order, of course.)

One of the nicer aspects is that because this is done at the trigger
level, the user does NOT have to have any access to the log table,
the trigger can use SECURITY DEFINER. That way you get full control
over who can even look at the log.
--
Mike Nolan

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #2
>
insert into xxxx_log select * from xxxx where keyfield = NEW.keyfield;


Oops, that should be OLD.keyfield.
--
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 #3

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

Similar topics

8
by: Frnak McKenney | last post by:
Back when computer dinosaurs roamed the earth and the precursors to today's Internet were tiny flocks of TDMs living symbiotically with the silicon giants, tracking access to data processing...
2
by: Domenico Discepola | last post by:
Hello all. Before my arrival at my current employer, our consultants physically set up our MSSQL 7 server as follows: drive c: contains the mssql engine drive d: contains the transaction log...
2
by: john.livermore | last post by:
Quick question about deleting data from SQL Server. We have a table that gets quite a bit of activity with an attribute of type text (inserts that store new text entries of 50-200k apiece)....
10
by: Jim H | last post by:
I have a UDP socket that sends out a request on a multicast socket and waits for a response. This client is not listening on a multicast IP but the local IP. The server (UNIX) responds to the...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
3
by: Mike | last post by:
Hello: I was not able to find a regular ASP group, so I posted this here instead. I have a web app which is actually just ASP using VBScript as the server-side language, running on IIS6. ...
9
by: developing | last post by:
Hello how goes it? I am developing a database which needs approvals from two seperate usetrs before record gets updated. i need ideas for the approval part. So a record is changed by a user,...
9
by: KDawg44 | last post by:
Hi, I am brand new to Python. In learning anything, I find it useful to actually try to write a useful program to try to tackle an actual problem. I have a syslog server and I would like to...
0
by: shorti | last post by:
We are running DB2 UDB 8.1 fp 14 on AIX using archival logging and online backups. I was running some disaster tests and found my database restore was not restoring to the latest active log. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.