From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dennis White <dwhite@seawardmoon.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: GIST combo index condition chosen for users queries is different from table owner's query
Date: Mon, 19 Sep 2022 19:28:42 -0400
Message-ID: <545166.1663630122@sss.pgh.pa.us> (raw)
In-Reply-To: <CAE=rie_0u9rDBSzMKik4mD9jop=tmtrX2Ztrf014mTYVSyeZ_g@mail.gmail.com>
References: <CAE=rie_0u9rDBSzMKik4mD9jop=tmtrX2Ztrf014mTYVSyeZ_g@mail.gmail.com>
Dennis White <dwhite@seawardmoon.com> writes:
> Is there something I can do to allow users queries to use the index with a
> condition like that used for the table owner's query?
It looks like the problem in your badly-optimized query is that
there is not an indexable condition being extracted from the
ST_INTERSECTS() call. In the well-optimized one, we've got
-> Index Scan using qtest_posit_t1_p2022_09_02_posit_toi_security_tag_idx...
Index Cond: ((posit &&
'0103000020E610000001000000050000002C9B3924B5EE504091F3FE3F4E782F40BEA25BAFE9894840EE7C3F355EFA23C0F47002D369FF434019A9F7544EDB0FC0BE88B663EACE4940AB08371955FA31402C9B3924B5EE504091F3FE3F4E782F40'::geometry) AND ...
Filter: ((test.user_has_access(security_tag) = '1'::text) AND
st_intersects(posit,
'0103000020E610000001000000050000002C9B3924B5EE504091F3FE3F4E782F40BEA25BAFE9894840EE7C3F355EFA23C0F47002D369FF434019A9F7544EDB0FC0BE88B663EACE4940AB08371955FA31402C9B3924B5EE504091F3FE3F4E782F40'::geometry))
I presume what's happening there is that st_intersects() has got a support
function that knows that "st_intersects(foo, bar)" implies "foo && bar"
and the latter can be used with an index on foo.
However, to do that in the presence of RLS we have to know that the
extracted condition would be leakproof. I'm not sure that the geometry &&
operator is leakproof in the first place; and even if it is, we might not
consider this option unless st_intersects() is also marked leakproof,
which most likely it isn't. You'd have to ask the PostGIS crew whether
either of those things would be safe to consider leakproof ... but I'm
betting they'll say that doing so would create an unreasonably large
bug surface.
By and large, the combination of RLS with complicated WHERE conditions
is just deadly for performance, because most of the time we won't be
able to use the WHERE conditions until after applying the RLS filter.
Do you really need to use RLS in this application? If you're stuck
doing so, you could maybe ameliorate things by implementing the RLS
check functions in the fastest way you can, like writing C code
for them.
regards, tom lane
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, dwhite@seawardmoon.com
Subject: Re: GIST combo index condition chosen for users queries is different from table owner's query
In-Reply-To: <545166.1663630122@sss.pgh.pa.us>
References: <CAE=rie_0u9rDBSzMKik4mD9jop=tmtrX2Ztrf014mTYVSyeZ_g@mail.gmail.com> <545166.1663630122@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