Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL effective way to store a list of IDs

Tags:

postgresql

In my PostgreSQL I have two tables board and cards tables with OneToMany relationship between them(one board can have a multiple cards).

User can hold a few cards on the board. In order to implement this functionality typically I would created another table called for example cards_on_hold with OneToMany relationship and placed cards on hold IDs into this table. In order to fetch this data for board I'd use JOIN between board and cards_on_hold.

Is there any more effective way in PostgreSQL to store cards on hold IDs ? Maybe for example some feature to store this list inline in board table ? I'll need to use this IDs list later in IN SQL clause in order to filter card set.

like image 902
alexanoid Avatar asked Apr 28 '16 11:04

alexanoid


People also ask

Can you store a list in PostgreSQL?

PostgreSQL gives you this capability with the array datatype. Arrays are some of the most useful data types for storing lists of information. Whether you have a list of baseball scores, blog tags, or favorite book titles, arrays can be found everywhere.

How many records can Postgres store?

There is no limit on the number of rows in a table but it is limited to available disk space and memory/swap space. If you are storing rows that exceed 2 KB aggregated data size, then the maximum number of rows may be limited to 4 billion or less.

Can Postgres store arrays?

PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created.

What is Unnest in PostgreSQL?

Unnest function generates a table structure of an array in PostgreSQL. Unnest array function is beneficial in PostgreSQL for expanding the array into the set of values or converting the array into the structure of the rows. PostgreSQL offers unnest() function.


1 Answers

Postgres does support arrays of integers (assuming your ids are integers):

http://www.postgresql.org/docs/9.1/static/arrays.html

However manipulating that data is a bit hard compared to a separate table. For example with a separate table you can put a uniqueness guarantee so that you won't have duplicates of ids (assuming you'd want that). To achieve the same thing with an array you would have to create a stored procedure to detect duplicates (on insert for example). That would be hard (if possible at all) to be as efficient as simple unique constraint. Not to mention that you lose consistency guarantee because you can't put foreign key constraint on such array.

So in general conisistency would be an issue with inline list. At the same time I doubt you would get any noticable performance gain. After all arrays should not be used as an "aggregated foreign key" IMHO.

All in all: I suggest you stick to a separate table.

like image 128
freakish Avatar answered Sep 24 '22 17:09

freakish