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.98.2) (envelope-from ) id 1xFHC3-00000001XPm-3mtB for pgsql-bugs@arkaria.postgresql.org; Fri, 09 Oct 2026 20:27:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xFHC2-000000019yB-23ob for pgsql-bugs@arkaria.postgresql.org; Fri, 09 Oct 2026 20:27:50 +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.98.2) (envelope-from ) id 1xFHC2-000000019y3-0xt1 for pgsql-bugs@lists.postgresql.org; Fri, 09 Oct 2026 20:27:50 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xFHBz-00000001DsP-1RWl for pgsql-bugs@lists.postgresql.org; Fri, 09 Oct 2026 20:27:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 699KRiTj2924326; Fri, 9 Oct 2026 16:27:44 -0400 From: Tom Lane To: Chinmay Kanchi cc: pgsql-bugs@lists.postgresql.org Subject: Re: GIN disagrees with seq scan when NOT and AND operators are combined In-reply-to: References: Comments: In-reply-to Chinmay Kanchi message dated "Fri, 09 Oct 2026 10:04:44 -0700" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2924311.1791577659.0@sss.pgh.pa.us> Date: Fri, 09 Oct 2026 16:27:44 -0400 Message-ID: <2924325.1791577664@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2924311.1791577659.1@sss.pgh.pa.us> Chinmay Kanchi writes: > When working on pg_lion (https://github.com/cgkanchi/pg_lion), I stumbled > upon a bug where GIN returns fewer rows when multiple ! operators are > specified. I was able to repro this on all versions from 16 to trunk. Thanks for the report. (For the archives' sake, note this repro requires "set enable_seqscan = 0", else you get a seqscan plan and no bug.) I'm not too familiar with this code, but it looks like extract_jsp_bool_expr concludes that !($.b == 20) is not supported as a GIN scan condition, and then the next recursion level up does the wrong thing with the negated && condition. The attached quick hack seems to fix it. I didn't look around for other instances of the same thinko, but there might be some. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="wip-fix-jsonb-gin-not-semantics.patch"; charset="us-ascii" Content-ID: <2924311.1791577659.2@sss.pgh.pa.us> Content-Description: wip-fix-jsonb-gin-not-semantics.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/jsonb_gin.c b/src/backend/utils/adt/jso= nb_gin.c index f5dbd5589d3..3efc4943d65 100644 --- a/src/backend/utils/adt/jsonb_gin.c +++ b/src/backend/utils/adt/jsonb_gin.c @@ -591,6 +591,7 @@ extract_jsp_bool_expr(JsonPathGinContext *cxt, JsonPat= hGinPath path, case jpiAnd: /* expr && expr */ case jpiOr: /* expr || expr */ { + bool is_and =3D (not ^ (jsp->type =3D=3D jpiAnd)); JsonPathItem arg; JsonPathGinNode *larg; JsonPathGinNode *rarg; @@ -604,13 +605,18 @@ extract_jsp_bool_expr(JsonPathGinContext *cxt, JsonP= athGinPath path, = if (!larg || !rarg) { - if (jsp->type =3D=3D jpiOr) + /* + * For OR, we have to fail if either arm is unsupported. + * For AND, we can just ignore the unsupported arm and + * apply the supported one. + */ + if (!is_and) return NULL; = return larg ? larg : rarg; } = - type =3D not ^ (jsp->type =3D=3D jpiAnd) ? JSP_GIN_AND : JSP_GIN_OR; + type =3D is_and ? JSP_GIN_AND : JSP_GIN_OR; = return make_jsp_expr_node_binary(type, larg, rarg); } ------- =_aaaaaaaaaa0--