Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 317429FB322 for ; Fri, 18 May 2007 19:41:31 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 61921-04 for ; Fri, 18 May 2007 19:41:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by postgresql.org (Postfix) with ESMTP id 45DD59FA104 for ; Fri, 18 May 2007 19:41:27 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id BE72630AE2; Sat, 19 May 2007 00:41:25 +0200 (MET DST) From: C Storm X-Newsgroups: pgsql.performance Subject: Efficient recursion Date: 18 May 2007 15:41:24 -0700 Organization: http://groups.google.com Lines: 39 Message-ID: <1179528084.715044.215690@e65g2000hsc.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Complaints-To: groups-abuse@google.com User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3; Google-TR-3) Gecko/20070309 Firefox/2.0.0.3,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: e65g2000hsc.googlegroups.com; posting-host=209.209.36.196; posting-account=8cjn1A0AAABCEc9psZti4Q2gyhLtFgqt To: pgsql-performance@postgresql.org X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL=0.004, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 200705/366 X-Sequence-Number: 24756 This may be the wrong list to post to but I thought I'd post here first since it is a performance related problem. Essentially, I'm looking for the most efficient way to break a database into two 'shards' based on a top level table's primary key. For example, split a sales database into two using a territory. The database rows are constrained to have unique ownership, meaning each row of data can be traced to one and only one shard, e.g., sales territory. Multiple inheritance would make this a harder thing to do. In essence, I'd like to do a cascading delete using one of these territory's ids with the caveat that the data isn't just deleted but deleted and COPYed out to disk. This COPYed data could then be loaded into a fresh schema to bring up the second shard. Seemingly the data would be in the right insert order, for referential integrity purposes, as a result of this operation since it would be doing a breadth first search for the data. I can envision a couple different ways to do this: a) Gather the relational tree up to but not including the leaves and use it to parse out the shard from a db dump. Then do a cascading delete to remove the data from the database. b) Recursively COPY (query) to a file (breadth first COPY) while crawling down the relational tree. The complications I see are having to make sure the referential tree is a DAG (directed acyclic graph) or unroll it to become one. I know Live Journal, Skype, etc. have to do this sort of thing when they need to scale and didn't want to reinvent the wheel or, more importantly, step on the same land mines that others have stepped on. Thanks for any and all feedback. Christian