Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xAY43-00000002iD2-1izd for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Sep 2026 19:28:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xAY42-000000053XO-2p5R for pgsql-hackers@arkaria.postgresql.org; Sat, 26 Sep 2026 19:28:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xAY42-000000053XG-1e9V for pgsql-hackers@lists.postgresql.org; Sat, 26 Sep 2026 19:28:02 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xAY3y-00000001Oec-2uza for pgsql-hackers@lists.postgresql.org; Sat, 26 Sep 2026 19:28:01 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 68QJRqAq138753; Sat, 26 Sep 2026 15:27:52 -0400 From: Tom Lane To: Noah Misch cc: Richard Guo , Thom Brown , Jacob Brazeal , pgsql-hackers@lists.postgresql.org, underdog@theori.io Subject: Re: remove_useless_joins vs. bug #19560 In-reply-to: <79317.1790445906@sss.pgh.pa.us> References: <839346.1787765112@sss.pgh.pa.us> <954541.1787846010@sss.pgh.pa.us> <1147476.1787924590@sss.pgh.pa.us> <598915.1789613519@sss.pgh.pa.us> <20260926174457.13.noahmisch@microsoft.com> <79317.1790445906@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sat, 26 Sep 2026 14:05:06 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <138733.1790450856.0@sss.pgh.pa.us> Date: Sat, 26 Sep 2026 15:27:52 -0400 Message-ID: <138752.1790450872@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <138733.1790450856.1@sss.pgh.pa.us> I wrote: > Right. I think the appropriate fix is probably to require > that the checkAsUser values match in RTEs we want to combine. > If we do that, I'm not sure that we need to consider securityQuals > as such (but I've not tried it so maybe I'm missing something). Concretely, about like this. This fixes the given reproducer. I've not put thought into what sort of test cases we might want to add. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="sje-must-compare-user-ids-too.patch"; charset="us-ascii" Content-ID: <138733.1790450856.2@sss.pgh.pa.us> Content-Description: sje-must-compare-user-ids-too.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optim= izer/plan/analyzejoins.c index 2252185564c..d95a8b78dc6 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -37,23 +37,26 @@ #include "optimizer/prep.h" #include "optimizer/restrictinfo.h" #include "parser/parse_agg.h" +#include "parser/parse_relation.h" #include "rewrite/rewriteManip.h" #include "utils/lsyscache.h" = /* * Utility structure. A sorting procedure is needed to simplify the sear= ch - * of SJE-candidate baserels referencing the same database relation. Hav= ing - * collected all baserels from the query jointree, the planner sorts them - * according to the reloid value, groups them with the next pass and atte= mpts - * to remove self-joins. + * for SJE-candidate baserels, which must reference the same database rel= ation + * with the same reader permissions (checkAsUser value). Having collecte= d + * all baserels from the query jointree, remove_self_joins_recurse sorts = them + * according to the reloid and useroid values, groups them with the next = pass + * and attempts to remove self-joins. * - * Preliminary sorting prevents quadratic behavior that can be harmful in= the - * case of numerous joins. + * This preliminary sorting prevents quadratic behavior that can be harmf= ul + * in the case of numerous joins. */ typedef struct { int relid; Oid reloid; + Oid useroid; } SelfJoinCandidate; = bool enable_self_join_elimination; @@ -2011,16 +2014,27 @@ remove_self_joins_recurse(PlannerInfo *root, List = *joinlist) return removed; /* ... but don't fail to report sub-removals */ = /* - * In order to find relations with the same oid we first build an array = of - * candidates and then sort it by oid. + * In order to find relations with the same reloid/useroid we first buil= d + * an array of candidates and then sort it by the oids. */ candidates =3D palloc_array(SelfJoinCandidate, numRels); i =3D -1; j =3D 0; while ((i =3D bms_next_member(relids, i)) >=3D 0) { + RangeTblEntry *rte =3D root->simple_rte_array[i]; + candidates[j].relid =3D i; - candidates[j].reloid =3D root->simple_rte_array[i]->relid; + candidates[j].reloid =3D rte->relid; + if (rte->perminfoindex !=3D 0) + { + RTEPermissionInfo *perminfo; + + perminfo =3D getRTEPermissionInfo(root->parse->rteperminfos, rte); + candidates[j].useroid =3D perminfo->checkAsUser; + } + else + candidates[j].useroid =3D InvalidOid; j++; } = @@ -2028,7 +2042,7 @@ remove_self_joins_recurse(PlannerInfo *root, List *j= oinlist) self_join_candidates_cmp); = /* - * Iteratively form a group of relation indexes with the same oid and + * Iteratively form a group of relation indexes with the same oids and * launch the routine that detects self-joins in this group. * * We remove considered relations from relids as we scan, so that that s= et @@ -2037,11 +2051,13 @@ remove_self_joins_recurse(PlannerInfo *root, List = *joinlist) i =3D 0; for (j =3D 1; j <=3D numRels; j++) { - if (j =3D=3D numRels || candidates[j].reloid !=3D candidates[i].reloid) + if (j =3D=3D numRels || + candidates[j].reloid !=3D candidates[i].reloid || + candidates[j].useroid !=3D candidates[i].useroid) { if (j - i >=3D 2) { - /* Create a group of relation indexes with the same oid */ + /* Create a group of relation indexes with the same oids */ Relids group =3D NULL; = while (i < j) @@ -2073,7 +2089,7 @@ remove_self_joins_recurse(PlannerInfo *root, List *j= oinlist) } = /* - * Compare self-join candidates by their oids. + * Compare self-join candidates by their reloid and then useroid. */ static int self_join_candidates_cmp(const void *a, const void *b) @@ -2083,6 +2099,8 @@ self_join_candidates_cmp(const void *a, const void *= b) = if (ca->reloid !=3D cb->reloid) return (ca->reloid < cb->reloid ? -1 : 1); + else if (ca->useroid !=3D cb->useroid) + return (ca->useroid < cb->useroid ? -1 : 1); else return 0; } ------- =_aaaaaaaaaa0--