Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes (bylaw_postgres v0.2.0)

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

  • :validate - explicit false disables this check.
  • :rules - optional rule keyword list or non-empty list of rule keyword lists. Rules use only shared scope keys.

Run globally with defaults:

Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes

Run only for matching rule scopes:

{Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes,
 rules: [where: [schemas: ["public"]]]}

{Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes,
 rules: [where: [schemas: ["public"]], except: [[tables: ["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() | [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.