Re: BUG #17570: Unrecognized node type for query with statistics on expressions

Lists: pgsql-bugs
From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: a(dot)kozhemyakin(at)postgrespro(dot)ru
Subject: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 08:27:06
Message-ID: 17570-f2f2e0f4bccf0965@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17570
Logged by: Alexander Kozhemyakin
Email address: a(dot)kozhemyakin(at)postgrespro(dot)ru
PostgreSQL version: 14.4
Operating system: ubunu 20.04
Description:

When executing the following script:
CREATE TABLE t(a INT, b VARCHAR);
INSERT INTO t(a, b) SELECT i, i FROM generate_series(1,100) s(i);
CREATE STATISTICS t_stats (mcv) ON (a), (b) FROM t;
ANALYZE t;
CREATE ROLE u;
--GRANT SELECT ON t TO u;
SET SESSION AUTHORIZATION u;
SELECT * FROM t WHERE a = 1 AND b::int = 1;

I get:
ERROR: unrecognized node type: 1697192808

And it fails with error "permission denied for table t" when condition is
just "a = 1" or "b::int = 1".

The same query executed successfully, if the select permission is granted to
user.
The first bad commit is a4d75c86b


From: Japin Li <japinli(at)hotmail(dot)com>
To: a(dot)kozhemyakin(at)postgrespro(dot)ru, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 10:33:35
Message-ID: MEYP282MB166998A90E685A09A9576F59B69F9@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs


On Thu, 04 Aug 2022 at 16:27, PG Bug reporting form <noreply(at)postgresql(dot)org> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 17570
> Logged by: Alexander Kozhemyakin
> Email address: a(dot)kozhemyakin(at)postgrespro(dot)ru
> PostgreSQL version: 14.4
> Operating system: ubunu 20.04
> Description:
>
> When executing the following script:
> CREATE TABLE t(a INT, b VARCHAR);
> INSERT INTO t(a, b) SELECT i, i FROM generate_series(1,100) s(i);
> CREATE STATISTICS t_stats (mcv) ON (a), (b) FROM t;
> ANALYZE t;
> CREATE ROLE u;
> --GRANT SELECT ON t TO u;
> SET SESSION AUTHORIZATION u;
> SELECT * FROM t WHERE a = 1 AND b::int = 1;
>
> I get:
> ERROR: unrecognized node type: 1697192808
>
> And it fails with error "permission denied for table t" when condition is
> just "a = 1" or "b::int = 1".
>
> The same query executed successfully, if the select permission is granted to
> user.
> The first bad commit is a4d75c86b

Yeah, it is a bug.

When we don't have table privilege, we check individual columns, however, it
passes a wrong pointer to pull_varattnos(), which leads to this error.

There has another bug when checking the attribute privilege, since the bitmap
contains the offset by FirstLowInvalidHeapAttributeNumber.

Attach a patch to fix it. Any thoughts?

--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

Attachment Content-Type Size
fix-unrecognized-node-type-for-query-with-statistics.diff text/x-patch 1.0 KB

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 10:37:43
Message-ID: CAMbWs4_PPrJ7JFXqewQfkMLP6-2kHi+5=11U0csQVTx7iq7esw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Thu, Aug 4, 2022 at 4:28 PM PG Bug reporting form <noreply(at)postgresql(dot)org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference: 17570
> Logged by: Alexander Kozhemyakin
> Email address: a(dot)kozhemyakin(at)postgrespro(dot)ru
> PostgreSQL version: 14.4
> Operating system: ubunu 20.04
> Description:
>
> When executing the following script:
> CREATE TABLE t(a INT, b VARCHAR);
> INSERT INTO t(a, b) SELECT i, i FROM generate_series(1,100) s(i);
> CREATE STATISTICS t_stats (mcv) ON (a), (b) FROM t;
> ANALYZE t;
> CREATE ROLE u;
> --GRANT SELECT ON t TO u;
> SET SESSION AUTHORIZATION u;
> SELECT * FROM t WHERE a = 1 AND b::int = 1;
>
> I get:
> ERROR: unrecognized node type: 1697192808

Thanks for the report! I can reproduce it in HEAD. Propose the attached
for fix.

Thanks
Richard

Attachment Content-Type Size
v1-0001-fix-bug-17570.patch application/octet-stream 2.1 KB

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 11:25:58
Message-ID: CAMbWs49MoDfQzbcmH4VWisTNVDfCuZcALPOzJxGac2UV5KVi7w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: Postg메이저 토토 사이트SQL : Postg메이저 토토 사이트SQL 메일 링리스트 : 2022-08-04 이후 PGSQL-BUGS 11:25

On Thu, Aug 4, 2022 at 6:37 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:

