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 1odub2-0003y2-8j for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Sep 2022 14:33:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1odub1-0004vJ-5D for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Sep 2022 14:33:03 +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 1odub0-0004vA-Pv for pgsql-hackers@lists.postgresql.org; Thu, 29 Sep 2022 14:33:02 +0000 Received: from mx.zeyos.com ([88.99.153.70]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oduay-0004VF-6q for pgsql-hackers@lists.postgresql.org; Thu, 29 Sep 2022 14:33:02 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 25023619EF for ; Thu, 29 Sep 2022 16:32:59 +0200 (CEST) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=zeyos.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zeyos.com; h= content-transfer-encoding:content-type:content-type:mime-version :message-id:to:from:from:subject:subject:date:date; s=dkim; t= 1664461979; x=1665325980; bh=N8XFPI1pbnVU15tO2ZyN6VG1TB2rPdcULS+ +y59lqB0=; b=qx19QqNiB3ayo0aVa31MQOZq/B7hnTyOSovOOirewg+QRVe5PfD TZcdUgbEfqEJNmkkhF2jbjZPPg2rThatCVAk0uWq8XFe+wMGEitWKQHI95LikZtD 8zAOs201TTeYDeoft97CL3aFG93TWZaeyz0e5XbNCRq0JDRnNQL+38zo= X-Virus-Scanned: Debian amavisd-new at mx.zeyos.com Received: from mx.zeyos.com ([127.0.0.1]) by mx.zeyos.com (mx.zeyos.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id SEWXF5qEHyZC for ; Thu, 29 Sep 2022 16:32:59 +0200 (CEST) Received: from cloud.zeyos.com (static.182.82.88.23.clients.your-server.de [23.88.82.182]) by mx.zeyos.com (Postfix) with ESMTPSA id C4337619DB; Thu, 29 Sep 2022 16:32:58 +0200 (CEST) Date: Thu, 29 Sep 2022 16:32:58 +0200 Subject: Re: disfavoring unparameterized nested loops From: Benjamin Coutu To: PostgreSQL Hackers Cc: Tom Lane , Robert Haas , David Rowley , Peter Geoghegan Message-ID: <0d3f9971e82d2d969a5f@zeyos.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I'd like to revamp this important discussion.=0A=0AAs is well described in = this fairly recent paper here https://www.vldb.org/pvldb/vol9/p204-leis.pdf= (which also looks at Postgres) "estimation errors quickly grow as the numb= er of joins increases, and that these errors are usually the reason for bad= plans" - I think we can all get behind that statement.=0A=0AWhile nested l= oop joins work great when cardinality estimates are correct, they are notor= iously bad when the optimizer underestimates and they degrade very fast in = such cases - the good vs. bad here is very asymmetric. On the other hand ha= sh joins degrade much more gracefully - they are considered very robust aga= inst underestimation. The above mentioned paper illustrates that all mayor = DBMS (including Postgres) tend to underestimate and usually that underestim= ation increases drastically with the number of joins (see Figures 3+4 of th= e paper).=0A=0ANow, a simple approach to guarding against bad plans that ar= ise from underestimation could be to use what I would call a nested-loop-co= nviction-multiplier based on the current depth of the join tree, e.g. for a= base table that multiplier would obviously be 1, but would then grow (e.g.= ) quadratically. That conviction-multiplier would *NOT* be used to skew the= cardinality estimates themselves, but rather be applied to the overall nes= ted loop join cost at each particular stage of the plan when comparing it t= o other more robust join strategies like hash or sort-merge joins. That way= when we can be sure to have a good estimate at the bottom of the join tree= we treat all things equal, but favor nested loops less and less as we move= up the join tree for the sake of robustness.=0AAlso, we can expand the mul= tiplier whenever we fall back to using the default cardinality constant as = surely all bets are off at that point - we should definitely treat nested l= oop joins as out of favor in this instance and that could easily be incorpo= rated by simply increasing the conviction-mutliplier.=0A=0AWhat are your th= oughts on this simple idea - is it perhaps too simple?=0A=0ACheers, Ben