8.14. Типы JSON
Типы JSON предназначены для хранения данных JSON (JavaScript Object Notation, Запись объекта JavaScript) согласно стандарту RFC 7159. Такие данные можно хранить и в типе text
, но типы JSON лучше тем, что проверяют, соответствует ли вводимое значение формату JSON. Для работы с ними есть также несколько специальных функций и операторов; см. Раздел 9.15.
Существуют два типа данных JSON: json
и jsonb
. Они принимают на вход почти одинаковые наборы значений, а отличаются главным образом с точки зрения эффективности. Тип json
сохраняет точную копию введённого текста, которую функции обработки должны разбирать заново при каждом выполнении запроса, тогда как данные jsonb
сохраняются в разобранном двоичном формате, что несколько замедляет ввод из-за преобразования, но значительно ускоряет обработку, не требуя многократного разбора текста. Кроме того, jsonb
поддерживает индексацию, что тоже может быть очень полезно.
Так как тип json
сохраняет точную копию введённого текста, он сохраняет семантически незначащие пробелы между элементами, а также порядок ключей в JSON-объектах. И если JSON-объект внутри содержит повторяющиеся ключи, этот тип сохранит все пары ключ/значение. (Функции обработки будут считать действительной последнюю пару.) Тип jsonb
, напротив, не сохраняет пробелы, порядок ключей и значения с дублирующимися ключами. Если во входных данных оказываются дублирующиеся ключи, сохраняется только последнее значение.
Для большинства приложений предпочтительнее хранить данные JSON в типе jsonb
(если нет особых противопоказаний, например важны прежние предположения о порядке ключей объектов).
Postgres Pro позволяет использовать только одну кодировку символов в базе данных, поэтому данные JSON не будут полностью соответствовать спецификации, если кодировка базы данных не UTF-8. При этом нельзя будет вставить символы, непредставимые в кодировке сервера, и наоборот, допустимыми будут символы, представимые в кодировке сервера, но не в UTF-8.
RFC 7159 разрешает включать в строки JSON спецпоследовательности Unicode в виде \u
. В функцию ввода для типа XXXX
json
эти спецпоследовательности допускаются вне зависимости от кодировки базы данных, и проверяется только правильность их синтаксиса (за \u
должны следовать четыре шестнадцатеричных цифры). Однако функция ввода для типа jsonb
более строгая: она не допускает спецпоследовательности Unicode для не ASCII-символов (символов после U+007F
), если кодировка базы данных не UTF8. Тип jsonb
также не принимает \u0000
(так как это значение не может быть представлено в типе text
Postgres Pro) и требует, чтобы суррогатные пары Unicode использовались для представления символов вне основной многоязыковой плоскости (BMP) правильно. Корректные спецпоследовательности Unicode преобразуются для хранения в соответствующий символ ASCII или UTF-8 (это подразумевает сворачивание суррогатных пар в один символ).
Примечание
Многие из функций обработки JSON, описанные в Разделе 9.15, преобразуют спецпоследовательности Unicode в обычные символы, поэтому могут выдавать подобные ошибки, даже если им на вход поступает тип json
, а не jsonb
. То, что функция ввода в тип json
не производит этих проверок, можно считать историческим артефактом, хотя это и позволяет просто сохранять (но не обрабатывать) в JSON спецкоды Unicode в базе данных с кодировкой не UTF-8. Вообще же, по возможности следует избегать смешения спецкодов Unicode в JSON с кодировкой базой данных не UTF-8.
При преобразовании вводимого текста JSON в тип jsonb
, примитивные типы, описанные в RFC 7159, по сути отображаются в собственные типы Postgres Pro как показано в Таблице 8.23. Таким образом, к содержимому типа jsonb
предъявляются некоторые дополнительные требования, продиктованные ограничениями представления нижележащего типа данных, которые не распространяются ни на тип json
, ни на формат JSON вообще. В частности, тип jsonb
не принимает числа, выходящие за диапазон типа данных Postgres Pro numeric
, тогда как с json
такого ограничения нет. Такие ограничения, накладываемые реализацией, допускаются согласно RFC 7159. Однако на практике такие проблемы более вероятны в других реализациях, так как обычно примитивный тип JSON number
представляется в виде числа с плавающей точкой двойной точности IEEE 754 (что RFC 7159 явно признаёт и допускает). При использовании JSON в качестве формата обмена данными с такими системами следует учитывать риски потери точности чисел, хранившихся в Postgres Pro.
И напротив, как показано в таблице, есть некоторые ограничения в формате ввода примитивных типов JSON, не актуальные для соответствующих типов Postgres Pro.
Таблица 8.23. Примитивные типы JSON и соответствующие им типы Postgres Pro
Примитивный тип JSON | Тип Postgres Pro | Замечания |
---|---|---|
string | text | \u0000 не допускается, как не ASCII символ, если кодировка базы данных не UTF-8 |
number | numeric | Значения NaN и infinity не допускаются |
boolean | boolean | Допускаются только варианты true и false (в нижнем регистре) |
null | (нет) | NULL в SQL имеет другой смысл |
8.14.1. Синтаксис вводимых и выводимых значений JSON
Синтаксис ввода/вывода типов данных JSON соответствует стандарту RFC 7159.
Примеры допустимых выражений с типом json
(или jsonb
):
-- Простое скалярное/примитивное значение -- Простыми значениями могут быть числа, строки в кавычках, true, false или null SELECT '5'::json; -- Массив из нуля и более элементов (элементы могут быть разных типов) SELECT '[1, 2, "foo", null]'::json; -- Объект, содержащий пары ключей и значений -- Заметьте, что ключи объектов — это всегда строки в кавычках SELECT '{"bar": "baz", "balance": 7.77, "active": false}'::json; -- Массивы и объекты могут вкладываться произвольным образом SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::json;
Как было сказано ранее, когда значение JSON вводится и затем выводится без дополнительной обработки, тип json
выводит тот же текст, что поступил на вход, а jsonb
не сохраняет семантически незначащие детали, такие как пробелы. Например, посмотрите на эти различия:
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::json; json ------------------------------------------------- {"bar": "baz", "balance": 7.77, "active":false} (1 row) SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb; jsonb -------------------------------------------------- {"bar": "baz", "active": false, "balance": 7.77} (1 row)
Первая семантически незначимая деталь, заслуживающая внимания: с jsonb
числа выводятся по правилам нижележащего типа numeric
. На практике это означает, что числа, заданные в записи с E
, будут выведены без неё, например:
SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb; json | jsonb -----------------------+------------------------- {"reading": 1.230e-5} | {"reading": 0.00001230} (1 row)
Однако как видно из этого примера, jsonb
сохраняет конечные нули дробного числа, хотя они и не имеют семантической значимости, в частности для проверки на равенство.
8.14.2. Эффективная организация документов JSON
Представлять данные в JSON можно гораздо более гибко, чем в традиционной реляционной модели данных, что очень привлекательно там, где нет жёстких условий. И оба этих подхода вполне могут сосуществовать и дополнять друг друга в одном приложении. Однако даже для приложений, которым нужна максимальная гибкость, рекомендуется, чтобы документы JSON имели некоторую фиксированную структуру. Эта структура обычно не навязывается жёстко (хотя можно декларативно диктовать некоторые бизнес-правила), но когда она предсказуема, становится гораздо проще писать запросы, которые извлекают полезные данные из набора «документов» (информации) в таблице.
Данные JSON, как и данные любых других типов, хранящиеся в таблицах, находятся под контролем механизма параллельного доступа. Хотя хранить большие документы вполне возможно, не забывайте, что при любом изменении устанавливается блокировка всей строки (на уровне строки). Поэтому для оптимизации блокировок транзакций, изменяющих данные, стоит ограничить размер документов JSON разумными пределами. В идеале каждый документ JSON должен собой представлять атомарный информационный блок, который, согласно бизнес-логике, нельзя разделить на меньшие, индивидуально изменяемые блоки.
8.14.3. Проверки на вхождение и существование jsonb
Проверка вхождения — важная особенность типа jsonb
, не имеющая аналога для типа json
. Эта проверка определяет, входит ли один документ jsonb
в другой. В следующих примерах возвращается истинное значение (кроме упомянутых исключений):
-- Простые скалярные/примитивные значения включают только одно идентичное значение:
SELECT '"foo"'::jsonb @> '"foo"'::jsonb;
-- Массив с правой стороны входит в массив слева:
SELECT '[1, 2, 3]'::jsonb @> '[1, 3]'::jsonb;
-- Порядок элементов в массиве не важен, поэтому это условие тоже выполняется:
SELECT '[1, 2, 3]'::jsonb @> '[3, 1]'::jsonb;
-- А повторяющиеся элементы массива не имеют значения:
SELECT '[1, 2, 3]'::jsonb @> '[1, 2, 2]'::jsonb;
-- Объект с одной парой справа входит в объект слева:
SELECT '{"product": "PostgreSQL", "version": 9.4, "jsonb": true}'::jsonb @> '{"version": 9.4}'::jsonb;
-- Массив справа не считается входящим в
-- массив слева, хотя в последний и вложен подобный массив:
SELECT '[1, 2, [1, 3]]'::jsonb @> '[1, 3]'::jsonb; -- выдаёт false
-- Но если добавить уровень вложенности, проверка на вхождение выполняется:
SELECT '[1, 2, [1, 3]]'::jsonb @> '[[1, 3]]'::jsonb;
-- Аналогично, это вхождением не считается:
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"bar": "baz"}'::jsonb; -- выдаёт false
-- Ключ с пустым объектом на верхнем уровне входит в объект с таким ключом:
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"foo": {}}'::jsonb;
Общий принцип этой проверки в том, что входящий объект должен соответствовать объекту, содержащему его, по структуре и данным, возможно, после исключения из содержащего объекта лишних элементов массива или пар ключ/значение. Но помните, что порядок элементов массива для проверки на вхождение не имеет значения, а повторяющиеся элементы массива считаются только один раз.
В качестве особого исключения для требования идентичности структур, массив может содержать примитивное значение:
-- В этот массив входит примитивное строковое значение: SELECT '["foo", "bar"]'::jsonb @> '"bar"'::jsonb; -- Это исключение действует только в одну сторону -- здесь вхождения нет: SELECT '"bar"'::jsonb @> '["bar"]'::jsonb; -- выдаёт false
Для типа jsonb
введён также оператор существования, который является вариацией на тему вхождения: он проверяет, является ли строка (заданная в виде значения text
) ключом объекта или элементом массива на верхнем уровне значения jsonb
. В следующих примерах возвращается истинное значение (кроме упомянутых исключений):
-- Строка существует в качестве элемента массива: SELECT '["foo", "bar", "baz"]'::jsonb ? 'bar'; -- Строка существует в качестве ключа объекта: SELECT '{"foo": "bar"}'::jsonb ? 'foo'; -- Значения объектов не рассматриваются: SELECT '{"foo": "bar"}'::jsonb ? 'bar'; -- выдаёт false -- Как и вхождение, существование определяется на верхнем уровне: SELECT '{"foo": {"bar": "baz"}}'::jsonb ? 'bar'; -- выдаёт false -- Строка считается существующей, если она соответствует примитивной строке JSON: SELECT '"foo"'::jsonb ? 'foo';
Объекты JSON для проверок на существование и вхождение со множеством ключей или элементов подходят больше, чем массивы, так как, в отличие от массивов, они внутри оптимизируются для поиска, и поиск элемента не будет линейным.
Подсказка
Так как вхождение в JSON проверяется с учётом вложенности, правильно написанный запрос может заменить явную выборку внутренних объектов. Например, предположим, что у нас есть столбец doc
, содержащий объекты на верхнем уровне, и большинство этих объектов содержит поля tags
с массивами вложенных объектов. Данный запрос найдёт записи, в которых вложенные объекты содержат ключи "term":"paris"
и "term":"food"
, и при этом пропустит такие ключи, находящиеся вне массива tags
:
SELECT doc->'site_name' FROM websites WHERE doc @> '{"tags":[{"term":"paris"}, {"term":"food"}]}';
Этого же результата можно добиться, например, так:
SELECT doc->'site_name' FROM websites WHERE doc->'tags' @> '[{"term":"paris"}, {"term":"food"}]';
Но данный подход менее гибкий и часто также менее эффективный.
С другой стороны, оператор существования JSON не учитывает вложенность: он будет искать заданный ключ или элемент массива только на верхнем уровне значения JSON.
Различные операторы вхождения и существования, а также все другие операторы и функции для работы с JSON документированы в Разделе 9.15.
8.14.4. Индексация jsonb
Для эффективного поиска ключей или пар ключ/значение в большом количестве документов jsonb
можно успешно применять индексы GIN. Для этого предоставляются два «класса операторов» GIN, предлагающие выбор между производительностью и гибкостью.
Класс операторов GIN по умолчанию для jsonb
поддерживает запросы с операторами существования ключа на верхнем уровне (?
, ?&
и ?|
) и оператором существования пути/значения (@>
). (Подробнее семантика, реализуемая этими операторами, описана в Таблице 9.44.) Пример создания индекса с этим классом операторов:
CREATE INDEX idxgin ON api USING GIN (jdoc);
Дополнительный класс операторов GIN jsonb_path_ops
поддерживает индексацию только для оператора @>
. Пример создания индекса с этим классом операторов:
CREATE INDEX idxginp ON api USING GIN (jdoc jsonb_path_ops);
Рассмотрим пример таблицы, в которой хранятся документы JSON, получаемые от сторонней веб-службы, с документированным определением схемы. Типичный документ:
{ "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a", "name": "Angela Barton", "is_active": true, "company": "Magnafone", "address": "178 Howard Place, Gulf, Washington, 702", "registered": "2009-11-07T08:53:22 +08:00", "latitude": 19.793713, "longitude": 86.513373, "tags": [ "enim", "aliquip", "qui" ] }
Мы сохраняем эти документы в таблице api
, в столбце jdoc
типа jsonb
. Если по этому столбцу создаётся GIN-индекс, он может применяться в подобных запросах:
-- Найти документы, в которых ключ "company" имеет значение "Magnafone" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"company": "Magnafone"}';
Однако в следующих запросах он не будет использоваться, потому что, несмотря на то, что оператор ?
— индексируемый, он применяется не к индексированному столбцу jdoc
непосредственно:
-- Найти документы, в которых ключ "tags" содержит ключ или элемент массива "qui" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> 'tags' ? 'qui';
И всё же, правильно применяя индексы выражений, в этом запросе можно задействовать индекс. Если запрос определённых элементов в ключе "tags"
выполняется часто, вероятно стоит определить такой индекс:
CREATE INDEX idxgintags ON api USING GIN ((jdoc -> 'tags'));
Теперь предложение WHERE
jdoc -> 'tags' ? 'qui'
будет выполняться как применение индексируемого оператора ?
к индексируемому выражению jdoc -> 'tags'
. (Подробнее об индексах выражений можно узнать в Разделе 11.7.)
Ещё один подход к использованию проверок на существование:
-- Найти документы, в которых ключ "tags" содержит элемент массива "qui" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"tags": ["qui"]}';
Этот запрос может задействовать простой GIN-индекс по столбцу jdoc
. Но заметьте, что такой индекс будет хранить копии всех ключей и значений в поле jdoc
, тогда как индекс выражения из предыдущего примера хранит только данные внутри объекта с ключом tags
. Хотя подход с простым индексом гораздо более гибкий (так как он поддерживает запросы по любому ключу), индексы конкретных выражений скорее всего будут меньше и быстрее, чем простые индексы.
Класс операторов jsonb_path_ops
поддерживает только запросы с оператором @>
, но зато он значительно производительнее класса по умолчанию jsonb_ops
. Индекс jsonb_path_ops
обычно гораздо меньше индекса jsonb_ops
для тех же данных и более точен при поиске, особенно если запросы обращаются к ключам, часто встречающимся в данных. Таким образом, с ним операции поиска выполняются гораздо лучше, чем с классом операторов по умолчанию.
Техническое различие между GIN-индексами jsonb_ops
и jsonb_path_ops
состоит в том, что для первых создаются независимые элементы индекса для каждого ключа/значения в данных, тогда как для вторых создаются элементы только для значений. [6] По сути, каждый элемент индекса jsonb_path_ops
представляет собой хеш значения и ключа(ей), приводящего к нему; например, при индексации {"foo": {"bar": "baz"}}
будет создан один элемент индекса с хешем, рассчитанным по всем трём значениям: foo
, bar
и baz
. Таким образом, проверка на вхождение этой структуры будет использовать крайне точный поиск по индексу, но определить, является ли foo
ключом, с помощью такого индекса нельзя. С другой стороны, индекс jsonb_ops
создаст три отдельных элемента индекса, представляющих foo
, bar
и baz
по отдельности; для выполнения проверки на вхождение будут проверены строки таблицы, содержащие все эти три значения. Хотя GIN-индексы позволяют вычислить AND довольно эффективно, такой поиск всё же будет менее точным и более медленным, чем равнозначный поиск с jsonb_path_ops
, особенно если любое одно из этих трёх значений содержится в большом количестве строк.
Недостаток класса jsonb_path_ops
заключается в том, что он не учитывает в индексе структуры JSON, не содержащие никаких значений {"a": {}}
. Для поиска по документам, содержащих такие структуры, потребуется выполнить полное сканирование индекса, что довольно долго, поэтому jsonb_path_ops
не очень подходит для приложений, часто выполняющих такие запросы.
Тип jsonb
также поддерживает индексы btree
и hash
. Они полезны, только если требуется проверять равенство JSON-документов в целом. Порядок сортировки btree
для типа jsonb
редко имеет большое значение, но для полноты он приводится ниже:
Объект
>Массив
>Логическое значение
>Число
>Строка
>Null
Объект с n парами
>Объект с n - 1 парами
Массив с n элементами
>Массив с n - 1 элементами
Объекты с равным количеством пар сравниваются в таком порядке:
ключ-1
,значение-1
,ключ-2
...
Заметьте, что ключи объектов сравниваются согласно порядку при хранении; в частности, из-за того, что короткие ключи хранятся перед длинными, результаты могут оказаться несколько не интуитивными:
{ "aa": 1, "c": 1} > {"b": 1, "d": 1}
Массивы с равным числом элементом упорядочиваются аналогично:
элемент-1
,элемент-2
...
Примитивные значения JSON сравниваются по тем же правилам сравнения, что и нижележащие типы данных Postgres Pro. Строки сравниваются с учётом порядка сортировки по умолчанию в текущей базе данных.
8.14.5. Трансформации
Для различных процедурных языков представлены дополнительные расширения, реализующие трансформации для типа jsonb
.
Расширения для PL/Perl называются jsonb_plperl
и jsonb_plperlu
. Когда они используются, значения jsonb
отображаются в соответствующие структуры Perl: массивы, хеши или скаляры.
Расширения для PL/Python называются jsonb_plpythonu
, jsonb_plpython2u
и jsonb_plpython3u
(принятое в PL/Python соглашение об именовании описано в Разделе 47.1). Когда они используются, значения jsonb
отображаются в соответствующие структуры Python: массивы, хеши или скаляры.
[6] Поэтому понятие «значение» включает и элементы массивов, хотя в терминологии JSON иногда элементы массивов считаются отличными от значений внутри объектов.
8.14. JSON Types
JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 7159. Such data can also be stored as text
, but the JSON data types have the advantage of enforcing that each stored value is valid according to the JSON rules. There are also assorted JSON-specific functions and operators available for data stored in these data types; see Section 9.15.
There are two JSON data types: json
and jsonb
. They accept almost identical sets of values as input. The major practical difference is one of efficiency. The json
data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb
data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb
also supports indexing, which can be a significant advantage.
Because the json
type stores an exact copy of the input text, it will preserve semantically-insignificant white space between tokens, as well as the order of keys within JSON objects. Also, if a JSON object within the value contains the same key more than once, all the key/value pairs are kept. (The processing functions consider the last value as the operative one.) By contrast, jsonb
does not preserve white space, does not preserve the order of object keys, and does not keep duplicate object keys. If duplicate keys are specified in the input, only the last value is kept.
In general, most applications should prefer to store JSON data as jsonb
, unless there are quite specialized needs, such as legacy assumptions about ordering of object keys.
Postgres Pro allows only one character set encoding per database. It is therefore not possible for the JSON types to conform rigidly to the JSON specification unless the database encoding is UTF8. Attempts to directly include characters that cannot be represented in the database encoding will fail; conversely, characters that can be represented in the database encoding but not in UTF8 will be allowed.
RFC 7159 permits JSON strings to contain Unicode escape sequences denoted by \u
. In the input function for the XXXX
json
type, Unicode escapes are allowed regardless of the database encoding, and are checked only for syntactic correctness (that is, that four hex digits follow \u
). However, the input function for jsonb
is stricter: it disallows Unicode escapes for non-ASCII characters (those above U+007F
) unless the database encoding is UTF8. The jsonb
type also rejects \u0000
(because that cannot be represented in Postgres Pro's text
type), and it insists that any use of Unicode surrogate pairs to designate characters outside the Unicode Basic Multilingual Plane be correct. Valid Unicode escapes are converted to the equivalent ASCII or UTF8 character for storage; this includes folding surrogate pairs into a single character.
Note
Many of the JSON processing functions described in Section 9.15 will convert Unicode escapes to regular characters, and will therefore throw the same types of errors just described even if their input is of type json
not jsonb
. The fact that the json
input function does not make these checks may be considered a historical artifact, although it does allow for simple storage (without processing) of JSON Unicode escapes in a non-UTF8 database encoding. In general, it is best to avoid mixing Unicode escapes in JSON with a non-UTF8 database encoding, if possible.
When converting textual JSON input into jsonb
, the primitive types described by RFC 7159 are effectively mapped onto native Postgres Pro types, as shown in Table 8.23. Therefore, there are some minor additional constraints on what constitutes valid jsonb
data that do not apply to the json
type, nor to JSON in the abstract, corresponding to limits on what can be represented by the underlying data type. Notably, jsonb
will reject numbers that are outside the range of the Postgres Pro numeric
data type, while json
will not. Such implementation-defined restrictions are permitted by RFC 7159. However, in practice such problems are far more likely to occur in other implementations, as it is common to represent JSON's number
primitive type as IEEE 754 double precision floating point (which RFC 7159 explicitly anticipates and allows for). When using JSON as an interchange format with such systems, the danger of losing numeric precision compared to data originally stored by Postgres Pro should be considered.
Conversely, as noted in the table there are some minor restrictions on the input format of JSON primitive types that do not apply to the corresponding Postgres Pro types.
Table 8.23. JSON primitive types and corresponding Postgres Pro types
JSON primitive type | Postgres Pro type | Notes |
---|---|---|
string | text | \u0000 is disallowed, as are non-ASCII Unicode escapes if database encoding is not UTF8 |
number | numeric | NaN and infinity values are disallowed |
boolean | boolean | Only lowercase true and false spellings are accepted |
null | (none) | SQL NULL is a different concept |
8.14.1. JSON Input and Output Syntax
The input/output syntax for the JSON data types is as specified in RFC 7159.
The following are all valid json
(or jsonb
) expressions:
-- Simple scalar/primitive value -- Primitive values can be numbers, quoted strings, true, false, or null SELECT '5'::json; -- Array of zero or more elements (elements need not be of same type) SELECT '[1, 2, "foo", null]'::json; -- Object containing pairs of keys and values -- Note that object keys must always be quoted strings SELECT '{"bar": "baz", "balance": 7.77, "active": false}'::json; -- Arrays and objects can be nested arbitrarily SELECT '{"foo": [true, "bar"], "tags": {"a": 1, "b": null}}'::json;
As previously stated, when a JSON value is input and then printed without any additional processing, json
outputs the same text that was input, while jsonb
does not preserve semantically-insignificant details such as whitespace. For example, note the differences here:
SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::json; json ------------------------------------------------- {"bar": "baz", "balance": 7.77, "active":false} (1 row) SELECT '{"bar": "baz", "balance": 7.77, "active":false}'::jsonb; jsonb -------------------------------------------------- {"bar": "baz", "active": false, "balance": 7.77} (1 row)
One semantically-insignificant detail worth noting is that in jsonb
, numbers will be printed according to the behavior of the underlying numeric
type. In practice this means that numbers entered with E
notation will be printed without it, for example:
SELECT '{"reading": 1.230e-5}'::json, '{"reading": 1.230e-5}'::jsonb; json | jsonb -----------------------+------------------------- {"reading": 1.230e-5} | {"reading": 0.00001230} (1 row)
However, jsonb
will preserve trailing fractional zeroes, as seen in this example, even though those are semantically insignificant for purposes such as equality checks.
8.14.2. Designing JSON documents effectively
Representing data as JSON can be considerably more flexible than the traditional relational data model, which is compelling in environments where requirements are fluid. It is quite possible for both approaches to co-exist and complement each other within the same application. However, even for applications where maximal flexibility is desired, it is still recommended that JSON documents have a somewhat fixed structure. The structure is typically unenforced (though enforcing some business rules declaratively is possible), but having a predictable structure makes it easier to write queries that usefully summarize a set of “documents” (datums) in a table.
JSON data is subject to the same concurrency-control considerations as any other data type when stored in a table. Although storing large documents is practicable, keep in mind that any update acquires a row-level lock on the whole row. Consider limiting JSON documents to a manageable size in order to decrease lock contention among updating transactions. Ideally, JSON documents should each represent an atomic datum that business rules dictate cannot reasonably be further subdivided into smaller datums that could be modified independently.
8.14.3. jsonb
Containment and Existence
Testing containment is an important capability of jsonb
. There is no parallel set of facilities for the json
type. Containment tests whether one jsonb
document has contained within it another one. These examples return true except as noted:
-- Simple scalar/primitive values contain only the identical value:
SELECT '"foo"'::jsonb @> '"foo"'::jsonb;
-- The array on the right side is contained within the one on the left:
SELECT '[1, 2, 3]'::jsonb @> '[1, 3]'::jsonb;
-- Order of array elements is not significant, so this is also true:
SELECT '[1, 2, 3]'::jsonb @> '[3, 1]'::jsonb;
-- Duplicate array elements don't matter either:
SELECT '[1, 2, 3]'::jsonb @> '[1, 2, 2]'::jsonb;
-- The object with a single pair on the right side is contained
-- within the object on the left side:
SELECT '{"product": "PostgreSQL", "version": 9.4, "jsonb": true}'::jsonb @> '{"version": 9.4}'::jsonb;
-- The array on the right side is not considered contained within the
-- array on the left, even though a similar array is nested within it:
SELECT '[1, 2, [1, 3]]'::jsonb @> '[1, 3]'::jsonb; -- yields false
-- But with a layer of nesting, it is contained:
SELECT '[1, 2, [1, 3]]'::jsonb @> '[[1, 3]]'::jsonb;
-- Similarly, containment is not reported here:
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"bar": "baz"}'::jsonb; -- yields false
-- A top-level key and an empty object is contained:
SELECT '{"foo": {"bar": "baz"}}'::jsonb @> '{"foo": {}}'::jsonb;
The general principle is that the contained object must match the containing object as to structure and data contents, possibly after discarding some non-matching array elements or object key/value pairs from the containing object. But remember that the order of array elements is not significant when doing a containment match, and duplicate array elements are effectively considered only once.
As a special exception to the general principle that the structures must match, an array may contain a primitive value:
-- This array contains the primitive string value: SELECT '["foo", "bar"]'::jsonb @> '"bar"'::jsonb; -- This exception is not reciprocal -- non-containment is reported here: SELECT '"bar"'::jsonb @> '["bar"]'::jsonb; -- yields false
jsonb
also has an existence operator, which is a variation on the theme of containment: it tests whether a string (given as a text
value) appears as an object key or array element at the top level of the jsonb
value. These examples return true except as noted:
-- String exists as array element: SELECT '["foo", "bar", "baz"]'::jsonb ? 'bar'; -- String exists as object key: SELECT '{"foo": "bar"}'::jsonb ? 'foo'; -- Object values are not considered: SELECT '{"foo": "bar"}'::jsonb ? 'bar'; -- yields false -- As with containment, existence must match at the top level: SELECT '{"foo": {"bar": "baz"}}'::jsonb ? 'bar'; -- yields false -- A string is considered to exist if it matches a primitive JSON string: SELECT '"foo"'::jsonb ? 'foo';
JSON objects are better suited than arrays for testing containment or existence when there are many keys or elements involved, because unlike arrays they are internally optimized for searching, and do not need to be searched linearly.
Tip
Because JSON containment is nested, an appropriate query can skip explicit selection of sub-objects. As an example, suppose that we have a doc
column containing objects at the top level, with most objects containing tags
fields that contain arrays of sub-objects. This query finds entries in which sub-objects containing both "term":"paris"
and "term":"food"
appear, while ignoring any such keys outside the tags
array:
SELECT doc->'site_name' FROM websites WHERE doc @> '{"tags":[{"term":"paris"}, {"term":"food"}]}';
One could accomplish the same thing with, say,
SELECT doc->'site_name' FROM websites WHERE doc->'tags' @> '[{"term":"paris"}, {"term":"food"}]';
but that approach is less flexible, and often less efficient as well.
On the other hand, the JSON existence operator is not nested: it will only look for the specified key or array element at top level of the JSON value.
The various containment and existence operators, along with all other JSON operators and functions are documented in Section 9.15.
8.14.4. jsonb
Indexing
GIN indexes can be used to efficiently search for keys or key/value pairs occurring within a large number of jsonb
documents (datums). Two GIN “operator classes” are provided, offering different performance and flexibility trade-offs.
The default GIN operator class for jsonb
supports queries with top-level key-exists operators ?
, ?&
and ?|
operators and path/value-exists operator @>
. (For details of the semantics that these operators implement, see Table 9.44.) An example of creating an index with this operator class is:
CREATE INDEX idxgin ON api USING GIN (jdoc);
The non-default GIN operator class jsonb_path_ops
supports indexing the @>
operator only. An example of creating an index with this operator class is:
CREATE INDEX idxginp ON api USING GIN (jdoc jsonb_path_ops);
Consider the example of a table that stores JSON documents retrieved from a third-party web service, with a documented schema definition. A typical document is:
{ "guid": "9c36adc1-7fb5-4d5b-83b4-90356a46061a", "name": "Angela Barton", "is_active": true, "company": "Magnafone", "address": "178 Howard Place, Gulf, Washington, 702", "registered": "2009-11-07T08:53:22 +08:00", "latitude": 19.793713, "longitude": 86.513373, "tags": [ "enim", "aliquip", "qui" ] }
We store these documents in a table named api
, in a jsonb
column named jdoc
. If a GIN index is created on this column, queries like the following can make use of the index:
-- Find documents in which the key "company" has value "Magnafone" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"company": "Magnafone"}';
However, the index could not be used for queries like the following, because though the operator ?
is indexable, it is not applied directly to the indexed column jdoc
:
-- Find documents in which the key "tags" contains key or array element "qui" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc -> 'tags' ? 'qui';
Still, with appropriate use of expression indexes, the above query can use an index. If querying for particular items within the "tags"
key is common, defining an index like this may be worthwhile:
CREATE INDEX idxgintags ON api USING GIN ((jdoc -> 'tags'));
Now, the WHERE
clause jdoc -> 'tags' ? 'qui'
will be recognized as an application of the indexable operator ?
to the indexed expression jdoc -> 'tags'
. (More information on expression indexes can be found in Section 11.7.)
Another approach to querying is to exploit containment, for example:
-- Find documents in which the key "tags" contains array element "qui" SELECT jdoc->'guid', jdoc->'name' FROM api WHERE jdoc @> '{"tags": ["qui"]}';
A simple GIN index on the jdoc
column can support this query. But note that such an index will store copies of every key and value in the jdoc
column, whereas the expression index of the previous example stores only data found under the tags
key. While the simple-index approach is far more flexible (since it supports queries about any key), targeted expression indexes are likely to be smaller and faster to search than a simple index.
Although the jsonb_path_ops
operator class supports only queries with the @>
operator, it has notable performance advantages over the default operator class jsonb_ops
. A jsonb_path_ops
index is usually much smaller than a jsonb_ops
index over the same data, and the specificity of searches is better, particularly when queries contain keys that appear frequently in the data. Therefore search operations typically perform better than with the default operator class.
The technical difference between a jsonb_ops
and a jsonb_path_ops
GIN index is that the former creates independent index items for each key and value in the data, while the latter creates index items only for each value in the data. [6] Basically, each jsonb_path_ops
index item is a hash of the value and the key(s) leading to it; for example to index {"foo": {"bar": "baz"}}
, a single index item would be created incorporating all three of foo
, bar
, and baz
into the hash value. Thus a containment query looking for this structure would result in an extremely specific index search; but there is no way at all to find out whether foo
appears as a key. On the other hand, a jsonb_ops
index would create three index items representing foo
, bar
, and baz
separately; then to do the containment query, it would look for rows containing all three of these items. While GIN indexes can perform such an AND search fairly efficiently, it will still be less specific and slower than the equivalent jsonb_path_ops
search, especially if there are a very large number of rows containing any single one of the three index items.
A disadvantage of the jsonb_path_ops
approach is that it produces no index entries for JSON structures not containing any values, such as {"a": {}}
. If a search for documents containing such a structure is requested, it will require a full-index scan, which is quite slow. jsonb_path_ops
is therefore ill-suited for applications that often perform such searches.
jsonb
also supports btree
and hash
indexes. These are usually useful only if it's important to check equality of complete JSON documents. The btree
ordering for jsonb
datums is seldom of great interest, but for completeness it is:
Object
>Array
>Boolean
>Number
>String
>Null
Object with n pairs
>object with n - 1 pairs
Array with n elements
>array with n - 1 elements
Objects with equal numbers of pairs are compared in the order:
key-1
,value-1
,key-2
...
Note that object keys are compared in their storage order; in particular, since shorter keys are stored before longer keys, this can lead to results that might be unintuitive, such as:
{ "aa": 1, "c": 1} > {"b": 1, "d": 1}
Similarly, arrays with equal numbers of elements are compared in the order:
element-1
,element-2
...
Primitive JSON values are compared using the same comparison rules as for the underlying Postgres Pro data type. Strings are compared using the default database collation.
8.14.5. Transforms
Additional extensions are available that implement transforms for the jsonb
type for different procedural languages.
The extensions for PL/Perl are called jsonb_plperl
and jsonb_plperlu
. If you use them, jsonb
values are mapped to Perl arrays, hashes, and scalars, as appropriate.
The extensions for PL/Python are called jsonb_plpythonu
, jsonb_plpython2u
, and jsonb_plpython3u
(see Section 47.1 for the PL/Python naming convention). If you use them, jsonb
values are mapped to Python dictionaries, lists, and scalars, as appropriate.
[6] For this purpose, the term “value” includes array elements, though JSON terminology sometimes considers array elements distinct from values within objects.