{"id":662,"date":"2005-01-26T20:48:31","date_gmt":"2005-01-26T23:48:31","guid":{"rendered":"http:\/\/www.hoogervorst.ca\/arthur\/?p=662"},"modified":"2005-01-26T20:48:31","modified_gmt":"2005-01-26T23:48:31","slug":"on-a-related-note","status":"publish","type":"post","link":"http:\/\/www.hoogervorst.ca\/arthur\/?p=662","title":{"rendered":"On a related note"},"content":{"rendered":"<p><span class=dropcap>F<\/span>or users of Postgres 7.2 (and probably up to 7.3, but it might be compatible with all higher versions) who do not have a clue how to get those triggers going because it&#8217;s all undocumented, here goes.\n<\/p>\n<p>Two things you need to know first: <\/p>\n<ol>\n<li>Triggers rely on functions created by users.\n<li>Functions (in 7.2) for triggers MUST return a result of <em>OPAQUE<\/em>.\n<li>Triggered functions can not have parameters!\n<\/ol>\n<p>Right, tell me you didn&#8217;t know that and go back to start.\n<\/p>\n<p>Take the following simple (and not real-life) scenario:\n<\/p>\n<p><!--more--><\/p>\n<p>We have a table &#8216;currency&#8217; that needs to have an exact copy, &#8216;back_currency&#8217;. Everytime something is added to currency, it should be copied to  the backup table.\n<\/p>\n<p>First we create a function called &#8216;currencyupdate&#8217;:\n<\/p>\n<p>\n<PRE><br \/>\nCREATE FUNCTION currencyupd() RETURNS opaque AS &#8216;<br \/>\nBEGIN<br \/>\n   insert into back_currency (<br \/>\n    currency_code,<br \/>\n    currency_description,<br \/>\n    user_lada) VALUES<br \/>\n    (<br \/>\n     NEW.currency_code,<br \/>\n     NEW.currency_description,<br \/>\n     current_timestamp<br \/>\n    );<br \/>\n   RETURN NEW;<br \/>\nEND;<br \/>\n&#8216;  LANGUAGE &#8216;plpgsql&#8217; WITH (ISSTRICT);<br \/>\n<\/PRE>\n<\/p>\n<p>(note: if we were using  a DELETE\/UPDATE trigger,  older recordsets are stored in the OLD object).\n<\/p>\n<p>Now we can activate the trigger by sending the following query to the Postgres server:\n<\/p>\n<p>\n<PRE><br \/>\nCREATE TRIGGER OnCurrencyUpdate BEFORE INSERT ON currency<br \/>\n\tFOR EACH ROW EXECUTE PROCEDURE currencyupd();<br \/>\n<\/PRE>\n<\/p>\n<p>Just in case you needed to know.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For users of Postgres 7.2 (and probably up to 7.3, but it might be compatible with all higher versions) who do not have a clue how to get those triggers going because it&#8217;s all undocumented, here goes. Two things you &hellip; <a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=662\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/662"}],"collection":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=662"}],"version-history":[{"count":0,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/662\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=662"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}