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 1hOsNC-0005O9-Dk for pgsql-bugs@arkaria.postgresql.org; Thu, 09 May 2019 23:22:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hOsNA-0007Kb-Q6 for pgsql-bugs@arkaria.postgresql.org; Thu, 09 May 2019 23:22:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hOsNA-0007Gq-F5 for pgsql-bugs@lists.postgresql.org; Thu, 09 May 2019 23:22:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hOsN7-0007BO-W5 for pgsql-bugs@lists.postgresql.org; Thu, 09 May 2019 23:22:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x49NMd9d024526; Thu, 9 May 2019 19:22:39 -0400 From: Tom Lane To: Alan Jackson cc: pgsql-bugs@lists.postgresql.org Subject: Re: inconsistent results querying table partitioned by date In-reply-to: References: Comments: In-reply-to Alan Jackson message dated "Thu, 09 May 2019 17:45:55 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <24524.1557444159.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 09 May 2019 19:22:39 -0400 Message-ID: <24525.1557444159@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alan Jackson writes: > Im having a problem with querying a table partitioned by date. > Depending on the sequence of operations on a date parameter used in a wh= ere clause, I either get the expected results, or no results. Yeah, this is pretty clearly broken. It looks to me like the partition pruning code is making insupportable assumptions about a comparison to a stable expression. Using your example table: regression=3D# explain select * from dataid where id=3D1 and datadatetime = < localtimestamp; = QUERY PLAN = = --------------------------------------------------------------------------= -------- Bitmap Heap Scan on dataid_default (cost=3D4.19..11.31 rows=3D3 width=3D= 12) Recheck Cond: ((id =3D 1) AND (datadatetime < LOCALTIMESTAMP)) -> Bitmap Index Scan on dataid_default_pkey (cost=3D0.00..4.19 rows=3D= 3 width=3D0) Index Cond: ((id =3D 1) AND (datadatetime < LOCALTIMESTAMP)) (4 rows) It should absolutely not have pruned away the dataid_201902 partition, but it did. It's okay with an immutable expression: regression=3D# explain select * from dataid where id=3D1 and datadatetime = < '2019-05-09'::timestamp; QUERY PLAN = = --------------------------------------------------------------------------= ------------------------------------ Append (cost=3D4.18..22.63 rows=3D6 width=3D12) -> Bitmap Heap Scan on dataid_201902 (cost=3D4.18..11.30 rows=3D3 wid= th=3D12) Recheck Cond: ((id =3D 1) AND (datadatetime < '2019-05-09 00:00:0= 0'::timestamp without time zone)) -> Bitmap Index Scan on dataid_201902_pkey (cost=3D0.00..4.18 r= ows=3D3 width=3D0) Index Cond: ((id =3D 1) AND (datadatetime < '2019-05-09 00:= 00:00'::timestamp without time zone)) -> Bitmap Heap Scan on dataid_default (cost=3D4.18..11.30 rows=3D3 wi= dth=3D12) Recheck Cond: ((id =3D 1) AND (datadatetime < '2019-05-09 00:00:0= 0'::timestamp without time zone)) -> Bitmap Index Scan on dataid_default_pkey (cost=3D0.00..4.18 = rows=3D3 width=3D0) Index Cond: ((id =3D 1) AND (datadatetime < '2019-05-09 00:= 00:00'::timestamp without time zone)) (9 rows) or a volatile one: regression=3D# explain select * from dataid where id=3D1 and datadatetime = < '2019-05-09'::timestamp + random()*'1 day'::interval; QUERY PLAN = = --------------------------------------------------------------------------= ---------------------------------------------- Append (cost=3D4.23..29.80 rows=3D6 width=3D12) -> Bitmap Heap Scan on dataid_201902 (cost=3D4.23..14.88 rows=3D3 wid= th=3D12) Recheck Cond: (id =3D 1) Filter: (datadatetime < ('2019-05-09 00:00:00'::timestamp without= time zone + (random() * '1 day'::interval))) -> Bitmap Index Scan on dataid_201902_pkey (cost=3D0.00..4.23 r= ows=3D10 width=3D0) Index Cond: (id =3D 1) -> Bitmap Heap Scan on dataid_default (cost=3D4.23..14.88 rows=3D3 wi= dth=3D12) Recheck Cond: (id =3D 1) Filter: (datadatetime < ('2019-05-09 00:00:00'::timestamp without= time zone + (random() * '1 day'::interval))) -> Bitmap Index Scan on dataid_default_pkey (cost=3D0.00..4.23 = rows=3D10 width=3D0) Index Cond: (id =3D 1) (11 rows) but somebody's confused about what can be done with stable expressions. While I'm on about it, this behavior is also insupportable: regression=3D# explain select * from dataid where id=3D1 and datadatetime = < '2018-05-09'::timestamptz ; QUERY PLAN = = --------------------------------------------------------------------------= ------------------------------ Bitmap Heap Scan on dataid_default (cost=3D4.18..11.30 rows=3D3 width=3D= 12) Recheck Cond: ((id =3D 1) AND (datadatetime < '2018-05-09 00:00:00-04':= :timestamp with time zone)) -> Bitmap Index Scan on dataid_default_pkey (cost=3D0.00..4.18 rows=3D= 3 width=3D0) Index Cond: ((id =3D 1) AND (datadatetime < '2018-05-09 00:00:00-= 04'::timestamp with time zone)) (4 rows) because timestamp-against-timestamptz comparison is inherently only stable; the pruning code is way exceeding its authority by supposing that a comparison that holds at plan time will hold at runtime, even with a constant comparison value. The reason for the difference in your results is that one expression is immutable and the other is only stable: regression=3D# explain verbose select = (('2019-02-26T00:00:00'::timestamp::timestamp at time zone 'America/New_Yo= rk' ) at time zone 'UTC' + '2 days'::interval) as workingdate, (('2019-02-26T00:00:00'::timestamp::timestamp at time zone 'America/New_Yo= rk' + '2 days'::interval) at time zone 'UTC') as notworkingdate; = QUERY PLAN = = --------------------------------------------------------------------------= --------------------------------------------------------------------------= ------------ Result (cost=3D0.00..0.01 rows=3D1 width=3D16) Output: '2019-02-28 05:00:00'::timestamp without time zone, timezone('U= TC'::text, ('2019-02-26 00:00:00-05'::timestamp with time zone + '2 days':= :interval)) (2 rows) the reason being that timestamptz + interval depends on the timezone setting (for some intervals) but timestamp + interval never does. Seems to be equally broken in v11 and HEAD. I didn't try v10. > I hope there is something simple I can change in the partition definitio= ns to work around this. Until we fix the bug, I think the best you can do is not use stable expressions in this context. regards, tom lane