Crash in add_paths_to_append_rel

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Crash in add_paths_to_append_rel
Date: 2023-10-09 09:45:41
Message-ID: CAMbWs49w3t03V69XhdCuw+GDwivny4uQUxrkVp6Gejaspt0wMQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I came across a crash in add_paths_to_append_rel() with the query below.

create table t (a int);

create table inh (b int);
create table inh_child() inherits(inh);

explain (costs off)
select * from t left join lateral (select t.a from inh) on true limit 1;
server closed the connection unexpectedly

It was introduced by commit a8a968a8 where we referenced
cheapest_startup_path->param_info without first checking that we have a
valid cheapest_startup_path. We do have checked that the pathlist is
not empty, but that does not imply that the cheapest_startup_path is not
NULL. Normally only unparameterized paths are considered candidates for
cheapest_startup_path, because startup cost does not have too much value
for parameterized paths since they are on the inside of a nestloop. So
if there are no unparameterized paths, the cheapest_startup_path will be
NULL. That is the case in the repro query above. Because of the
lateral reference within PHV, the child rels of 'inh' do not have
unparameterized paths, so their cheapest_startup_path is NULL.

I think we should explicitly check that cheapest_startup_path is not
NULL here. Doing that seems to make the check of pathlist not being
empty unnecessary. Besides, I think we do not need to check that
cheapest_startup_path->param_info is NULL, since cheapest_startup_path
must be unparameterized path. Maybe we can use an Assert instead.

Attached is my proposed fix.

Thanks
Richard

Attachment Content-Type Size
v1-0001-Don-t-assume-that-cheapest_startup_path-exists.patch application/octet-stream 3.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ashutosh Bapat 2023-10-09 09:47:50 Re: Check each of base restriction clauses for constant-FALSE-or-NULL
Previous Message Michael Paquier 2023-10-09 09:37:21 Re: Add a new BGWORKER_BYPASS_ROLELOGINCHECK flag