public inbox for [email protected]  
help / color / mirror / Atom feed
Arrays vs separate tables
5+ messages / 4 participants
[nested] [flat]

* Arrays vs separate tables
@ 2025-10-19 20:43 Rich Shepard <[email protected]>
  2025-10-19 20:50 ` Re: Arrays vs separate tables Adrian Klaver <[email protected]>
  2025-10-19 20:53 ` Re: Arrays vs separate tables Ray O'Donnell <[email protected]>
  2025-10-20 11:28 ` Re: Arrays vs separate tables Peter J. Holzer <[email protected]>
  0 siblings, 3 replies; 5+ messages in thread

From: Rich Shepard @ 2025-10-19 20:43 UTC (permalink / raw)
  To: pgsql-general

I started developing business and science databases in the mid-1980s using
dBASE III, then Paradox on DOS. I defenestrated in 1997 and have used only
linux for both business and personal needs ever since and using only
PostgreSQL for my databaes (primarily for my own use.) I read Joe Celko's
monthly column in 'Database Advisor' and all his SQL books (in multiple
editions.)

Now I'm slowly cleaning up my business tracking database using features not
available way back when I developed it. That's why I ask questions that must
seem obvious to all of you who work with postgres everyday and have for
years. I'm reading 'SQL Queries for Mere Mortals' and just ordered the 4th
edition of 'Database Design for Mere Mortals' to update my SQL knowledge.

In the former book I read that sometimes it's better to have multiple values
for an atribute in a single row by creating a separate table for that
attribute rather than using the postgres array capability. The people table
in my database (1706 rows) has two attributes using the array type
(direct_phone and email), and only a few indivuals have multiple landline
phone numbers (cell_phone is a separate column) or email addresses (office
and personal?). Would it make sense for me to create new landline and email
address tables and replace the array contents?

Other than remembering to use curly braces {} when entering data into an
array column I don't think there'd be any performance benefit for making the
change. I'm curious to learn about arrays vs separate tables.

TIA,

Rich






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Arrays vs separate tables
  2025-10-19 20:43 Arrays vs separate tables Rich Shepard <[email protected]>
@ 2025-10-19 20:50 ` Adrian Klaver <[email protected]>
  2 siblings, 0 replies; 5+ messages in thread

From: Adrian Klaver @ 2025-10-19 20:50 UTC (permalink / raw)
  To: Rich Shepard <[email protected]>; pgsql-general

On 10/19/25 13:43, Rich Shepard wrote:

> In the former book I read that sometimes it's better to have multiple 
> values
> for an atribute in a single row by creating a separate table for that
> attribute rather than using the postgres array capability. The people table
> in my database (1706 rows) has two attributes using the array type
> (direct_phone and email), and only a few indivuals have multiple landline
> phone numbers (cell_phone is a separate column) or email addresses (office
> and personal?). Would it make sense for me to create new landline and email
> address tables and replace the array contents?

For direct_phone and email entries that have more then one value, how do 
you know what the values are pointing at e.g home vs office vs second 
office location, etc?

> 
> Other than remembering to use curly braces {} when entering data into an
> array column I don't think there'd be any performance benefit for making 
> the
> change. I'm curious to learn about arrays vs separate tables.
> 
> TIA,
> 
> Rich
> 
> 


-- 
Adrian Klaver
[email protected]






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Arrays vs separate tables
  2025-10-19 20:43 Arrays vs separate tables Rich Shepard <[email protected]>
@ 2025-10-19 20:53 ` Ray O'Donnell <[email protected]>
  2025-10-19 21:49   ` Re: Arrays vs separate tables Rich Shepard <[email protected]>
  2 siblings, 1 reply; 5+ messages in thread

From: Ray O'Donnell @ 2025-10-19 20:53 UTC (permalink / raw)
  To: Rich Shepard <[email protected]>; pgsql-general

On 19/10/2025 21:43, Rich Shepard wrote:

