{"id":1689,"date":"2025-01-14T14:32:17","date_gmt":"2025-01-14T07:32:17","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1689"},"modified":"2025-01-15T22:19:22","modified_gmt":"2025-01-15T15:19:22","slug":"postgresql-triggers","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/","title":{"rendered":"PostgreSQL Triggers"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about PostgreSQL triggers and how to use them to automate some database tasks.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-triggers'>Introduction to PostgreSQL triggers <a href=\"#introduction-to-postgresql-triggers\" class=\"anchor\" id=\"introduction-to-postgresql-triggers\" title=\"Anchor for Introduction to PostgreSQL triggers\">#<\/a><\/h2>\n\n\n\n<p>In PostgreSQL, a trigger is a user-defined function or procedure invoked automatically when an event such as <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">INSERT<\/a><\/code>, <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-update\/\">UPDATE<\/a><\/code>, <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-delete\/\">DELETE<\/a><\/code>, or <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-truncate-table\/\">TRUNCATE<\/a><\/code> occurs on a table or <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-view\/\">view<\/a>.<\/p>\n\n\n\n<p>In practice, you can find the triggers helpful in the following situations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logging<\/li>\n\n\n\n<li>Auditing<\/li>\n\n\n\n<li>Validating complex business rules that simple constraints cannot do.<\/li>\n<\/ul>\n\n\n\n<p>Triggers include four key components:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trigger events<\/li>\n\n\n\n<li>Trigger timing<\/li>\n\n\n\n<li>Trigger function<\/li>\n\n\n\n<li>Trigger scope<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='trigger-events'>Trigger Events <a href=\"#trigger-events\" class=\"anchor\" id=\"trigger-events\" title=\"Anchor for Trigger Events\">#<\/a><\/h3>\n\n\n\n<p>Trigger events are events that cause the trigger to fire. In PostgreSQL, the trigger events are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>INSERT<\/code><\/li>\n\n\n\n<li><code>UPDATE<\/code><\/li>\n\n\n\n<li><code>DELETE<\/code><\/li>\n\n\n\n<li><code>TRUNCATE<\/code><\/li>\n<\/ul>\n\n\n\n<p>For example, inserting a new row into a table will fire a trigger.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='trigger-functions'>Trigger Functions <a href=\"#trigger-functions\" class=\"anchor\" id=\"trigger-functions\" title=\"Anchor for Trigger Functions\">#<\/a><\/h3>\n\n\n\n<p>Trigger functions are <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-functions\/\">user-defined functions<\/a> or <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-procedure\/\">procedures<\/a> that execute when triggers fire.<\/p>\n\n\n\n<p>Trigger functions always return a <code>TRIGGER<\/code>. Inside trigger functions, you can access special variables such as <code>TG_OP<\/code>, <code>NEW<\/code>, and <code>OLD<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>TG_OP<\/code> variable stores the operation that causes the trigger to fire. It can be <code>'INSERT'<\/code>, <code>'UPDATE'<\/code> ,<code>'DELETE'<\/code> and <code>'TRUNCATE'<\/code>.<\/li>\n\n\n\n<li>The <code>NEW<\/code> variable stores the new row. This variable is available only when the operation is <code>'INSERT'<\/code> or <code>'UPDATE'<\/code>.<\/li>\n\n\n\n<li>The <code>OLD<\/code> variable stores the old row, available in the <code>'UPDATE'<\/code> or <code>'DELETE'<\/code> operation.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='trigger-timing'>Trigger Timing <a href=\"#trigger-timing\" class=\"anchor\" id=\"trigger-timing\" title=\"Anchor for Trigger Timing\">#<\/a><\/h3>\n\n\n\n<p>Trigger timing specifies the time when the triggers fire:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>BEFORE<\/code> &#8211; Executes the trigger function before the triggering event.<\/li>\n\n\n\n<li><code>AFTER<\/code> &#8211; Executes the trigger function after the triggering event.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='trigger-scope'>Trigger Scope <a href=\"#trigger-scope\" class=\"anchor\" id=\"trigger-scope\" title=\"Anchor for Trigger Scope\">#<\/a><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Row-level: The trigger function executes for every affected row. For example, if you issue an <code>UPDATE<\/code> statement that updates 10 rows, the trigger will fire 10 times, each per row.<\/li>\n\n\n\n<li>Statement-level: The trigger function executes per SQL statement. For example, if you run an <code>UPDATE<\/code> statement, the statement-level trigger will fire once, regardless of the number of rows updated.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-triggers'>Creating Triggers <a href=\"#creating-triggers\" class=\"anchor\" id=\"creating-triggers\" title=\"Anchor for Creating Triggers\">#<\/a><\/h2>\n\n\n\n<p>To create a trigger, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-function\/\">define a user-defined function<\/a> or <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-procedure\/\">procedure<\/a> to execute when the trigger fires.<\/li>\n\n\n\n<li>Second, associate the trigger function with a trigger using the <code>CREATE TRIGGER<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<p>The <code>CREATE TRIGGER<\/code> statement allows you to create a new trigger with key trigger components:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> &#91;<span class=\"hljs-keyword\">OR REPLACE<\/span>] <span class=\"hljs-keyword\">TRIGGER<\/span> trigger_name\n{ <span class=\"hljs-keyword\">BEFORE<\/span> | <span class=\"hljs-keyword\">AFTER<\/span> }\n{ <span class=\"hljs-keyword\">INSERT<\/span> | <span class=\"hljs-keyword\">DELETE<\/span> | <span class=\"hljs-keyword\">TRUNCATE<\/span> | <span class=\"hljs-keyword\">UPDATE<\/span> &#91;<span class=\"hljs-keyword\">OF<\/span> <span class=\"hljs-built_in\">column_name<\/span>, ...] }\n<span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n&#91; <span class=\"hljs-keyword\">FOR<\/span> &#91; <span class=\"hljs-keyword\">EACH<\/span> ] { <span class=\"hljs-keyword\">ROW<\/span> | <span class=\"hljs-keyword\">STATEMENT<\/span> } ]\n<span class=\"hljs-keyword\">EXECUTE<\/span> { <span class=\"hljs-keyword\">FUNCTION<\/span> | <span class=\"hljs-keyword\">PROCEDURE<\/span> } function_name(arguments);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>CREATE TRIGGER<\/code> creates a new trigger, while the <code>CREATE OR REPLACE TRIGGER<\/code> creates a new trigger or replaces an existing one if the trigger already exists.<\/li>\n\n\n\n<li>Second, specify the trigger&#8217;s name (<code>trigger_name<\/code>) after the <code>CREATE TRIGGER<\/code> keywords.<\/li>\n\n\n\n<li>Third, indicate the trigger timing, such as <code>BEFORE<\/code> or <code>AFTER<\/code>, and the trigger event, like <code>INSERT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, and <code>TRUNCATE<\/code>.<\/li>\n\n\n\n<li>Fourth, specify the table name with which the trigger associates after the <code>ON<\/code> keyword.<\/li>\n\n\n\n<li>Fifth, define the trigger scope, which can be <code>ROW<\/code> or <code>STATEMENT<\/code> after the <code>FOR EACH<\/code> keyword.<\/li>\n\n\n\n<li>Finally, associate a user-defined function or procedure name with a trigger in the <code>EXECUTE<\/code> clause.<\/li>\n<\/ul>\n\n\n\n<p>To create a trigger function, you need to use the <a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/\">PL\/pgSQL<\/a> procedure language.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='creating-trigger-example'>Creating Trigger Example <a href=\"#creating-trigger-example\" class=\"anchor\" id=\"creating-trigger-example\" title=\"Anchor for Creating Trigger Example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll create a trigger that logs the safety stock changes in the <code>products<\/code> table:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"159\" height=\"254\" src=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" alt=\"PostgreSQL triggers - Products Table\" class=\"wp-image-1051\"\/><\/figure>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a table<\/a> called <code>safety_stock_logs<\/code> that stores the logging data including product id, old safety stock, new safety stock, and updated time:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> safety_stock_logs (\n  id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  old_safety_stock <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  new_safety_stock <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  updated_at <span class=\"hljs-type\">TIMESTAMPTZ<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> NOW(),\n  <span class=\"hljs-keyword\">FOREIGN KEY<\/span> (product_id) <span class=\"hljs-keyword\">REFERENCES<\/span> products (product_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIFRBQkxFIElGIE5PVCBFWElTVFMgc2FmZXR5X3N0b2NrX2xvZ3MgKCBpZCBJTlQgR0VORVJBVEVEIEFMV0FZUyBBUyBJREVOVElUWSBQUklNQVJZIEtFWSwgcHJvZHVjdF9pZCBJTlQgTk9UIE5VTEwsIG9sZF9zYWZldHlfc3RvY2sgSU5UIE5PVCBOVUxMLCBuZXdfc2FmZXR5X3N0b2NrIElOVCBOT1QgTlVMTCwgdXBkYXRlZF9hdCBUSU1FU1RBTVBUWiBOT1QgTlVMTCBERUZBVUxUIE5PVygpLCBGT1JFSUdOIEtFWSAocHJvZHVjdF9pZCkgUkVGRVJFTkNFUyBwcm9kdWN0cyAocHJvZHVjdF9pZCkgT04gREVMRVRFIENBU0NBREUgKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Next, create a trigger function that captures the product id, old safety stock, and new safety stock and <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">inserts<\/a> them into the <code>safety_stock_logs<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> log_safety_stock_changes()\n<span class=\"hljs-keyword\">RETURNS<\/span> <span class=\"hljs-type\">TRIGGER<\/span> \n<span class=\"hljs-keyword\">AS<\/span> \n$$<span class=\"pgsql\">\n<span class=\"hljs-keyword\">BEGIN<\/span>\n  <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-built_in\">NEW<\/span>.safety_stock != <span class=\"hljs-built_in\">OLD<\/span>.safety_stock <span class=\"hljs-keyword\">THEN<\/span>\n    <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> safety_stock_logs (product_id, old_safety_stock, new_safety_stock)\n    <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-built_in\">OLD<\/span>.product_id, <span class=\"hljs-built_in\">OLD<\/span>.safety_stock, <span class=\"hljs-built_in\">NEW<\/span>.safety_stock);\n  <span class=\"hljs-keyword\">END<\/span> <span class=\"hljs-keyword\">IF<\/span>;\n  <span class=\"hljs-keyword\">RETURN<\/span> <span class=\"hljs-built_in\">NEW<\/span>;\n<span class=\"hljs-keyword\">END<\/span>;\n$$<\/span> \n<span class=\"hljs-keyword\">LANGUAGE<\/span> plpgsql;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIE9SIFJFUExBQ0UgRlVOQ1RJT04gbG9nX3NhZmV0eV9zdG9ja19jaGFuZ2VzKCkKUkVUVVJOUyBUUklHR0VSIApBUyAKJCQKQkVHSU4KICBJRiBORVcuc2FmZXR5X3N0b2NrICE9IE9MRC5zYWZldHlfc3RvY2sgVEhFTgogICAgSU5TRVJUIElOVE8gc2FmZXR5X3N0b2NrX2xvZ3MgKHByb2R1Y3RfaWQsIG9sZF9zYWZldHlfc3RvY2ssIG5ld19zYWZldHlfc3RvY2spCiAgICBWQUxVRVMgKE9MRC5wcm9kdWN0X2lkLCBPTEQuc2FmZXR5X3N0b2NrLCBORVcuc2FmZXR5X3N0b2NrKTsKICBFTkQgSUY7CiAgUkVUVVJOIE5FVzsKRU5EOwokJCAKTEFOR1VBR0UgcGxwZ3NxbDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Then, create a trigger that invokes the <code>log_safety_stock_changes<\/code> function whenever the safety stock changes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR REPLACE<\/span> <span class=\"hljs-keyword\">TRIGGER<\/span> safety_stock_update_trigger\n<span class=\"hljs-keyword\">AFTER<\/span> <span class=\"hljs-keyword\">UPDATE<\/span> <span class=\"hljs-keyword\">OF<\/span> safety_stock\n<span class=\"hljs-keyword\">ON<\/span> products\n<span class=\"hljs-keyword\">FOR<\/span> <span class=\"hljs-keyword\">EACH<\/span> <span class=\"hljs-keyword\">ROW<\/span>\n<span class=\"hljs-keyword\">EXECUTE<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> log_safety_stock_changes();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIE9SIFJFUExBQ0UgVFJJR0dFUiBzYWZldHlfc3RvY2tfdXBkYXRlX3RyaWdnZXIKQUZURVIgVVBEQVRFIE9GIHNhZmV0eV9zdG9jawpPTiBwcm9kdWN0cwpGT1IgRUFDSCBST1cKRVhFQ1VURSBGVU5DVElPTiBsb2dfc2FmZXR5X3N0b2NrX2NoYW5nZXMoKTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>After that, update the safety stock for the product with id 1 to 15:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">UPDATE<\/span> products \n<span class=\"hljs-keyword\">SET<\/span> safety_stock = <span class=\"hljs-number\">15<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span> product_id = <span class=\"hljs-number\">1<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-update\/\">UPDATE<\/a><\/code> statement causes the trigger to fire, which inserts a new row into the <code>safety_stock_logs<\/code> table.<\/p>\n\n\n\n<p>Finally, retrieve the data from the <code>safety_stock_logs<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  product_id,\n  old_safety_stock,\n  new_safety_stock\n<span class=\"hljs-keyword\">FROM<\/span>\n  safety_stock_logs;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGlkLCBwcm9kdWN0X2lkLCBvbGRfc2FmZXR5X3N0b2NrLCBuZXdfc2FmZXR5X3N0b2NrIEZST00gc2FmZXR5X3N0b2NrX2xvZ3M7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id | product_id | old_safety_stock | new_safety_stock |\n<span class=\"hljs-comment\">----+------------+------------------+------------------+-<\/span>\n  <span class=\"hljs-number\">1<\/span> |          <span class=\"hljs-number\">1<\/span> |               <span class=\"hljs-number\">20<\/span> |               <span class=\"hljs-number\">15<\/span> |<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Triggers are user-defined functions or procedures that are called automatically when events such as <code>INSERT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, and <code>TRUNCATE<\/code> occur on a table or view.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=triggers\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1689\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Triggers\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1689\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL Triggers\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn about PostgreSQL triggers and how to use them to automate some database tasks.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":97,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1689","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL Triggers<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn about PostgreSQL triggers and how to use them to automate some database tasks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL Triggers\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn about PostgreSQL triggers and how to use them to automate some database tasks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-15T15:19:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png\" \/>\n\t<meta property=\"og:image:width\" content=\"159\" \/>\n\t<meta property=\"og:image:height\" content=\"254\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/\",\"name\":\"PostgreSQL Triggers\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"datePublished\":\"2025-01-14T07:32:17+00:00\",\"dateModified\":\"2025-01-15T15:19:22+00:00\",\"description\":\"In this tutorial, you'll learn about PostgreSQL triggers and how to use them to automate some database tasks.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"contentUrl\":\"https:\\\/\\\/www.pgtutorial.com\\\/wp-content\\\/uploads\\\/2024\\\/12\\\/products.png\",\"width\":159,\"height\":254,\"caption\":\"PostgreSQL Expression Index\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-triggers\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL Triggers\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL Triggers","description":"In this tutorial, you'll learn about PostgreSQL triggers and how to use them to automate some database tasks.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL Triggers","og_description":"In this tutorial, you'll learn about PostgreSQL triggers and how to use them to automate some database tasks.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-15T15:19:22+00:00","og_image":[{"width":159,"height":254,"url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/","name":"PostgreSQL Triggers","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/#primaryimage"},"image":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","datePublished":"2025-01-14T07:32:17+00:00","dateModified":"2025-01-15T15:19:22+00:00","description":"In this tutorial, you'll learn about PostgreSQL triggers and how to use them to automate some database tasks.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/#primaryimage","url":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","contentUrl":"https:\/\/www.pgtutorial.com\/wp-content\/uploads\/2024\/12\/products.png","width":159,"height":254,"caption":"PostgreSQL Expression Index"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-triggers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL Triggers"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1689"}],"version-history":[{"count":12,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1689\/revisions"}],"predecessor-version":[{"id":1704,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1689\/revisions\/1704"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}