Solved

Using the OLD record in a trigger to pull back dynamic column names

Posted on 2007-11-16
10
1,185 Views
Last Modified: 2010-05-18
I am trying to create a trigger in postgresql using the OLD record

basically the column I want to refer to will be in a string for example:

myColumn = "some_column_name";
OLD.myColumn

however doing it like this would mean that it would be looking for a column called myColumn

Does anyone know if it is possible to pull back an OLD record with a dynamic record as the column name?
0
Comment
Question by:tricl
10 Comments
 
LVL 22

Expert Comment

by:earth man2
ID: 20304176
an old record cannot be "dynamic??"
what do you mean by dynamic ??
old.x at the end of the day refers to a table record.
0
 

Author Comment

by:tricl
ID: 20318503
for example if there is a table with column names:

col1, col2, col3, col4

I could then refer to the fields with some kind of loop

for (i=0;i<4;i++) {

OLD.col || i

}

if that makes any more sense to refer to the OLD record dynamically (I know that the loop is not a valid one for plpgsql but it just an example of a loop to show an example of the problem
0
 
LVL 19

Expert Comment

by:grant300
ID: 20321074
Let's step back and figure out what you are actually trying to accomplish with your trigger(s) rather than looking at a detailed technique you may not need.  There may be any number of ways to accomplish the overall goal.

What, in general, do the triggers do?  How many do you have to create?

Regards,
Bill
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:tricl
ID: 20321127
Ok,
the idea is to log every update made to any single field

so there will be a table called table_log with the fields:

id, ref_id, table_name, fieldname, oldfieldvalue, newfieldvalue

so I will create a function which will essentially be the same code for each table that is used on an update trigger to store the fieldname, table name the row id and the new and old value for that field

essentially that is what I want to be able to do, this way I am then able to store and refer back to any historical record.

I will plan to do the same kind of method for delete operation too so nothing will be deleted or changed without having a audit log of it

Hope this makes more sense for what I am needing it for. I have the function ready so all of the logic / code is there just need to refer to a dynamic OLD record
0
 
LVL 19

Expert Comment

by:grant300
ID: 20321960
O.K.  Take a look at the entire discussion on the following EE Question.  This person had a similar requirement in Sybase ASA.

http://www.experts-exchange.com/Database/Sybase/Q_22860493.html

The point made in Q_22860493 is that it is not such a good idea to try and log every field change anyway.  I presented a workable solution to keeping history/audit information that will work in PostgreSQL as well.

Check it out and let us know what you think.

Regards,
Bill
0
 

Author Comment

by:tricl
ID: 20326474
Hi,
I have read through that question/answers however I dont think it totally relates to my issue. The reason for wanting to be able to refer to the OLD and NEW records dynamically is that I can then use the same function for all tables. Rather than having to declare each column for each table to be inserted.

Also by it being just one function, if I then want to advance or change it I only have to change it in one place (modifications to the function)

For example in PHP this would be done with
$stringName = "mystring";
$stringValue = "mystringvalue";
$evalstring="$".$stringName."=".$stringValue.";";
eval($evalstring);

I understand what you are saying with regards to performance of storing every field that is changed on a table, however for this particular project / database use the majority of queries will be SELECT or INSERT UPDATES will not happen as a main task, but need to be logged to protect ourselves

Is there any similar way to achieve what I need as there is in PHP ? the language I am using for PostgreSQL is plpgsql but can use another language if required as well if it offers the solution to what I need

I have given more points to this issue as it appears to be more complex than I originally thought it would be

Cheers
0
 
LVL 19

Expert Comment

by:grant300
ID: 20328919
Why not write a little code generator that creates the Update/Delete triggers automatically based on the meta-data for each target table?  That way you are not stuck writing a custom trigger for each procedure yet you still get reasonable performance and query capabilities of storing an entire record per delta.

Bill
0
 

Author Comment

by:tricl
ID: 20329114
I didnt really want to do that because then changing any element means changing every function so one for each table

I have come up with a possible solution (code attached) this actually works, yes it is a bit of a "bodge" to a solution but it does store the information I need (although it actually stores more as it will store all of the table column names and values not just the ones that have been adjusted)

The issue is that it actually doesnt update the table I am editing for example

if I edit a table called table1 (id, name)

it will not actually update the value I changed in table1 but it will log the changes I made in the below function into _sys_record_log

failing any other solution I will have to go with the one I have mentioned, however does anyone know any reason why it will not let me update the actual table itself (for example table1) ?
DECLARE
colrec RECORD;
colrecsub RECORD;
colname VARCHAR;
colnamet VARCHAR;
coll VARCHAR;
 
begin
 
  
FOR colrec IN
select attname from pg_attribute where attrelid = (select oid from pg_class where relname = TG_RELNAME) and attnum > 0
LOOP
 
FOR colrecsub IN
EXECUTE 'SELECT text(' || colrec.attname || ') as coltest FROM ' || TG_RELNAME || ' WHERE id=' || OLD.id || ';'
LOOP
coll = colrecsub.coltest;
if coll is not null THEN
INSERT INTO _sys_record_log (ref_table,ref_id,event,field_name,field_value) VALUES (TG_RELNAME,OLD.id,TG_OP,colrec.attname,coll);
END IF;
END LOOP;
END LOOP;
return null;
end;

Open in new window

0
 
LVL 3

Accepted Solution

by:
wide_awake earned 400 total points
ID: 20694908
You can easily do this if you write your function in plpythonu instead of plpgsql.  The "old" and "new" records in the trigger become normal Hash objects, so you can access their keys, and can then dynamically reference columns.

I used this approach successfully to do almost exactly what you describe.
0
 

Author Comment

by:tricl
ID: 20704685
I have now come up with a work around, it is not ideal but it will store all of the columns for an updated row,  I can see your solution worth implementing in the future when my work around has started to fill the table up with too many row that are not needed which in turns slows down any future requirements of it. I have not used plpythonu before but will look into how to do it, I am sure it will be easy enough to pick up
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question