diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2015-11-05 18:15:48 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2015-11-05 18:15:48 -0500 |
commit | 788e35ac0bc00489e2b86a930d8c1264100fb94b (patch) | |
tree | 95b240a8d971b36d7d71d3257c24fb747f120fef /src/test | |
parent | 038aa89af53ee6ee26dfc9e73704d4e94701588f (diff) |
Fix erroneous hash calculations in gin_extract_jsonb_path().
The jsonb_path_ops code calculated hash values inconsistently in some cases
involving nested arrays and objects. This would result in queries possibly
not finding entries that they should find, when using a jsonb_path_ops GIN
index for the search. The problem cases involve JSONB values that contain
both scalars and sub-objects at the same nesting level, for example an
array containing both scalars and sub-arrays. To fix, reset the current
stack->hash after processing each value or sub-object, not before; and
don't try to be cute about the outermost level's initial hash.
Correcting this means that existing jsonb_path_ops indexes may now be
inconsistent with the new hash calculation code. The symptom is the same
--- searches not finding entries they should find --- but the specific
rows affected are likely to be different. Users will need to REINDEX
jsonb_path_ops indexes to make sure that all searches work as expected.
Per bug #13756 from Daniel Cheng. Back-patch to 9.4 where the faulty
logic was introduced.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/regress/expected/jsonb.out | 50 | ||||
-rw-r--r-- | src/test/regress/sql/jsonb.sql | 20 |
2 files changed, 70 insertions, 0 deletions
diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index 4051c31ef49..e2cb57097d2 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -2193,6 +2193,56 @@ SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}'; t (1 row) +-- check some corner cases for indexed nested containment (bug #13756) +create temp table nestjsonb (j jsonb); +insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}'); +insert into nestjsonb (j) values ('[[14,2,3]]'); +insert into nestjsonb (j) values ('[1,[14,2,3]]'); +create index on nestjsonb using gin(j jsonb_path_ops); +set enable_seqscan = on; +set enable_bitmapscan = off; +select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb; + j +--------------------------------------------------- + {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3} +(1 row) + +select * from nestjsonb where j @> '{"c":3}'; + j +--------------------------------------------------- + {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3} +(1 row) + +select * from nestjsonb where j @> '[[14]]'; + j +----------------- + [[14, 2, 3]] + [1, [14, 2, 3]] +(2 rows) + +set enable_seqscan = off; +set enable_bitmapscan = on; +select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb; + j +--------------------------------------------------- + {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3} +(1 row) + +select * from nestjsonb where j @> '{"c":3}'; + j +--------------------------------------------------- + {"a": [["b", {"x": 1}], ["b", {"x": 2}]], "c": 3} +(1 row) + +select * from nestjsonb where j @> '[[14]]'; + j +----------------- + [[14, 2, 3]] + [1, [14, 2, 3]] +(2 rows) + +reset enable_seqscan; +reset enable_bitmapscan; -- nested object field / array index lookup SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n'; ?column? diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index 1c58b5cda92..74d34dcab4f 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -488,6 +488,26 @@ SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4}]}'; SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},3]}'; SELECT '{"a":[1,2,{"c":3,"x":4}],"c":"b"}'::jsonb @> '{"a":[{"x":4},1]}'; +-- check some corner cases for indexed nested containment (bug #13756) +create temp table nestjsonb (j jsonb); +insert into nestjsonb (j) values ('{"a":[["b",{"x":1}],["b",{"x":2}]],"c":3}'); +insert into nestjsonb (j) values ('[[14,2,3]]'); +insert into nestjsonb (j) values ('[1,[14,2,3]]'); +create index on nestjsonb using gin(j jsonb_path_ops); + +set enable_seqscan = on; +set enable_bitmapscan = off; +select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb; +select * from nestjsonb where j @> '{"c":3}'; +select * from nestjsonb where j @> '[[14]]'; +set enable_seqscan = off; +set enable_bitmapscan = on; +select * from nestjsonb where j @> '{"a":[[{"x":2}]]}'::jsonb; +select * from nestjsonb where j @> '{"c":3}'; +select * from nestjsonb where j @> '[[14]]'; +reset enable_seqscan; +reset enable_bitmapscan; + -- nested object field / array index lookup SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'n'; SELECT '{"n":null,"a":1,"b":[1,2],"c":{"1":2},"d":{"1":[2,3]}}'::jsonb -> 'a'; |