Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
plperl-triggers.html74 linesDownload Raw Back to html
1<?xml version="1.0" encoding="UTF-8" standalone="no"?>2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>45.6. PL/Perl Triggers</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl" /><link rel="next" href="plperl-event-triggers.html" title="45.7. PL/Perl Event Triggers" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">45.6. PL/Perl Triggers</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><th width="60%" align="center">Chapter 45. PL/Perl — Perl Procedural Language</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="plperl-event-triggers.html" title="45.7. PL/Perl Event Triggers">Next</a></td></tr></table><hr /></div><div class="sect1" id="PLPERL-TRIGGERS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">45.6. PL/Perl Triggers <a href="#PLPERL-TRIGGERS" class="id_link">#</a></h2></div></div></div><p>3   PL/Perl can be used to write trigger functions.  In a trigger function,4   the hash reference <code class="varname">$_TD</code> contains information about the5   current trigger event. <code class="varname">$_TD</code> is a global variable,6   which gets a separate local value for each invocation of the trigger.7   The fields of the <code class="varname">$_TD</code> hash reference are:8 9   </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">$_TD-&gt;{new}{foo}</code></span></dt><dd><p>10       <code class="literal">NEW</code> value of column <code class="literal">foo</code>11      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{old}{foo}</code></span></dt><dd><p>12       <code class="literal">OLD</code> value of column <code class="literal">foo</code>13      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{name}</code></span></dt><dd><p>14       Name of the trigger being called15      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{event}</code></span></dt><dd><p>16       Trigger event: <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>,17       <code class="literal">DELETE</code>, <code class="literal">TRUNCATE</code>, or <code class="literal">UNKNOWN</code>18      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{when}</code></span></dt><dd><p>19       When the trigger was called: <code class="literal">BEFORE</code>,20       <code class="literal">AFTER</code>, <code class="literal">INSTEAD OF</code>, or21       <code class="literal">UNKNOWN</code>22      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{level}</code></span></dt><dd><p>23       The trigger level: <code class="literal">ROW</code>, <code class="literal">STATEMENT</code>, or <code class="literal">UNKNOWN</code>24      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{relid}</code></span></dt><dd><p>25       OID of the table on which the trigger fired26      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{table_name}</code></span></dt><dd><p>27       Name of the table on which the trigger fired28      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{relname}</code></span></dt><dd><p>29       Name of the table on which the trigger fired. This has been deprecated,30       and could be removed in a future release.31       Please use $_TD-&gt;{table_name} instead.32      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{table_schema}</code></span></dt><dd><p>33       Name of the schema in which the table on which the trigger fired, is34      </p></dd><dt><span class="term"><code class="literal">$_TD-&gt;{argc}</code></span></dt><dd><p>35       Number of arguments of the trigger function36      </p></dd><dt><span class="term"><code class="literal">@{$_TD-&gt;{args}}</code></span></dt><dd><p>37       Arguments of the trigger function.  Does not exist if <code class="literal">$_TD-&gt;{argc}</code> is 0.38      </p></dd></dl></div><p>39  </p><p>40   Row-level triggers can return one of the following:41 42   </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">return;</code></span></dt><dd><p>43       Execute the operation44      </p></dd><dt><span class="term"><code class="literal">"SKIP"</code></span></dt><dd><p>45       Don't execute the operation46      </p></dd><dt><span class="term"><code class="literal">"MODIFY"</code></span></dt><dd><p>47       Indicates that the <code class="literal">NEW</code> row was modified by48       the trigger function49      </p></dd></dl></div><p>50  </p><p>51   Here is an example of a trigger function, illustrating some of the52   above:53</p><pre class="programlisting">54CREATE TABLE test (55    i int,56    v varchar57);58 59CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$60    if (($_TD-&gt;{new}{i} &gt;= 100) || ($_TD-&gt;{new}{i} &lt;= 0)) {61        return "SKIP";    # skip INSERT/UPDATE command62    } elsif ($_TD-&gt;{new}{v} ne "immortal") {63        $_TD-&gt;{new}{v} .= "(modified by trigger)";64        return "MODIFY";  # modify row and execute INSERT/UPDATE command65    } else {66        return;           # execute INSERT/UPDATE command67    }68$$ LANGUAGE plperl;69 70CREATE TRIGGER test_valid_id_trig71    BEFORE INSERT OR UPDATE ON test72    FOR EACH ROW EXECUTE FUNCTION valid_id();73</pre><p>74  </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="plperl-trusted.html" title="45.5. Trusted and Untrusted PL/Perl">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="plperl.html" title="Chapter 45. PL/Perl — Perl Procedural Language">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="plperl-event-triggers.html" title="45.7. PL/Perl Event Triggers">Next</a></td></tr><tr><td width="40%" align="left" valign="top">45.5. Trusted and Untrusted PL/Perl </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 45.7. PL/Perl Event Triggers</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai