Обсуждение: Changes in Trigger Firing

Поиск
Список
Период
Сортировка

Changes in Trigger Firing

От
Sameer Kumar
Дата:
Hi all,

I am doing some changes in trigger firing mechanism (as a POC first, I will share my work if people find it useful and if it works). 

I am going to extend the trigger calls to child tables. So that in trigger definition I can create triggers with CASCADED TRUE | FALSE option.

This will enable me to extend certain triggers to child tables and get over the scenarios when a record gets inserted in child table and any *after insert* triggers on parent tables never get fired.

I am right now trying to change pg_trigger and CreateTrigger function. I have made changes in below files:
trigger.c:
     - Added a New Variable cascadedOption (bool)
     - Added a new statement in trigger.c to set the value:
                 values[Anum_pg_trigger_tgiscascaded - 1] = BoolGetDatum(stmt->cascadedOption);
     -  Added a new column in CATALOG definition of pg_trigger in pg_trigger.h 
               bool tgiscascaded;
     -  In pg_trigger.h, added a new position for values 
            #define Anum_pg_trigger_tgiscascaded    16
     - Modified parsenode.h to add a new element in Structure for trigger statement -CreateTrigStmt

                 bool        cascadedOption;

CreateTrigStmt is passed to CreateTrigger function as an arguement. I am struggling to understand how the values for various members of trigger are set and where [which file] calls CreateTrigStmt.


Can someone provide some help on this?


Best Regards,
Sameer Kumar | Database Consultant
ASHNIK PTE. LTD.
101 Cecil Street, #11-11 Tong Eng Building, Singapore 069533
M : +65 8110 0350 T: +65 6438 3504 | www.ashnik.com
www.facebook.com/ashnikbiz | www.twitter.com/ashnikbiz

email patch

This email may contain confidential, privileged or copyright material and is solely for the use of the intended recipient(s).

Вложения

Re: Changes in Trigger Firing

От
Alvaro Herrera
Дата:
Sameer Kumar wrote:

> 
> CreateTrigStmt is passed to CreateTrigger function as an arguement. I am
> struggling to understand how the values for various members of trigger are
> set and where [which file] calls CreateTrigStmt.
> 
> 
> Can someone provide some help on this?

I think you need better tools to guide you in exploring the source code.
For example, you can use cscope to tell you where is CreateTrigStmt
used, and you would find gram.y; and use it to tell you where
CreateTrigger is used, and you would find utility.c.

Any half-decent code editor should be able to generate a "database" of
symbols and let you frolic around the various files quickly.  Without
that, anyone would be completely lost in developing new features of even
the lowest complexity.

-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services



Re: Changes in Trigger Firing

От
Sameer Kumar
Дата:

>
> CreateTrigStmt is passed to CreateTrigger function as an arguement. I am
> struggling to understand how the values for various members of trigger are
> set and where [which file] calls CreateTrigStmt.
>
>
> Can someone provide some help on this?

I think you need better tools to guide you in exploring the source code.
For example, you can use cscope to tell you where is CreateTrigStmt
used, and you would find gram.y; and use it to tell you where
CreateTrigger is used, and you would find utility.c.

Thanks for your advice. I was relying on PostgreSQL documentation which was quite helpful so far. Let me try some development tool.
 
Any half-decent code editor should be able to generate a "database" of
symbols and let you frolic around the various files quickly.  Without
that, anyone would be completely lost in developing new features of even
the lowest complexity.

Got the point!

Re: Changes in Trigger Firing

От
Michael Paquier
Дата:
On Wed, Dec 4, 2013 at 11:16 PM, Alvaro Herrera
<alvherre@2ndquadrant.com> wrote:
> Sameer Kumar wrote:
> I think you need better tools to guide you in exploring the source code.
> For example, you can use cscope to tell you where is CreateTrigStmt
> used, and you would find gram.y; and use it to tell you where
> CreateTrigger is used, and you would find utility.c.
>
> Any half-decent code editor should be able to generate a "database" of
> symbols and let you frolic around the various files quickly.  Without
> that, anyone would be completely lost in developing new features of even
> the lowest complexity.
Not always, other people find as well git grep quite useful when
coding a complex tree. I read somewhere that "the best editor is the
one you master" (1) :)
1: http://www.postgresql.org/message-id/m2wrs6giyp.fsf@hi-media.com
-- 
Michael



Re: Changes in Trigger Firing

От
Sameer Kumar
Дата:

 I read somewhere that "the best editor is the
one you master" (1) :)
1: http://www.postgresql.org/message-id/m2wrs6giyp.fsf@hi-media.com


Thanks, I am using eclipse now. 


Any comments about the utility of this feature? Or is it just me who thinks this can be useful? I think users/developers of trigger based replication tools can benefit from this.

I am not sure how replication tools like slony handle a scenario where replicated table is partitioned and I add a new partition to it. 


Regards
Sameer

Re: Changes in Trigger Firing

От
Sameer Kumar
Дата:
<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">I have been
finallyable to get the right set of files. I going with below approach:</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999"><br/></div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999"><br/></div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">1)Add a new column in pg_trigger called
tgiscascaded</div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br
/></div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">2) Change pg_trigger.h
forthis</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br /></div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999">3) Made changes in trigger.c to
insertthis values</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br
/></div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">4) Corresponding changes
madein </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">                 -
reltrigger.h</div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">             
  - parsenode.h</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br
/></div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">5) Changed gram.y to
understanda new key word in CREATE TRIGGER statement "CASCADED". </div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">               - The new option will be optional and will
applyonly to non-constraint triggers</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">                - If the option is specified trigger will
CASCADEto child tables</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br
/></div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"> 6) I just complied the
sourcecode (modified with above changes) and it </div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">               - Added a new column in
pg_trigger</div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">               
-The new column is able to derive its value from CREATE TRIGGER statement based on whether CASDADED was specified or
not<br/></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">                -
Thevalue is True if the option was specified</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">                - The value is false if the option was not
specified</div><divclass="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999"><br /></div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999"><br /></div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999">7) Now I will work on trigger firing
mechanismwith below approach</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">               - Before firing triggers, check if it's an
inheritedtable</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:#339999">           
  - First get CASCADED triggers for parents and fire them</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">              - Proceed as usual</div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999"><br /></div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999"><br /></div><div
class="gmail_default"style="font-family:arial,helvetica,sans-serif;color:#339999">Any suggestion on improving the
approachor comments on need for this feature are welcome.</div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999"><br/></div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999"><br/></div><div class="gmail_default"
style="font-family:arial,helvetica,sans-serif;color:#339999">Regards<br />Sameer</div></div> 

Re: Changes in Trigger Firing

От
Sameer Kumar
Дата:
One scenario where I can forsee an issue is when someone uses the tablename
with-in the trigger function on which the trigger was fired. I am not sure
how and what issue might crop up but this will be one of my test cases.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Hacker-RFC-Changes-in-Trigger-Firing-tp5781566p5781861.html
Sent from the PostgreSQL - hackers mailing list archive at Nabble.com.