pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Applictaion data Logging
4+ messages / 2 participants
[nested] [flat]

* Applictaion data Logging
@ 2002-12-10 01:53  Jesus Sandoval <meli@mzt.megared.net.mx>
  0 siblings, 1 reply; 4+ messages in thread

From: Jesus Sandoval @ 2002-12-10 01:53 UTC (permalink / raw)
  To: pgsql-admin

Hi, I will do this programatically but I wished to post this question just to
know if somebody have any suggestion, because I am new to server side functions
or rules or trigger programming.

I want to log any data change in one application, for audit reasons, I created a
table for that purpose the table has the next fields:

id SERIAL                                        -- Primary key
tablename VARCHAR(40)        -- Table name that changed
fieldname VARCHAR(40)        -- fieldname that changed in the table
user VARCHAR(16)                    -- User that made the change
timedate TIMESTAMP WITHOUT TIME ZONE    -- Timestamp of change
keyvalue TEXT            -- The key value of the record that changed
oldcontent TEXT        -- the field's content before the change
newcontent TEXT        -- the field's new content

I'm programming in Java and using JDBC.

Notes:
1) The program knows the user id that made the change (postgres is not aware of
this user id, because there is one user that connect from the application, after
the connection to postgresql, the applications validate the user id from a
postgresql table, if it is ok, the program continues).
2) In order to know wich record is updated (or inserted or deleted) the keyvalue
is saved in the log record.
3) Maybe newcontent is redundant, because I can check that value in the actual
table, so maybe it can be dropped from this table.

As I said, I plan to do this log from the program, but I think it can be done
with Triggers or rules, but I don't know if it is possibly or how to do the
following.

1) How to pass the user id from the application to postgres.
2) How to save the tablename of the table that is modifying in the logtable, the
same for the fieldname.
3) On one update there may be 2 or more fields (columns) changing, so I need to
store 2 or more records to the logtable, How can I do that.
4) to get the key value of the changed record. One query can change many records
and many changes.
5) Which is best option: Triggers, Rules or application logic???? (none of
them????)

Thanks

Jesus Sandoval





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Applictaion data Logging
@ 2002-12-10 11:08  Harald Krake <harald@krake.de>
  parent: Jesus Sandoval <meli@mzt.megared.net.mx>
  0 siblings, 1 reply; 4+ messages in thread

From: Harald Krake @ 2002-12-10 11:08 UTC (permalink / raw)
  To: pgsql-admin

Hi!

when it comes to portability the right place to implement such
auditing is in the OR-mapper. 
Most commercial mappers already provide such a feature. 
However, if you don't like "black-boxed" third party
software, writing your own mapper isn't a big deal. 
It's worth the effort!

Some hints:
- writing a mapper usually ends up with methods like
  MySpecialDbObject.save() or insert() or update() or alike.
- your mapper should provide a method to turn on/off logging
  on a per class basis.
- one solution to the auditing problem is an extra "logging table"
  per "data table". The logging table holds _all_ the columns of the
  data table _plus_ a serial number (incremented each time the 
  corresponding data tuple is modified) and things like userid/name,
  timestamp and so on. 
- you should store the serial-number in the data-table too.

At least, this is how we did it and it works fine.
If you don't mind portability postgres probably provides some 
auditing support, but I don't know.
Anyway, you should either use a dbms-inherent feature _or_ do it 
in your application. Mixing both levels is not a good idea, imho.

Hope it helps,
Harald.




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Applictaion data Logging
@ 2002-12-12 00:35  Jesus Sandoval <meli@mzt.megared.net.mx>
  parent: Harald Krake <harald@krake.de>
  0 siblings, 1 reply; 4+ messages in thread

From: Jesus Sandoval @ 2002-12-12 00:35 UTC (permalink / raw)
  To: Harald Krake <harald@krake.de>; +Cc: pgsql-admin

Harald Krake escribió:

> Hi!
>
> when it comes to portability the right place to implement such
> auditing is in the OR-mapper.
> Most commercial mappers already provide such a feature.
> However, if you don't like "black-boxed" third party
> software, writing your own mapper isn't a big deal.
> It's worth the effort!
>
> Some hints:
> - writing a mapper usually ends up with methods like
>   MySpecialDbObject.save() or insert() or update() or alike.
> - your mapper should provide a method to turn on/off logging
>   on a per class basis.
> - one solution to the auditing problem is an extra "logging table"
>   per "data table". The logging table holds _all_ the columns of the
>   data table _plus_ a serial number (incremented each time the
>   corresponding data tuple is modified) and things like userid/name,
>   timestamp and so on.
> - you should store the serial-number in the data-table too.
>
> At least, this is how we did it and it works fine.
> If you don't mind portability postgres probably provides some
> auditing support, but I don't know.
> Anyway, you should either use a dbms-inherent feature _or_ do it
> in your application. Mixing both levels is not a good idea, imho.
>
> Hope it helps,
> Harald.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