>
> On Thu, Aug 4, 2022 at 4:28 PM PG Bug reporting form <
> noreply(at)postgresql(dot)org> wrote:
>
>> The following bug has been logged on the website:
>>
>> Bug reference: 17570
>> Logged by: Alexander Kozhemyakin
>> Email address: a(dot)kozhemyakin(at)postgrespro(dot)ru
>> PostgreSQL version: 14.4
>> Operating system: ubunu 20.04
>> Description:
>>
>> When executing the following script:
>> CREATE TABLE t(a INT, b VARCHAR);
>> INSERT INTO t(a, b) SELECT i, i FROM generate_series(1,100) s(i);
>> CREATE STATISTICS t_stats (mcv) ON (a), (b) FROM t;
>> ANALYZE t;
>> CREATE ROLE u;
>> --GRANT SELECT ON t TO u;
>> SET SESSION AUTHORIZATION u;
>> SELECT * FROM t WHERE a = 1 AND b::int = 1;
>>
>> I get:
>> ERROR: unrecognized node type: 1697192808
>
>
> Thanks for the report! I can reproduce it in HEAD. Propose the attached
> for fix.
>

Oops.. sorry I didn't notice Japin's email before I hit the 'send' for
my reply. The two patches are almost the same, except that before we
check against whole-row reference, I also adjust the attnum by
FirstLowInvalidHeapAttributeNumber.

Thanks
Richard


From: Japin Li <japinli(at)hotmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: a(dot)kozhemyakin(at)postgrespro(dot)ru, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 14:59:07
Message-ID: ME3P282MB1667E822B3DEA81E46EEE11CB69F9@ME3P282MB1667.AUSP282.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: Postg사설 토토SQL : Postg사설 토토SQL 메일 링리스트 : 2022-08-04 이후 PGSQL-BUGS 14:59


On Thu, 04 Aug 2022 at 19:25, Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> On Thu, Aug 4, 2022 at 6:37 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
>
>>
>> On Thu, Aug 4, 2022 at 4:28 PM PG Bug reporting form <
>> noreply(at)postgresql(dot)org> wrote:
>>
>>> The following bug has been logged on the website:
>>>
>>> Bug reference: 17570
>>> Logged by: Alexander Kozhemyakin
>>> Email address: a(dot)kozhemyakin(at)postgrespro(dot)ru
>>> PostgreSQL version: 14.4
>>> Operating system: ubunu 20.04
>>> Description:
>>>
>>> When executing the following script:
>>> CREATE TABLE t(a INT, b VARCHAR);
>>> INSERT INTO t(a, b) SELECT i, i FROM generate_series(1,100) s(i);
>>> CREATE STATISTICS t_stats (mcv) ON (a), (b) FROM t;
>>> ANALYZE t;
>>> CREATE ROLE u;
>>> --GRANT SELECT ON t TO u;
>>> SET SESSION AUTHORIZATION u;
>>> SELECT * FROM t WHERE a = 1 AND b::int = 1;
>>>
>>> I get:
>>> ERROR: unrecognized node type: 1697192808
>>
>>
>> Thanks for the report! I can reproduce it in HEAD. Propose the attached
>> for fix.
>>
>
> Oops.. sorry I didn't notice Japin's email before I hit the 'send' for
> my reply. The two patches are almost the same, except that before we
> check against whole-row reference, I also adjust the attnum by
> FirstLowInvalidHeapAttributeNumber.
>

Doesn't matter.

Attached to new patch add a test-case from Alexander Kozhemyakin.

--
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.

Attachment Content-Type Size
v1-0001-Fix-unrecognized-node-type-about-statistics-on-ex.patch text/x-patch 3.6 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Japin Li <japinli(at)hotmail(dot)com>
Cc: Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, Richard Guo <guofenglinux(at)gmail(dot)com>, a(dot)kozhemyakin(at)postgrespro(dot)ru, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-04 15:42:38
Message-ID: 3508845.1659627758@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Japin Li <japinli(at)hotmail(dot)com> writes:
> Attached to new patch add a test-case from Alexander Kozhemyakin.

It appears to me that this is actually very thoroughly broken.

* statext_is_compatible_clause_internal is not on board with
offsetting the attnums.

* Neither is this check for a whole-row reference (line 1628, in HEAD):

if (bms_is_member(InvalidAttrNumber, clause_attnums))

* Although the coverage report claims this code is exercised, the
fact that nothing failed when you made only a partial fix says it's
not exercised well enough.

* The comments for statext_is_compatible_clause suck. If they'd
defined what the arguments are, perhaps this mess would have been
prevented. For extra credit, it'd be really nice to define what
"is compatible" means. I'd sure not have thought that that would
include permissions checks.

regards, tom lane


