473,498 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PL/TCL trigger error "can't read "tgname": no such variable"

2 New Member
Hi all,
I am trying to get a PL/TCL trigger to update an audit table working. The problem is when I create the trigger on an exiting table, I get the following error

"ERROR: can't read "tgname": no such variable
CONTEXT: can't read "tgname": no such variable
while executing ....."

If I drop the table and then recreate the table and add the rigger, the trigger works fine. However if I alter the table, again I have to drop and re-create the bale and triggers.

My problem is I have over 60 table to add the triggers to and each time I implement a change I can not have the tables to be dropped and recreated.

Following is sample of my tcl function

CREATE OR REPLACE FUNCTION xxx.log_to_audit_table()
RETURNS "trigger" AS
$BODY$


spi_exec "SELECT CURRENT_USER AS tguser"


spi_exec "SELECT c.relname AS tgname,n.nspname AS schema_name
FROM pg_class c , pg_namespace n
WHERE n.oid = c.relnamespace
AND relfilenode = $TG_relid"



spi_exec "insert into xxx.testlogtable values ('inserted in log_to_audit_table $tgname')";

set pk_name ""

spi_exec "SELECT a.attname AS pk_name FROM pg_class c, pg_attribute a, pg_index i
WHERE c.relname = '$tgname'
AND c.oid=i.indrelid
AND a.attnum > 0
AND a.attrelid = i.indexrelid
AND i.indisprimary='t'"

spi_exec "SELECT audit_table_name AS m_aud_tbl_name
FROM $main_schema.audit_table_mapping
where schema_name = '$schema_name'
and tabel_name = '$tgname'"


.....

return OK
$BODY$
LANGUAGE 'pltcl' VOLATILE CALLED ON NULL INPUT SECURITY INVOKER;

and this is my trigger

CREATE TRIGGER trig_audit_admin_user
AFTER INSERT OR UPDATE OR DELETE
ON xxx."user"
FOR EACH ROW
EXECUTE PROCEDURE "xxx"."log_to_audit_table"();

The problem is the error comes where I am trying to use the values queried from the catalogue $tgname, $schema_name etc...

Any help would be much apprciated
Jul 6 '10 #1
1 4209
indikamaligaspe
2 New Member
Guys,
I found the solution to this, sorry was now able to post s reply till now.
The issue is in the following code

spi_exec "SELECT c.relname AS tgname,n.nspname AS schema_name
FROM pg_class c , pg_namespace n
WHERE n.oid = c.relnamespace
AND relfilenode = $TG_relid"

When you create a table the pg_class table gets a record for that table and the OID and relfilenode are the same in that record. When you create the trigger, the trigger gets mapped to the OID of the table in pg_class.

So in effect the $TG_relid is equal to both OID and relfilenode in the pg_class table.

However when you do modifications to the table (change column types / add constraints etc... the "relfilenode" value keeps on changing to match the physical data location. So when I run the query the $TG_relid no longer matches to "relfilenode".

So I changed the code to check the OID in pg_class rather then the "relfilenode" and that fixed the problem.
Jul 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2382
by: Bill Cohagan | last post by:
I'm writing a console app in c# and am encountering a strange problem. I'm trying to use redirection of the standard input stream to read input from a (xml) file. The following code snippet is from...
33
4736
by: coosa | last post by:
I have a table: ---------------------------------------------------- CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL,...
13
2837
by: Nigel J. Andrews | last post by:
This will be a little vague, it was last night and I can't now do the test in that db (see below) so can't give the exact wording. I seem to remember a report a little while ago about tsearch v2...
9
3190
by: Raquel | last post by:
Following is a simple trigger definition: CREATE TRIGGER TRIG_EMPLOYEE_1 AFTER UPDATE OF SALARY ON DB2ADMIN.EMPLOYEE FOR EACH STATEMENT MODE DB2SQL CALL DB2ADMIN.SQLSP10( ) This gives an...
12
4736
by: Bob Stearns | last post by:
I am trying to create a duplicate prevention trigger: CREATE TRIGGER is3.ard_u_unique BEFORE UPDATE OF act_recov_date ON is3.flushes REFERENCING NEW AS N FOR EACH ROW MODE DB2SQL WHEN...
3
5849
by: Madhu | last post by:
Hi, I need to trap EAGAIN error while using read(). I read in some website that read() has to wait till write() is completed for EAGAIN to occur. But read() doesnt seem to wait, it reads...
1
1131
by: umesh049 | last post by:
hello I want to update in one trigger the tables in to another schema but it doesn't work but when i select data from sql prompt it works fine. can any body help me. thanks. CREATE OR...
1
2172
by: tb1 | last post by:
Hi, I have written a trigger and now im getting this error when i try to add it to my database. ERROR 1415 (0A000): Not allowed to return a result set from a trigger What does that mean?
1
2839
by: amijai11 | last post by:
I am getting following error for a trigger, please let me know if anyone knows how to fix the problem. Thks in advance for the help. SQLCODE = -20100, ERROR: AN ERROR OCCURRED WHEN BINDING A...
0
7005
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
7168
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,...
0
7210
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
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
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...
0
4595
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...
0
1424
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 ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
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...

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.