A v2 versioning write costs queries proportional to the flag's segment overrides
Maintainers usually reply within 1 day
@Ishita-Singh-12 is already working on this.
Since Oct 3, 2026.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- python
- Domain
- backend, performance
Research direction
Start with FeatureState.clone and FeatureSegment.clone in api/features/models.py, then trace EnvironmentFeatureVersion.objects.create(...) and version.publish(...) using the reported CaptureQueriesContext measurements. Inspect the publish path and its signal handlers for per-state relation lookups, and add a service-level query-count test covering the selected optimization scope.
Written by the indexing model from the issue text.
Description
Every write to a flag in a v2-versioned environment opens a new EnvironmentFeatureVersion and publishes it. Both halves of that cost grow with the number of feature states the flag has, so a flag with many segment overrides pays a query count proportional to its overrides.
Measured on main with CaptureQueriesContext around EnvironmentFeatureVersion.objects.create(...) and version.publish(...), for a flag with 1 and with 3 segment overrides (2 and 4 feature states):
| Step | 1 override | 3 overrides | Per extra state |
|---|---|---|---|
EnvironmentFeatureVersion.objects.create |
26 | 54 | ~8 |
version.publish |
45 | 61 | ~5 |
Extrapolated, a flag with 50 segment overrides costs roughly 400 queries to open a version and 250 to publish it. Every v2 write path pays this: the versioning serializers, change request commits, and the experimental update-flag endpoints alike.
Creating a version copies row by row
FeatureState.clone (api/features/models.py:661) is a deepcopy plus save() per state, and FeatureSegment.clone (api/features/models.py:328) does the same. Per cloned state the create issues, roughly:
SELECT features_featureSELECT features_featurestatevalueINSERT features_featurestateandINSERT features_historicalfeaturestateINSERT features_featurestatevalueandINSERT features_historicalfeaturestatevalue- a
SAVEPOINT/RELEASEpair
Some copying is inherent to immutable versions, but the row-at-a-time shape is not: bulk_create for the states and their values, with the history rows batched alongside, would collapse most of it.
Publishing re-fetches relations per state
This half looks like a plain N+1 rather than inherent work. Between 1 and 3 overrides, publish goes from 45 to 61 queries, and the growth is all in per-state relation lookups:
| Query | 1 override | 3 overrides |
|---|---|---|
SELECT multivariate_multivariatefeaturestatevalue |
6 | 10 |
SELECT features_feature |
4 | 8 |
SELECT features_featurestatevalue |
4 | 8 |
SELECT features_featuresegment |
3 | 7 |
Whatever the publish path and its signal handlers iterate should be able to hydrate these with select_related / prefetch_related in one pass.
Suggested scope
- Flatten the publish-side N+1 first — it is the cheaper fix and is not intrinsic to versioning.
- Then consider bulk-creating the cloned states, values and multivariate values when a version is created.
Worth pinning either fix with a query-count test at the service level.
- Dominant language
- Python
- Stars
- 9.9k
- Forks
- 658
- Avg merge
- 1d 15h
- Merged PRs (30d)
- 168
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Flagsmith/flagsmith
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Flagsmith/flagsmith#9119 · 1 comment ·
Maintainers usually reply within 1 day
-
Fix Button Alignment in SDK Installation SectionPossibly taken @manueldezman claimed this 3 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Flagsmith/flagsmith#9056 · 2 comments ·
Maintainers usually reply within 1 day
-
Cloud Python warehouse snippet omits the import needed by its short-lived-job guidancePossibly taken @jcd1991 claimed this 4 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 75/100
Flagsmith/flagsmith#9045 · 1 comment ·
Maintainers usually reply within 1 day
-
Expose whether an organisation is blocked for API usagePossibly taken @PiUnknown claimed this 38 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Flagsmith/flagsmith#8773 · 1 comment ·
Maintainers usually reply within 1 day
-
influx_query_manager doesn't catch ApiException, so Influx error responses 500Possibly taken @SAY-5 claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Flagsmith/flagsmith#8510 · 1 comment ·
Maintainers usually reply within 1 day
All issues in Flagsmith/flagsmith
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 73/100
-
day_or=True union silently becomes the ISC cron AND bug when a day field expands to its full range (e.g. '0 0 */1 * */2' fires only Sun/Tue/Thu/Sat)Possibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
pallets-eco/croniter#309 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
chore
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
nyjc-computing/campus-admin#105 ·
Maintainers usually reply within 1 day
-
bug enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
AOSSIE-Org/Gitcord-GithubDiscordBot#248 · 1 comment ·
Maintainers usually reply within 4 days