In postgres, what data type should I use to store a unique set of strings?
Is using a simple array then when I want to write, I read the current set of strings, determine what the final set should be considering uniqueness then write it back the best way? Seems somewhat inefficient.
I likely will be commonly do = ANY
queries too to determine if the array contain an element
A possible data model looks like:
BlogPost
- title
- tags: string[]
2 common operations:
- Find blog posts with a given tag
- Update the list of tags (while ideally keeping it a unique set)
3
Answers
In postgreSQL, we can utilize the ‘ text[] ‘ information type to store a variety of strings. If we want uniqueness, we can utilize cluster capabilities like unnest and array_agg to dispose of copies. For effective questions, think about utilizing fitting ordering on the cluster segment, e.g., GIN or Substance files, while performing activities like
= ANY
. The selection of information model and order relies upon the particular use case and information circulation.I would suggest to use a separate table for the tags. The advantage is that the tags can be indexed and efficiently accessed. The index further guarantees that the tags are unique per post.
What I understood from your question that you want to assure you have unique set of strings for each post, and if so then you can use
TEXT
with UNIQUE to assure that like so:UNIQUE
will assure every set you have in the table is unique. If you tried to insert the same set twice you will get an error like so:I hope this answers your question, and let me know how it goes with you!