From b579cfceefdbcd95886585cd6efa8415bb209fbb Mon Sep 17 00:00:00 2001 From: "s.kazemi" Date: Wed, 14 Oct 2026 13:19:43 +0330 Subject: [PATCH] feat: inline enum filters that match a partial index predicate (0.20.0) asyncpg runs every query as a server-side prepared statement, and after a few executions Postgres may switch it to a generic plan. A generic plan cannot prove a partial index's WHERE predicate from a bound parameter, so a filter like `status IN ($1)` stops using `... WHERE status = 'PENDING'` indexes and falls back to a broader index or a full scan, with no error and usually no sign of it on a small development database. Add the pattern to using-archipy-adapters.mdc: render closed-enum values that match a partial index predicate with `literal(..., literal_execute=True)`, keep index-key columns and user input bound, and confirm with a forced generic plan. The archipy-reviewer flags bound filters on predicate columns as Should fix and does not flag the inlined enum literals as unparameterized SQL. Co-Authored-By: Claude Opus 5.5 --- .claude-plugin/marketplace.json | 2 +- .claude-plugin/plugin.json | 2 +- .cursor-plugin/marketplace.json | 2 +- .cursor-plugin/plugin.json | 2 +- CHANGELOG.md | 9 ++++++++ agents/archipy-reviewer.md | 11 ++++++++++ rules/using-archipy-adapters.mdc | 37 ++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 4 deletions(-) diff --git a/.claude-plugin/marketplace.json b/.claude-plugin/marketplace.json index 4885bf0..7172f17 100644 --- a/.claude-plugin/marketplace.json +++ b/.claude-plugin/marketplace.json @@ -8,7 +8,7 @@ "name": "archipy", "source": ".", "description": "Cursor and Claude Code plugin: rules, skills, agents, and hooks for building apps with ArchiPy", - "version": "0.19.1" + "version": "0.20.0" } ] } diff --git a/.claude-plugin/plugin.json b/.claude-plugin/plugin.json index c328c27..ef168f3 100644 --- a/.claude-plugin/plugin.json +++ b/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "archipy", - "version": "0.19.1", + "version": "0.20.0", "description": "Cursor and Claude Code plugin: rules, skills, agents, and hooks for building apps with ArchiPy", "author": { "name": "SyntaxArc" diff --git a/.cursor-plugin/marketplace.json b/.cursor-plugin/marketplace.json index 4885bf0..7172f17 100644 --- a/.cursor-plugin/marketplace.json +++ b/.cursor-plugin/marketplace.json @@ -8,7 +8,7 @@ "name": "archipy", "source": ".", "description": "Cursor and Claude Code plugin: rules, skills, agents, and hooks for building apps with ArchiPy", - "version": "0.19.1" + "version": "0.20.0" } ] } diff --git a/.cursor-plugin/plugin.json b/.cursor-plugin/plugin.json index bb7a4dd..a8ce9b5 100644 --- a/.cursor-plugin/plugin.json +++ b/.cursor-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "archipy", - "version": "0.19.1", + "version": "0.20.0", "description": "Cursor and Claude Code plugin: rules, skills, agents, and hooks for building apps with ArchiPy", "author": { "name": "SyntaxArc" diff --git a/CHANGELOG.md b/CHANGELOG.md index 914497c..fa2fb1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.20.0] - 2026-10-14 + +### Added + +- Partial-index rule: a filter on a column named in a partial index predicate renders its enum values inline with + `literal(..., literal_execute=True)`, since a bound parameter stops matching the index once Postgres switches the + prepared statement to a generic plan; covered in `using-archipy-adapters.mdc` and an `archipy-reviewer` check + reported as **Should fix** + ## [0.19.1] - 2026-10-10 ### Fixed diff --git a/agents/archipy-reviewer.md b/agents/archipy-reviewer.md index 5bf8a1b..60e7fac 100644 --- a/agents/archipy-reviewer.md +++ b/agents/archipy-reviewer.md @@ -87,6 +87,15 @@ makes it worse. - A logic method that reads or writes through a SQLAlchemy-backed repository without an atomic decorator is a finding. - Sync and async are not mixed within one class or one server. +**Partial indexes** (Postgres) + +- A changed query that filters a column named in a partial index predicate (`Index(..., postgresql_where=...)` in the + entity's `__table_args__`, or `CREATE INDEX ... WHERE` in the app's SQL migrations) renders that filter's values + inline with `literal(value, literal_execute=True)`, values from a closed enum only. Bound (`field == value`, + `field.in_(values)`, `_apply_filter`), the filter stops matching the index once Postgres switches the prepared + statement to a generic plan, and the query falls back to a broader index or a full scan (**Should fix**). Columns in + the index key are fine as bound parameters. + **Errors** - Raise specific ArchiPy `BaseError` subclasses or domain errors, not bare `Exception`. @@ -180,6 +189,8 @@ Do not flag these; they are allowed by the rules: - Local CLI flags in `manage.py` that override `config.FASTAPI` defaults. - `hashlib` digests for cache keys/ETags (not passwords), and process/task projects calling logics from Behave steps via `process_logic`. +- `literal(.value, literal_execute=True)` filters that match a partial index predicate. The values come from a + closed enum and SQLAlchemy quotes them, so this does not break the parameterized-queries rule. - Style choices the app's own formatter/linter config accepts. ## Report diff --git a/rules/using-archipy-adapters.mdc b/rules/using-archipy-adapters.mdc index 7170bba..6db710d 100644 --- a/rules/using-archipy-adapters.mdc +++ b/rules/using-archipy-adapters.mdc @@ -71,6 +71,43 @@ except KafkaError as e: Never leak raw driver exceptions into logics/services. +## Partial Indexes and Bound Parameters + +asyncpg runs every query as a server-side prepared statement (psycopg 3 does once a query passes its +`prepare_threshold`). From about the sixth execution on a connection, Postgres may switch that statement to a generic +plan, which sees `$1` instead of the value. A partial index (`CREATE INDEX ... WHERE status = 'PENDING'`) is usable only +when the planner can prove its predicate, so a filter that binds the predicate's column (`field == value`, +`field.in_(values)`, or `SQLAlchemyFilterMixin._apply_filter`) stops matching the index once the plan goes generic. The +query falls back to a broader index or a full scan. Nothing errors, and a small development database rarely shows it. + +When a filter targets a column named in a partial index predicate, render its values inline. Do this only for values +from a closed enum; keep user input and IDs bound: + +```python +from sqlalchemy import literal + +if query_dto.statuses is not None: + query = query.where( + OrderEntity.status.in_( + [literal(status.value, literal_execute=True) for status in query_dto.statuses], + ), + ) +``` + +Columns in the index key (`user_id` in `(user_id, created_at) WHERE status = 'PENDING'`) work as bound parameters; only +the predicate's columns need inlining. Each distinct set of inlined values is its own prepared statement, which is +another reason to keep this to small enums. + +Confirm the plan with a forced generic plan: + +```sql +SET plan_cache_mode = force_generic_plan; +PREPARE q(uuid) AS SELECT count(*) FROM orders WHERE user_id = $1 AND status IN ('PENDING'); +EXPLAIN EXECUTE q('00000000-0000-0000-0000-000000000000'); -- should show the partial index +DEALLOCATE q; +RESET plan_cache_mode; +``` + ## Optional Dependencies Install the matching ArchiPy extra and import adapter dependencies normally. Do not hide missing extras behind lazy