Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l0DLF-0000UQ-VI for pgsql-general@arkaria.postgresql.org; Fri, 15 Jan 2021 00:51:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l0DLE-00047N-Nb for pgsql-general@arkaria.postgresql.org; Fri, 15 Jan 2021 00:51:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l0DLE-00047G-9G for pgsql-general@lists.postgresql.org; Fri, 15 Jan 2021 00:51:52 +0000 Received: from mail-pj1-x1035.google.com ([2607:f8b0:4864:20::1035]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l0DL9-00020f-VQ for pgsql-general@postgresql.org; Fri, 15 Jan 2021 00:51:51 +0000 Received: by mail-pj1-x1035.google.com with SMTP id cq1so4092673pjb.4 for ; Thu, 14 Jan 2021 16:51:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=/+XCRdAMmNFbPWhYkaGVBI5XwtNEWLtbZMl3UTFfaM4=; b=pkOW0vWjbrEYIfFvBC60f6/ALgh22hpWZsSwD+uZ9AWs9p5XshaTCIrCshIwNA+GuW 7Z1QrIlMX4/eKhQPG0yBpMByLjOkMyv2Azd0Xf/o1hQEZDwJ9g16oco3UErj8dXf7PVz Fkz8FqoLZ7k3Iig8rkuy9IkaeLHhw9fDipBzKfJUC7bHY0Z2XaQQaeyk42h3tpqJofct PWXwy3kVhY6sozVPS8vRZX3UB6ZXSOYWCK9Ht3URCqpWWyO+I4pTAANyH4N7jXUTF608 TZm2AfATre6vnjL9fDLeVYi+eLSjF3BBeJBVBUrFt+FF3XH9zriWkc1R5IToY/GX76/3 KDFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=/+XCRdAMmNFbPWhYkaGVBI5XwtNEWLtbZMl3UTFfaM4=; b=Cm0bJqY7+ASH6PjSxiCjxRzjRtXLisoEiW1UeGEkO20tsOe2pp+5LKQ4EFm8a5n5Dr aWuClKlUr8hzT40ufwmXQuNxra/K9C8KU29AqDM9ubfj3WYfALuyzIXUpqxyjyNiGdfS YWFdgF2AaCxohCP5/bohcX5pkUJrAp9UhA+nk2miKhhz6+XqYFaIhIaS4djgDZ/kNV+i RdFJ6xyt8auCY7Yayv1ocdmuW5NJmxyPMbBIJoBgw6LRyBWTNilYXReK+uesEU/jvcOG 0MuVH2cYSrHL5nkUEUucXe8020f86BmYyjXFNozsV1dQkFdpwjuCJmloOXgibymIlvRD lWJA== X-Gm-Message-State: AOAM532w8Z9aMU1vmTtZx3NwJBjE4dejP9iqxpnCphuhr+/Pw5J/FmDO NwnXvYdHIgie5N1vvQ+hxVo= X-Google-Smtp-Source: ABdhPJwozdXvSc3GxHNcvTQ/R2sj0/fK7UUaX/ycGKxzLKNNe5MC41Ffrh36mPOIBbmY6L6IjbfVPw== X-Received: by 2002:a17:90a:5287:: with SMTP id w7mr7841821pjh.17.1610671905819; Thu, 14 Jan 2021 16:51:45 -0800 (PST) Received: from [192.168.1.101] (27-32-247-51.tpgi.com.au. [27.32.247.51]) by smtp.gmail.com with ESMTPSA id t1sm6201089pfq.154.2021.01.14.16.51.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Jan 2021 16:51:45 -0800 (PST) Message-ID: Subject: Re: Strange (and good) side effect of partitioning ? From: rob stone To: Phil Florent , "pgsql-general@postgresql.org" Date: Fri, 15 Jan 2021 11:51:41 +1100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.38.2-1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On Thu, 2021-01-14 at 20:48 +0000, Phil Florent wrote: > Hi, > > I read that on Jonathan Lewis' blog : > > (I believe that there may be some RDBMS which will treat (e.g.) “X > between 20 and 10” as being identical to“X between 10 and 20” ) > > I am puzzled. PostgreSQL seems NOT to treat X between 20 and 10” as > being identical to“X between 10 and 20" but it's complicated. > > Here is my test case: > > select version();                                                    >          version                                                     >           > --------------------------------------------------------------------- > ------------------------------------------------------------- >  PostgreSQL 13.1 (Ubuntu 13.1-1.pgdg20.04+1) on x86_64-pc-linux-gnu, > compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit > > create table t1 (rn integer , object_name text) partition by > range(rn); > > create table t1a partition of t1 for values from (1) to (50001); > > > \d+ t1                                         Table partitionnée « > public.t1 » >    Colonne   |  Type   | Collationnement | NULL-able | Par défaut | > Stockage | Cible de statistiques | Description > -------------+---------+-----------------+-----------+------------+-- > --------+-----------------------+------------- >  rn          | integer |                 |           |            | > plain    |                       | >  object_name | text    |                 |           |            | > extended |                       | > Clé de partition : RANGE (rn) > Partitions: t1a FOR VALUES FROM (1) TO (50001) > > > > insert into t1 select                                    rownum  rn, >         upper(md5(random()::text)) object_name > from >         (select generate_series(1,50000) rownum) serie > ; > > explain analyze select  object_namefrom    t1 > where >         rn between 20 and 10 > ; >                                      QUERY PLAN                       >               > --------------------------------------------------------------------- > --------------- >  Result  (cost=0.00..0.00 rows=0 width=0) (actual time=0.002..0.002 > rows=0 loops=1) >    One-Time Filter: false >  Planning Time: 0.116 ms >  Execution Time: 0.020 ms > > It's OK but: > > explain analyze select  object_namefrom    t1a > where >         rn between 20 and 10 > ; >                                            QUERY PLAN                 >                             > --------------------------------------------------------------------- > ---------------------------- >  Seq Scan on t1a  (cost=0.00..1167.00 rows=1 width=33) (actual > time=6.553..6.553 rows=0 loops=1) >    Filter: ((rn >= 20) AND (rn <= 10)) >    Rows Removed by Filter: 50000 >  Planning Time: 0.092 ms >  Execution Time: 6.573 ms > > At first I thought it was related to partition pruning but: > > set enable_partition_pruning = false; > > explain analyze select  object_namefrom    t1 > where >         rn between 20 and 10 > ; > >                                      QUERY PLAN                       >              -------------------------------------------------------- > ---------------------------- >  Result  (cost=0.00..0.00 rows=0 width=0) (actual time=0.002..0.003 > rows=0 loops=1) >    One-Time Filter: false >  Planning Time: 0.104 ms >  Execution Time: 0.021 ms > > > Confirmation since I still obtain "One-Time Filter: false" if I don't > filter on the partition key: > > create table t2 (rn integer , rn2 integer, object_name text) > partition by range(rn); > > create table t2a partition of t2 for values from (1) to (50001); > > d+ t2                                         Table partitionnée « > public.t2 » >    Colonne   |  Type   | Collationnement | NULL-able | Par défaut | > Stockage | Cible de statistiques | Description > -------------+---------+-----------------+-----------+------------+-- > --------+-----------------------+------------- >  rn          | integer |                 |           |            | > plain    |                       | >  rn2         | integer |                 |           |            | > plain    |                       | >  object_name | text    |                 |           |            | > extended |                       | > Clé de partition : RANGE (rn) > Partitions: t2a FOR VALUES FROM (1) TO (50001) > > insert into t2 select                                                >   rownum  rn, rownum rn2, >         upper(md5(random()::text)) object_name > from >         (select generate_series(1,50000) rownum) serie > ; > > explain analyze select  object_namefrom    t2 > where >         rn2 between 20 and 10 > ; > > >                                      QUERY PLAN                       >              -------------------------------------------------------- > ---------------------------- >  Result  (cost=0.00..0.00 rows=0 width=0) (actual time=0.002..0.003 > rows=0 loops=1) >    One-Time Filter: false >  Planning Time: 0.185 ms >  Execution Time: 0.019 ms > > I don't understand why I don't obtain " One-Time Filter: false" with > a classic table or a partition ? > > Best regards, > > Phil See table 9.2 in the documentation. BETWEEN 10 AND 20 returns TRUE. BETWEEN 20 AND 10 returns FALSE. BETWEEN SYMMETRIC 20 AND 10 returns TRUE. HTH,