{"id":3757,"date":"2012-03-14T08:34:03","date_gmt":"2012-03-14T16:34:03","guid":{"rendered":"http:\/\/www.chesnok.com\/daily\/?p=3757"},"modified":"2012-03-26T02:50:52","modified_gmt":"2012-03-26T10:50:52","slug":"inheritance-and-sharding-with-postgres","status":"publish","type":"post","link":"https:\/\/www.chesnok.com\/daily\/2012\/03\/14\/inheritance-and-sharding-with-postgres\/","title":{"rendered":"Inheritance and sharding with Postgres"},"content":{"rendered":"<p>A friend told me about their <a href=\"http:\/\/en.wikipedia.org\/wiki\/Shard_(database_architecture)\">sharding<\/a> scheme last night, and it made me very curious about how others are handling this problem. This question about database design turns into a devops issue, so it&#8217;s something really the entire development group and devops and DBAs need to be aware of and concerned about. And it&#8217;s not a problem exclusive to Postgres.<br \/>\n<!--more--><br \/>\nThey&#8217;re using <a href=\"http:\/\/www.postgresql.org\/docs\/current\/static\/ddl-inherit.html\">Postgres&#8217; table inheritance<\/a> to constrain the properties of the sharded tables. And I&#8217;m deliberately using &#8216;sharding&#8217; because this ends up being a functional grouping, rather than, say, <a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/ddl-partitioning.html\">partitioning by date<\/a>. Groups of customers live on each shard, and can be moved around.<\/p>\n<p>In theory, this is awesome. Everything inherited is in lockstep, you never have to worry about one shard&#8217;s tables being different from any other shard. <\/p>\n<p>But that&#8217;s dubious, because you can change or add columns to child tables. The only columns that are constrained are the ones defined by the parent.<\/p>\n<p>And&#8230; the problems I&#8217;ve seen with this setup are when you need to make a schema change on a column that&#8217;s in a parent table. Typically, devs (and sometimes DBAs) give up, and just add columns to each shard&#8217;s table individually. Because they can&#8217;t get the downtime they need to modify the tables across all shards.<\/p>\n<p>In this case, we&#8217;re talking about 1024 tables for each sharded table, and an <a href=\"http:\/\/www.postgresql.org\/docs\/current\/static\/explicit-locking.html\">ACCESS EXCLUSIVE lock<\/a> needs to be acquired on them all before the change can be applied. <\/p>\n<p>There are some simple things one can do to get around this, but acquiring that lock is a significant undertaking on a busy system. In one case, the table being modified is an audit table. (why this is problematic, exercise for reader, etc) <\/p>\n<p>And I still have scars from working on a system that had 100k+ inherited tables.<\/p>\n<p>So, my thought was: <strong>just don&#8217;t use inheritance for sharded designs<\/strong>. For schema changes, not using inheritance gets you: <\/p>\n<ul>\n<li>Only one ACCESS EXCLUSIVE lock required at a time<\/li>\n<li>The ability to apply a change per-shard, instead of globally<\/li>\n<li>Preparation for the day when you move a shard to a separate system entirely<\/li>\n<\/ul>\n<p>If you&#8217;re using 9.0 or later, you can use <a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/sql-createtable.html\">CREATE TABLE &#8230; LIKE<\/a> instead of using INHERITS, if you&#8217;re deploying shards with SQL commands. <\/p>\n<p>Giving up inheritance is a pain because: <\/p>\n<ul>\n<li>Now you have to ensure that your tables remain in sync across shards without inheritance&#8217;s help (but again, dubious help!)<\/li>\n<li>You can no longer write queries against the parent table that will pull data from all child tables (but I&#8217;d say &#8211; that&#8217;s for your data warehouse, not your prod OLTP database)<\/li>\n<li>You&#8217;re no longer using inheritance, which is a pretty cool feature<\/li>\n<\/ul>\n<p>I&#8217;d really like to know what others are doing.  Tell me in the comments.<\/p>\n<p><strong>Some links you might be interested in<\/strong><\/p>\n<p>And relevant, but doesn&#8217;t mention  but about 5 months old: <a href=\"http:\/\/instagram-engineering.tumblr.com\/post\/10853187575\/sharding-ids-at-instagram\">Instagram&#8217;s sharding technique<\/a> <\/p>\n<p><a href=\"http:\/\/www.startuplessonslearned.com\/2009\/01\/sharding-for-startups.html\">Sharding for startups<\/a><\/p>\n<p><a href=\"http:\/\/blog.maxindelicato.com\/2008\/12\/scalability-strategies-primer-database-sharding.html\">Scalability Strategies Primer: Database Sharding<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How are people using inheritance with sharding? &hellip; <a href=\"https:\/\/www.chesnok.com\/daily\/2012\/03\/14\/inheritance-and-sharding-with-postgres\/\">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":[97,9],"tags":[661,551,647,624,550],"class_list":["post-3757","post","type-post","status-publish","format-standard","hentry","category-postgres","category-postgresql","tag-devops","tag-inheritance","tag-postgres","tag-postgresql","tag-sharding"],"_links":{"self":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/3757","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=3757"}],"version-history":[{"count":10,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/3757\/revisions"}],"predecessor-version":[{"id":3905,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/posts\/3757\/revisions\/3905"}],"wp:attachment":[{"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/media?parent=3757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/categories?post=3757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chesnok.com\/daily\/wp-json\/wp\/v2\/tags?post=3757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}