# `Bylaw.Db.Adapters.Postgres.Checks.DuplicateIndexes`
[🔗](https://github.com/ryanzidago/bylaw/blob/v0.1.0-alpha.1/lib/bylaw/db/adapters/postgres/checks/duplicate_indexes.ex#L1)

Flags equivalent Postgres indexes on the same table.

## Examples

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

```sql
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:

```sql
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:

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

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

```elixir
{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](readme.html#usage) for the full ExUnit setup.

# `check_opt`

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

# `check_opts`

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

# `validate`

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

Implements the `Bylaw.Db.Check` validation callback.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
