feat: add native_in/2 function for SQL IN (...) syntax#728
Merged
zachdaniel merged 2 commits intoash-project:mainfrom Mar 27, 2026
Merged
feat: add native_in/2 function for SQL IN (...) syntax#728zachdaniel merged 2 commits intoash-project:mainfrom
zachdaniel merged 2 commits intoash-project:mainfrom
Conversation
Adds AshPostgres.Functions.NativeIn as an escape hatch for cases where PostgreSQL's query planner produces suboptimal plans with = ANY(...) array syntax. The native_in/2 function generates IN ($1, $2, ...) with individually typed parameters instead. Usage: filter(query, native_in(field, [^v1, ^v2, ^v3])) Closes ash-project/ash#2605
Contributor
|
WDYT about |
Renames the function from native_in/2 to postgres_in/2 so it clearly maps to PostgreSQL's IN syntax.
Contributor
|
🚀 Thank you for your contribution! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AshPostgres.Functions.NativeInas an escape hatch for cases where PostgreSQL's query planner produces suboptimal plans with= ANY(...)array syntaxnative_in/2function generatesIN ($1, $2, ...)with individually typed parameters instead of= ANY($1::type[])Usage
Generates:
Instead of:
Context
Closes ash-project/ash#2605
PostgreSQL's query planner may choose different (sometimes suboptimal) indexes when using
= ANY('{...}'::type[])compared toIN ($1, $2, ...). This was reported as a significant performance issue on PostgreSQL 14. As suggested by @zachdaniel, this provides a function-based escape hatch rather than changing the default behavior.Changes
lib/functions/native_in.ex— Function definition ([:any, {:array, :any}]args)lib/sql_implementation.ex— Expression handler that builds a SQL fragment with expanded parameterslib/data_layer.ex— Register the functiontest/native_in_test.exs— Tests for SQL generation and functional correctnessTest plan
IN (...)notANY(...)