{"canonical_url":"https://engineer.company/glossary/composite-index/","content_markdown":"# Composite index\n\n## What it is\n\nA composite index is a database index with more than one key column. A\nPostgreSQL B-tree on tenant identifier and reading time can keep rows arranged\nby tenant and then by time within each tenant. The database can use this\narrangement to find a particular tenant's interval without reading every row in\nthe table. Column order matters because it shapes which parts of the index the\ndatabase can locate and scan efficiently.\n\nA dashboard request for readings from one tenant during a time interval fits\nthat ordering. Equality on the tenant selects one region of the index and the\ntime range narrows the scan within it. A query for all tenants during the same\ninterval has a different shape. The database may have to visit many separate\nregions. PostgreSQL can sometimes use a skip scan on a multicolumn B-tree if the\nplanner judges it beneficial, but that depends on the distribution and available\nalternatives. Test the actual query and data before relying on that choice.\n\nIndex definitions can contain key columns that determine ordering as well as\nextra columns stored so that the index can provide other values. PostgreSQL\nsupports INCLUDE columns for eligible index types. These values can help an\nindex-only scan when the visibility information also makes that possible. They\nhave different semantics from key columns. A unique composite key enforces\nuniqueness of the combination under the engine's rules. If code expects to find\neach component on its own, check that it actually has a separately useful index\nor access path.\n\n## Why it matters\n\nA suitable index can reduce page reads and sorting for common access patterns.\nThe effect is greatest when index order and the query agree about what narrows\nthe result or permits an early stop. A dashboard that fetches twenty recent\nreadings from one tenant can often stop after finding enough candidates if the\nindex gives them in the needed order. A monthly export may need most rows in a\npartition and benefit differently. Inspect both with representative parameters.\n\nAdditional indexes also add storage and work to inserts, updates and deletes. An\nindex with wider keys or included values can become expensive on a table that\nreceives many writes. It can increase the amount of data to maintain and hold in\nmemory. Decide which request or job needs improvement and collect both read and\nwrite measurements. A faster query that is rarely called may be less valuable\nthan a smaller index with strong benefits for several popular routes.\n\nColumn order is a design choice about the workload. Equality predicates, range\npredicates, join keys and requested ordering all matter. For separately queried\ncolumns, multiple single-column indexes may be useful and the database may be\nable to combine them for some queries. A composite index can serve an ordered\nlookup particularly well. Compare the alternatives on the real sizes and\ndistributions, and allow for changes in the most frequent query shapes as the\nproduct grows.\n\n## What it looks like in practice\n\nSuppose a readings table stores a tenant, a device, a time and a value. Collect\ncommon queries from the dashboard before creating an index. Compare a single\ntenant's latest readings with a fleet report across tenants, an individual\ndevice drill-down and a broad export. Record which columns are filtered by\nequality, which use ranges and how the result is ordered. Use a sample large\nenough to show useful differences between one heavily used tenant and many\nlightly used ones.\n\nTry the proposed tenant and reading-time index in a safe test environment.\nCompare plans and end-to-end elapsed time before and after. Include the effect\nduring sustained ingest. A plan shows the rows produced by the index scan and\nwhether subsequent row visits or sorting still account for most of the work.\nBuffer counters add evidence about the pages accessed. Confirm that any\nimprovement persists for the query groups you selected. If a later condition is\nselective but almost every page must be visited first, reconsider the key order,\nquery or layout.\n\nOur\n[PostgreSQL optimisation case study](/portfolio/accelerated-postgresql-performance-by-10x/)\ndescribes adding composite indexes for filters such as tenant ID plus a date\nrange for electrical data. It also describes partitioning and query rewrites, so\nthe overall result reflects several changes. The readings example isolates an\nindex decision and gives a way to measure how much of that specific workload it\nhelps.\n\n## Where it goes wrong\n\nAn index can match a query's list of columns without matching how the query uses\nthem. A function on a filtered column, incompatible type coercion or a different\ncollation can change the available access paths. Sort direction and null\nordering matter for some ordered scans. Check the exact expression and plan seen\nin production. A different expression index, key ordering or query shape may be\nnecessary.\n\nA wide covering index can look attractive in a mostly read-only test and be\ncostly in operation. Measure disk, cache pressure and update cost. Validate\nindexes under the table's actual churn because PostgreSQL may need heap visits\nfor visibility checks even when an index carries every requested column. Add\nincluded fields only for reads whose measured benefit justifies the larger\nstructure.\n\nMultiple near-identical indexes can accumulate as features change. Find which\nqueries and constraints use an index before dropping or replacing it. Confirm\nthat a proposed replacement supports common leading-column lookups and the\nuniqueness rules the old one enforced. Use the database's documented build and\nreplacement procedure, observe its lock and storage demands, and define how to\nstop if the operation threatens normal traffic.\n\n## Further reading\n\n- [PostgreSQL multicolumn indexes](https://www.postgresql.org/docs/18/indexes-multicolumn.html)\n  explains column order, qualifying conditions and skip scans.\n- [PostgreSQL indexes and ordering](https://www.postgresql.org/docs/18/indexes-ordering.html)\n  describes when a B-tree can provide the order a query requests.\n- [PostgreSQL covering indexes](https://www.postgresql.org/docs/18/indexes-index-only-scans.html)\n  covers included columns and the visibility requirements for index-only scans.\n\n\u003chttps://engineer.company/glossary/composite-index/\u003e\n","date_modified":"2026-11-24T21:48:58Z","description":"How to choose the column order of a composite index and measure its effect on reads and writes.","formats":{"gemtext":"https://engineer.company/glossary/composite-index/index.gmi","gopher":"https://engineer.company/glossary/composite-index/gophermap","html":"https://engineer.company/glossary/composite-index/","json":"https://engineer.company/glossary/composite-index/index.json","markdown":"https://engineer.company/glossary/composite-index/index.md","pdf":"https://engineer.company/glossary/composite-index/index.pdf"},"kind":"page","language":"en","title":"What is a composite index?","url":"https://engineer.company/glossary/composite-index/","version":1}