7

I have a table with a JSON type column and I have 1 row in the table. Following request show me result 20761 bytes:

SELECT pg_column_size(test_column) FROM test_table;

The value from test_column has size 45888 bytes so it means that PostgreSQL compressed this data, but it compressed 45888/20761=~2.1 times. How can I do compression of JSON type more than existing value?

7
  • 1
    What exactly is your question? How to improve the built-in compression algorithm of Postgres? That's only possible by changing the Postgres source code? Commented Jul 17, 2017 at 11:57
  • maybe is there any extensions for Postgres that can do it? Commented Jul 17, 2017 at 12:00
  • 3
    You can't change the built-in compression algorithm. You might want to have a look in to the extension zson which is a dictionary based compression tailored for JSONB: github.com/postgrespro/zson Commented Jul 17, 2017 at 12:23
  • 2
    @JohnChristopherJones is that really true? In some cases I've found JSONB size to be 130% of the JSON string size. There's also parse and encode time with JSONB. I'm curious how you reconcile these differences but still conclude with "basically no advantage"? Commented Nov 14, 2017 at 22:03
  • 1
    In many scenarios JSONB could be a better option than JSON though definitely not in all cases, e.g., see heapanalytics.com/blog/engineering/… (there are many other articles on the topic). I'd consider saying that there is no advantage to be fairly misleading, indeed these are not advantages but disadvantages of JSONB ;-) Commented Mar 12, 2019 at 15:23

1 Answer 1

8

Changing the type to jsonb does not make it use less disk space, it might in some cases even use more. Take a look at ZSON. It is a PostgreSQL extension that compresses the JSON data by creating a lookup table for the most common data, most likely the json-keys, and it claims to be able to save up to half of the needed disk space.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.