Documente Academic
Documente Profesional
Documente Cultură
HOME
SUBSCRIBE
Why JSON in
PostgreSQL is
Awesome
10 NOVEMBER 2013 on PostgreSQL, JSON
1/7
21/10/2014
{
"fullName": "Charles John Huffam Dickens",
"names":
[
{ "type": "firstName", "value": "Charles" },
{ "type": "middleName", "value": "John" },
{ "type": "middleName", "value": "Huffam" },
{ "type": "lastName", "value": "Dickens" }
]
}
Book Store
Imagine an application that manages books, such as that
used by a library or a book store. Books have authors,
authors have names and these names have name parts
(e.g. first name, last name, etc.). The application must
https://functionwhatwhat.com/json-in-postgresql/
2/7
21/10/2014
title
A Christmas Carol
Author Table
id
book_id
full_name
101
id
author_id
type
value
201
101
first_name
Charles
202
101
middle_name
John
203
101
middle_name
Huffam
204
101
last_name
Dickens
3/7
21/10/2014
4/7
21/10/2014
Serialisation to JSON
Fortunately, it is possible to serialise the entirety of this
madness into a single JSON string (or XML or something
similar but this post is about JSON). The serialised object
can then be stored in a text column. When reading from
the database, the text can be deserialised back to an object
graph. While this was possible with earlier versions of the
software, PostgreSQL 9.2 and 9.3 have introduced a
number of features that make working with JSON data a
very pleasant experience:
The json data type is basically the same as text in
terms of behaviour and storage, but the database
checks that the value is valid JSON. This was introduced
in PostgreSQL 9.2.
With JSON functions and operators introduced in
PostgreSQL 9.3 one can select authors->1->'fullName'
from book to get the full name of the first author of every
A Christmas
Carol
authors
[
{
"fullName": "Charles John Huffam
https://functionwhatwhat.com/json-in-postgresql/
5/7
21/10/2014
Dickens",
"names":
[
{ "type":
"Charles" },
{ "type":
"John" },
{ "type":
"Huffam" },
{ "type":
"Dickens" }
]
}
]
"firstName", "value":
"middleName", "value":
"middleName", "value":
"lastName", "value":
The Downsides
No loops in the object graph. Circular references are not
permitted in JSON. An author cannot belong to two books
and authors cannot reference each other. The first problem
can be tackled by repeating an authors data for each book
he/she has authored. This means, however, that the
database will be larger and updating (renaming) authors a
very expensive operation. It is up to the programmer to
decide if this is compatible with the requirements of the
application.
No referential integrity. While it is possible to generate
pseudo unique keys by creating a unique index on a JSON
expression, foreign keys cannot be created on (or
reference) JSON expressions. For a field to reference
another column or be referenced by another column, it
https://functionwhatwhat.com/json-in-postgresql/
6/7
21/10/2014
Tamas Czinege
London, UK
https://twitter.com/tamasczinege
https://functionwhatwhat.com/json-in-postgresql/
7/7