In the era of AI and data-driven decision making, organizations process massive volumes of data at unprecedented scale. As pipelines grow more complex and intermediate datasets reach hundreds of millions of rows, even well-intentioned database optimizations can backfire. This issue was identified while working on a large-scale data processing pipeline in PostgreSQL. During performance tuning of intermediate working tables, we observed that creating multiple indexes too early in the pipeline significantly increased overall runtime, even though the indexes were intended to improve query performance.
Indexes are one of the most effective tools for improving query performance. In the majority of cases, they deliver significant gains. However, indexes are not free. Creating and maintaining them consumes CPU, memory, and I/O. When indexes are created too early or without a clear need, they can slow down an entire process rather than speed it up.
This article describes a real production incident where well-intentioned indexing on a large intermediate table degraded overall performance — and how a simple adjustment resolved it.
Before diving into the issue, it is useful to understand how indexes operate at a high level.
An index is a separate data structure (most commonly a B-Tree) that stores the values of one or more columns along with pointers to the actual rows in the table.
B-Tree Index Structure:

Without an Index (Table Scan):
Query → Full Table Scan → Read all 500 million rows → Filter matching rows
With an Index (Index Seek):
Query → Index Seek → Quickly locate matching keys → Fetch only required rows
Indexes dramatically reduce the amount of data the database engine needs to read. However, every index must be built, stored, and maintained. On large tables, this cost becomes significant.
We had a multi-step data processing pipeline in PostgreSQL. One process prepared a large intermediate result set containing approximately 500 million rows. As its final step, the process created three indexes on this working table:
A composite index on columns A and B
A composite index on columns B and C
A single-column index on column B
Immediately after this process finished, a second process was executed. The second process performed joins only on column A.
Although the next process only required efficient access on column A, the system still paid the full cost of building three indexes on 500 million rows. Index creation at this scale is an expensive operation in PostgreSQL. Two of the three indexes provided no benefit to the subsequent step, yet their creation time was fully incurred.
In addition, the presence of multiple indexes can influence the query optimizer. When the access pattern is simple (a join on a single column), extra indexes often add overhead without delivering proportional value.
-- End of Process 1 (500 million rows)
CREATE INDEX IX_1 ON IntermediateTable (ColumnA, ColumnB);
CREATE INDEX IX_2 ON IntermediateTable (ColumnB, ColumnC);
CREATE INDEX IX_3 ON IntermediateTable (ColumnB);
-- Process 2 (executed immediately after)
SELECT ...
FROM IntermediateTable t
JOIN OtherTable o ON t.ColumnA = o.ColumnA;
Process Flow (Before):
.jpeg)
Issues observed:
The join in Process 2 only benefited from an index leading with ColumnA.
Overall pipeline duration increased due to unnecessary index creation.
-- End of Process 1 (only the required index)
CREATE INDEX IX_ColumnA ON IntermediateTable (ColumnA, ColumnB);
-- or simply (ColumnA) if that is sufficient
-- Process 2 now runs without waiting for unused indexes
-- Later in the process (when actually needed):
CREATE INDEX IX_ColumnB_C ON IntermediateTable (ColumnB, ColumnC);
CREATE INDEX IX_ColumnB ON IntermediateTable (ColumnB);
Process Flow (After):

Results:
Index creation cost was deferred until the indexes were genuinely required.
Process 2 executed faster because it no longer waited for unused indexes on 500 million rows.
Overall process duration improved noticeably.
Create indexes only when they are needed — Avoid building every possible index at the earliest opportunity.
Align indexes with the next access pattern — If the next query joins or filters only on ColumnA, an index starting with that column is sufficient.
Respect the leading column rule — A composite index on (ColumnB, ColumnC) cannot efficiently support a query that filters solely on ColumnA.
Large intermediate tables require extra caution — On datasets of 500 million rows, the cost of creating indexes is substantial. Creating indexes that are not immediately used often outweighs any later benefit.
Measure before and after — Always evaluate whether the time spent creating an index is recovered by the queries that use it.
Before creating an index on a large intermediate table, ask these three questions:
Will the immediate next step use this index?
Does the leading column of the index match the join or filter predicates?
Can this index be deferred to a later stage where it is truly required?
If the answer to the first two questions is no, it is usually better to postpone index creation.
Indexes remain one of the most powerful performance tools available in PostgreSQL. The key is to apply them deliberately — at the right time and for the right access patterns — rather than by default.