views:

72

answers:

3

I need to make a function that would be triggered after every UPDATE and INSERT operation and would check the key fields of the table that the operation is performed on vs some conditions.

The function (and the trigger) needs to be an universal one, it shouldn't have the table name / fields names hardcoded.

I got stuck on the part where I need to access the table name and its schema part - check what fields are part of the PRIMARY KEY.

+1  A: 

Have a look at http://stackoverflow.com/questions/1214576/how-do-i-get-the-primary-keys-of-a-table-from-postgres-via-plpgsql The answer in that one should be able to help you.

Jimmy Stenke
+1  A: 

Note that you can't use dynamic SQL in PL/pgSQL; it's too strongly-typed a language for that. You'll have more luck with PL/Perl, on which you can access a hash of the columns and use regular Perl accessors to check them. (PL/Python would also work, but sadly that's an untrusted language only. PL/Tcl works too.)

In 8.4 you can use EXECUTE 'something' USING NEW, which in some cases is able to do the job.

alvherre
Great info, thanks for that. I'll stick to PL/Perl then, I suppose.
kender
+1  A: 

After getting the primary key info as already posted in the first answer you can check the code in http://github.com/fgp/pg_record_inspect to get record field values dynamicaly in PL/pgSQL.

Diogo Biazus