{"id":72265,"date":"2020-03-03T08:42:17","date_gmt":"2020-03-03T05:42:17","guid":{"rendered":"https:\/\/prohoster.info\/blog\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera"},"modified":"2020-03-03T16:13:51","modified_gmt":"2020-03-03T13:13:51","slug":"postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","status":"publish","type":"post","link":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","title":{"rendered":"PostgreSQL Antipatterns: changing data bypassing triggers","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Sooner or later, many face the need to make bulk changes to table entries. I've already <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/post\/481610\/\">discussed the best ways to do this,<\/a><\/noindex>and what to avoid. Today, I will talk about the second aspect of bulk updates \u2014 <b>trigger firing.<\/b>.<\/p>\n<p>For example, a malicious trigger is hanging over the table where you need to make adjustments, <code>ON UPDATE,<\/code>transferring all changes to some aggregates. You need to update everything (initialize a new field, for instance) carefully, so that these aggregates are not affected.<\/p>\n<h2>Let's just disable the triggers!<\/h2>\n<p><\/p>\n<pre><code class=\"sql\">BEGIN;\n  ALTER TABLE ... DISABLE TRIGGER ...;\n  UPDATE ...; -- this will take a while\n  ALTER TABLE ... ENABLE TRIGGER ...;\nCOMMIT;<\/code><\/pre>\n<p>\nThat's basically it \u2014 <b>everything is already hanging.<\/b>.<\/p>\n<p>Because <code>ALTER TABLE<\/code> imposes <b>AccessExclusive<\/b>- the lock, under which no one performing parallel operations, even a simple one, <code>SELECT<\/code>, will be able to read anything from the table. This means that until this transaction is completed, everyone wishing to 'just read' will have to wait. And we remember that <code>UPDATE<\/code> it's going to be a l-o-o-o-ng wait\u2026<br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h4>So let's quickly disable it, then quickly enable it!<\/h4>\n<p><\/p>\n<pre><code class=\"sql\">BEGIN;\n  ALTER TABLE ... DISABLE TRIGGER ...;\nCOMMIT;\n\nUPDATE ...;\n\nBEGIN;\n  ALTER TABLE ... ENABLE TRIGGER ...;\nCOMMIT;<\/code><\/pre>\n<p>\nThe situation is already better here, the wait time is significantly reduced. But two problems spoil the beauty:<\/p>\n<ul>\n<li><code>ALTER TABLE<\/code> it still waits for all other operations on the table, including long ones. <code>SELECT<\/code><\/li>\n<li>While the trigger is off, <b>any change<\/b> to the table, even if it's not ours, will 'fly by'. And it won\u2019t make its way to the aggregates, although it should. That\u2019s a problem!<\/li>\n<\/ul>\n<p><\/p>\n<h2>Session Variable Management<\/h2>\n<p>\nSo, in the previous option, we encountered a fundamental point \u2014 we need to teach the trigger to distinguish 'our' changes in the table from 'not ours'. 'Our' changes should pass as is, while 'not ours' should trigger. To achieve this, we can use <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-client\">session variables.<\/a><\/noindex>.<\/p>\n<h4>session_replication_role.<\/h4>\n<p>\nLet's read <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/sql-altertable\">manual<\/a><\/noindex>:<\/p>\n<blockquote><p>The trigger firing mechanism is also influenced by the configuration variable <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-client#GUC-SESSION-REPLICATION-ROLE\">session_replication_role.<\/a><\/noindex>. Triggers enabled without further specifications (by default) will fire when the replication role is 'origin' (by default) or 'local'. Triggers enabled with the specification <code>ENABLE REPLICA<\/code>, will only fire if <b>the current session mode<\/b> is 'replica', and triggers enabled with the specification <code>ENABLE ALWAYS<\/code>, will fire regardless of the current replication mode.<\/p><\/blockquote>\n<p>I want to emphasize that this setting does not apply to all at once, as <code>ALTER TABLE<\/code>, but only to our separate special connect. In total, to ensure that no application triggers are activated:<\/p>\n<pre><code class=\"sql\">SET session_replication_role = replica; -- disabled triggers\nUPDATE ...;\nSET session_replication_role = DEFAULT; -- reverted to the initial state<\/code><\/pre>\n<p><\/p>\n<h4>The condition inside the trigger<\/h4>\n<p>\nHowever, the above option works for all triggers at once (or you need to 'alter' in advance the triggers you do not want to disable). But if we need to <b>disable one specific trigger<\/b>?<\/p>\n<p>This will be aided by <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-custom\">a 'user' session variable<\/a><\/noindex>:<\/p>\n<blockquote><p>Extension parameter names are recorded as follows: the name of the extension, a dot, and then the actual name of the parameter, similar to fully qualified object names in SQL. For example: plpgsql.variable_conflict.<br \/>\nSince non-system parameters can be set in processes that do not load the corresponding extension module, PostgreSQL accepts <b>values for any names with two components<\/b>.<\/p><\/blockquote>\n<p>First, we improve the trigger, approximately as follows:<\/p>\n<pre><code class=\"sql\">BEGIN\n    -- the conversion process can do everything\n    IF current_setting('mycfg.my_table_convert_process') = 'TRUE' THEN\n        IF TG_OP IN ('INSERT', 'UPDATE') THEN\n            RETURN NEW;\n        ELSE\n            RETURN OLD;\n        END IF;\n    END IF;\n...<\/code><\/pre>\n<p>\nBy the way, this can be done 'live', without locks, through <code>CREATE OR REPLACE<\/code> for the trigger function. Then in the special connect we set our variable:<\/p>\n<pre><code class=\"sql\">\nSET mycfg.my_table_convert_process = 'TRUE';\nUPDATE ...;\nSET mycfg.my_table_convert_process = ''; -- reverted to the initial state\n<\/code><\/pre>\n<p>\nDo you know other methods? Share in the comments.<br \/>\n<br \/>Source: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/489900\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0447\u0442\u043e-\u0442\u043e \u043c\u0430\u0441\u0441\u043e\u0432\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0437\u0430\u043f\u0438\u0441\u044f\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u042f \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u043b, \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043b\u0443\u0447\u0448\u0435, \u0430 \u043a\u0430\u043a \u2014 \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0430\u0441\u043f\u0435\u043a\u0442\u0435 \u043c\u0430\u0441\u0441\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u2014 \u043e \u0441\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u043c \u043d\u0430\u0434\u043e \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0432\u0438\u0441\u0438\u0442 \u0437\u043b\u043e\u0431\u043d\u044b\u0439 \u0442\u0440\u0438\u0433\u0433\u0435\u0440 ON UPDATE, \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044f\u0449\u0438\u0439 \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-72265","post","type-post","status-publish","format-standard","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0447\u0442\u043e-\u0442\u043e \u043c\u0430\u0441\u0441\u043e\u0432\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0437\u0430\u043f\u0438\u0441\u044f\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47PostgreSQL Antipatterns: \u043c\u0435\u043d\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0445\u043e\u0434 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0447\u0442\u043e-\u0442\u043e \u043c\u0430\u0441\u0441\u043e\u0432\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0437\u0430\u043f\u0438\u0441\u044f\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-03-03T05:42:17+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-03-03T13:13:51+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47PostgreSQL Antipatterns: changing data bypassing the trigger | ProHoster","description":"Sooner or later, many face the need to correct something in bulk in table records.","canonical_url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"en_US","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47PostgreSQL Antipatterns: \u043c\u0435\u043d\u044f\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u0445\u043e\u0434 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430 | ProHoster","og:description":"\u0420\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0447\u0442\u043e-\u0442\u043e \u043c\u0430\u0441\u0441\u043e\u0432\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0432 \u0437\u0430\u043f\u0438\u0441\u044f\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.","og:url":"https:\/\/prohoster.info\/en\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-03-03T05:42:17+00:00","article:modified_time":"2020-03-03T13:13:51+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"72265","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 18:47:24","updated":"2022-09-27 18:26:38","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/72265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/comments?post=72265"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/posts\/72265\/revisions"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/media?parent=72265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/categories?post=72265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/en\/wp-json\/wp\/v2\/tags?post=72265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}