Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r2rNy-0017qk-WF for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Nov 2023 11:15:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r2rNw-006uO4-Nl for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Nov 2023 11:15:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r2rNw-006uNt-9p for pgsql-hackers@lists.postgresql.org; Tue, 14 Nov 2023 11:15:12 +0000 Received: from mail-yw1-x1130.google.com ([2607:f8b0:4864:20::1130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r2rNt-005Wav-PB for pgsql-hackers@postgresql.org; Tue, 14 Nov 2023 11:15:11 +0000 Received: by mail-yw1-x1130.google.com with SMTP id 00721157ae682-5af6c445e9eso64013447b3.0 for ; Tue, 14 Nov 2023 03:15:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1699960509; x=1700565309; darn=postgresql.org; h=cc:to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=fz2VdvcQVZk94MKU4G+rULJr3pYPcjFTNSdktg1Pfgg=; b=JVeLaW4wwqJc5thnKMt63BNEQQ1jJnddQzE7qrqGiNcZu2SMYkI3Ix3zJnJI8hU/xV TBE/fM5wArzCH7elPbm9m9BgrYDexuknbY6WlM0qgzHhsf07pF40alZ83ZTm9kC6ugY7 B1tzi06i3BUWm1FpDOgKotnHuzJ7xDooe3fZP1pW+05WqAwGwvmfjmh0sUzUf6Ib0Bl+ mhee7fyqW2YxBr1xag12BDDxtsGsU+yPqGP8N0A1XpwNRSb2TI1Zo/3HKqDQ9xYCwErx Np0easPefLH7a1PsG4KZRAHC+iprenoyk5lzqDar/4aAqEUnnEyd0YCT2ZLQ27OYNQkx w7ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699960509; x=1700565309; h=cc:to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=fz2VdvcQVZk94MKU4G+rULJr3pYPcjFTNSdktg1Pfgg=; b=Xv5z3hwS6NzZ+bQO0hYmH5T5DqAYgt0UllL3Sn/kdxVhByH4iovliLRk5rzSimihLi Fc3OktST1QpVTPuveikga04XwpRp6hEpEi8LEzU1fc5tJIqO7VgxD0AlmD8hPvckAq5R E+XBiMMUMyDmsLaPD5Bwq3JyzhgxJ+kc+y6FyMdBUYXo/NrWctRwXLHFeCw7woBw5joI 6D+zB/69Fg2W2DaSJH+6ol5AiOAYdG7tX0sCZkPyh5f6mQqSfjCQRSrHGZSI+qZ/QuZb NBjdTnAPS6vW2xmqcIRfOhOAIIfJfi8cyo59zcjUhAm12fSgO1ReSw8t1ZjjrwUEd7af LWQg== X-Gm-Message-State: AOJu0Yw8BM6fLjYx0ofuVPvu4GiA4thYW0rXAdm4FPz0aS2LKC0pDdbl SWsKzBwBgq1ebUO3W8sucqLacavDXRjHYhW3MoO1oXtukZI= X-Google-Smtp-Source: AGHT+IH3QNx5poci6Xy0isOBf9FgQ8JzPKFTdFy0U+ewKRUDSfjTMtvQ+g7EYLr6o5+E2XIEU2RHMIfiJrWBkqnNggs= X-Received: by 2002:a0d:d5c3:0:b0:5b3:26e1:320d with SMTP id x186-20020a0dd5c3000000b005b326e1320dmr9377135ywd.40.1699960508837; Tue, 14 Nov 2023 03:15:08 -0800 (PST) MIME-Version: 1.0 From: Richard Guo Date: Tue, 14 Nov 2023 19:14:57 +0800 Message-ID: Subject: Assert failure on 'list_member_ptr(rel->joininfo, restrictinfo)' To: PostgreSQL-development Cc: Alexander Korotkov Content-Type: multipart/mixed; boundary="000000000000436bf0060a1ae67d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000436bf0060a1ae67d Content-Type: multipart/alternative; boundary="000000000000436bee060a1ae67b" --000000000000436bee060a1ae67b Content-Type: text/plain; charset="UTF-8" While working on BUG #18187 [1], I noticed that we also have issues with how SJE replaces join clauses involving the removed rel. As an example, consider the query below, which would trigger an Assert. create table t (a int primary key, b int); explain (costs off) select * from t t1 inner join t t2 on t1.a = t2.a left join t t3 on t1.b > 1 and t1.b < 2; server closed the connection unexpectedly The Assert failure happens in remove_self_join_rel() when we're trying to remove t1. The two join clauses of t1, 't1.b > 1' and 't1.b < 2', share the same pointer of 'required_relids', which is {t1, t3} at first. After we've performed replace_varno for the first clause, the required_relids becomes {t2, t3}, which is no problem. However, the second clause's required_relids also becomes {t2, t3}, because they are actually the same pointer. So when we proceed with replace_varno on the second clause, we'd trigger the Assert. Off the top of my head I'm thinking that we can fix this kind of issue by bms_copying the bitmapset first before we make a substitution in replace_relid(), like attached. Alternatively, we can revise distribute_qual_to_rels() as below so that different RestrictInfos don't share the same pointer of required_relids. --- a/src/backend/optimizer/plan/initsplan.c +++ b/src/backend/optimizer/plan/initsplan.c @@ -2385,7 +2385,7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause, * nonnullable-side rows failing the qual. */ Assert(ojscope); - relids = ojscope; + relids = bms_copy(ojscope); Assert(!pseudoconstant); } else With this way, I'm worrying that there are other places where we should avoid sharing the same pointer to Bitmapset structure. I'm not sure how to discover all these places. Any thoughts? [1] https://www.postgresql.org/message-id/flat/18187-831da249cbd2ff8e%40postgresql.org Thanks Richard --000000000000436bee060a1ae67b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
While working on BUG #18187 [1], I noticed that we also ha= ve issues with
how SJE replaces join clauses involving the removed rel.= =C2=A0 As an example,
consider the query below, which would trigger an A= ssert.

create table t (a int primary key, b int);

explain (co= sts off)
select * from t t1
=C2=A0 =C2=A0inner join t t2 on t1.a =3D = t2.a
=C2=A0 =C2=A0 left join t t3 on t1.b > 1 and t1.b < 2;
ser= ver closed the connection unexpectedly

The Assert failure happens in= remove_self_join_rel() when we're trying
to remove t1.=C2=A0 The tw= o join clauses of t1, 't1.b > 1' and 't1.b < 2',
s= hare the same pointer of 'required_relids', which is {t1, t3} at fi= rst.
After we've performed replace_varno for the first clause, therequired_relids becomes {t2, t3}, which is no problem.=C2=A0 However, the=
second clause's required_relids also becomes {t2, t3}, because they= are
actually the same pointer.=C2=A0 So when we proceed with replace_va= rno on the
second clause, we'd trigger the Assert.

Off the to= p of my head I'm thinking that we can fix this kind of issue
by bms_= copying the bitmapset first before we make a substitution in
replace_rel= id(), like attached.

Alternatively, we can revise distribute_qual_to= _rels() as below so that
different RestrictInfos don't share the sam= e pointer of required_relids.

--- a/src/backend/optimizer/plan/inits= plan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -2385,7 +2385,= 7 @@ distribute_qual_to_rels(PlannerInfo *root, Node *clause,
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0* nonnullable-side rows failing the qual.
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0*/
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Assert(ojscop= e);
- =C2=A0 =C2=A0 =C2=A0 relids =3D ojscope;
+ =C2=A0 =C2=A0 =C2=A0= relids =3D bms_copy(ojscope);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 Assert(!pseud= oconstant);
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 else

With this way, = I'm worrying that there are other places where we should
avoid shari= ng the same pointer to Bitmapset structure.=C2=A0 I'm not sure how
t= o discover all these places.

Any thoughts?

[1] https://www.postgresql.org/message-id/flat/18187-831da249cbd2ff8e%40p= ostgresql.org

Thanks
Richard
--000000000000436bee060a1ae67b-- --000000000000436bf0060a1ae67d Content-Type: application/octet-stream; name="v1-0001-Fix-how-SJE-replaces-join-clauses.patch" Content-Disposition: attachment; filename="v1-0001-Fix-how-SJE-replaces-join-clauses.patch" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_loy8jseq0 RnJvbSA4NjY5ZWRiOTkxZjRjYjlkNDhmZThmODViZWM4NjYzNmVlZjI0NjUyIE1vbiBTZXAgMTcg MDA6MDA6MDAgMjAwMQpGcm9tOiBSaWNoYXJkIEd1byA8Z3VvZmVuZ2xpbnV4QGdtYWlsLmNvbT4K RGF0ZTogVHVlLCAxNCBOb3YgMjAyMyAxODo1NToyNCArMDgwMApTdWJqZWN0OiBbUEFUQ0ggdjFd IEZpeCBob3cgU0pFIHJlcGxhY2VzIGpvaW4gY2xhdXNlcwoKLS0tCiBzcmMvYmFja2VuZC9vcHRp bWl6ZXIvcGxhbi9hbmFseXplam9pbnMuYyB8ICA5ICsrKysrKystLQogc3JjL3Rlc3QvcmVncmVz cy9leHBlY3RlZC9qb2luLm91dCAgICAgICAgfCAxNSArKysrKysrKysrKysrKysKIHNyYy90ZXN0 L3JlZ3Jlc3Mvc3FsL2pvaW4uc3FsICAgICAgICAgICAgIHwgIDYgKysrKysrCiAzIGZpbGVzIGNo YW5nZWQsIDI4IGluc2VydGlvbnMoKyksIDIgZGVsZXRpb25zKC0pCgpkaWZmIC0tZ2l0IGEvc3Jj L2JhY2tlbmQvb3B0aW1pemVyL3BsYW4vYW5hbHl6ZWpvaW5zLmMgYi9zcmMvYmFja2VuZC9vcHRp bWl6ZXIvcGxhbi9hbmFseXplam9pbnMuYwppbmRleCBiOWJlMTlhNjg3Li5mOTQ0NDlkZjk0IDEw MDY0NAotLS0gYS9zcmMvYmFja2VuZC9vcHRpbWl6ZXIvcGxhbi9hbmFseXplam9pbnMuYworKysg Yi9zcmMvYmFja2VuZC9vcHRpbWl6ZXIvcGxhbi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