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 1gjXcv-0001hK-3v for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jan 2019 22:56:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjXcr-0002Ml-NT for pgsql-bugs@arkaria.postgresql.org; Tue, 15 Jan 2019 22:56:05 +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 1gjXcr-0002Md-Cc for pgsql-bugs@lists.postgresql.org; Tue, 15 Jan 2019 22:56:05 +0000 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjXcn-0000qt-8D for pgsql-bugs@lists.postgresql.org; Tue, 15 Jan 2019 22:56:03 +0000 Received: by mail-qt1-x844.google.com with SMTP id l12so4982299qtf.8 for ; Tue, 15 Jan 2019 14:56:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=Lyft1TTa7upc1qGQLd6zU3Y8cReTvur9TBHt7+v3vrM=; b=nwigtg9mLdcpHljRBltJ/YRjeVsR76ykNrcYMyo3f85uCBhoTqj2tuffQbExHSHSlr VONyYIgVTqU4ieEP1q291PXf1+THITXmoTxtLF67ZJbHNcZiWfB9gtZ/YvhfAHm45SIr U/T5GVbu1yK9Gg3xF48WEF0IthnfPkUTWgUdvELiF7jQAP65mBaKTYwKpK3hJ1kO+MWE On+w8iDS+3NrF0ic3UCnB5ZkEJvKXhf6PolfMYqLY47hf5FkEwEvuBSJzmhE/tMUNFYD /1bRcUKNmto1Hd6J3g4tAAr/L8oD+s6ibACgEd+twF4qWhwD2BOcdy0ZuOO3+WKEHxQ/ jIlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=Lyft1TTa7upc1qGQLd6zU3Y8cReTvur9TBHt7+v3vrM=; b=PMDTS6nrSSYChYZJu/CQrGLiIVxL9/E44AxNZIY0HfsVgFY08WX/QY9M/r7PA1v4Fs n2tLSqo3yOSbeD2VKjBwgMEGTUs93e5KkNGWwUUYnRupAxTaiXKZsW5DF+hqKIp9kCZs ruHhNhNQhQvmbWGofEiTTkYiXOkFCOSOmh0heIa1AWqzMvj9XnxhvsMPoxY3TbHUtsiZ YtQjewtXrWrLoWmn1KMW2QrUL5xgWbHUkDBJHJejvXYqMQ1MPm0RfLpnZYjPSAG/yO5B HEN2q4iX6tseYHN6jTT8EZ69C8R3itSkN3ZcyTrWrkmi1iEBjjS/v4Fhl0RslsW5pryo JnZQ== X-Gm-Message-State: AJcUukcC6uGxds+XcogvSRabqaNIrVtpbyL6l+115VZ9b0+qgjEXqGua V+2OuBtrhMw9fBNWkBs1FBq+qLfmcOGtb9LErZMCxCK8waJA7bepuU3TjRoFrqA3ckDpfFkIfd9 QmmWoTaqsm7/21YCDKNVjEvRQhwxH8UC6/OzgQt+SN/8MarV9VWAXOLzfXmUsuBpF4TpTwq+sUD umP59+FQCmKA== X-Google-Smtp-Source: ALg8bN6RcOSS5cBpDYIZ/+NS16qBCFpTB7oznfbmRSaIyLxKdzy13SwlCVSun5JRZowAxmGPFroNlA== X-Received: by 2002:ac8:4258:: with SMTP id r24mr4873366qtm.213.1547592959259; Tue, 15 Jan 2019 14:55:59 -0800 (PST) Received: from alvin.alvh.no-ip.org ([201.186.80.67]) by smtp.gmail.com with ESMTPSA id i73sm40367054qke.80.2019.01.15.14.55.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 15 Jan 2019 14:55:58 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id CA0E7C41; Tue, 15 Jan 2019 19:55:56 -0300 (-03) Date: Tue, 15 Jan 2019 19:55:56 -0300 From: Alvaro Herrera To: Tom Lane Cc: Jesper Pedersen , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15587: Partitions with ALTER TABLE ADD CONSTRAINT Message-ID: <201901152255.cklz3mhytriv@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <201901151935.zfadrzvyof4k@alvherre.pgsql> User-Agent: NeoMutt/20180716-1013-2c1e88 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Jan-15, Alvaro Herrera wrote: > What was on my head ("can we add ONLY to ADD FOREIGN KEY?") was the idea > that it'd be useful to construct the foreign keys in partitions, one by > one, and as a final step you construct a foreign key in the partitioned > table and then attach each FK in partition to the master one. Right > now, adding the foreign key in the parent table just creates duplicates > in the partitions, which is silly. I had put this aside and started reviewing Amit's patch 0002 here https://postgr.es/m/f2b8ead5-4131-d5a8-8016-2ea0a31250af@lab.ntt.co.jp when I realized that this is already implemented ... for the case where we attach a new partition, and the new partition already contains the constraint. The case of creating a constraint from scratch is just doing the recursion badly and not checking for pre-existing matching constraints, which is why it ends up with a dupe. Fixing it is pretty simple -- we just need to call clone_fk_constraints() with only the constraint being created, and everything works correctly as far as I can tell. The only issue is that clone_fk_constraints is a static function in pg_constraint.c, so I'd have to export it for use in tablecmds.c ... or I could just apply patch 0002 that I posted here https://www.postgresql.org/message-id/20181130191216.5xcxcsx3ascgqayv@alvherre.pgsql which takes care precisely of moving that function to tablecmds.c (with a better name, too). -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services