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 1xHokE-00000003JS1-0Gk9 for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Oct 2026 20:41:38 +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 1xHokC-0000000FtUR-3M7K for pgsql-bugs@arkaria.postgresql.org; Fri, 16 Oct 2026 20:41:36 +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.98.2) (envelope-from <9erthalion6@gmail.com>) id 1xHokC-0000000FtUI-2DPK for pgsql-bugs@lists.postgresql.org; Fri, 16 Oct 2026 20:41:36 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from <9erthalion6@gmail.com>) id 1xHok9-00000002YAT-35Cm for pgsql-bugs@lists.postgresql.org; Fri, 16 Oct 2026 20:41:35 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-6ae14eecc64so321310a12.1 for ; Fri, 16 Oct 2026 13:41:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1792183292; x=1792788092; darn=lists.postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=0thqWCC7HnbKY6MhgucWr7Hi9PO6HmTbUyWTTImHXVQ=; b=qb4LhEMHR5wENNKcIYzIWK1TjRgcDbdh6zEA9u6Btb5JJrQHIzybWxCYESu95nSPp8 2qGdxAUgwGEcRUKzrKNk4aOtxDu3mrlw+0P/vyuoP0RRHLnZ3+XHbmGYv6HjlrDrnarU iMiRBu6IrWkwyvcKqmm1oXKO++vWn4yXcbgUs8EM185BoGDzPmNcG/9fszDt/MNHqot+ cwyFLsxZFa5AxBvhaFRrs4hwfK2bbonDeql+KkePzzGQSt2CvZ7p+490NPX1dOIltw5C RaxYmoJS1cun2uEioq870LZHmv2+kl4ikbDikDuxVT+awhZMsWFddbV8FzEBrqCgH5Kb e5qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792183292; x=1792788092; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=0thqWCC7HnbKY6MhgucWr7Hi9PO6HmTbUyWTTImHXVQ=; b=N3JNYgEHgM8yBm7vY4z8vUxwG6/4VVT0sX1sIApBlnI9Yeuw5Kx9MZ+THECTfXAfkD kJ4PUbOm3JmZx7mOjaS/2GwobYNqMBOytD4R8OWlXEEfdjWwzgpn8gvCvEhgjXTBf0oV 1TDDcbjKl2XvXH3nRTNTyXTcp0oh8A9g1av/5lb5Dglw50c0sylnzLqhJyuN+XMyh9p9 W0L8++IZWJnMndNx3729yfxvpMCIeqEI9r3UcxK2ghPS9/03WaCoP9ratIWyNB3N1iJN zauaBt0a1Vo0s9R/PMnteV1VykTb4V0A2mhNUUuB3UaGNarsynd4kTuYK6bQAWOuxbPj LL9g== X-Gm-Message-State: AFq9FYLnsdh+2iviJZotBMLRAuSalmXLXxUFsOZS+6XJUBDwfX5PfWj9 i2FeFjJ0JYWJlWpB7/Q9cUZUa0/EzvQNjKRSxMrORCEv7CLFSGjKOpfv X-Gm-Gg: AYBFou3tWlJvrg2Eug/LGCr3oP91fZQu65Ph0to0Q8qZZqnZ3VACMHLkRKL3Ufyq7Dk kN2DtrnZrrZoxDhfooSs7F6tUSluKd4Cq4UukTaE9Z0CD8szEKhLViBVlRZSuThFJjFKsQWjZz1 mm1pI10i1XvbzfuVt2pcA/+iEZfZZleUskSaG1T8uBWpAKRW/TF86vJG7St2Bn8BmIZVqplJkb+ Ivu9KHL8qh4EXB0IdXyPmG12JOzILxjXPGeQRe0+91gpscSSmCq59gcVqK97N7hQkDNiUHao0DN 8U0Rn0mPWQ4cZWdVYFKgmF8yieRVU59vHtkzw+AU7C6TgQyBqWjpS72H5fEh6ab5lZDx70xTXDU yVJ3BEOXsymMwd+jxGczwGVdP9gFRjqKC5nXiv2y7yaeUxML1ibtKhd9EHwsekLtmXLa2DpZf6C ADp0PVir9apyuGib8nxtig9dQDqrTIS2o7Cwq81YqNC0lL3FnLqYuFWPQBnvWXW486r9epAZmyM LG8D8+FoZ+W+uJLqnDaihol4AvbZHqg28kNYTEo4OL4Y40UXs10JXJFvP90ZqDmaVivE7I1HMHC DmeIYPHlr1E= X-Received: by 2002:a17:907:e005:20b0:c41:b94c:4538 with SMTP id a640c23a62f3a-c41b94c5e53mr31018366b.47.1792183292211; Fri, 16 Oct 2026 13:41:32 -0700 (PDT) Received: from ddolgov-thinkpadt14sgen1.rmtde.csb (dslb-092-077-223-190.092.077.pools.vodafone-ip.de. [92.77.223.190]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c40919bcad6sm104039166b.52.2026.10.16.13.41.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Oct 2026 13:41:30 -0700 (PDT) Date: Fri, 16 Oct 2026 22:41:27 +0200 From: Dmitry Dolgov <9erthalion6@gmail.com> To: =?utf-8?B?66+87JiB7J6s?= Cc: pgsql-bugs@lists.postgresql.org, theshallow27@gmail.com Subject: Re: BUG #19736: `JSON_OBJECT ... ABSENT ON NULL` accepts invalid keys when the value is NULL Message-ID: References: <19736-3a44617ea5534c77@postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Sun, Oct 11, 2026 at 07:26:02PM +0900, 민영재 wrote: > I reproduced both examples on master at 9e7d152488f1. > > In json_build_object_worker(), the ABSENT ON NULL skip happens before > NULL-key validation. Both json and jsonb paths also skip the scalar-key > checks when omitting an entry without a uniqueness check. > > The attached patch moves the json NULL-key check before the skip and > validates the key type before omitting an entry in either output path. > It avoids calling output or cast functions for omitted keys and preserves > the existing uniqueness checks. > > Regression tests cover NULL, array, composite, json and jsonb keys with > NULL values. The new tests fail without the fix; with the patch, make > check passes all 239 tests on macOS/arm64 with assertions enabled. Thanks for investigating. I think it's worth to clarify the documentation a bit -- one can argue that we don't need to verify keys if the pair is going to be skipped, and only the wording happen to insist on it. Currently it says: key_expression is a scalar expression defining the JSON key, which is converted to the text type. It cannot be NULL nor can it belong to a type that has a cast to the json type. Maybe extending it to "It cannot be ..., even if the pair is going to be eventually omitted due to ABSENT ON NULL". It also probably makes sense to verify if there are more such examples. E.g. looks like jsonb_object_agg_strict exhibit something similar: -- jsonb_object_agg_transfn_worker tests if the key is NULL select jsonb_object_agg_strict(NULL, NULL); ERROR: 22023: field name must not be null -- it also checks if the key is not a composite or json, if the -- value is not NULL select jsonb_object_agg_strict('{"a": 1}'::jsonb, 'value'); ERROR: 22023: key value must be scalar, not array, composite, or json -- but it doesn't do so if the value is NULL select jsonb_object_agg_strict('{"a": 1}'::jsonb, NULL); jsonb_object_agg_strict ------------------------- {} (1 row) Regarding the proposed patch, maybe the condition on tcategory could be factored out to not repeat it few times (including one in the datum_to_jsonb_internal). It's also not clear to me why JSONTYPE_JSONB is not included in json_build_object_worker, shouldn't it be included since it has a cast to json?