dbml icon indicating copy to clipboard operation
dbml copied to clipboard

[PostgreSQL] New feature - Consider partial Index

Open jullierme opened this issue 2 years ago • 1 comments

Please, consider partial Index (PostgreSQL)

A partial index is an index built over a subset of a table; the subset is defined by a conditional expression (called the predicate of the partial index). The index contains entries only for those table rows that satisfy the predicate.

SQL example

CREATE TABLE tests (
    subject text,
    target text,
    success boolean,
    ...
);

CREATE UNIQUE INDEX tests_success_constraint ON tests (subject, target)
    WHERE success is true;

Suggestion:

table test {
    subject text
    target text
    success boolean

    Indexes {
      (subject, target) [name: 'tests_success_constraint', unique, expression: `success is true`]
    }
}

Important note:

There is already a perfect comment from Yokozuna59 here issue 68 that could solve this one graciously

jullierme avatar Jan 17 '24 14:01 jullierme

Hi, I'd like to work on this one, if nobody else is.

My plan would be:

  1. dbml-parse: accept a setting on index definitions (validator + interpreter).
  2. dbml-core: store it on Index, include it in shallowExport and the types. Today shallowExport() lists its fields explicitly, so a new field has to be added there to reach the normalized model, the JSON output and the exporters.
  3. Postgres importer: read the WHERE predicate of CREATE [UNIQUE] INDEX (the grammar already has where_clause?, but visitIndexstmt ignores it).
  4. Exporters: write it back in DbmlExporter and PostgresExporter (... WHERE <predicate>).
  5. Tests, docs and llms.txt.

One question before I start: which setting name do you want? The issue suggests expression: \success is true`, which is the same word used for functional index columns. where: `success is true`` would be closer to the SQL. I'd go with whichever you prefer.

Also, should other dialects be in scope (for example SQL Server filtered indexes, or SQLite partial indexes), or only PostgreSQL for now?

ore-codes avatar Nov 21 '26 15:11 ore-codes