Skip to content
Advertisement

LEFT OUTER JOIN on array column with multiple values

I cannot seem to find the trick to join two tables through an array column when one table is not an array value, and the other table’s array value can contain multiple values. It does work when there is a single valued array.

Here’s a simple minimal example of what I’m talking about. The real tables have GIN indexes on the array columns FWIW. These do not, but the query behaves the same.

The actual results are this like so. The issue here is that name column comes up NULL if either actors or benefactors contains more than one value.

I was expecting this:

It would be really nice if I could get it to look like this, though:

I’m aware that this schema denormalized, and I’m willing to go to a normal representation if need be. However, this is for a summary query and it already involves a lot more joins than I’d like.

Advertisement

Answer

Yes, the overlap operator && could use a GIN index on arrays. Very useful for queries this one to find rows with a given person (1) among an array of actors:

However, the logic of your query is the other way round, looking for all persons listed in the arrays in eg_assoc. A GIN index is no help here. We just need the btree index of the PK person.id.

Proper queries

Basics:

The following queries preserve original arrays exactly as given, including possible duplicate elements and original order of elements. Works for 1-dimenstional arrays. Additional dimensions are folded into a single dimension. It’s more complex to preserve multiple dimensions (but totally possible):

WITH ORDINALITY in Postgres 9.4 or later

LATERAL queries

For PostgreSQL 9.3+.

db<>fiddle here with a couple of variants.
Old sqlfiddle

Subtle detail: If a person is not found, it’s just dropped. Both of these queries generate an empty array ('{}') if no person is found for the whole array. Other query styles would return NULL. I added variants to the fiddle.

Correlated subqueries

For Postgres 8.4+ (where generate_subsrcipts() was introduced):

May still perform best, even in Postgres 9.3.
The ARRAY constructor is faster than array_agg(). See:

Your failed query

The query provided by @a_horse seems to do the job, but it is unreliable, misleading, potentially incorrect and needlessly expensive.

  1. Proxy cross join because of two unrelated joins. A sneaky anti-pattern. See:

    Fixed superficially with DISTINCT in array_agg()to eliminates the generated duplicates, but that’s really putting lipstick on a pig. It also eliminates duplicates in the original because its impossible to tell the difference at this point – which is potentially incorrect.

  2. The expression a_person.id = any(eg_assoc.actors) works, but eliminates duplicates from the result (happens two times in this query), which is wrong unless specified.

  3. Original order of array elements is not preserved. This is tricky in general. But it’s aggravated in this query, because actors and benefactors are multiplied and made distinct again, which guarantees arbitrary order.

  4. No column aliases in the outer SELECT result in duplicate column names, which makes some clients fails (not working in the fiddle without aliases).

  5. min(actors) and min(benefactors) are useless. Normally one would just add the columns to GROUP BY instead of fake-aggregating them. But eg_assoc.aid is the PK column anyway (covering the whole table in GROUP BY), so that’s not even necessary. Just actors, benefactors.

Aggregating the whole result is wasted time and effort to begin with. Use a smarter query that doesn’t multiply the base rows, then you don’t have to aggregate them back.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement