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 1jRb9s-0004qu-IB for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2020 12:40:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRb9r-0004Wh-CZ for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Apr 2020 12:40:47 +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 1jRb9r-0004WZ-51 for pgsql-hackers@lists.postgresql.org; Thu, 23 Apr 2020 12:40:47 +0000 Received: from tamriel.snowman.net ([96.255.250.162]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRb9o-0003Gz-AD for pgsql-hackers@postgresql.org; Thu, 23 Apr 2020 12:40:46 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 249F05F7AD; Thu, 23 Apr 2020 08:40:47 -0400 (EDT) Date: Thu, 23 Apr 2020 08:40:47 -0400 From: Stephen Frost To: Robert Haas Cc: Tom Lane , Andres Freund , Alvaro Herrera , Corey Huinker , Antonin Houska , Pavel Stehule , PostgreSQL Hackers Subject: Re: More efficient RI checks - take 2 Message-ID: <20200423124046.GD13712@tamriel.snowman.net> References: <20200422154231.6shz4kdor4yb5w5b@alap3.anarazel.de> <20200422171806.GA12435@alvherre.pgsql> <20200422183600.tpl5745dfbnozi6t@alap3.anarazel.de> <6442.1587595207@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="Ar40rvwHfUXOgY8v" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Ar40rvwHfUXOgY8v Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Robert Haas (robertmhaas@gmail.com) wrote: > On Wed, Apr 22, 2020 at 6:40 PM Tom Lane wrote: > > But it's not entirely clear to me that we know the best plan for a > > statement-level RI action with sufficient certainty to go that way. > > Is it really the case that the plan would not vary based on how > > many tuples there are to check, for example? If we *do* know > > exactly what should happen, I'd tend to lean towards Andres' > > idea that we shouldn't be using the executor at all, but just > > hard-wiring stuff at the level of "do these table scans". >=20 > Well, I guess I'd naively think we want an index scan on a plain > table. It is barely possible that in some corner case a sequential > scan would be faster, but could it be enough faster to save the cost > of planning? I doubt it, but I just work here. >=20 > On a partitioning hierarchy we want to figure out which partition is > relevant for the value we're trying to find, and then scan that one. >=20 > I'm not sure there are any other cases. We have to have a UNIQUE > constraint or we can't be referencing this target table. So it can't > be a plain inheritance hierarchy, nor (I think) a foreign table. In the cases where we have a UNIQUE constraint, and therefore a clear index to use, I tend to agree that we should just be getting to it and avoiding the planner/executor, as Andres suggest. I'm not super thrilled about the idea of throwing an ERROR when we haven't got an index to use though, and we don't require an index on the referring side, meaning that, with such a change, a DELETE or UPDATE on the referred table with an ON CASCADE FK will just start throwing errors. That's not terribly friendly, even if it's not really best practice to not have an index to help with those cases. I'd hope that we would at least teach pg_upgrade to look for such cases and throw errors (though maybe that could be downgraded to a WARNING with a flag..?) if it finds any when upgrading, so that users don't upgrade and then suddenly start getting errors for simple statements that used to work just fine. > > On the whole I still think that generating a Query tree and then > > letting the planner do its thing might be the best approach. >=20 > Maybe, but it seems awfully heavy-weight. Once you go into the planner > it's pretty hard to avoid considering indexes we don't care about, > bitmap scans we don't care about, a sequential scan we don't care > about, etc. You'd certainly save something just from avoiding > parsing, but planning's pretty expensive too. Agreed. Thanks, Stephen --Ar40rvwHfUXOgY8v Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJeoYzOAAoJEO1sijiDR2RVwaUP/ijZF5v4XzaIlaVM2RJtvYDO e3u2UMrQzTmLXzOKtIyfBV5LW9NUHN/pId1RjpzMBRZqXnS2wdlG8FoKln0kEZBd V8XpZ1z7RNCRY6xP6rk+oi5pJclhtlkvEp0dNcHiElXfs/hEkzC24Ke3LDFyjdwG 7FiY1AISB1UKXNVB0CvEruKjv5UkPeB4USRjKl7CiDlBWjmoAUJ0Mk7NNF5bk4Gq BHFUemudFuRuC+OhEu4raIcaK1S1WRiJr8aGjlyZXWUApgOtKijFrzgVN4A4fbYY smTSw07mtVrI/kAJy16QHiKLpGgjDlk0KYfPByJYIA6tYf4ssRssr3csuU1FqbhH 4pY6+Jo57tabesxgGdjbHMVyd8VA7E64O1ia7xNTLUAW9sOsWJG3ip94Rlyq22ki e8uBkHsFzLEWw33G2sDwavsqmAR+1gREb5vNAf1yXTFNA9Rbaa0fAQIMaPRjBBvj 9OhvfK3aFjzEK2ckazPUV1V1aAwpBqfjGHGGGnIBck/+LPvITtGUR6cxb1Dv6Cvy pb98LsihUGkJgwSTAB+ZjNJPdNOyLmj9rEJiEBaKfvZUisX3VZ21MFFpvr7kEo3x SNZNQ4Uh5X+ciaDVLXhSJJY/YBegimLPshvy/mIiLrSebe3woLfkQixR6M2k7RXM 8OZ41B6Sz4tYTiG1B0Wn =bJtN -----END PGP SIGNATURE----- --Ar40rvwHfUXOgY8v--