Review pg_index failure states before trusting a concurrent index build

Review pg_index failure states before trusting a concurrent index build

●3 ●60
calendar_today ago • schedule2 min read
— Originally published at chriseugenerodriguez.com

Start with the failure assertion

The important failure case is not “the index is absent.” It is a live index row where indisvalid is false while indisready is true. That combination represents an object PostgreSQL may still maintain during writes, even though it must not trust the object for query planning. A diagnostic that checks only whether an index name exists will miss this costly remnant.

Prerequisites

You need a PostgreSQL connection capable of reading system catalogs, the index relation name to inspect, and a caller that can act on a classified result. The supplied implementation expects a catalog row containing the four boolean fields from pg_index: live status, validity, insert readiness, and the old-snapshot gate.

Implementation decision

The implementation first builds a parameterized query for the requested index relation, then converts the returned row into explicit operational states. It checks indislive before diagnosing a failure: an index being dropped should not be called a concurrent-build leftover.

For live indexes, invalidity takes priority. A false indisvalid produces invalid-leftover, while preserving the separate insert-readiness result. A valid row with indcheckxmin becomes valid-but-waiting-for-old-snapshots. Only a live and valid row without that gate is query-usable.

Expected result

Callers can see three independent facts: whether writes maintain the index, whether queries can use it now, and whether it resembles a failed concurrent-build artifact. That makes the next decision clearer: rebuild an invalid remnant, wait for old snapshots, or investigate other reasons an otherwise usable index is not chosen.

Limitation

The example checks classification behavior against supplied rows through an injected dependency; it is not a browser or external PostgreSQL-engine reproduction. It cannot demonstrate a real concurrent build, deadlock, snapshot lifetime, or planner choice. Confirm those engine-level conditions in your own database session.

Working example

export function indexCatalogQuery(indexName) {
  if (typeof indexName !== 'string' || indexName.trim() === '') {
    throw new TypeError('indexName must be a non-empty string');
  }

  return {
    text: `
      SELECT
        index_class.relname AS index_name,
        table_class.relname AS table_name,
        index_data.indisvalid,
        index_data.indisready,
        index_data.indislive,
        index_data.indcheckxmin
      FROM pg_index AS index_data
      JOIN pg_class AS index_class ON index_class.oid = index_data.indexrelid
      JOIN pg_class AS table_class ON table_class.oid = index_data.indrelid
      WHERE index_data.indexrelid = $1::regclass
    `,
    values: [indexName],
  };
}

const BOOLEAN_FIELDS = [
  'indisvalid',
  'indisready',
  'indislive',
  'indcheckxmin',
];

export function inspectPgIndexRow(row) {
  if (row === null || typeof row !== 'object' || Array.isArray(row)) {
    throw new TypeError('pg_index row must be an object');
  }

  for (const field of BOOLEAN_FIELDS) {
    if (typeof row[field] !== 'boolean') {
      throw new TypeError(`pg_index row field ${field} must be boolean`);
    }
  }

  const readyForInserts = row.indisready && row.indislive;
  const readyForQueries = row.indisvalid && row.indislive && !row.indcheckxmin;

  let state;
  if (!row.indislive) {
    state = 'being-dropped';
  } else if (!row.indisvalid) {
    state = 'invalid-leftover';
  } else if (row.indcheckxmin) {
    state = 'valid-but-waiting-for-old-snapshots';
  } else {
    state = 'query-usable';
  }

  return {
    indexName: row.index_name ?? null,
    tableName: row.table_name ?? null,
    state,
    readyForInserts,
    readyForQueries,
    // An invalid concurrent-build artifact can still be maintained by writes.
    failedConcurrentBuildLeftover: row.indislive && !row.indisvalid,
  };
}

Read the full article: https://chriseugenerodriguez.com/blog/pg-index-concurrent-build-state

Want more #architecture deep dives?

Join 6,178+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

More Posts

The Sovereign Vault — A Comprehensive Guide to Protocol-Driven AI

Ken W. Algerverified - Jun 4

Stop trusting verdicts your policy no longer supports

VEACESLAV - Dec 1

Short links that outlive the vendor: a newsroom URL shortener on WordPress and AWS

jaafarabazid - Nov 10

What Security Teams Want From AI Agents Before They Let Them Act Alone

Tom Smithverified - Nov 18

SEO-Friendly Web Design Checklist: Architecture Before Aesthetics

floxolab - Aug 30
chevron_left
1.3k Points • 64 Badges
Santa Monica, CA • chriseugenerodriguez.com
48Posts
0Comments
12Connections
10 years fe

Related Jobs

View all jobs →

Commenters (This Week)

13 comments
13 comments
2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!