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 1kVUDm-0005TQ-0Z for pgsql-performance@arkaria.postgresql.org; Thu, 22 Oct 2020 06:37:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kVUDk-0005sv-U5 for pgsql-performance@arkaria.postgresql.org; Thu, 22 Oct 2020 06:37:08 +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 1kVUDk-0005sn-JD for pgsql-performance@lists.postgresql.org; Thu, 22 Oct 2020 06:37:08 +0000 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kVUDh-0006Vh-IV for pgsql-performance@postgresql.org; Thu, 22 Oct 2020 06:37:08 +0000 Received: by mail-io1-xd42.google.com with SMTP id q25so422249ioh.4 for ; Wed, 21 Oct 2020 23:37:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=j8pMORO5F+hXsiDVJAB6tCGzpdo5W2/ZrieXKa5XlM4=; b=fIO2hUt6ZF6t/fNtbbb1G13rFzhhYEc7++7hW4knTq0kiweUHnG13PSMjY80nzgFWT DdVLkj/pzMV/iA1AegxxdF7XIOoRikbu7uGu9myBcoYnee9qiyND1KLu7/IoISicBiYt GHg104kZwHnfN6xxGd/ghpRCGG6dqi9sy46aELYcQuScK9AgdDLi4Wwq8rTfsBXjfm5H r4o8DkPX6Ew77/OKNbL27NGZyrtFlLQiAC/EdLwZrFvSgPp7MAhNgFdiSISMOS6vK5or BiTRl7l/ewobyH8X66GrIhgtcJn8dRIU/dBRCJgMt5QUhjB8ekhAsbxoJTY36CJcXa96 xRDA== 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:references :mime-version:content-disposition:in-reply-to:user-agent; bh=j8pMORO5F+hXsiDVJAB6tCGzpdo5W2/ZrieXKa5XlM4=; b=PdInL48qd83R72Yh2rQQJYaN7o0cB1G8HsfS6sG0ZPfEpAQekt+e5woQKg919wzMYm nbZ14PGKAGIbNRU3GxXJ/3rE/xaa4HQqsFxsA+s4/vj7welAeh7yuoHZnuoow3/I/o2A NRf4/f0T86eegtlLik7hKDT12iK9FrIwfyBtfno9zri+LAKrGEVwH3mP3AeJiMWwgvDW 9U6p3Zp+kIm1sotMPgaUcF8KRA9rrCUn59Wn6tbEtHEtz3DxLHxAhY6vKfKxzkJ156CH TWUkb1obSgsJ1rEAk1dY056a+PtBmS+l9VOo4FG4ZX0n3EQ7ohZxumN0kLNNX/QZCxGw pHaQ== X-Gm-Message-State: AOAM530Ao55FvS/m0nocLSYNgamQ59xYCJA+oiXpPEuuJBPFWgybGymf UyxRK9cjH+bYA9SeAZ5m3kextQ== X-Google-Smtp-Source: ABdhPJxV+R2dUJt5or1k7vE+d+TUr0Bc7gOQieUdrmLUaYmolV/gi02istY/1LndFyD1SbcNalS5CQ== X-Received: by 2002:a5d:87c7:: with SMTP id q7mr799201ios.162.1603348623819; Wed, 21 Oct 2020 23:37:03 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id i82sm533199ill.84.2020.10.21.23.37.02 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Oct 2020 23:37:03 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id F3EB68008E3; Thu, 22 Oct 2020 01:37:01 -0500 (CDT) Date: Thu, 22 Oct 2020 01:37:01 -0500 From: Justin Pryzby To: Mats Olsen Cc: Sebastian Dressler , "pgsql-performance@postgresql.org" Subject: Re: Query Performance / Planner estimate off Message-ID: <20201022063701.GU9241@telsasoft.com> References: <61c9fd35-bdb8-3fe6-bda0-81c7e6a9eaf8@duneanalytics.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <61c9fd35-bdb8-3fe6-bda0-81c7e6a9eaf8@duneanalytics.com> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Wed, Oct 21, 2020 at 04:42:02PM +0200, Mats Olsen wrote: > On 10/21/20 2:38 PM, Sebastian Dressler wrote: > > > On 20. Oct 2020, at 11:37, Mats Julian Olsen > > > > > [...] > > > > > > 1) Vanilla plan (16 min) : https://explain.depesz.com/s/NvDR > > > > > > 2) enable_nestloop=off (4 min): https://explain.depesz.com/s/buKK > > > > > > 3) enable_nestloop=off; enable_seqscan=off (2 min): > > > https://explain.depesz.com/s/0WXx > > > > > > > > > How can I get Postgres not to loop over 12M rows? > > > > I looked at the plans and your config and there are some thoughts I'm > > having: > > > > - The row estimate is off, as you possibly noticed. This can be possibly > > solved by raising `default_statistics_target` to e.g. 2500 (we typically > > use that) and run ANALYZE > I've `set default_statistics_target=2500` and ran analyze on both tables > involved, unfortunately the plan is the same. The columns we use for joining > here are hashes and we expect very few duplicates in the tables. Hence I > think extended statistics (storing most common values and histogram bounds) > aren't useful for this kind of data. Would you say the same thing? In postgres, extended statistics means "MV stats objects", not MCV+histogram, which are "simple statistics", like ndistinct. Your indexes maybe aren't ideal for this query, as mentioned. The indexes that do exist might also be inefficient, due to being unclustered, or bloated, or due to multiple columns. These look redundant (which doesn't matter for this the query): Partition key: RANGE (block_number) Indexes: "transactions_block_number_btree" btree (block_number DESC) "transactions_block_number_hash_key" UNIQUE CONSTRAINT, btree (block_number, hash) "transactions_block_number_time" btree (hash, block_number) Maybe that would be an index just on "hash", which might help here. Possibly you'd want to try to use a BRIN index on timestamp (or maybe block_number?). Maybe you'd want to VACUUM the table to allow index-only scan on the hash columns ? Maybe you'd want to check if reindexing reduces the index size ? We don't know if the table gets lots of UPDATE/DELETE or if any of the columns have high logical vs physical "correlation". https://wiki.postgresql.org/wiki/Slow_Query_Questions#Statistics:_n_distinct.2C_MCV.2C_histogram Have you ANALYZED the partitioned parent recently ? This isn't handled by autovacuum. -- Justin