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 1nqy0V-0007qM-R1 for pgsql-hackers@arkaria.postgresql.org; Tue, 17 May 2022 14:17:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nqy0T-0001V0-Ru for pgsql-hackers@arkaria.postgresql.org; Tue, 17 May 2022 14:17:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nqy0T-0001Uf-HM for pgsql-hackers@lists.postgresql.org; Tue, 17 May 2022 14:17:01 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nqy0M-0002eE-KU for pgsql-hackers@lists.postgresql.org; Tue, 17 May 2022 14:17:00 +0000 Received: by mail-wr1-x433.google.com with SMTP id k30so12276239wrd.5 for ; Tue, 17 May 2022 07:16:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; h=from:to:cc:subject:date:message-id:organization:in-reply-to :references:mime-version:content-transfer-encoding; bh=0cvWqgnWTcDaG/MekQOrBYIXK69dOHd8cUkBK4DEWuY=; b=DwXZgyPsR7hcB0KH4n47oXO2up765B1sE2svq+TlyA/uO1HPjxLA7HvhhEJO9WUbhi /PVgCo3aEGwwV97fK8+9aWSrjvIkCMlKPv82JaOoxLLwVtLc/s1i6hItXK/DdYUYB+Fv goeyP2FoVeiRdAkiLwB4BHq6g22UA6Od1auPA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:organization :in-reply-to:references:mime-version:content-transfer-encoding; bh=0cvWqgnWTcDaG/MekQOrBYIXK69dOHd8cUkBK4DEWuY=; b=uz8296uLLNMTsve7g0Y3J5sA6lX93ZohLzmSCeYfD+F8FLRgHWz02EehGzrMEDmadd B2+A3JnIUquA9I/XCPq/BPtFXuwJdnooG/UmjkFqypPLkCsj+YaLru8t59MPiRU1vhzx FAhwKPDKUEkj1y+B1LzgJ5yyanLUrEM4uEq5/52TrVvNf+V/2eREMBS/WlEDTueEFbAi KdeylmNz/HUjqIHhOhyN0ysV8ev0r1f0juHJcxANZstal0pNqUiu2Eg0KGtV2GUCoEXH 52aM4Nh8Rs3cuIm2xFjT22d6+c1BFjoHSpII8Js+C99c7dKT2DpJ4AqvJ/iVPqtcodGd a6jw== X-Gm-Message-State: AOAM5334oVD/KJmD+eSM5YXKQc2fPIhgVX1kpC6NUwYZ7PI3HDtVc3OT dL9WqXAL+bBDv8+9EEtQiHOWUg== X-Google-Smtp-Source: ABdhPJyDyd/wkt13e7bnXx/wNvzKqSaSIAcPtANa4mKNbVDZ0b4Nifx+N1yOIz19WueZd192u9mo/Q== X-Received: by 2002:adf:dd06:0:b0:20d:c4b:e76f with SMTP id a6-20020adfdd06000000b0020d0c4be76fmr7091015wrm.581.1652797013031; Tue, 17 May 2022 07:16:53 -0700 (PDT) Received: from aivenronan.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id q5-20020a1cf305000000b003942a244eddsm2004930wmq.34.2022.05.17.07.16.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 May 2022 07:16:52 -0700 (PDT) From: Ronan Dunklau To: Greg Stark , Andrey Lepikhov Cc: Andres Freund , Laurenz Albe , Hywel Carver , PostgreSQL Hackers Subject: Re: Removing unneeded self joins Date: Tue, 17 May 2022 16:14:46 +0200 Message-ID: <2537176.Lt9SDvczpP@aivenronan> Organization: aiven In-Reply-To: References: <64486b0b-0404-e39e-322d-0801154901f3@postgrespro.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Le vendredi 13 mai 2022, 07:07:47 CEST Andrey Lepikhov a =E9crit : > New version of the feature. > Here a minor bug with RowMarks is fixed. A degenerated case is fixed, > when uniqueness of an inner deduced not from join quals, but from a > baserestrictinfo clauses 'x=3Dconst', where x - unique field. > Code, dedicated to solve second issue is controversial, so i attached > delta.txt for quick observation. > Maybe we should return to previous version of code, when we didn't split > restriction list into join quals and base quals? Hello, I tried to find problematic cases, which would make the planning time grow= =20 unacceptably, and couldn't devise it. The worst case scenario I could think of was as follows: - a query with many different self joins - an abundance of unique indexes on combinations of this table columns to= =20 consider - additional predicates on the where clause on columns. The base table I used for this was a table with 40 integers. 39 unique inde= xes=20 were defined on every combination of (c1, cX) with cX being columns c2 to c= 40. I turned geqo off, set from_collapse_limit and join_collapse_limit to=20 unreasonably high values (30), and tried to run queries of the form: SELECT * FROM test_table t1 JOIN test_table tX ON t1.c1 =3D tX.c1 AND t1.c[X+2] =3D tX.cX =2E.. JOIN test_table tX ON t1.c1 =3D tX.c1 AND t1.c[X+2] =3D tX.cX. So no self join can be eliminated in that case. The performance was very similar with or without the GUC enabled. I tested = the=20 same thing without the patch, since the test for uniqueness has been slight= ly=20 altered and incurs a new allocation, but it doesn't seem to change.=20 One interesting side effect of this patch, is that removing any unneeded se= lf=20 join cuts down the planification time very significantly, as we lower the n= umber=20 of combinations to consider.=20 As for the code:=20 - Comments on relation_has_unique_index_ext and relation_has_unique_index_= for=20 should be rewritten, as relation_has_unique_index_for is now just a special= =20 case of relation_has_unique_index_ext. By the way, the comment would probab= ly=20 be better read as: "but if extra_clauses isn't NULL". - The whole thing about "extra_clauses", ie, baserestrictinfos which were= =20 used to determine uniqueness, is not very clear. Most functions where the n= ew=20 argument has been added have not seen an update in their comments, and the= =20 name itself doesn't really convey the intented meaning: perhaps=20 required_non_join_clauses ? The way this works should be explained a bit more thoroughly, for example i= n=20 remove_self_joins_one_group the purpose of uclauses should be explained. Th= e=20 fact that degenerate_case returns true when we don't have any additional ba= se=20 restrict info is also confusing, as well as the degenerate_case name. I'll update if I think of more interesting things to add.=20 =2D-=20 Ronan Dunklau