> In the former book I read that sometimes it's better to have multiple 
> values
> for an atribute in a single row by creating a separate table for that
> attribute rather than using the postgres array capability. The people 
> table
> in my database (1706 rows) has two attributes using the array type
> (direct_phone and email), and only a few indivuals have multiple landline
> phone numbers (cell_phone is a separate column) or email addresses 
> (office
> and personal?). Would it make sense for me to create new landline and 
> email
> address tables and replace the array contents?
>
My experience of doing something similar was that arrays work very well 
for the use-case you describe, as long as you don't have to search 
inside the arrays... I found that, if you have to search for a specific 
value inside an array, then performance really goes out the window. Mind 
you, clever use of an index would probably help here.

Ray.

-- 
Ray O'Donnell // Galway // Ireland
[email protected]







^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Arrays vs separate tables
  2025-10-19 20:43 Arrays vs separate tables Rich Shepard <[email protected]>
  2025-10-19 20:53 ` Re: Arrays vs separate tables Ray O'Donnell <[email protected]>
@ 2025-10-19 21:49   ` Rich Shepard <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Rich Shepard @ 2025-10-19 21:49 UTC (permalink / raw)
  To: pgsql-general

On Sun, 19 Oct 2025, Ray O'Donnell wrote:

> My experience of doing something similar was that arrays work very well
> for the use-case you describe, as long as you don't have to search inside
> the arrays... I found that, if you have to search for a specific value
> inside an array, then performance really goes out the window. Mind you,
> clever use of an index would probably help here.

Ray,

So far searching has not been an issue so I'll keep the status quo.

Thanks,

Rich






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Arrays vs separate tables
  2025-10-19 20:43 Arrays vs separate tables Rich Shepard <[email protected]>
@ 2025-10-20 11:28 ` Peter J. Holzer <[email protected]>
  2 siblings, 0 replies; 5+ messages in thread

From: Peter J. Holzer @ 2025-10-20 11:28 UTC (permalink / raw)
  To: [email protected]

On 2025-10-19 13:43:09 -0700, Rich Shepard wrote:
> Now I'm slowly cleaning up my business tracking database using features not
> available way back when I developed it. That's why I ask questions that must
> seem obvious to all of you who work with postgres everyday and have for
> years. I'm reading 'SQL Queries for Mere Mortals' and just ordered the 4th
> edition of 'Database Design for Mere Mortals' to update my SQL knowledge.
> 
> In the former book I read that sometimes it's better to have multiple values
> for an atribute in a single row by creating a separate table for that
> attribute rather than using the postgres array capability.

Replace "sometimes" with "usually", or "conventionally".

"No composite values" is the core of the first normal form. And of
course the other normal forms build on the first normal form, so your
dataase will never be normalized if you have arrays or other composite
values (e.g. json) in your database.

That said, sometimes it is advantageous to eschew normalization (if that
wasn't the case, PostgreSQL wouldn't have arrays, json, etc.).

The main reasons (IMHO) are:

* Performance: It's just a lot faster to get a row from a single table
  than to join multiple tables.
* Simplicity: If a set of values is always fetched together and updated
  together, you might as well treat it as a unit and not split it over
  multiple tables

I think the first reason is moot in your case. But it's the reason why
datawarehouses often contain huge tables with hundreds of columns and
lots and lots of redundant data.

The second may be relevant for you. If you always display and edit the
phone numbers of a contact together and your frontend makes it easier to
edit an array than a subset of rows from a table, you might just stuff
them into a table and ignore "purity".

        hjp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"


Attachments:

  [application/pgp-signature] signature.asc (833B, 2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2025-10-20 11:28 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-10-19 20:43 Arrays vs separate tables Rich Shepard <[email protected]>
2025-10-19 20:50 ` Adrian Klaver <[email protected]>
2025-10-19 20:53 ` Ray O'Donnell <[email protected]>
2025-10-19 21:49   ` Rich Shepard <[email protected]>
2025-10-20 11:28 ` Peter J. Holzer <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox