Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUCu6-0004L9-VX for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Dec 2018 15:46:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gUCu3-0005oe-Ja for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Dec 2018 15:46:27 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUCu3-0005o0-6U for pgsql-hackers@lists.postgresql.org; Tue, 04 Dec 2018 15:46:27 +0000 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gUCtz-0004na-Fh for pgsql-hackers@lists.postgresql.org; Tue, 04 Dec 2018 15:46:26 +0000 Received: by mail-qt1-x842.google.com with SMTP id y20so18489798qtm.13 for ; Tue, 04 Dec 2018 07:46:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=x7QrMG4k5A64wlg4WMZWlzBZomcUFjGz+p082OTxSkE=; b=a8YZYpMY3kjhyIK+GkY7lFlZkd/i81GXOTcvSz14/G3ez9JEOpptbZjt1mSM89eglQ UxuksG866vUMJWtBaGB2KOOWelYQAjjbKxrOrkKwwL9C3ZTFFLoO8qBvPYsJM67WHxA9 uh6Y7nr+LekiNcabEJ6UMQ0n/qVJQCvbzmvMNeOmafa5chw4Iudf9yxBiwJqYKFu2c7d 1LPAoHh0Tsd6MfmOeNE0QZ/eYn0EpQB2i9Ztm5hrril3w5NunJFGYFLvJmgSmHLvyYbv 5KBSufIarM4rz37x1KkP7x3XE/E00DvcQ+zoZrlC+8C4n7HBZWiOVSEqu2l0weLVuwCL zKDQ== 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:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=x7QrMG4k5A64wlg4WMZWlzBZomcUFjGz+p082OTxSkE=; b=GGG7vbtdpwdXRU+yBip+J7w/jmJi0wtp3iapXwlknhAB7sfO7yPbuzNjdJfBAgDrMS 4GSMuX4/nOee+bHoP/A2+M8lWzWSQ05Q9VZgaALM2PXahRO1KlfK+dlNcU6yOHUYcMFv mfe+ZVmguMFB/+ETSbEa1wspuKxJRRav2WFf6jSREscPt0qieQWg/nXsHZBPS1douKQu HCoNcArZMl/3uUiJEir+eU33iW1Q1PuXqgJAAy7EshdmubhHHV2U1kj+UP5+5P/eefOC 05SjwowE6ZmZVOxLJ/0AA/VcBUbc9oJeygBGTjQcd8gAwlbRjUB+0sHjxwApkPklamCC Gxsw== X-Gm-Message-State: AA+aEWbU+QF97hFrcVrmi2om8BFTgmaYtwLSJndv3rkCzKOPPZ7+5L9D HxS5w790Xxx4l5zBA/MyiDJEkFrcoLM= X-Google-Smtp-Source: AFSGD/We2iJ3GdFakdLrUd9ZyhzFXgxNF/CcVokqllqJXiaIjK0t1sxzjhAivq1YzAHvUOuGcYLPQQ== X-Received: by 2002:a0c:8322:: with SMTP id j31mr20265503qva.56.1543938380626; Tue, 04 Dec 2018 07:46:20 -0800 (PST) Received: from alvin.alvh.no-ip.org ([201.186.80.75]) by smtp.gmail.com with ESMTPSA id d50sm11392234qta.31.2018.12.04.07.46.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 04 Dec 2018 07:46:19 -0800 (PST) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id 1D6C2422; Tue, 4 Dec 2018 12:46:18 -0300 (-03) Date: Tue, 4 Dec 2018 12:46:18 -0300 From: Alvaro Herrera To: David Fetter Cc: Tom Lane , Pg Hackers Subject: Re: psql display of foreign keys Message-ID: <20181204154618.5diwgpndpyst775g@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20181204152330.GH20931@fetter.org> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2018-Dec-04, David Fetter wrote: > On Tue, Dec 04, 2018 at 10:00:00AM -0500, Tom Lane wrote: > > That's probably a win performance-wise anyway, as I have no doubt > > that the performance of this query is awful compared to what it > > replaces, so we don't really want to use it if we don't have to. Sure thing. Fixed the easy one. On to the other one ... > Do you have cases where we should be measuring performance dips? > Also, is there something about about indexes involved in this query > or WITH RECURSIVE itself that's pessimizing performance, generally? Note that there are two queries being changed in this patch, one for each side of any foreign key. They start with either a lookup on conrelid or confrelid; only one of those columns has an index (so priming the CTE is a little slow for the confrelid one, if your pg_constraint is bloated). But after that the CTE iterates on the OID column, which is indexed, so it should be quick enough. This is the conrelid plan: Sort (cost=1605.38..1605.39 rows=1 width=101) Sort Key: ((constraints.conrelid = '311099'::oid)) DESC, constraints.conname CTE constraints -> Recursive Union (cost=0.29..1600.82 rows=202 width=76) -> Index Scan using pg_constraint_conrelid_contypid_conname_index on pg_constraint (cost=0.29..11.77 rows=2 width=76) Index Cond: (conrelid = '311099'::oid) Filter: (contype = 'f'::"char") -> Nested Loop (cost=0.29..158.50 rows=20 width=76) -> WorkTable Scan on constraints constraints_1 (cost=0.00..0.40 rows=20 width=4) -> Index Scan using pg_constraint_oid_index on pg_constraint pc (cost=0.29..7.90 rows=1 width=76) Index Cond: (oid = constraints_1.parent) -> CTE Scan on constraints (cost=0.00..4.55 rows=1 width=101) Filter: (parent = '0'::oid) This is the confrelid plan: Sort (cost=1793.40..1793.40 rows=1 width=100) Sort Key: constraints.conname CTE constraints -> Recursive Union (cost=0.00..1791.11 rows=101 width=80) -> Seq Scan on pg_constraint (cost=0.00..956.59 rows=1 width=80) Filter: ((contype = 'f'::"char") AND (confrelid = '311099'::oid)) -> Nested Loop (cost=0.29..83.25 rows=10 width=80) -> WorkTable Scan on constraints constraints_1 (cost=0.00..0.20 rows=10 width=4) -> Index Scan using pg_constraint_oid_index on pg_constraint pc (cost=0.29..8.30 rows=1 width=80) Index Cond: (oid = constraints_1.parent) -> CTE Scan on constraints (cost=0.00..2.27 rows=1 width=100) Filter: (parent = '0'::oid) Of course, the original queries did the same thing (lookup via unindexed confrelid) and nobody has complained about that yet. Then again, the difference between a query taking 0.1 ms (the original query on conrelid, without recursive CTE) and one that takes 6ms (recursive one on confrelid) is not noticeable to humans anyway; it's not like this is a hot path. In any case, if anyone can think of another method to obtain the topmost constraint of a hierarchy involving the current table (not involving a recursive CTE, or maybe with a better one), I'm all ears. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services