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 1tEFwz-006KmK-Kc for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 22:47:02 +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 1tEFwy-002b3J-6U for pgsql-hackers@arkaria.postgresql.org; Thu, 21 Nov 2024 22:47:00 +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 1tEFwx-002b3B-Iw for pgsql-hackers@lists.postgresql.org; Thu, 21 Nov 2024 22:46:59 +0000 Received: from mail-oo1-xc31.google.com ([2607:f8b0:4864:20::c31]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tEFwu-0037kp-1p for pgsql-hackers@postgresql.org; Thu, 21 Nov 2024 22:46:58 +0000 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-5ebc52deca0so853844eaf.3 for ; Thu, 21 Nov 2024 14:46:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1732229214; x=1732834014; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=uwWs+TQXvreFZyKOVlC7PcEKGb8eXyZtZzqjjS/kue4=; b=Qp1WfzVsNA+VbRfEWxlTSC04cCSgmmEZ/4kxpXyhlN65WpSGOPKglDMlDPz+xUIKnF CZnG3Spm8Ax35ZNHadbP6RIrBh68iiVgbmELJhJTuHrtcANq5JuVX5KRWQEQfbVIan4S 1A4P6tI2bzX4F1OZU5ixCvStCvgdYHENV5y7r9jhN3e0XjqfM9DD7aVjDCoh7dnkw3D6 aFXbCk+AkcDQrgGnF8uXkffxisaqs6trvE0X3szDG5CwZyxLXFWYOGBU0lv/qW2EwvN7 wRl7vFxWr1XxzEW2wM1JEjiOpBhdMgc9qKhEPNUbbgAJdAC4HUD1Sme6SC0Qmxu4FaOQ KKMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732229214; x=1732834014; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:cc:to:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=uwWs+TQXvreFZyKOVlC7PcEKGb8eXyZtZzqjjS/kue4=; b=dnf45GEQmvkYN1oblkygiKA+hqpB7ufCIIqT0VWjndc5CHELT8eXOIKkJJTk+u9F4E ac0I1GiHEEdhhWLe0ZV7GQzpwJEL500yZ7m33+RcLH95DKwjOdjiaUNIMJq0HT427eLr Ls6hfc4zimPfvFZ2qhi1ILqQEZI8c7/lN9pmVeFXUEaFhR6FIhxrsOYQc1gMA9KtyHbc ChqAT1p+/pRZgG4lxcTZ4WqPBNHUkgjtoI0mN63Lf+L9k0otbkQu0PX5c2ZjQHL2sNlu 9phcvj6/kX0bOeKh/GSJzgyJbQcDigkTtiQn2H1t3mAlmwwQp8Fgd2N3euXCaY44XgQM 1kxg== X-Forwarded-Encrypted: i=1; AJvYcCVrfmNBkZLd7OEQGsd6efmQZe7COGjWx63qI+1p6WMt5bPZp00SKNl3SZwur5L22LsvRb+YUdhtRQH1VK9x@postgresql.org X-Gm-Message-State: AOJu0YyZ+PKyfby+L/5JhHlbSGFRCOBDukBdmwB2CA8UcJv2EEy4vr8n kAivDPqFeFQf7+9WRnWmtQ8LsmJ8UmceuDDdrRG7DgJO+gwwGoCUpAK9XH4kEBM= X-Gm-Gg: ASbGnctQmUJxLF77itL6/Jemh4CRCzMybpGyxtvT6q1AWnRIT6oHW31lxdbvw0+WfcO KAgQpGj+n5qEEVre21IKyoAqLrtri5MkfsaAyoR4VCh6C7zuQA07G/pUH+hLXbu0IeomuXlvwHq pKP0WQfHmd2tF0OzieMtlanyyk6gAIMq9sl99h5jXJjFU7QuntRgpSPppR3GnKHy4m8qHvxniKI L4vCZ5fAtqtDsenBukrS8OpVJxsI5lYTUYAWbT0ljlxoXdFMWoXp2PiUbsQ X-Google-Smtp-Source: AGHT+IFuFFkg33dfDFmpmT0Lezubfi2rXItThU1hzHgD+Pp959XUr5xX3sE6rspd/H4KLzPDp8KMKA== X-Received: by 2002:a05:6358:60cd:b0:1c2:f71f:7e4d with SMTP id e5c5f4694b2df-1ca797bddb3mr118968455d.23.1732229214589; Thu, 21 Nov 2024 14:46:54 -0800 (PST) Received: from ?IPV6:2605:a601:a681:6b00::2bb? ([2605:a601:a681:6b00::2bb]) by smtp.googlemail.com with ESMTPSA id d75a77b69052e-4653c3dc08dsm4157391cf.3.2024.11.21.14.46.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Nov 2024 14:46:53 -0800 (PST) Message-ID: Date: Thu, 21 Nov 2024 17:46:50 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: SQL:2023 JSON simplified accessor support To: Alexandra Wang , Nikita Glukhov Cc: Peter Eisentraut , PostgreSQL Hackers , "David E. Wheeler" , jian he References: From: Andrew Dunstan Content-Language: en-US Autocrypt: addr=andrew@dunslane.net; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJC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7gQUCZFlx 5wUJGVGAjgAKCRCZ+n/LWfw7gf+iB/4g8CPY5jihf5r/8EsoIGe2H+dpVmpPF8YGBzTIvCz/ fQoOq8AX/pE76QEuFnFZWfjw+wgBXgCVmkox2Eflkk6z4ND3pcwGZ6CfCxTQCDk/dij+2DQ4 6bmDCy/sBgcbz9mTpoLC11HLoPae6YN9nBNQRZDcEFEu54OaVOqlIdbA6m+POIBCXZdHOFc0 WoDTgxHRzC1jgQNidyd6tKqcsVJs0dzF0oKTmFFmUAqTdJO12LBuNA1rlqrR3EtpYk8B/wtS 5dIMD7Q8hwQpL+4C6GNpb6ZKnPkLi47pDOLhz2qBrqN+rqUEsT3YnExYpzj5yOBi+FlmV1Hw 49QYe1sn2ZPs In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-11-21 Th 3:52 PM, Alexandra Wang wrote: > Hi, > > On Tue, Nov 19, 2024 at 6:06 PM Nikita Glukhov wrote: >> Hi, hackers. >> >> I have implemented dot notation for jsonb using type subscripting back >> in April 2023, but failed post it because I left Postgres Professional >> company soon after and have not worked anywhere since, not even had >> any interest in programming. >> >> But yesterday I accidentally decided to look what is going on at >> commitfests and found this thread. I immediately started to rebase >> code from PG16, fixed some bugs, and now I'm ready to present my >> version of the patches which is much more complex. >> >> Unfortunately, I probably won't be able to devote that much time to >> the patches as before. > Thank you so much, Nikita, for revisiting this topic and sharing your > v6 patches! > > Now that we have two solutions, I’d like to summarize our current > options. > > In Postgres, there are currently three ways to access json/jsonb > object fields and array elements: > > 1. '->' operator (Postgres-specific, predates SQL standard): > > postgres=# select ('{"a": 1, "b": "c", "d": [1, 2, 3]}'::json) -> 'd' > -> 0; -- returns 1 > > 2. jsonb subscripting (not available for the plain json type): > > postgres=# select ('{"a": 1, "b": "c", "d": [1, 2, > 3]}'::jsonb)['d'][0]; --returns 1 > > 3. json_query() function: > > postgres=# select json_query(jsonb '{"a": 1, "b": "c", "d": [1, 2, > 3]}', 'lax $.d[0]'); --returns 1 > > A few weeks ago, I did the following performance benchmarking of the > three approaches: > > -- setup: > create table tbl(id int, col1 jsonb); > insert into tbl select i, '{"x":"vx", "y":[{"a":[1,2,3]}, {"b":[1, 2, > {"j":"vj"}]}]}' from generate_series(1, 100000)i; > > -- jsonb_operator.sql > SELECT id, col1 -> 'y' -> 1 -> 'b' -> 2 -> 'j' AS jsonb_operator FROM tbl; > > -- jsonb_subscripting.sql > SELECT id, col1['y'][1]['b'][2]['j'] AS jsonb_subscript FROM tbl; > > -- jsonb_path_query.sql > SELECT id, jsonb_path_query(col1, '$.y[1].b[2].j') FROM tbl; > > # pgbench on my local MacOS machine, using -O3 optimization: > pgbench -n -f XXX.sql postgres -T100 > > Results (Latency | tps): > > "->" operator: 14ms | 68 > jsonb subscripting: 17ms | 58 > jsonb_path_query() function: 23ms | 43 > > So performance from best to worst: > "->" operator > jsonb subscripting >> jsonb_path_query() function. > > I’m excited to see your implementation of dot notation for jsonb using > type subscripting! This approach rounds out the three possible ways to > implement JSON simplified accessors: > > ## v1: json_query() implementation > > Pros: > - Fully adheres to the SQL standard. > > According to the SQL standard, if the JSON simplified accessor is > not a JSON item method, it is equivalent to a : > > JSON_QUERY ( VEP, 'lax $.JC' WITH CONDITIONAL ARRAY WRAPPER NULL ON > EMPTY NULL ON ERROR) > > (I’m skipping that includes a JSON item method, as it is > currently outside the scope of both sets of patches.) > > - Easiest to implement > > Cons: > - Slow due to function call overhead. > > ## v2-v5: "->" operator implementation > > We initially chose this approach for its performance benefits. > However, while addressing Peter’s feedback on v5, I encountered the > following issue: > > -- setup > create table test_json_dot(id serial primary key, test_json json); > insert into test_json_dot values (5, '[{"a": 1, "b": 42}, {"a": 2, > "b": {"c": 42}}]'); > > -- problematic query: > test1=# select id, (test_json).b, json_query(test_json, 'lax $.b' WITH > CONDITIONAL WRAPPER NULL ON EMPTY NULL ON ERROR) as expected from > test_json_dot; > id | b | expected > ----+---+----------------- > 5 | | [42, {"c": 42}] > (1 row) > > This issue arises from the semantic differences between the "->" > operator and json_query’s "lax" mode. One possible workaround is to > redefine the "->" operator and modify its implementation. However, since > the "->" operator has been in use for a long time, such changes would > break backward compatibility. > > ## v6: jsonb subscription implementation > > Nikita's patches pass all my functional test cases, including those > that failed with the previous approach. > > Supported formats: > - JSON member accessor > - JSON wildcard member accessor (Not available in v5, so this is also a plus) > - JSON array accessor > > Questions: > > 1. Since Nikita’s patches did not address the JSON data type, and JSON > currently does not support subscripting, should we limit the initial > feature set to JSONB dot-notation for now? In other words, if we aim > to fully support JSON simplified accessors for the plain JSON type, > should we handle support for plain JSON subscripting as a follow-up > effort? > > 2. I have yet to have a more thorough review of Nikita’s patches. > One area I am not familiar with is the hstore-related changes. How > relevant is hstore to the JSON simplified accessor? > We can't change the way the "->" operator works, as there could well be uses of it in the field that rely on its current behaviour. But maybe we could invent a new operator which is compliant with the standard semantics for dot access, and call that. Then we'd get the best performance, and also we might be able to implement it for the plain JSON type. If that proves not possible we can think about not implementing for plain JSON, but I'd rather not go there until we have to. I don't think we should be including hstore changes here - we should just be aiming at implementing the standard for JSON access. hstore changes if any should be a separate feature. The aren't relevant to JSON access, although they might use some of the same infrastructure, depending on implementation. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com