{"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\/ro\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","title":{"rendered":"Antipatterns PostgreSQL: modific\u0103m datele ocolind trigger-ul","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Mai devreme sau mai t\u00e2rziu, mul\u021bi se confrunt\u0103 cu necesitatea de a corecta \u00een mas\u0103 \u00eenregistr\u0103rile dintr-un tabel. Eu deja <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/post\/481610\/\">am povestit despre cum s\u0103 faci acest lucru mai bine<\/a><\/noindex>, dar cum \u2014 mai bine nu ar trebui f\u0103cut. Ast\u0103zi voi vorbi despre al doilea aspect al actualiz\u0103rii \u00een mas\u0103 \u2014 <b>declan\u0219area trigger-elor<\/b>.<\/p>\n<p>De exemplu, pe tabela pe care trebuie s\u0103 corect\u0103m ceva, exist\u0103 un trigger agresiv <code>ON UPDATE<\/code>, care mut\u0103 toate modific\u0103rile \u00een anumite agregate. \u0218i trebuie s\u0103 actualiz\u0103m tot (de exemplu, s\u0103 ini\u021bializ\u0103m un nou c\u00e2mp) \u00een a\u0219a fel \u00eenc\u00e2t aceste agregate s\u0103 nu fie afectate.<\/p>\n<h2>S\u0103 dezactiv\u0103m trigger-urile!<\/h2>\n<p><\/p>\n<pre><code class=\"sql\">BEGIN;\n  ALTER TABLE ... DISABLE TRIGGER ...;\n  UPDATE ...; -- aici dureaz\u0103 mult\n  ALTER TABLE ... ENABLE TRIGGER ...;\nCOMMIT;<\/code><\/pre>\n<p>\nDe fapt, asta e tot \u2014 <b>totul este deja suspendat<\/b>.<\/p>\n<p>Pentru c\u0103 <code>ALTER TABLE<\/code> impune <b>AccessExclusive<\/b>-blocarea, sub care nimeni nu va putea citi, chiar \u0219i cele mai simple <code>SELECT<\/code>, nu va putea recupera nimic din tabel. Cu alte cuvinte, p\u00e2n\u0103 nu se termin\u0103 aceast\u0103 tranzac\u021bie, to\u021bi cei care doresc chiar s\u0103 \u201eciteasc\u0103\u201d vor a\u0219tepta. Iar noi ne amintim c\u0103 <code>UPDATE<\/code> avem o a\u0219teptare lung\u0103\u2026<br \/>\n<noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h4>Atunci s\u0103 dezactiv\u0103m rapid, apoi s\u0103 activ\u0103m rapid!<\/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>\nAici situa\u021bia este deja mai bun\u0103, timpul de a\u0219teptare este semnificativ mai mic. Dar dou\u0103 probleme stric\u0103 toat\u0103 frumuse\u021bea:<\/p>\n<ul>\n<li><code>ALTER TABLE<\/code> a\u0219teapt\u0103 toate celelalte opera\u021biuni pe tabel, inclusiv cele lungi <code>SELECT<\/code><\/li>\n<li>\u00cen timp ce trigger-ul este oprit, <b>orice modificare<\/b> \u00een tabel, chiar dac\u0103 nu este a noastr\u0103, va \u201etrece pe l\u00e2ng\u0103\u201d. \u0218i \u00een agregate nu va ajunge, de\u0219i ar trebui. O problem\u0103!<\/li>\n<\/ul>\n<p><\/p>\n<h2>Gestionarea variabilelor de sesiune<\/h2>\n<p>\nDeci, \u00een varianta anterioar\u0103 ne-am lovit de o chestiune principial\u0103 \u2014 trebuie s\u0103 \u00eenv\u0103\u021b\u0103m trigger-ul s\u0103 disting\u0103 modific\u0103rile \u201enoastre\u201d \u00een tabel de cele \u201eneale noastre\u201d. \u201eAle noastre\u201d s\u0103 fie acceptate a\u0219a cum sunt, iar la \u201eneale noastre\u201d \u2014 s\u0103 reac\u021bioneze. Pentru aceasta putem folosi <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-client\">variabilele de sesiune<\/a><\/noindex>.<\/p>\n<h4>session_replication_role<\/h4>\n<p>\nCitim <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/sql-altertable\">manualul<\/a><\/noindex>:<\/p>\n<blockquote><p>Mecanismul de declan\u0219are a trigger-elor este influen\u021bat \u0219i de variabila de configurare <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-client#GUC-SESSION-REPLICATION-ROLE\">session_replication_role<\/a><\/noindex>. Trigger-ele activate f\u0103r\u0103 indica\u021bii suplimentare (implicit) se vor declan\u0219a atunci c\u00e2nd rolul de replicare este \u2014 \u201eorigin\u201d (implicit) sau \u201elocal\u201d. Trigger-ele activate prin indica\u021bia <code>ENABLE REPLICA<\/code>, se vor declan\u0219a doar dac\u0103 <b>modul curent al sesiunii<\/b> este \u201ereplica\u201d, iar trigger-urile activate prin indica\u021bia <code>ENABLE ALWAYS<\/code>, se vor declan\u0219a indiferent de modul curent al replic\u0103rii.<\/p><\/blockquote>\n<p>Vreau s\u0103 subliniez c\u0103 aceast\u0103 setare nu se aplic\u0103 tuturor simultan, a\u0219a cum <code>ALTER TABLE<\/code>, ci doar la conexiunea noastr\u0103 special\u0103 separat\u0103. A\u0219adar, pentru a nu activa niciun declan\u0219ator aplicativ:<\/p>\n<pre><code class=\"sql\">SET session_replication_role = replica; -- am dezactivat declan\u0219atoarele\nUPDATE ...;\nSET session_replication_role = DEFAULT; -- am revenit la starea ini\u021bial\u0103<\/code><\/pre>\n<p><\/p>\n<h4>Condi\u021bia din interiorul declan\u0219atorului<\/h4>\n<p>\nDar varianta de mai sus func\u021bioneaz\u0103 pentru toate declan\u0219atoarele deodat\u0103 (sau trebuie s\u0103 \u00abalter\u0103m\u00bb dinainte declan\u0219atoarele pe care nu dorim s\u0103 le dezactiv\u0103m). Dar dac\u0103 trebuie <b>s\u0103 \u00abdezactiv\u0103m\u00bb un declan\u0219ator specific<\/b>?<\/p>\n<p>Acest lucru ne va ajuta <noindex><a rel=\"nofollow\" href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/runtime-config-custom\">variabila de sesiune \u00abutilizator\u00bb<\/a><\/noindex>:<\/p>\n<blockquote><p>Numele parametrilor extensiilor sunt scrise astfel: numele extensiei, punct \u0219i apoi numele parametrului, similar cu numele complete ale obiectelor \u00een SQL. De exemplu: plpgsql.variable_conflict.<br \/>\nDeoarece parametrii extra-sistemici pot fi seta\u021bi \u00een procese care nu \u00eencarc\u0103 modulul extensiei corespunz\u0103toare, PostgreSQL accept\u0103 <b>valoarea pentru orice nume cu dou\u0103 componente<\/b>.<\/p><\/blockquote>\n<p>Mai \u00eent\u00e2i, modific\u0103m declan\u0219atorul, cam a\u0219a:<\/p>\n<pre><code class=\"sql\">BEGIN\n    -- procesului de conversie \u00eei este permis s\u0103 fac\u0103 tot\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>\nApropo, asta se poate face \u201e\u00een live\u201d, f\u0103r\u0103 blocaje, prin <code>CREATE OR REPLACE<\/code> pentru func\u021bia declan\u0219ator. Apoi, \u00een conexiunea special\u0103 set\u0103m variabila \u201ea noastr\u0103\u201d:<\/p>\n<pre><code class=\"sql\">\nSET mycfg.my_table_convert_process = 'TRUE';\nUPDATE ...;\nSET mycfg.my_table_convert_process = ''; -- am revenit la starea ini\u021bial\u0103\n<\/code><\/pre>\n<p>\nCunoa\u0219te\u021bi alte metode? \u00cemp\u0103rt\u0103\u0219i\u021bi \u00een comentarii.<br \/>\n<br \/>Sursa: <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.1 - 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\/ro\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"ro_RO\" \/>\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\/ro\/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\udd47Antipatterns PostgreSQL: modificarea datelor ocolind declan\u0219atorul | ProHoster","description":"Mai devreme sau mai t\u00e2rziu, mul\u021bi se confrunt\u0103 cu necesitatea de a corecta \u00een mas\u0103 \u00eenregistr\u0103rile dintr-un tabel.","canonical_url":"https:\/\/prohoster.info\/ro\/blog\/administrirovanie\/postgresql-antipatterns-menyaem-dannye-v-obhod-triggera","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"ro_RO","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\/ro\/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\/ro\/wp-json\/wp\/v2\/posts\/72265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/comments?post=72265"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/posts\/72265\/revisions"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/media?parent=72265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/categories?post=72265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/ro\/wp-json\/wp\/v2\/tags?post=72265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}