Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9jGU-00025X-F6 for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 01:23:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b9jGS-0000zM-Mo for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 01:23:36 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b9jGR-0000yg-De for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 01:23:35 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1b9jGO-0002vy-6L for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 01:23:34 +0000 Received: by mail-wm0-x22b.google.com with SMTP id c74so23778932wme.0 for ; Sun, 05 Jun 2016 18:23:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding; bh=T4hoJjPXJYtXuQ0ZvT9LoaG+hQ8NK0GdsWFUq6nzn1M=; b=I7LzX+jZT7lF9zaaSYr/oHSfVz2ZJ8VZlC+VgD2yaMCg+p/BtzEoKoOGiIib5T4E8k 4+u9RJh/e0CQQ3K2gc88OPNrPscCJsHMNV71vA8aYL/ZG0BXWtHJU3nxO8DE8HS8JNeF QGUS8r/NEzL/Na+wIIqMajnle8jbgQ5eGOCaAV9uOO+UWpfEfAoInla4I4ib3sc1UB3C LRyCsfqc2WM7Fh5FcMC9e1+aN7THsKc8QD45WR/0gPcVAd+3A43TaJORtm5xCN7wbJ6V 4CzWQDI3AM213fWu+YMP5PTeqffPFAf3hlTIGV4+KcdxZ1AkpWXZTS3D+5y16bqlO2X7 7E6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=T4hoJjPXJYtXuQ0ZvT9LoaG+hQ8NK0GdsWFUq6nzn1M=; b=JpvZlFDx0aIQU6mm+Xkdm9lypJuqSpbkMY2jXPLxJZMDghzzJpdVORScJ6dP6F31Nv LBCuxO1cxL3LVkZYdQoNJNQFCW6BPn5apjXCphREITeYje1PdlRGGvTDAvuayLr+XAC+ k9tKq00VvyD4fivJri71CZ5Q1qkRvPfUVmCiQq5KmOdXh5vUsNGiMUjBzrjQOgQc4EIz +2HmoWb0G3kCI1bG05ZE3bkW7trCp/h7A7hfVCOMNmBkzk35qjp79X5K/wUBkfuQNd35 Q4/+1dy8ej9JiVKhWb4SpFbzn+j5eKSDBQfpUuRJCthZCmS1zv3gFrnMG3vjkmlU7o9D BjiA== X-Gm-Message-State: ALyK8tKibaxzNI53bPBboAtU0jhpn+Q1c9W/bzzfOvtrg3VGvICvD37GL6nh1S6oTgUf/E9eti16eAiJJXY8dxFggAV0exYN3tdRoZXcyz8Or7QlMo2O+dZm7A1fIsycH2BS2DeRtHeZRwIAL2flS6yKiAiPbcQffFsWSGOa2M2JpSbVVrzwT66djX16vol9T6AG9gcX4Yev X-Received: by 10.28.142.82 with SMTP id q79mr9722519wmd.88.1465176209653; Sun, 05 Jun 2016 18:23:29 -0700 (PDT) Received: from [10.137.2.17] (ip-89-177-44-58.net.upcbroadband.cz. [89.177.44.58]) by smtp.gmail.com with ESMTPSA id t1sm17643374wjy.3.2016.06.05.18.23.28 for (version=TLSv1/SSLv3 cipher=OTHER); Sun, 05 Jun 2016 18:23:28 -0700 (PDT) Subject: Re: New design for FK-based join selectivity estimation To: pgsql-hackers@postgresql.org References: <31041.1465069446@sss.pgh.pa.us> From: Tomas Vondra Message-ID: Date: Mon, 6 Jun 2016 03:23:27 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:46.0) Gecko/20100101 Thunderbird/46.0a2 MIME-Version: 1.0 In-Reply-To: <31041.1465069446@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org Hi, On 06/04/2016 09:44 PM, Tom Lane wrote: > This is a branch of the discussion in > https://www.postgresql.org/message-id/flat/20160429102531.GA13701%40huehner.biz > but I'm starting a new thread as the original title is getting > increasingly off-topic. > > I complained in that thread that the FK join selectivity patch had a > very brute-force approach to matching join qual clauses to FK > constraints, requiring a total of seven nested levels of looping to > get anything done, and expensively rediscovering the same facts over > and over. Here is a sketch of what I think is a better way: > > * During the planner's catalog data collection phase, construct a > single list (per PlannerInfo, not per RTE) of all relevant FKs. > In this data structure, each FK's referenced and referencing relations > are identified by relid (that is, RTE index) not just OID. In the case > of a query containing a self-join, that would mean that the same FK > constraint could give rise to multiple list entries, one for each RTE > occurrence of its referenced or referencing target relation. FKs not > relating two tables of the query are necessarily not in this list, > and we could also choose not to include single-column FKs. > > * After finalizing equivalence classes, make a single pass through > the FK list and check each column-pair to see if it can be matched > to any EC (that is, both source and target columns are in the EC and > the comparison operator is in the EC's opfamily). Mark each matched > column pair in the FK list data structure with a pointer to the EC. > > * When performing join selectivity estimation, run through the FK list > a single time, ignoring entries that do not link a member of the join's > LHS to a member of the join's RHS. This is a fairly cheap test given > the relid labels; it'd be approximately > > if ((bms_is_member(fkinfo->src_relid, outer_rel->relids) && > bms_is_member(fkinfo->dst_relid, inner_rel->relids)) || > (bms_is_member(fkinfo->dst_relid, outer_rel->relids) && > bms_is_member(fkinfo->src_relid, inner_rel->relids))) > > For each potentially interesting FK entry, run through the join > qual list. A RestrictInfo that was generated from an EC matches > the FK if and only if that EC appears in the per-column markings; > other RestrictInfos are matched to one of the FK columns normally > (I think this path can ignore FK columns that have been matched to ECs). > At the end of that, we can determine whether all the FK columns have > been matched to some qual item, and we have a count and/or bitmapset > of the qual list entries that matched the FK. Remember the FK entry > with the largest such count. > > * After scanning the list, we have our best FK match and can proceed > with making the actual selectivity estimate as in the current code. > > With this approach, we have an iteration structure like > > * once per join relation created > * for each foreign key constraint relevant to the query > (but skipping the loops below if it's not relevant to this join) > * for each join qual for the joinrel pair > * for each key column in that FK > > which gets us down from seven nested loops to four, and also makes the > work done in the innermost loops significantly cheaper for the EC case, > which will be the more common one. It's also much easier to make this > structure do zero extra work when there are no relevant FKs, which is > a pleasant property for extra planner work to have. > > Now, we'll also add some per-FK-per-EC setup work, but my guess is > that that's negligible compared to the per-join-relation work. > > It's possible that we could reduce the cost of matching non-EC join > quals as well, with some trick along the line of pre-matching non-EC > RestrictInfos to FK items. I'm unsure that that is worth the trouble > though. > > Thoughts? Firstly thanks for looking into this, and also for coming up with a very detailed design proposal. I do agree this new design seems superior to the current one and it seems worth a try. I'd like to see how far we can get over the next few days (say, until the end of the week). One of the recent issues with the current design was handling of inheritance / appendrels. ISTM the proposed design has the same issue, no? What happens if the relations are partitioned? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers