INFINITE UPSIDE

Technology and operations

When Can PostgreSQL Use a Partial Index?

A smaller index helps only when the planner can connect it to the question being asked.

A support team searches an orders table for the small fraction of orders still awaiting review. The table is large, most orders are already complete, and the team wants that queue to open quickly. An index covering every row is one possibility. Another is an index that contains only rows meeting a particular condition.

A PostgreSQL partial index indexes a subset of a table, defined by a predicate. A query can benefit only when PostgreSQL can recognize that the query’s conditions imply the index predicate, and the planner still decides whether using it is worthwhile. The PostgreSQL partial-index documentation explains both the use cases and the matching limitation.

The idea is powerful because the operational question is specific. It is also easy to misuse if the team treats a smaller index as a general accelerator for every query. Start with the real workload and verify the actual plan.

An index for a narrower population

An ordinary index may include entries for rows across the table. A partial index includes entries only for rows that satisfy its condition. That condition can describe, for example, rows whose status is awaiting review. The indexed columns and the predicate serve different roles: the predicate selects the population; the index keys determine how that population can be searched or ordered.

Imagine a fictional table with one million orders, of which 20,000 currently await review. A partial index on a useful search key for just those 20,000 rows may be smaller than a comparable index including all one million. The ratio is an illustration of row counts, not a promise that the index will occupy exactly 2% of another index’s bytes. Row width, keys, storage structure and other details affect actual size.

If a query searches the review queue by a customer identifier and includes the review-status condition, that index may be a candidate. A query looking for completed orders cannot generally rely on an index that intentionally omits them. Nor does an index necessarily improve a query that returns most of the qualifying rows; reading the table by another plan may cost less.

Keeping fewer entries can also reduce some index-maintenance work compared with an otherwise similar full index. It does not remove write costs. A row can enter or leave the indexed subset when its status changes, and updates to indexed values still matter. Measure the actual workload rather than assuming every write becomes cheap.

The predicate must be recognizable

The database needs to establish that every row a query might require is represented by the partial index before it can use that index as a complete access path for the relevant condition. PostgreSQL’s documentation describes this as the query condition implying the index predicate. The planner recognizes some straightforward relationships, but it is not a general-purpose proof engine for every equivalent expression.

Suppose the index predicate is a status equal to “review,” and the query explicitly includes that same status alongside a customer filter. The relationship is straightforward. If a query uses a different expression, a function, a broader condition or a parameter whose value is not known in the plan being considered, do not assume the same match will occur just because a human knows which rows are intended.

PostgreSQL notes that predicate matching takes place during planning and gives a parameterized condition as an example that cannot establish a general implication for every possible parameter value. Prepared statements and planning choices need to be examined in the actual application. A test query typed with a literal value is not automatically evidence that a production prepared query will receive the same plan.

The safest practical habit is to inspect representative queries and their plans under the actual server version and execution pattern. An index definition that looks elegant in isolation is less important than whether the application’s real query can use it and whether using it improves the outcome.

The index exists. Why is the query ignoring it?

Even when a predicate is eligible, PostgreSQL’s planner can select another plan. It estimates costs using available statistics, table characteristics, expected row counts and other factors. A sequential scan can be reasonable when a query is expected to read much of the table or when an index path would involve many scattered reads.

Consider two fictional customer searches. One customer has three orders awaiting review; another has 15,000. The same partial index may be a useful access path for one and less attractive for the other, depending on the rest of the query and data. Those counts do not determine a universal threshold. They show why a single benchmark using one convenient value is not enough.

Take a fictional review queue whose partial index includes rows with status “review.” The developer tests a query with that literal status and sees the index in the plan. The application uses a prepared query in which status is a parameter; its generic plan instead scans the table. That observation does not prove all prepared queries avoid the index. It tells the team to compare the actual query and planning mode, because a plan that must work for any status cannot assume the parameter always means “review.” A second check might reveal that a literal-status plan can use the index but chooses another path for a customer with many matching rows. Predicate eligibility and cost selection are two different explanations.

Use PostgreSQL’s EXPLAIN guidance to inspect estimated plans and, when appropriate, compare them with measured execution. Execution-based analysis actually runs the statement; a write can change data. Use a suitable environment and understand the command before running it. The point is to reproduce the application’s conditions, not to win an argument with a query pasted into a console.

Look at both estimated and observed rows, execution time over representative conditions, and whether other work changes. One fast result after a cache has warmed is not necessarily the same as a sustained improvement. Record the query, parameters, version, dataset and test conditions so a later comparison has context.

Data moves, and the subset moves with it

A partial index is most compelling when its condition captures a useful subset that is stable enough for the workload. If the review queue grows from 20,000 to 700,000 of one million rows, it no longer represents the small exceptional population that motivated the design. The definition may still be valid, but its size, maintenance burden and planner value can change.

A status transition can change membership. A newly reviewed order may leave the subset; a previously complete order reopened for investigation may enter it. The application’s definition of “review” must match the predicate. If a new status called “escalated review” is introduced and the queue should include it, a predicate for only the original status does not magically acquire that new meaning.

That is a product change as well as a database change. Review the query, reporting rules, indexes and operational dashboards together. A fast queue that omits the cases the team meant to see is not an optimization worth celebrating.

Uniqueness can apply to a subset

A partial unique index can enforce uniqueness among rows satisfying its predicate. PostgreSQL’s documentation gives this as a use case. For example, an application might need at most one active assignment for a particular resource while retaining historical inactive assignments. A suitable unique partial index can express part of that rule for the rows selected by the predicate.

The exact definition matters: which columns make up the key, which rows are considered active, how null values are treated, and how concurrent changes behave all affect the real constraint. A test with one user making one change at a time does not establish behavior during two simultaneous attempts. The team should specify and test the business rule under its actual schema and database version.

Do not use an index as a substitute for understanding every application invariant. If there are legitimate transitions through intermediate states or multiple types of active assignment, the predicate and transaction design need to reflect them. A database constraint can prevent a class of invalid states; it cannot tell the business which state it intended.

Be careful with the operational change

Creating or dropping an index changes the database’s storage and can affect concurrent activity. PostgreSQL documents different build options and their operational implications in CREATE INDEX. Choose an approach appropriate to the version, privileges, deployment process, table size and availability requirements. Do not run a production schema change merely because a generic example uses it.

Before adding an index, identify existing indexes that already serve the query, available disk headroom, expected build time and a rollback plan. Afterward, check index validity, query plans, write behavior and the workload over a representative period. An unused index can still impose maintenance and storage costs; removing it also requires confirming what depends on it.

A fleet of many overlapping partial indexes can become difficult to maintain and can increase planning work. Partitioning or a different data model may be worth evaluating for some workloads, but neither should be selected solely to avoid reading the query plan. Match the design to the actual access pattern rather than multiplying indexes for every imaginable filter.

Write the test before the index

Choose a concrete question: for example, how long does a particular review-queue query take with realistic parameters, concurrency and current data? Record the existing plan and baseline, then define what improvement matters without harming writes or other queries. Include both a common case and a large or unusual case.

In a representative environment, build the proposed index, update or collect statistics as appropriate, and compare the same workload. Check whether the planner recognizes the predicate for the application’s actual query form. Then observe maintenance and space, not just the most favorable read. Our guide to PostgreSQL vacuuming covers a separate maintenance process that also affects how table activity and space are understood.

Save the before-and-after plans with the query and its parameter pattern. If the intended index is absent, first ask whether the planner can prove it applies; only then ask why it estimated another path would be cheaper. Those two questions turn “Postgres ignored my index” into an investigation someone else can reproduce.

Explore Infinite Upside

For Vijay Sheth’s personal accounts, read Infinite Upside: Building Wealth With Room to Live, published by Sheth Capital, Inc.

Find your edition · Chapter companion

ABOUT THE CONTRIBUTOR

Luna Lim

DIRECTOR OF FINANCIAL RESEARCH & EDITORIAL STRATEGY

SHETH CAPITAL, INC.

Luna Lim writes about business, investing, and the judgment behind financial decisions. She joined PCH Exotics Inc. as Director of Operations in 2022, working across La Jolla and Miami. Today, she reports directly to Vijay Sheth at Sheth Capital, Inc., bringing an operator’s perspective to the ideas she covers.

Based in Pacific Beach, California, Luna is curious about the people behind the numbers: what they’re building, how they weigh uncertainty, and what makes a risk worth taking. Her articles for Infinite Upside follow those questions through the details, with clear explanations and a feel for how decisions play out in everyday business.

More from Luna Lim

The Infinite Upside store