Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best PostgreSQL datatype for storing key-value maps?

I'd like to store a simple map of key-value strings as a field in my PostgreSQL table. I intend to treat the map as a whole; i.e, always select the entire map, and never query by its keys nor values.

I've read articles comparing between hstore, json and jsonb, but those didn't help me choose which data-type is most fitting for my requirements, which are:

  • Only key-value, no need for nesting.
  • Only strings, no other types nor null.
  • Storage efficiency, given my intended use for the field.
  • Fast parsing of the queried maps.

What data-type would best fit my use case?

like image 909
Eyal Roth Avatar asked Apr 06 '17 16:04

Eyal Roth


People also ask

Which is better JSON or Jsonb?

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.

Should I use JSON or Jsonb Postgres?

PostgreSQL jsonb Json preserves the original formatting like the whitespaces as well as the ordering of keys. Jsonb does not preserve the original formatting of text like the whitespaces and the ordering of keys. Json processes input faster than jsonb as there is no conversion involved in this.

What is Jsonb data type?

The JSONB data type stores JSON (JavaScript Object Notation) data as a binary representation of the JSONB value, which eliminates whitespace, duplicate keys, and key ordering. JSONB supports GIN indexes.

What is serial4 in PostgreSQL?

The type names serial and serial4 are equivalent: both create integer columns. The type names bigserial and serial8 work just the same way, except that they create a bigint column.

What is the best data type for primary keys in PostgreSQL?

UUID is an ideal Data type for primary keys. Postgre also accepts alternative forms of UUID inputs like all capital case, no hyphens, braces, etc. PostgreSQL allows you to store XML data in a data type, but it is nothing more than an extension to a text data type.

Does PostgreSQL support character data types for storing text?

PostgreSQL supports character data types for storing text values. PostgreSQL builds character data types off of the same internal structures. PostgreSQL offers three character data types: CHAR(n), VARCHAR(n), and TEXT.

Which data type is used to store date-time data in PostgreSQL?

This data type is used to store date-time data. PostgreSQL has 5 temporal data type: DATE is used to store the dates only. TIME is used to stores the time of day values. TIMESTAMP is used to stores both date and time values. TIMESTAMPTZ is used to store a timezone-aware timestamp data type.

Is it possible to use PostgreSQL data types in Java?

Well, mostly, as long as you remember what the PostgreSQL data types used in the table columns are, and use the correct PostgreSQL to Java mappings. As an experiment I tried a few data types including int2/int4/int8 (short, int, long), char/text (String), numeric (BigDecimal).


1 Answers

you could use hstore which is a keymap, however I personally would us jsonb. It's a little overkill, however, most languages can convert json natively without having to decode it yourself.

In json, I'd just store a simple object or array for the info you're trying to store.

Both support indexes and are efficiently stored.

Hstore in text is a custom type format that your language may be unaware of and thus require processing the data to insert or query.

like image 92
Joe Love Avatar answered Sep 17 '22 22:09

Joe Love