Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes (bylaw_postgres v0.1.0-alpha.1)

Copy Markdown View Source

Flags equivalent Postgres indexes on the same table.

Examples

Before, the table has two indexes with the same definition:

CREATE INDEX users_email_index ON users (email);
CREATE INDEX users_email_duplicate_index ON users (email);

That slows writes and migrations without improving reads, because Postgres maintains both indexes for the same lookup shape.

After, keep one index for that access path:

CREATE INDEX users_email_index ON users (email);

This preserves the read plan while removing duplicate write overhead and schema noise.

Notes

A plain index and a partial index on the same column are not treated as duplicates because their predicates differ.

Options

By default the check inspects all non-system schemas in a Postgres target. Use schemas: [...] or tables: [...] for simple filtering:

{Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes,
 schemas: ["public"],
 tables: ["users", "accounts"]}

Use rules: [...] when the scope needs matchers or exclusions:

{Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes,
 rules: [
   [
     only: [schema: "public"],
     except: [[table: "spatial_ref_sys"]]
   ]
 ]}

Indexes are treated as duplicates when they have the same table, access method, uniqueness, validity, key and included columns, operator classes, collations, sort options, expressions, and predicate.

Usage

Add this module to the checks passed to Bylaw.Db.Adapters.Postgres.validate/2. See the README usage section for the full ExUnit setup.

Summary

Functions

Implements the Bylaw.Db.Check validation callback.

Types

check_opt()

@type check_opt() :: {:validate, boolean()} | {:rules, [keyword()]}

check_opts()

@type check_opts() :: [check_opt()]

Functions

validate(target, opts)

@spec validate(target :: Bylaw.Db.Target.t(), opts :: check_opts()) ::
  Bylaw.Db.Check.result()

Implements the Bylaw.Db.Check validation callback.