{"id":4963,"date":"2013-08-16T11:46:46","date_gmt":"2013-08-16T19:46:46","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=4963"},"modified":"2013-08-16T11:52:09","modified_gmt":"2013-08-16T19:52:09","slug":"catalog-sql-friday-using-do-when-weve-got-tables-as-variables","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2013\/08\/16\/catalog-sql-friday-using-do-when-weve-got-tables-as-variables\/","title":{"rendered":"Catalog SQL Friday: using DO when we&#8217;ve got tables as variables"},"content":{"rendered":"<p>Just a quick note about modifying constraints:<\/p>\n<p>There&#8217;s no such thing as <code>ALTER CONSTRAINT<\/code>. So, if you want to safely change a <code>CHECK<\/code> constraint, like on a partition, you need to <code>DROP<\/code> and <code>ADD<\/code> it in a single transaction.<\/p>\n<p>Below is a snippet for finding partitions, their CHECK constraints based on a <code>WHERE<\/code> clause. Then we <code>DROP<\/code> the existing constraint and add back the correct constraint. It doesn&#8217;t take much sleuthing to figure out what the problem was. \ud83d\ude42<\/p>\n<pre><code> DO $$\n  DECLARE myrecord record;\n  DECLARE theweek text;\n  BEGIN\n    FOR myrecord IN SELECT relname, conname from pg_constraint\n      JOIN pg_class ON pg_constraint.conrelid = pg_class.oid\n      WHERE consrc ~ 'without' and split_part(relname, '_201', 1)\n      IN (select table_name from report_partition_info\n      WHERE partition_column = 'date_processed') LIMIT 1\n    LOOP\n       EXECUTE 'ALTER TABLE ' || quote_ident(myrecord.relname)\n       || ' DROP CONSTRAINT IF EXISTS '\n       || quote_ident(myrecord.conname) || ';';&lt;\/p&gt;\n\n   theweek = substring(myrecord.relname from '........$');\n\n   EXECUTE 'ALTER TABLE ' || quote_ident(myrecord.relname)\n   || ' ADD CONSTRAINT ' || quote_ident(myrecord.conname)\n   || ' CHECK ((date_processed &gt;= timestamptz('\n   || quote_literal(to_char(date(theweek), 'YYYY-MM-DD')) || '))'\n   || ' AND (date_processed &lt; timestamptz('\n   || quote_literal(to_char(date(theweek) + 7, 'YYYY-MM-DD'))\n   || ')));';\n\n   RAISE NOTICE 'DONE: %', myrecord.relname;\nEND LOOP;\n\nEND$$; \n<\/code><\/pre>\n<p>Here&#8217;s the <a href=\"https:\/\/gist.github.com\/selenamarie\/fc4588ff594576f86982\">gist version for easier reading<\/a>.<\/p>\n<p>The couple things I learned in this process was a nice feature in <code>substring()<\/code> allowing me to return the date portion of my partition names easily, and <code>split_part()<\/code> which allowed me to return the parent table name and compare it to my list of partitionable tables for the specific partition column. I recently added support for partitioning on a different column for certain tables, so I have to differentiate for this fix. The <a href=\"http:\/\/www.postgresql.org\/docs\/9.2\/static\/functions-string.html\">string function docs<\/a> are pretty great.<\/p>\n<p>I didn&#8217;t do any optimization of this &#8212; just got it working and am now testing it in our stage environment. The final script is going to perform the changes on a month&#8217;s worth of partitions at a time to help reduce the chance of deadlocking.<\/p>\n<p>If you have thoughts on how I could have done this more efficiently, let me know in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just a quick note about modifying constraints: There&#8217;s no such thing as ALTER CONSTRAINT. So, if you want to safely change a CHECK constraint, like on a partition, you need to DROP and ADD it in a single transaction. Below &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2013\/08\/16\/catalog-sql-friday-using-do-when-weve-got-tables-as-variables\/\">Continue reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-4963","post","type-post","status-publish","format-standard","hentry","category-postgresql"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/comments?post=4963"}],"version-history":[{"count":10,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4963\/revisions"}],"predecessor-version":[{"id":5009,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/4963\/revisions\/5009"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=4963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=4963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=4963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}