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 1qY2c2-000d8R-VH for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Aug 2023 10:58:23 +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 1qY2c1-005lH0-Ra for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Aug 2023 10:58:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qY2c1-005lGs-G6 for pgsql-hackers@lists.postgresql.org; Mon, 21 Aug 2023 10:58:21 +0000 Received: from mail-oo1-xc2b.google.com ([2607:f8b0:4864:20::c2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qY2by-0009nR-NN for pgsql-hackers@postgresql.org; Mon, 21 Aug 2023 10:58:20 +0000 Received: by mail-oo1-xc2b.google.com with SMTP id 006d021491bc7-570c5d0fac6so829493eaf.3 for ; Mon, 21 Aug 2023 03:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692615497; x=1693220297; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=tVPioKCvnpbNXxm9tCu+ydfHCbgvR+qw17V5cVBXw6o=; b=K3FvWsVVYXZsxj7xfyOHEKEyl465Lr9sJiWTUU058EK/K7fsnkzng5KfVOkk5Z4Dkm rP3ErcMSnPtmTCrRzW3QnR+q/zPF2QQakZRg7T6vW1c1L37EKulNxVd/dovcSyzD/eth LCzQaWCfNLGO6Nxg+7tQxiJEMG91YaRKp92DT/xQi6EfxSC0+ToT7ZnuLFuJGWd7xXYH cGiYvM8GT4kTI/mutyJWI6z0txb7Lxymud1Gbmnfqf8ReQprop3RRs5ps5AIzwERNZ/j egj1x6GHdnf7NTkE0lMQ9XoPIj+CXcush/m9iVhCjxhZ3S8rXcj9FeNR0n6QNZAUQYNb 2Nqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692615497; x=1693220297; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tVPioKCvnpbNXxm9tCu+ydfHCbgvR+qw17V5cVBXw6o=; b=UJr41ltf5wdnBJXZWY5mrtAI/FUhtYHmtpcmJumX/o/rJhxwCVbq2wYqLUzQQVZICh kMhGzXF/0o72hrqzQIhTZ3y4jitxTypSJI/Ksf7Lq3a5Eln0ucACqYUlF3vTBxid5mYy VcTHZqYoBPOWXivjU4DewQT78f7xP/jAQ4rjU3httkxR4laFbs6jz7RsaKxnQ+uWPsje ClXyLEgpUxGSeWy4kzMM9BenNHacYeafpM8ef9wBDu5zuvgx0OpHRXsoFbwe+NLRsG5z oSU7EacjXS1uAVG+T2oeP7zky9rLE0sLvRwP0EhI1G7kstaFUy2z89NQ1KaOfmVlhugb HFHg== X-Gm-Message-State: AOJu0YzQ2kB5luda5izlVHEXHgrVfeJDGYIL/bLe3P+NBDwURUElJ1F5 NJUaTuC4FKyA4k/NNC+0ANhqnypX5JUFrRTkMdc= X-Google-Smtp-Source: AGHT+IGah6/XFKR2/djBYLfGgd7B3rb2faqswkAz37wZdAZUQIdPzfXIPYciMC+MOQ/LItB/rO85J7yntH4cyn95Rkw= X-Received: by 2002:a4a:245a:0:b0:569:ac56:ca98 with SMTP id v26-20020a4a245a000000b00569ac56ca98mr6574192oov.3.1692615496948; Mon, 21 Aug 2023 03:58:16 -0700 (PDT) MIME-Version: 1.0 References: <3507485.1691090027@sss.pgh.pa.us> <900892914fdc3f477b101d699efb40e0@anastigmatix.net> <903341.1692022214@sss.pgh.pa.us> <4b97f1a1dd9b6e45443d24870d3be698@anastigmatix.net> <111272f2dc112c7becdd35ad89f6b935@anastigmatix.net> In-Reply-To: From: Andy Fan Date: Mon, 21 Aug 2023 18:58:05 +0800 Message-ID: Subject: Re: Extract numeric filed in JSONB more effectively To: Chapman Flack Cc: jian he , Pavel Stehule , Tom Lane , pgsql-hackers Content-Type: multipart/alternative; boundary="0000000000006fdfee06036cc15c" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006fdfee06036cc15c Content-Type: text/plain; charset="UTF-8" > > > Interestingly, when I relabel both places, like this: > > Oid targetOid = fexpr->funcresulttype; > Const *target = makeConst( > OIDOID, -1, InvalidOid, sizeof(Oid), > ObjectIdGetDatum(targetOid), false, true); > RelabelType *rTarget = makeRelabelType((Expr *)target, > INTERNALOID, -1, InvalidOid, COERCE_IMPLICIT_CAST); > fexpr->funcid = new_func_id; > fexpr->args = opexpr->args; > fexpr->args = list_insert_nth(fexpr->args, 0, rTarget); > expr = (Expr *)makeRelabelType((Expr *)fexpr, > targetOid, -1, InvalidOid, COERCE_IMPLICIT_CAST); > } > PG_RETURN_POINTER(expr); > > EXPLAIN looks like this: > > Seq Scan on pg_temp.test_jsonb > Output: jsonb_array_element_type(('23'::oid)::internal, test_json, > 0), (test_json -> 0) > Filter: (test_jsonb.json_type = 'scalarint'::text) > > With COERCE_IMPLICIT_CAST both places, the relabeling of the > function result is invisible, but the relabeling of the argument > is visible. > > I think this is because get_rule_expr's showimplicit is always true for args in this case, checking the implementation of get_rule_expr, I found PG behavior like this in many places. -- Best Regards Andy Fan --0000000000006fdfee06036cc15c Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Interestingly, when I relabel both places, like this:

=C2=A0 =C2=A0 =C2=A0Oid=C2=A0 =C2=A0targetOid =3D fexpr->funcresulttype;=
=C2=A0 =C2=A0 =C2=A0Const *target=C2=A0 =3D makeConst(
=C2=A0 =C2=A0 =C2=A0 =C2=A0OIDOID, -1, InvalidOid, sizeof(Oid),
=C2=A0 =C2=A0 =C2=A0 =C2=A0ObjectIdGetDatum(targetOid), false, true);
=C2=A0 =C2=A0 =C2=A0RelabelType *rTarget =3D makeRelabelType((Expr *)target= ,
=C2=A0 =C2=A0 =C2=A0 =C2=A0INTERNALOID, -1, InvalidOid, COERCE_IMPLICIT_CAS= T);
=C2=A0 =C2=A0 =C2=A0fexpr->funcid =3D new_func_id;
=C2=A0 =C2=A0 =C2=A0fexpr->args =3D opexpr->args;
=C2=A0 =C2=A0 =C2=A0fexpr->args =3D list_insert_nth(fexpr->args, 0, r= Target);
=C2=A0 =C2=A0 =C2=A0expr =3D (Expr *)makeRelabelType((Expr *)fexpr,
=C2=A0 =C2=A0 =C2=A0 =C2=A0targetOid, -1, InvalidOid, COERCE_IMPLICIT_CAST)= ;
=C2=A0 =C2=A0}
=C2=A0 =C2=A0PG_RETURN_POINTER(expr);

EXPLAIN looks like this:

=C2=A0 Seq Scan on pg_temp.test_jsonb
=C2=A0 =C2=A0 Output: jsonb_array_element_type(('23'::oid)::interna= l, test_json,
0), (test_json -> 0)
=C2=A0 =C2=A0 Filter: (test_jsonb.json_type =3D 'scalarint'::text)<= br>
With COERCE_IMPLICIT_CAST both places, the relabeling of the
function result is invisible, but the relabeling of the argument
is visible.


I think this is because=C2=A0get_rule_= expr's showimplicit is always
true for args in this case, che= cking the implementation of=C2=A0
get_rule_expr, I found PG behav= ior like this in many places.

--
<= div dir=3D"ltr" class=3D"gmail_signature">
Best RegardsAndy Fan
--0000000000006fdfee06036cc15c--