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 1oR9em-00069Q-Kw for pgsql-performance@arkaria.postgresql.org; Thu, 25 Aug 2022 10:00:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oR9ek-0000HQ-Mw for pgsql-performance@arkaria.postgresql.org; Thu, 25 Aug 2022 10:00:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oR9ek-0000HH-99 for pgsql-performance@lists.postgresql.org; Thu, 25 Aug 2022 10:00:10 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oR9eh-0005p6-G1 for pgsql-performance@lists.postgresql.org; Thu, 25 Aug 2022 10:00:09 +0000 Received: by mail-ed1-x534.google.com with SMTP id b16so25424362edd.4 for ; Thu, 25 Aug 2022 03:00:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc; bh=n5F4/Xxrw0Kdcf/9QpAKZ7pY9DyczKWXU6FfJnTQM3s=; b=eU07eOY9ENUZqhj4xTREjf91ZQoiWrbA4CbRcHg6X2oIevwnB2YoOTPduwoyqYRVeG PWtQHs32sYJC1iBrRPPz8eV+5JhvgQn791i9tEQg2S3sHBeraBbeJsUwGTID4MNhb2D2 9QZ0Gdw52FDucZlarnE5x6PjE7POOM56zyZHaVOPYb/H9G+V9pdtU+ycDCMX3F4c2sYs Kef/PZ8YzHnlrHAz68fRZPzcrPrEqrPB9R9OXnDJ8WVuNCK/iFUEPDOj2TFH3xl4hdJd B30yfbl9IZtO8xrfkclOXby1Yj4W6WzXXOvSytR4cQpusKBSslPqLqeOHyvoWkjFOTzC QWag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc; bh=n5F4/Xxrw0Kdcf/9QpAKZ7pY9DyczKWXU6FfJnTQM3s=; b=d/D+Lo7vMFuDIVktck7OwBaztK727vsOe5GcS0xK7jzMPzjJsW+9iOIibzspRu9+/+ yDHQoO2c4slnqrhFiye65/ZTqk8mONL/1LSR1JUNuuMqjR+clls+T+bn6uGO3Ls6VKcj M9yk3sLq85X+PSJERg2HGegO9jEfG1rY/Ss5xg75cJgKoM3F4yFBDjYRFYKyar2Ez3E8 xP2+P8iG9U98/Wy5Z/hmbCJeiIC4jkWPh/9+AcZhjr19ZSL9lPcZcv2YxfCwskMaGF05 5wuMKnxu+konQAmGV94z2WkQQEDjRnacx6pRCO9st4L4uZPPvzL0j0hnlxvPNK51Glh2 nfnA== X-Gm-Message-State: ACgBeo09PZIIAHbfz+ODyc5Cwu2Udb52t1Xnphk5jeiZshObbLhRNfX2 r1e53GkFSRS6ekVRPuXsm47CbQ== X-Google-Smtp-Source: AA6agR556ry0xEY6EdgsIYbW+rQFcx0GfgpqBQI3o+A0Zz2T5fYQpFk/pf3rcnBpQeFqr9XtEcAOCg== X-Received: by 2002:a05:6402:1704:b0:447:811f:1eef with SMTP id y4-20020a056402170400b00447811f1eefmr2589566edu.14.1661421605579; Thu, 25 Aug 2022 03:00:05 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:e9a8:d89:4e2a:5b5b:3a79]) by smtp.gmail.com with ESMTPSA id p5-20020a17090653c500b007305d408b3dsm2248696ejo.78.2022.08.25.03.00.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 03:00:04 -0700 (PDT) Message-ID: Subject: Re: Select on partitioned table is very slow From: Laurenz Albe To: depesz@depesz.com, Jose Osinde Cc: pgsql-performance@lists.postgresql.org Date: Thu, 25 Aug 2022 12:00:04 +0200 In-Reply-To: <20220825091014.GA26288@depesz.com> References: <20220825091014.GA26288@depesz.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.4 (3.42.4-2.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2022-08-25 at 11:10 +0200, hubert depesz lubaczewski wrote: > Hi, > > On Thu, Aug 25, 2022 at 10:49:51AM +0200, Jose Osinde wrote: > > select logical_identifier, version_id, lastproduct > >    from test_product_ui_partition.product_ui pui > >     where pui.mission_id='urn:esa:psa:context:investigation:mission.em16' > >       and pui.logical_identifier='urn:esa:psa:em16_tgo_frd:data_raw:frd_raw_sc_n_20220729t000000-20220729t235959' > > > EXPLAIN ANALYZE FROM PGADMIN > > > > Index Scan using product_ui_em16_logical_identifier_idx on > > product_ui_em16 pui  (cost=0.69..19.75 rows=7 width=112) (actual > > time=0.133..0.134 rows=1 loops=1) > > [...]  Index Cond: (logical_identifier = > > 'urn:esa:psa:em16_tgo_frd:data_raw:frd_raw_sc_n_20220729t000000-20220729t235959'::citext)" > > [...]  Filter: (mission_id = > > 'urn:esa:psa:context:investigation:mission.em16'::citext)" > > Planning Time: 0.237 ms > > Execution Time: 0.149 ms > > I really wish you didn't butcher explains like this, but we can work > with it. > > Please note that the condition for filter is: > > mission_id = 'urn:esa:psa:context:investigation:mission.em16'::citext > > Specifically, column mission_id (which is partition key) is compared > with some value that is in citext type - same as column. > This means that pg can take this value, compare with partitioning > schema, and pick one partition. > > Now look at the explain from java: > > >               Filter: (((mission_id)::text = > > 'urn:esa:psa:context:investigation:mission.em16'::text) AND > > The rest is irrelevant. > > The important part is that java sent query that doesn't compare value of > column mission_id with some value, but rather compares *cast* of the > column. > > Since it's not column value, then partitioning can't check what's going > on (cast can just as well make it totally different value), and it also > can't really use index on mission_id. > > Why it happens - no idea, sorry, I don't grok java. > > But you should be able to test/work on fix with simple, non-partitioned > table, just make there citext column, and try searching for value in it, > and check explain from the search. If it will cast column - it's no > good. > > Sorry I can't tell you what to fix, but perhaps this will be enough for > you to find solution. Quite so. You are probably using a prepared statement in JDBC. You probably have to use explicit type casts, like: select logical_identifier, version_id, lastproduct from test_product_ui_partition.product_ui pui where pui.mission_id = ? :: citext and pui.logical_identifier = ? :: citext Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com