2

I have a bunch of properties (columns in db) that should have a string value from a list of predefined values, for example active, inactive, pending.

Outside the database, which is javascript (deno+react in front-end) I really want to have these as string enums, and not a integers, because it adds some clarity to the code and actually reduces the amount of code you need to write.

In the database I have two options, either I store them as they are (strings), or as smallint then convert them to/from strings during the database select/insert.

Problem with the second option is that it requires additional transformation in the code, and when using typescript, this transformation is not so straightforward as doing a simple mapping, because ts doesn't let you re-assign different types to existing props, so you need to copy and do more stuff...

Anyway, I just wanted to ask, how much is the postgres performance and/or size overhead if I store them directly as strings in the db too, to eliminate transformation step? I imagine that each record would grow by around 20 bytes if I change from smallint to text and that would have some kind effect on performance? Also these columns have to be indexed, and that adds another 20 bytes for each record

1
  • 2
    You could use ENUM in the DB too. Commented Apr 6 at 9:03

1 Answer 1

3

The performance impact is hard to predict; you would have to run a benchmark.

The bigger problem is consistency: you'd need check constraints to make sure only the proper strings are stored.

The best solutions would be:

  • Use a lookup table with a smallint primary key, have a foreign key referencing that table and join with that table when you query the data.

  • Use an ENUM type that looks like a string, but is a real internally. That is a good solution if you never need to delete a value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.