GCP Error: 'The query requires an index' — Cause, Fix, and Troubleshooting Guide
Fix Firestore FAILED_PRECONDITION 'The query requires an index': create the composite index and deploy firestore.indexes.json to fix the query.
- #gcp
- #troubleshooting
- #errors
- #database
Stuck on this GCP with AI error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Firestore rejects queries that need a composite index that doesn’t exist yet. Simple single-field queries are auto-indexed, but a query that combines multiple fields (e.g. an equality filter plus an ordering, or multiple range/inequality filters) requires an explicit composite index:
FAILED_PRECONDITION: The query requires an index. You can create it here:
https://console.firebase.google.com/project/acme-prod-platform/firestore/indexes?create_composite=Cl...
The error is a FAILED_PRECONDITION (HTTP 400) — the data and permissions are fine; the index that makes the query servable is missing.
Symptoms
- Queries fail with
FAILED_PRECONDITION: The query requires an indexand acreate_composite=...link. - A query works in one environment (indexes deployed) but fails in another (indexes not deployed).
- The failure appears only for multi-field queries; single-field ones succeed.
- Client libraries surface it as
FailedPrecondition/ gRPC code 9.
Common Root Causes
1. A new composite query with no matching index
You added an ordering or a second filter field and never created the composite index it needs.
2. Indexes not deployed to this project/database
firestore.indexes.json exists in source but was never firebase deploy-ed to the target project.
3. Index still building
The index was created but is still in the CREATING state; queries fail until it is READY.
4. Query shape doesn’t match any existing index
Field order, sort direction, or an added array-contains differs from the index definition.
How to Diagnose
All read-only.
# List existing composite indexes and their state
gcloud firestore indexes composite list \
--project=acme-prod-platform \
--format="table(name.basename(), state, fields[].fieldPath)"
# Confirm the active database/project
gcloud config get-value project
# In source control, check the tracked index definitions
cat firestore.indexes.json | grep -A6 '"collectionGroup"'
Compare the failing query’s fields/sort order against the listed indexes. A state: CREATING row means you just need to wait.
Fixes
Fastest: click the console link in the error. It pre-fills the exact composite index; create it and wait for READY.
Reproducible: define and deploy the index as code. Add it to firestore.indexes.json:
{
"indexes": [
{
"collectionGroup": "orders",
"queryScope": "COLLECTION",
"fields": [
{ "fieldPath": "customerId", "order": "ASCENDING" },
{ "fieldPath": "createdAt", "order": "DESCENDING" }
]
}
]
}
firebase deploy --only firestore:indexes --project acme-prod-platform
Wait for the build. Large collections take minutes to hours; poll until state is READY:
gcloud firestore indexes composite list --project=acme-prod-platform \
--format="value(name.basename(), state)"
What to Watch Out For
- Never rely on the console click-through as your only source of truth — commit indexes to
firestore.indexes.jsonso every environment gets them. - Index build time scales with existing document count; deploy indexes before the query ships to avoid production failures.
- The index field order and sort directions must match the query exactly; reversing a sort needs a different (or additional) index.
- This is not a permissions error — do not touch Firestore Security Rules or IAM for a
FAILED_PRECONDITIONindex message.
Related
- GCP Error: ‘INVALID_ARGUMENT’
- GCP Error: ‘The resource … was not found’ (404)
- GCP Error: ‘DEADLINE_EXCEEDED’
- More in the GCP error guides.
Fixed it? Get 500 GCP with AI & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.