You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Vary Contains translation (@> vs. ANY) based on modeled indexes #3546
EF Core translates this query into something like this:
SELECT (
SELECTcount(*)::intFROM"Entities"AS a0
WHERE a."Property1Values" @> ARRAY[a0."Property1"]::text[] AND a."Property2Values" @> ARRAY[a0."Property2"]::text[])
FROM"Conditions"AS a
I.e. it creates an array from a single value, and compares them via @> operator. In this case Postgres ignores indexes, and on a huge table (60m rows) it executes more than 1 minute.
On the other hand, if I rewrite this query to this:
SELECT (
SELECTcount(*)::intFROM"Entities"AS a0
WHERE a0."Property1"= ANY(a."Property1Values") AND a0."Property2"= ANY(a."Property2Values"))
FROM"Conditions"AS a
It uses indexes for Property1 and Property2, and executes in ~1ms.
Describe the solution you'd like
EF Core should translate Array.Contains as ANY operator instead of creating single-element arrays.
@Dreamescaper what kind of index do you have on your array columns? The @> operator is supposed to work well with GIN operators.
Regardless, I'm closing this issue as specific to the PostgreSQL provider (this is the general EF repo). Please don't answer here, but rather reopen (if necessary) on npgsql/efcore.pg.
I have no indexes on array columns. I do have regular B-TREE indexes on the other side - Entity.Property1 and Entity.Property2.
I can see some related issue, requesting the opposite (i.e. current behavior - should it be closed as done?) #460
Thanks, yeah - I just closed that one as requesting the current behavior.
I have no indexes on array columns. I do have regular B-TREE indexes on the other side - Entity.Property1 and Entity.Property2.
OK, I understand now - thanks for the extra context.
I think what we need is to have the provider generate different SQLs based on the indexes defined in the EF model. That is, if there's a (GIN) index on the column, translate to @>, if there's an index on the other side translate to ANY. The current strategy (code) is to pick the translation based on what the array is, without looking at actual indexes: if it's a column we assume a GIN index and translate to @>, in most other cases we translate to ANY.
Otherwise, at some point we could consider exposing explicit mechanisms to allow users to pick the specific translation mechanism via EF.Functions. However, leaving the decision-making inside the provider - based on modeled indexes - seems to make sense.
changed the title [-]Consider translating Array.Contains as ANY operator on PostgreSQL[/-][+]Vary Contains translation (@> vs. ANY) based on modeled indexes[/+]on Jun 9, 2025
Activity
Dreamescaper commentedon Jun 9, 2025
@roji
I have no indexes on array columns. I do have regular B-TREE indexes on the other side -
Entity.Property1andEntity.Property2.Dreamescaper commentedon Jun 9, 2025
I can see some related issue, requesting the opposite (i.e. current behavior - should it be closed as done?)
#460
roji commentedon Jun 9, 2025
Thanks, yeah - I just closed that one as requesting the current behavior.
OK, I understand now - thanks for the extra context.
I think what we need is to have the provider generate different SQLs based on the indexes defined in the EF model. That is, if there's a (GIN) index on the column, translate to
@>, if there's an index on the other side translate to ANY. The current strategy (code) is to pick the translation based on what the array is, without looking at actual indexes: if it's a column we assume a GIN index and translate to@>, in most other cases we translate to ANY.Otherwise, at some point we could consider exposing explicit mechanisms to allow users to pick the specific translation mechanism via EF.Functions. However, leaving the decision-making inside the provider - based on modeled indexes - seems to make sense.
[-]Consider translating Array.Contains as ANY operator on PostgreSQL[/-][+]Vary Contains translation (@> vs. ANY) based on modeled indexes[/+]Dreamescaper commentedon Jun 9, 2025
I'm attaching a small console repro project (with Testcontainers). It creates context, feeds data, invokes both queries and prints EXPLAIN ANALYZE.
EfCorePgRepro.zip
WIP on varying Contains translation based on indexes
roji commentedon Jun 11, 2025
Doing this properly depends on dotnet/efcore#36231 (I've submitted a PR). Partial work in https://github.com/roji/efcore.pg/tree/ContainsIndexes.
Vary array containment translation based on indexes
Vary array containment translation based on indexes
Vary array containment translation based on indexes (npgsql#3611)
arrayColumn.Contains(element)translates intoelement = ANY(arrayColumn)in version 10 #3862 as a duplicate of this issue