Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b9y4X-0004zR-Lc for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 17:12:17 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b9y4W-0000eM-IA for pgsql-hackers@arkaria.postgresql.org; Mon, 06 Jun 2016 17:12:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b9y3B-0007cB-Mg for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 17:10:53 +0000 Received: from mail-lf0-x235.google.com ([2a00:1450:4010:c07::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1b9y33-0006L5-1P for pgsql-hackers@postgresql.org; Mon, 06 Jun 2016 17:10:52 +0000 Received: by mail-lf0-x235.google.com with SMTP id b73so98833701lfb.3 for ; Mon, 06 Jun 2016 10:10:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:cc:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=IoSIOmUgsws6DazHlYh3vpMj6nUk2NI6glJTGjwtmLY=; b=WB9PfW+4NtscmqXneTzeX23qrXrWR1QoROeePDe7NweVP3rtx9EQe3lMQ9wfDTSTWu sMDiZDVP4IfCqKhZX8deWiWfj53UOY31CXxaxzAAS797BipkM016GFaBd0wHC3tL3RxI gK7sHHULou90U2wqUrtnp0LLrWviiolp9KJMkTPJjB38QekSVNT49Dxr6HpkMA1uGQwM sKLotH5dDt9JWp81wN4Gi9lA1nbQzwXsVWxgt7nCTjvBA9WMzUl5Z2Zwe9dpIXRLJRKG XUxJ7Z1bd3k2xTuJzeTo+KuF5vwYApRHz1D9zyJ0vlH5K7kRvCtNmGlk+82kLXwd6XHY 55ew== 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:cc:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=IoSIOmUgsws6DazHlYh3vpMj6nUk2NI6glJTGjwtmLY=; b=HV/sRLIMmlq1PrRTDwpM2n1YCxGFWjf/CkxcLliWI3hIs2CBPi4aHkhpFYGfGFuBzI kK3k5NiFq2G3LNKSjGcoHBpwETPGlie+y8SsM/cveoOZpBVQrCZtvqd0S766eV2uFqo4 v33yaftD27HF2xHsCK1ZAPiHER0S936RmpJLRCjjNYMMOuTt413VcBbf7K6c31uFFd7t bLmRT5S8cr7sB0CE7fFkLtX9JrpKvLSdSSgog6aAv+xJnLX1nT3DZr5MeDzZ12rWQuwy nFXOfBeJW0ZeP/Zjpruvbed7k3OY30ydyYTaS1Pqbq8rks/o2jHDWSdvt4Lz5eZ107ee MOEw== X-Gm-Message-State: ALyK8tKYJTStk8Di+Y9Tu0Lv8COs8WKL3MKMO2gNcpEYgk7vpC1r40CixatRyRyzGthlWqpaqXirihVH9/+DSuOQ1OBLrhGOXTCDQyxySt7IxB46r7qo/0ztBFkpjLZwE+Jbia23G0OUQ7of7NNMFcA+ngr1c2DhkKtJcAh6uNeUmrNhYDN1ZYYbZvRNFRUXOyBExEKRrIN6 X-Received: by 10.46.9.11 with SMTP id 11mr4714197ljj.5.1465233042388; Mon, 06 Jun 2016 10:10:42 -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 y5sm1871288lba.17.2016.06.06.10.10.40 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 06 Jun 2016 10:10:41 -0700 (PDT) Subject: Re: pg9.6 segfault using simple query (related to use fk for join estimates) To: Tom Lane References: <57238E64.8010702@dalibo.com> <22731.1462037712@sss.pgh.pa.us> <8344835e-18af-9d40-aed7-bd2261be9162@2ndquadrant.com> <16807.1462388069@sss.pgh.pa.us> <20160529053601.GE496776@tornado.leadboat.com> <20160602012954.GA619629@tornado.leadboat.com> <28109.1465064127@sss.pgh.pa.us> <039fca3d-890a-d09a-e0df-e045187eb69e@2ndquadrant.com> <23385.1465229755@sss.pgh.pa.us> Cc: Robert Haas , Noah Misch , Simon Riggs , David Rowley , Julien Rouhaud , Michael Paquier , Stefan Huehner , PostgreSQL mailing lists From: Tomas Vondra Message-ID: Date: Mon, 6 Jun 2016 19:10:39 +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: <23385.1465229755@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 On 06/06/2016 06:15 PM, Tom Lane wrote: > Tomas Vondra writes: >> On 06/04/2016 08:15 PM, Tom Lane wrote: >>> * Make RelationGetFKeyList cache a list of ForeignKeyOptInfo structs, >>> not just constraint OIDs. It's insane that the relcache scans >>> pg_constraint to collect those OIDs and then the planner re-reads all >>> those same rows on every planning cycle. > >> That seems like a fairly straightforward change, and I'm not opposed to >> doing that. However RelationGetFKeyList is basically a modified copy of >> RelationGetIndexList, so it shares the same general behavior, including >> caching of OIDs and then constructing IndexOptInfo objects on each >> get_relation_info() call. Why is it 'insane' for foreign keys but not >> for indexes? Or what am I missing? > > I would not be in favor of migrating knowledge of IndexOptInfo into the > relcache; it's too planner-specific. Also, it mostly copies info from > the index's relcache entry, not the parent relation's (which for one > thing would imply locking hazards if we tried to cache that info in the > parent rel). But for foreign keys, we can cache an image of certain > well-defined fields of certain well-defined rows of pg_constraint, and > that seems like a reasonably arm's-length definition of a responsibility > to give the relcache, especially when it has to visit all and only those > same rows to construct what it's caching now. > >>> would be okay if you checked after identifying a matching eclass member >>> that it belonged to the FK's referenced table, but AFAICS there is no such >>> check anywhere. > >> Perhaps I'm missing something, but I thought this is checked by these >> conditions in quals_match_foreign_key(): > >> 1) with ECs (line 3990) > >> if (foreignrel->relid == var->varno && >> fkinfo->confkeys[i] == var->varattno) >> foundvarmask |= 1; > > This checks that you found a joinclause mentioning foreignrel. But > foreignrel need have nothing to do with the foreign key; it could be any > table in the query. That comparison of confkeys[] and varattno is thus > checking for column-number equality of two columns that might be from > different relations. That is, if we have an FK "A.X references B.Y", > and the query contains "A.X = C.Z", this code could match the FK to that > clause if Y and Z happen to have the same data types and column numbers. I don't follow. How could it have 'nothing to do with the foreign key'? We're explicitly checking both varno and varattno on both sides of the foreign key, and we only consider the column is considered matched if both the checks pass. I've tried to come up with an example triggering the issue, but unsuccessfully ... 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