Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j06M2-0006E1-IT for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2020 16:19:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1j06M1-0001Z9-Av for pgsql-bugs@arkaria.postgresql.org; Fri, 07 Feb 2020 16:19:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1j06M0-0001Z2-NN for pgsql-bugs@lists.postgresql.org; Fri, 07 Feb 2020 16:19:41 +0000 Received: from mail.dalibo.com ([212.85.157.172]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1j06Lx-00089n-1Q for pgsql-bugs@lists.postgresql.org; Fri, 07 Feb 2020 16:19:39 +0000 Received: from firost (unknown [88.190.0.6]) by mail.dalibo.com (Postfix) with ESMTPSA id A2BD01F8ED; Fri, 7 Feb 2020 17:19:34 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=dalibo.com; s=a; t=1581092374; bh=u9Y4DjUaK+I25s8JmgWZemQcN0KKGO0LPr0PphSq9tc=; h=Date:From:To:Subject:In-Reply-To:References:From; b=AjvYKNc7Ej985uVKcM/ZHCCuqqDWe/AhZ/+YdzfLwO0Vgu3Ho+wcwtJu+fDr9Q4d4 sR1Hp121FjTW74z9I2o8PD2RFjTHoNj2Ol094xu3qJrw5ic18/F0cbRYoIoj8vbkNc CERtjp6opHkHvnCGX2mqWLX47zlNbynS+FB9wagA= Date: Fri, 7 Feb 2020 17:19:33 +0100 From: Jehan-Guillaume de Rorthais To: Alvaro Herrera , pgsql-bugs@lists.postgresql.org Subject: Re: FK violation in partitioned table after truncating a referenced partition Message-ID: <20200207171933.77aaaba6@firost> In-Reply-To: <20200207150432.GA21902@alvherre.pgsql> References: <20200207113203.3e26b28d@firost> <20200207150432.GA21902@alvherre.pgsql> Organization: Dalibo MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 7 Feb 2020 12:04:32 -0300 Alvaro Herrera wrote: > On 2020-Feb-07, Jehan-Guillaume de Rorthais wrote: > > > Well, when reading myself, I found a bug in my algorithm. When looking for > > parent constraints harvested during the first loop, I wasn't looking on > > pg_contraint.oid, but on conparentid again. So instead of gathering parent > > constraints to add the parent relation to the list of oids, I was only > > adding siblings constraints. Here the fix: > > > > ScanKeyInit(&key, > > - Anum_pg_constraint_conparentid, > > + Anum_pg_constraint_oid > > BTEqualStrategyNumber, F_OIDEQ, > > ObjectIdGetDatum(parent)); > > > > - fkeyScan = systable_beginscan(fkeyRel, ConstraintParentIndexId, > > + fkeyScan = systable_beginscan(fkeyRel, ConstraintOidIndexId, > > true, NULL, > > 1, &key); > > Doh, of course. I should have seen that. > > Here's another take at the formulation; IMO the loop is more obvious > this way, with a flag to restart from the top rather than keeping track > of the list length. But essentially this is your algorithm. Yes, I recognize my algo with some cosmetic improvements, this obvious restart flag I should have thought about and some welcomed code comments. I agree this is more clear. Thanks! Maybe I would just add: /* * If this constraint has a parent constraint which we have not seen * yet, keep track of it for the second loop, below. + * Tracking parent constraint allows to climb up to the top-level + * level constraint and look for all possible relation referencing + * the partioned table. */ > I couldn't find any fault in this. great! > It would be nice if the cascaded truncation was more precise, ie. only > truncate the referencing partitions that overlap the ranges covered by the > referenced partition being truncated. Yes, I was wondering about that when I was working on the first version of the patch. It seems like a dedicated partitioning syntax when looking at other RDBMSs. Eg. "PARTITION BY REFERENCE (col)" and "TRUNCATE PARTITION": https://oracle-base.com/articles/12c/cascade-functionality-for-truncate-partition-and-exchange-partition-12cr1 > But that seems more difficult to achieve, as well as less clearly defined; if > you really want something like that, I think you can detach the referenced > partition. This is out of the scope of this bug fix in my humble opinion. This would be a whole new feature, even if it could be done without a new syntax. Regards,