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 1mn6YQ-0007He-Bh for pgsql-general@arkaria.postgresql.org; Tue, 16 Nov 2021 22:03:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mn6YO-00089j-Qr for pgsql-general@arkaria.postgresql.org; Tue, 16 Nov 2021 22:03:48 +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 1mn6YO-00089a-1s for pgsql-general@lists.postgresql.org; Tue, 16 Nov 2021 22:03:48 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mn6YG-0003se-NW for pgsql-general@lists.postgresql.org; Tue, 16 Nov 2021 22:03:46 +0000 Received: by mail-ed1-x52e.google.com with SMTP id z10so1273203edc.11 for ; Tue, 16 Nov 2021 14:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=6d7f8pMAbHv95qvBWIv2BLXCfs6wHtqOByAZYPDJfuI=; b=MJXgJfAfjR6AfpzLRk5FQuk4jLOuuSGhURNuzD4tAoHkL6d+VgmZ6jYojwMQKse2eV CBbexmvBybQ5Ff4ZjkdV36zP4+k3xAEZMK/8bP3TcAcrWhd6DLzAlV3GKaZ4Os7DYG3D yK9ztEAa2RGqNRDlQ5j6mmIlXqFGCNxCh+rdythK36YN+dSIVUhAq48DaZkIMtyBk/3f XuM6u0qAbTfEIaOUGEr9s2QcJISBzQgoa8BFrEwGtOS3lHDbc9LY1fZku3Bug6abQlmZ pBu/4pMaHl6odRiMo5fuCYwlKmgRcrnDSyjGWLNlM1agc5rZucn4B0B8UgsPjGoreMjR QpQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=6d7f8pMAbHv95qvBWIv2BLXCfs6wHtqOByAZYPDJfuI=; b=SnM9pnTYXtUzIaWEMyOVYlQUdkkFc7E0X6Jo2nED7WWOmtyysVNhOOogVmZus3+DTs cXvh27ir/YasWuPyKmOQw5cpQq0/lgTWVP5VcCfIgpFpxGENgmopw7cd2h8zx8KYPGYR Bp5Bq/gvRiAEWG5z6w+Ju+ebqWwmDMN2ZmEV3nlL4MRKIqaScN19cEBnp5bCtO0GJFiJ Dq+NB9oTFr/Z0JfEJVSr/b41c6LQ59wo0uf6eqm4QXpRmOqww8qfVCIW+eC9CLa8y6uK wm6cu8qE8Bo6gluft1bMt2X4RPaMHVMT/6WJq2Rn/dvePalJPw3s7wKCuxquIte59jAm Bt/Q== X-Gm-Message-State: AOAM533r3GkvcWGdtvRnTk7CQkW4JKCww6AaEPluehkXRuzvaPB0rUL6 bez1AzrMHDTeMrGs4maro0ehFEFcOvGjmDAcLvpCbH2ABeE4mws2iQsDhHFnZBf5G4SqHSUlkNK zB5MeAciofGCeodIXfczDee1y/Pw9WF2uZRoHezrfDdIH8LiPt+Bnch1OR980HvXH3J/8E73FMy hwWFtpR+kScKw84fgCe1gCyqE7A2haK7mD7jG63+R/pmOkmtrbFaqVzLUJPfMeLSVDXTmks7suj yQYMcSBWuSzLu2NxKOfPhduK3oxVcAZ+f8btwZFrLybGnVZc7//0A== X-Google-Smtp-Source: ABdhPJw3oElkP3Jkg9NbFSq9G16NLUdvyWE88DVukkepN8Q4sNGlQiJL7p/A20nhPsdvB9Fb1RlTXg== X-Received: by 2002:a50:c44a:: with SMTP id w10mr14488867edf.47.1637100217971; Tue, 16 Nov 2021 14:03:37 -0800 (PST) Received: from [10.137.0.19] (ip-86-49-251-104.net.upcbroadband.cz. [86.49.251.104]) by smtp.gmail.com with ESMTPSA id hw8sm9129536ejc.58.2021.11.16.14.03.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Nov 2021 14:03:37 -0800 (PST) Subject: Re: Plans with bad estimates/paths To: Joe Wildish , pgsql-general@lists.postgresql.org References: From: Tomas Vondra Message-ID: <8a2680ef-318e-ee00-f485-5aff7ab7fa16@enterprisedb.com> Date: Tue, 16 Nov 2021 23:03:35 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/16/21 9:22 PM, Joe Wildish wrote: > Hello, > > I am struggling with a problem that appears planning related. I'm > hoping folk here may be able to advise on how best to tackle the > issue. > > We have a system that ingests JSON messages containing order data. > The data from these messages is inserted into a normalised table > structure; "order", "order_item", "order_discount", etc. Orders can > have multiple items, items can have multiple discounts and so forth. > Referential constraints exist between the tables in the expected way. > Fields in the tables are all declared NOT NULL. If the schema of the > JSON is such that a field is optional, we reflect that optionality in > another "order_" table, and make "order_" be a subset of > "order". Moreover, we ingest messages for different > clients/customers. Therefore each message-related table carries with > it a client identifier which forms part of the primary key on the > table. For example, "order" has a key of "(client_id, order-id)". > > We have written transformations that calculate various facts about > orders. For example, one of the transforms emits order item data > where we have calculated the overall discount for the item, and have > "filled in" some of the optional fields with defaults, and have > categorised the order item on the basis of some aspect of the order > (e.g. "this is an e-commerce order, this is retail order"). These > transforms are typically per client (e.g. `WHERE client_id = 123`) > although in some cases we transform over multiple clients (e.g. > `WHERE client_id = ANY (SELECT client_id FROM clients WHERE ...)`). > > The issue is that for some clients, or combination of clients, the > planner is choosing a path that takes substantially longer to > evaluate than the plan it typically chooses for other clients. The > number of tables being joined is in the region of 15. There is an > extended statistic object in place to help the one aggregation that > occurs (defined on the basis of the `GROUP BY` columns) to try and > get a better estimate of the likely number of rows emitted. However, > what I am often seeing in the explain plan is that the estimated rows > is small and the actuals are significantly larger e.g. > > Merge Join (cost=1.14..253250.32 rows=1099 width=69) (actual > time=1268.587..2400.353 rows=4282355 loops=1) > It sure smells like a case of correlated data for some clients but not others, but who knows ... Hard to say without seeing the nodes below the join. If the lower nodes are estimated accurately, then it's the join selectivity that is estimated poorly, and there's not much we can do about it :-( Do the "good" plans have the same underestimate? Maybe there's just much less data for those clients, and the "poor" plan ends up being fast anyway? > I am assuming this underestimation is the source of the planner > choosing the "wrong" path; in production, we have had to resort to > setting the join and from collapse limits to 1 to force a naive plan > to be generated. This is giving us execution times in the 10/20 > second range vs. >45m in some cases. > That may be happening, yes. So is it the join order that ends up being wrong, or the join methods? Have you tried increasing the collapse limit instead? Although, if it works for some queries but not others, that's likely not going to help. > (a) Do you have any suggestions on a general approach to tackling the > problem? For example, one option might be to pre-compute some of the > subqueries that are occurring in the transforms, write the results > into their own tables, and substitute those tables in place of the > subqueries in the main transform. Is this something people typically > do in this situation? > The first thing I'd do is reduce the query size as much as possible. In this case I'd try removing as many joins as possible until the issue disappears. The simpler the query, the easier it is to investigate. And yes, replacing parts of a query with a temporary table is a common solution, because it's possible to collect statistics on it, build indexes etc. That usually solves estimation issues in multi-tenancy. Sometimes even a CTE with materialization is enough. > (b) Do I need to provide a schema and explain plans to get any > concrete advice on how to proceed? > That would be helpful, particularly after making the query as small as possible. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company