Thanks for your answer.

I don't know what is OR-mapper, I tried to look for some information in the
internet with www.google.com, but got no luck.

Anyway because the rest of your answer, I can fegure out what a mapper is,
and how to implement such feature.

I have some thougths that want to share for your opinion:

1) I already have doInsert(), doUpdate() and doDelete() methods in my
classes, so there is where I have to do the logging (the mapper action I
think)

2) Because the logging action is in these methods, I'm not sure about the
convenience to turn on/off the data logging, but anyway it can be done.

3) In my design of data logging, I only have one "logging table" for all
the application, but this logging table has to have the table name and the
column name of what is being changed, inserted or deteled, like the
following:

id SERIAL                                        -- Primary key
tablename VARCHAR(40)        -- Table name that changed
fieldname VARCHAR(40)        -- fieldname that changed in the table
user VARCHAR(16)                    -- User that made the change
timedate TIMESTAMP WITHOUT TIME ZONE    -- Timestamp of change
keyvalue TEXT            -- The key value of the record that changed
oldcontent TEXT        -- the field's content before the change
newcontent TEXT        -- the field's new content

I'm not sure if the primary key (id SERIAL) is used in the way you
suggested the serial number???

4) What utility can have storing the serial number in the data table????

I know that my design can be more useful in my application and yours in
your application but I want to know your comments.

Another design that I'm aware, every time somebody updated the tuple, a new
tuple where written to the table with a timestamp, so if I wanted to know
the actual value I just get the record with the greatest timestamp, and if
I wanted to know the history I can see all the tuples with the same ke
value (not the primary key, because in this design, the primary key always
was a SERIAL value).

Thanks again...




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Applictaion data Logging
@ 2002-12-13 11:44  Harald Krake <harald@krake.de>
  parent: Jesus Sandoval <meli@mzt.megared.net.mx>
  0 siblings, 0 replies; 4+ messages in thread

From: Harald Krake @ 2002-12-13 11:44 UTC (permalink / raw)
  To: jesus@merkatek.com, Jesus Sandoval <meli@mzt.megared.net.mx>; +Cc: pgsql-admin

On Thursday 12 December 2002 01:35 am, Jesus Sandoval wrote:

> 3) In my design of data logging, I only have one "logging table" for all
> the application, but this logging table has to have the table name and the
> column name of what is being changed, inserted or deteled, like the
> following:
>
> id SERIAL                                        -- Primary key
> tablename VARCHAR(40)        -- Table name that changed
> fieldname VARCHAR(40)        -- fieldname that changed in the table
> user VARCHAR(16)                    -- User that made the change
> timedate TIMESTAMP WITHOUT TIME ZONE    -- Timestamp of change
> keyvalue TEXT            -- The key value of the record that changed
> oldcontent TEXT        -- the field's content before the change
> newcontent TEXT        -- the field's new content
>
> I'm not sure if the primary key (id SERIAL) is used in the way you
> suggested the serial number???

no. In your example, you're using SERIAL as a dbms-generated object-ID whereas
the "serial" I meant (think of it as a "version-number") is an integer 
that gets incremented each time the tuple is modified.

> 4) What utility can have storing the serial number in the data table????

for portability reasons you should do that at the OR-level, i.e. in
your doUpdate(). Again for portability I would suggest not to use
the "SERIAL"-type for "id". The same functionality can be achieved
via an extra table that holds the last valid object-ID. Whenever the mapper
needs to create a new object, it simply increments the number in that
table, sets it as the id in the new object (and the serial resp. version to 1) 
and inserts the record (in a transaction, of course). Nice side-effect:
the object id will be unique among _all_ tables which can be used
for other purposes as well.

> Another design that I'm aware, every time somebody updated the tuple, a new
> tuple where written to the table with a timestamp, so if I wanted to know
> the actual value I just get the record with the greatest timestamp, 

this design doesn't work well with some dbms (slow joins)

> and if
> I wanted to know the history I can see all the tuples with the same ke
> value (not the primary key, because in this design, the primary key always
> was a SERIAL value).

I see 2 probs with this design:
- there is one logging record for each modified field in a tuple,
  i.e. if 20 fields were modified, you generate 20 records!
- the original datatype is lost.

Well, it depends on your demands.

Harald.


(btw.: is this the proper list for this thread? ;-)




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2002-12-13 11:44 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-12-10 01:53 Applictaion data Logging Jesus Sandoval <meli@mzt.megared.net.mx>
2002-12-10 11:08 ` Harald Krake <harald@krake.de>
2002-12-12 00:35   ` Jesus Sandoval <meli@mzt.megared.net.mx>
2002-12-13 11:44     ` Harald Krake <harald@krake.de>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox