DEV Community

Cover image for How to classify CREATE INDEX CONCURRENTLY state with pg_index
Chris Rodriguez
Chris Rodriguez

Posted on Originally published at chriseugenerodriguez.com

How to classify CREATE INDEX CONCURRENTLY state with pg_index

A concurrent PostgreSQL index build can fail without making the index name disappear. That makes an existence check misleading: a catalog row may remain even though PostgreSQL cannot use the index for query planning.

The implementation queries pg_index for one named index relation and joins pg_class to return readable index and table names. It interprets four catalog booleans instead of collapsing them into one “healthy” result.

The key decision is to separate write maintenance from query use. indisready answers whether inserts and updates should maintain the index. indisvalid answers whether PostgreSQL can use it for queries. A live index with indisready true and indisvalid false is therefore classified as an invalid leftover: it can still impose write overhead while remaining ineligible for plans.

The interpretation starts with indislive. A false value means the index is being dropped, so it should not be reported as an active failed build. For a live index, false indisvalid becomes invalid-leftover. A valid index with indcheckxmin set becomes valid-but-waiting-for-old-snapshots; it is complete, but older transaction snapshots can temporarily prevent safe use. Only a live, valid index without that snapshot gate is reported as query-usable.

This gives callers separate results for insert readiness, query readiness, and the specific failed-concurrent-build signal. That distinction is useful when deciding whether to drop or rebuild an artifact, wait for old snapshots, or investigate a different cause of a plan choice.

The limitation is deliberate: the example validates an application-level interpretation of representative catalog rows. It does not start PostgreSQL, force a concurrent-build failure, or prove a live planner decision. Use it as a diagnostic classification layer, then verify engine behavior in the database and workload you operate.

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,
  };
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)