ERROR: PlaceHolderVar found where not expected

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: ERROR: PlaceHolderVar found where not expected
Date: 2023-03-14 03:01:40
Message-ID: CAMbWs48Mmvm-acGevXuwpB=g5JMqVSL6i9z5UaJyLGJqa-XPAA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I came across an ERROR as $subject with query below.

create table t (a int, b int);
create statistics if not exists t_s0 (dependencies, ndistinct) on a, b from
t;
insert into t values(1,1);
analyze t;

SELECT * FROM t LEFT JOIN (select true as c0) s0 ON true INNER JOIN (select
true as c0) s1 ON s0.c0 INNER JOIN (select true as c0) s2 ON s0.c0;

This is due to that we use 0 flags for pull_var_clause in
dependency_is_compatible_expression, assuming that the 'clause_expr'
cannot contain Aggrefs, WindowFuncs or PlaceHolderVars. This should be
an oversight as we can see that it's possible to find PHVs here. We can
fix it by

--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -1316,7 +1316,7 @@ dependency_is_compatible_expression(Node *clause,
Index relid, List *statlist, N
if (IsA(clause_expr, RelabelType))
clause_expr = (Node *) ((RelabelType *) clause_expr)->arg;

- vars = pull_var_clause(clause_expr, 0);
+ vars = pull_var_clause(clause_expr, PVC_RECURSE_PLACEHOLDERS);

But I'm not sure if Aggrefs and WindowFuncs are possible to be found
here.

This issue can be seen on 14, 15 and master.

Thanks
Richard

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2023-03-14 03:32:31 Re: ERROR: PlaceHolderVar found where not expected
Previous Message PG Bug reporting form 2023-03-14 01:51:09 BUG #17839: Heap-buffer overflow on float8_to_char with invalid template