From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Japin Li <japinli(at)hotmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-05 00:49:30
Message-ID: CAMbWs4-g44xwS6KCEmSaoc8jqcTd1n8tt8u10=hChFTEyRJQbA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Thu, Aug 4, 2022 at 11:42 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> * statext_is_compatible_clause_internal is not on board with
> offsetting the attnums.

Correct. Good catch! Fix this in v2 patch.

> * Neither is this check for a whole-row reference (line 1628, in HEAD):
>
> if (bms_is_member(InvalidAttrNumber, clause_attnums))

Actually this has been fixed in my v1 patch [1].

> * Although the coverage report claims this code is exercised, the
> fact that nothing failed when you made only a partial fix says it's
> not exercised well enough.

That's true. We need to provide a better test case to cover this.

>
> * The comments for statext_is_compatible_clause suck. If they'd
> defined what the arguments are, perhaps this mess would have been
> prevented. For extra credit, it'd be really nice to define what
> "is compatible" means. I'd sure not have thought that that would
> include permissions checks.
>

Concur with that.

[1]:
/message-id/CAMbWs4_PPrJ7JFXqewQfkMLP6-2kHi%2B5%3D11U0csQVTx7iq7esw%40mail.gmail.com

Thanks
Richard

Attachment Content-Type Size
v2-0001-fix-bug-17570.patch application/octet-stream 2.5 KB

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Japin Li <japinli(at)hotmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-05 01:25:08
Message-ID: CAMbWs4-w6qW-_Kr3771Y1VaJMHstSYDjLb8L7Vu++kUdev057g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: 503 토토 베이 페치 실패

On Thu, Aug 4, 2022 at 11:42 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> * The comments for statext_is_compatible_clause suck. If they'd
> defined what the arguments are, perhaps this mess would have been
> prevented. For extra credit, it'd be really nice to define what
> "is compatible" means. I'd sure not have thought that that would
> include permissions checks.

I think by 'compatible' it just means the clause is in the form that can
be estimated using MCV lists. Maybe we can define it as that.

In statext_is_compatible_clause() we have the permission checks to see
if we are allowed to read all required attributes, which indeed needs to
be mentioned in the function comments.

Thanks
Richard


From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Japin Li <japinli(at)hotmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-05 09:03:13
Message-ID: CAMbWs48FfgCZsA1URKboCHyNjXxDAZR=esFzwfRgqL+8H7+WBg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

On Fri, Aug 5, 2022 at 8:49 AM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:

> On Thu, Aug 4, 2022 at 11:42 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
>> * statext_is_compatible_clause_internal is not on board with
>> offsetting the attnums.
>
>
> Correct. Good catch! Fix this in v2 patch.
>

I realized the fix in v2 is not correct. The attnums generated by
statext_is_compatible_clause is expected to be NOT offsetting by its
caller, i.e. statext_mcv_clauselist_selectivity. Revise that in v3 patch
by doing the offsetting when composing clause_attnums that need to have
permission check.

> * Although the coverage report claims this code is exercised, the
>> fact that nothing failed when you made only a partial fix says it's
>> not exercised well enough.
>
>
> That's true. We need to provide a better test case to cover this.
>

Also I'm trying to provide a better test case and at last come up with a
case in v3 like:

SELECT * FROM tststats.priv_test_tbl where a = 1 and
tststats.priv_test_tbl.* > (1, 1) is not null;

For this case, the clause 'a = 1' would cover that the attnum comes from
statext_is_compatible_clause_internal. And the clause 'priv_test_tbl.* >
(1, 1) is not null' would cover that the attnum comes from 'exprs' and
it has a whole-row reference.

Thanks
Richard

Attachment Content-Type Size
v3-0001-fix-bug-17570.patch application/octet-stream 4.0 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: Japin Li <japinli(at)hotmail(dot)com>, Tomas Vondra <tomas(dot)vondra(at)enterprisedb(dot)com>, a(dot)kozhemyakin(at)postgrespro(dot)ru, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17570: Unrecognized node type for query with statistics on expressions
Date: 2022-08-05 15:08:27
Message-ID: 3943162.1659712107@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-bugs

Richard Guo <guofenglinux(at)gmail(dot)com> writes:
> I realized the fix in v2 is not correct. The attnums generated by
> statext_is_compatible_clause is expected to be NOT offsetting by its
> caller, i.e. statext_mcv_clauselist_selectivity. Revise that in v3 patch
> by doing the offsetting when composing clause_attnums that need to have
> permission check.

Yeah, I was afraid that that route would require the patch to metastasize
into more places. Agreed on keeping the argument definition the same
and coping locally instead. But I don't think that you've done enough
to address the root cause of this bug, which is the woeful
under-documentation of the argument. I'll have a go at that part.

regards, tom lane