0

Postgres has a lot of support for JSON (even indexing). I want to just put 1 jsonb field called everything instead of defining of bunch of typed columns (mongodb style).

But I assume this is a bad idea. My question is: Why shouldn't I do this? Is the only con just the fact that it'll probably take extra storage space?

(My table has a lot of optional fields which is why I want to do this)

3
  • That will depend on how well you want your data structured. A json field will make you lose normalization (and that is debatable) but for an application that it have a defined object (like a simple pojo) it could be a good option. I'm an old school I always prefer to have a good and well defined and normalized database model. Nowadays people tend to use json data. Anyways if you are using a json data just to get rid of columns i disagree but if it is to represent an object it is fine. Commented Jun 17, 2016 at 19:19
  • first concern - FK. jsonb indexing is great, but having constraints would be difficult Commented Jun 17, 2016 at 19:32
  • blog.2ndquadrant.com/… Commented Jun 17, 2016 at 23:40

1 Answer 1

4

I can think of a few reasons:

  • If the JSON blob has foreign keys to other columns, I don't think you can add a REFERENCES constraint.

  • With columns you can enforce NOT NULL constraints very easily, but with JSON you'll have to write CHECK constraints for each required value.

  • JSON has fewer types than Postgres. For instance, no date/time/timestamp, no intervals, no ranges, just one kind of numeric type, no IP addresses, no binary data.

  • Over time if you add/move/delete fields in the JSON structure, you'll find that older rows become obsolete and have JSON your app doesn't understand. Using regular columns forces you to keep the whole table up-to-date.

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.