pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Noah Misch <noah@leadboat.com>
Cc: Richard Guo <guofenglinux@gmail.com>
Cc: Thom Brown <thom@linux.com>
Cc: Jacob Brazeal <jacob.brazeal@gmail.com>
Cc: pgsql-hackers@lists.postgresql.org
Cc: underdog@theori.io
Subject: Re: remove_useless_joins vs. bug #19560
Date: Sat, 26 Sep 2026 15:27:52 -0400
Message-ID: <138752.1790450872@sss.pgh.pa.us> (raw)
In-Reply-To: <79317.1790445906@sss.pgh.pa.us>
References: <CAMbWs4-C4fwZTtY911MfAkW51G2d9TpqoRqmxCiJbjeO0GG1ug@mail.gmail.com>
	<839346.1787765112@sss.pgh.pa.us>
	<CAMbWs4-bgUE1SuNOgMmROhLJbRZz=f4bK7RJAEBQU9uCTSMBnQ@mail.gmail.com>
	<954541.1787846010@sss.pgh.pa.us>
	<CAMbWs4_mfPF5pQVT2FAdvjEMqN7pDT2MGJqkVoJLVyGLYAsU+w@mail.gmail.com>
	<1147476.1787924590@sss.pgh.pa.us>
	<CAMbWs484SQSdzWTgZYKvwtuC6J4m7acw0LfBjp3aWPvav7gC_w@mail.gmail.com>
	<598915.1789613519@sss.pgh.pa.us>
	<CAMbWs49LS9AMn-MV2Zm7xZ6kjZJMa4gVuH_h83nF=7U+0m322Q@mail.gmail.com>
	<CAMbWs48Vo7t2C6jH4aQHvFjkNAc=GCZMNyPY=G15rzw462keGA@mail.gmail.com>
	<20260926174457.13.noahmisch@microsoft.com>
	<79317.1790445906@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

Attachments:

sje-must-compare-user-ids-too.patch (text/x-diff, 3.9K) download diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/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 search - * of SJE-candidate baserels referencing the same database relation. Having - * collected all baserels from the query jointree, the planner sorts them - * according to the reloid value, groups them with the next pass and attempts - * to remove self-joins. + * for SJE-candidate baserels, which must reference the same database relation + * with the same reader permissions (checkAsUser value). Having collected + * 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 harmful + * 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 build + * an array of candidates and then sort it by the oids. */ candidates = palloc_array(SelfJoinCandidate, numRels); i = -1; j = 0; while ((i = bms_next_member(relids, i)) >= 0) { + RangeTblEntry *rte = root->simple_rte_array[i]; + candidates[j].relid = i; - candidates[j].reloid = root->simple_rte_array[i]->relid; + candidates[j].reloid = rte->relid; + if (rte->perminfoindex != 0) + { + RTEPermissionInfo *perminfo; + + perminfo = getRTEPermissionInfo(root->parse->rteperminfos, rte); + candidates[j].useroid = perminfo->checkAsUser; + } + else + candidates[j].useroid = InvalidOid; j++; } @@ -2028,7 +2042,7 @@ remove_self_joins_recurse(PlannerInfo *root, List *joinlist) 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 set @@ -2037,11 +2051,13 @@ remove_self_joins_recurse(PlannerInfo *root, List *joinlist) i = 0; for (j = 1; j <= numRels; j++) { - if (j == numRels || candidates[j].reloid != candidates[i].reloid) + if (j == numRels || + candidates[j].reloid != candidates[i].reloid || + candidates[j].useroid != candidates[i].useroid) { if (j - i >= 2) { - /* Create a group of relation indexes with the same oid */ + /* Create a group of relation indexes with the same oids */ Relids group = NULL; while (i < j) @@ -2073,7 +2089,7 @@ remove_self_joins_recurse(PlannerInfo *root, List *joinlist) } /* - * 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 != cb->reloid) return (ca->reloid < cb->reloid ? -1 : 1); + else if (ca->useroid != cb->useroid) + return (ca->useroid < cb->useroid ? -1 : 1); else return 0; }

view thread (74+ messages)  latest in thread

Message-ID: <138752.1790450872@sss.pgh.pa.us>
Permalink:  ../138752.1790450872@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/138752.1790450872@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, noah@leadboat.com, guofenglinux@gmail.com, thom@linux.com, jacob.brazeal@gmail.com, underdog@theori.io
  Subject: Re: remove_useless_joins vs. bug #19560
  In-Reply-To: <138752.1790450872@sss.pgh.pa.us>
  References: <CAMbWs4-C4fwZTtY911MfAkW51G2d9TpqoRqmxCiJbjeO0GG1ug@mail.gmail.com> <839346.1787765112@sss.pgh.pa.us> <CAMbWs4-bgUE1SuNOgMmROhLJbRZz=f4bK7RJAEBQU9uCTSMBnQ@mail.gmail.com> <954541.1787846010@sss.pgh.pa.us> <CAMbWs4_mfPF5pQVT2FAdvjEMqN7pDT2MGJqkVoJLVyGLYAsU+w@mail.gmail.com> <1147476.1787924590@sss.pgh.pa.us> <CAMbWs484SQSdzWTgZYKvwtuC6J4m7acw0LfBjp3aWPvav7gC_w@mail.gmail.com> <598915.1789613519@sss.pgh.pa.us> <CAMbWs49LS9AMn-MV2Zm7xZ6kjZJMa4gVuH_h83nF=7U+0m322Q@mail.gmail.com> <CAMbWs48Vo7t2C6jH4aQHvFjkNAc=GCZMNyPY=G15rzw462keGA@mail.gmail.com> <20260926174457.13.noahmisch@microsoft.com> <79317.1790445906@sss.pgh.pa.us> <138752.1790450872@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox