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 1gM2Gg-00043z-PI for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Nov 2018 02:48:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gM2Gc-0003Yx-N3 for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Nov 2018 02:47:58 +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_SHA384:256) (Exim 4.89) (envelope-from ) id 1gM2Gc-0003Pn-Ff for pgsql-hackers@lists.postgresql.org; Mon, 12 Nov 2018 02:47:58 +0000 Received: from tama500.ecl.ntt.co.jp ([129.60.39.148]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gM2GO-0007lU-Ra for pgsql-hackers@lists.postgresql.org; Mon, 12 Nov 2018 02:47:53 +0000 Received: from vc1.ecl.ntt.co.jp (vc1.ecl.ntt.co.jp [129.60.86.153]) by tama500.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id wAC2laBB018324; Mon, 12 Nov 2018 11:47:36 +0900 Received: from vc1.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id CB777EA7871; Mon, 12 Nov 2018 11:47:36 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id C0E15EA7435; Mon, 12 Nov 2018 11:47:36 +0900 (JST) Received: from [127.0.0.1] (unknown [129.60.241.61]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id B971740066A; Mon, 12 Nov 2018 11:47:36 +0900 (JST) Subject: Re: BUG #15212: Default values in partition tables don't work as expected and allow NOT NULL violation References: <20181109170712.pfn55lab5j7oae4v@alvherre.pgsql> <29152.1541802819@sss.pgh.pa.us> From: Amit Langote Message-ID: Date: Mon, 12 Nov 2018 11:47:27 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <29152.1541802819@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-CC-Mail-RelayStamp: 1 To: Tom Lane , Alvaro Herrera Cc: =?UTF-8?Q?J=c3=bcrgen_Strobel?= , Amit Langote , Ashutosh Bapat , PostgreSQL Hackers X-TM-AS-MML: disable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018/11/10 7:33, Tom Lane wrote: > Alvaro Herrera writes: >> On 2018-Nov-09, Jürgen Strobel wrote: >>> Regarding your example, what I expected is that *both* inserts would >>> consistently result in a tuple of (1, 42) since p should route the >>> insert to p1 and use p1's defaults. The current inconsistent behavior is >>> the heart of the bug. > >> I think that would be sensible behavior, as long as the partition >> doesn't override values for the partitioning column -- i.e. if the >> default values don't re-route the tuple to another partition, I think we >> should use the partition's default rather than the parent. This says we >> should expand defaults after routing. > > I'd argue not, actually. I think there is plausible precedent in > updatable views, where what we use is the defaults associated with the > view, not the underlying table. Correspondingly, what ought to govern > in a partitioned insert is the defaults associated with the table actually > named in the insert command, never mind what its partitions might say. > That is useful for many situations, and it avoids all the logical > inconsistencies you get into if you find that the defaults associated > with some partition would force re-routing elsewhere. > > In any case, you can't make this happen without basically blowing up > default processing altogether. Defaults are currently expanded by the > planner, and pretty early too. To make it work like the OP wishes, > we'd have to insert them sometime late in the executor. Considering multi-level partitioning, that'd mean the tuple being routed would need to be filled with the defaults of every table on the way to a leaf partition, including that of the leaf partition where the tuple finally ends up. If we re-route upon the final leaf partition's defaults leading to partition constraint violation, it's possible that someone might end up setting up an infinite re-routing loop with that behavior. create table p (a int) partition by list (a); create table p1 partition of p (a default 2) for values in (1); create table p2 partition of p (a default 1) for values in (2); It won't be fun for the server to try to prevent that kind of thing. IOW, it might be a good idea to call the ability to set partition-level defaults a deprecated feature? Thanks, Amit