Skip to content

describe() and alter() see secondary indexes and [unique] foreign keys on PostgreSQL - #1751

Open
dimitri-yatsenko wants to merge 3 commits into
masterfrom
fix/1699-postgres-indexes
Open

dimitri-yatsenko wants to merge 3 commits into
masterfrom
fix/1699-postgres-indexes

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

On PostgreSQL, heading.indexes was always empty. The index query returned indexname and indexdef from pg_indexes, but the heading reads index_name, column_name, non_unique and seq_in_index. As a result:

  • describe() dropped the [unique] option of foreign keys and every secondary index.
  • alter() with an unchanged definition raised NotImplementedError: table.alter cannot alter indexes (yet) on any table with a secondary index.

The query now returns the same columns as on MySQL. For the table in the issue, heading.indexes and describe() are now the same on both backends, and alter() with an unchanged definition does nothing.

Fixes #1699

Behavior changes

  • Indexes with expressions are left out, on both backends. An expression in an index has no column name. The heading used to list such an index by its plain columns alone. So a unique index on (a, lower(b)) was shown by describe() as UNIQUE INDEX (a): a stricter constraint than the real one. It could also replace a plain INDEX (a) in heading.indexes, because both map to the key ('a',). Now any index that contains an expression is left out of heading.indexes and describe(). This also changes MySQL: describe() no longer shows the plain-column part of a functional index that mixes columns and expressions.
  • alter() compares indexes without regard to order. describe() lists indexes in index-name order. MySQL names an index after its first column, and PostgreSQL names it idx_<table>_<columns>, so the order can differ between backends. For example, with columns a, a_b, c and the indexes index(a, c) and index(a_b), MySQL lists INDEX (a, c) first and PostgreSQL lists INDEX (a_b) first. alter() compared the lists in order. On PostgreSQL it raised NotImplementedError for that table with an unchanged definition, while MySQL succeeded. On either backend it also raised for any table whose declared index order differed from index-name order, such as one that declares index(z) before index(a). It now compares the indexes as sorted lists.

Changes

  • adapters/postgres.py: get_indexes_sql reads pg_index and pg_attribute. It returns one row per key column with index_name, column_name, non_unique and seq_in_index, ordered by index name and position, as the MySQL query does.
    • The primary-key index is left out (indisprimary).
    • Columns added with INCLUDE are left out. They are not part of the key.
    • An expression in an index has no row in pg_attribute, so it comes back with a NULL column_name, as on MySQL. Loading the heading does not fail.
  • adapters/base.py: the docstring of get_indexes_sql names the columns the heading reads.
  • heading.py: an index with any NULL column_name (an expression) is left out of heading.indexes. Before, only an index made entirely of expressions was left out.
  • declare.py: alter() compares the index lists of the new definition and of describe() after sorting them.

Tests

  • tests/integration/test_heading_indexes.py, on MySQL and PostgreSQL, with the Child table from the issue:
    • heading.indexes equals what MySQL reports, in the same order.
    • describe() returns the same text as on MySQL, including -> [unique] Parent, INDEX (a) and UNIQUE INDEX (b, c).
    • The output of describe() produces the same CREATE TABLE and CREATE INDEX statements as the definition. A table declared from it has the same indexes and describes the same way.
    • alter(prompt=False) with an unchanged definition runs no statement and leaves the table as it was.
    • test_expression_index_is_skipped: an index on an expression alone, an index whose first part is an expression, and a unique index on (a, lower(b)) are all left out. heading.indexes and describe() are unchanged, and INDEX (a) stays non-unique. The mixed-index case fails on both backends without the heading.py change.
    • test_alter_ignores_index_order, with two tables. alter(prompt=False) with an unchanged definition runs no statement on either. The a, a_b, c table above fails on PostgreSQL on master. A table that declares index(z) before index(a), so that declared order differs from index-name order on both backends, fails on both backends on master.
    • The first five Child tests fail on PostgreSQL on master and pass on MySQL before and after. test_expression_index_is_skipped fails on both backends without the heading.py change.
  • tests/unit/test_adapters.py: the index query of each adapter returns the four columns the heading reads, and the PostgreSQL query leaves out the primary key. The PostgreSQL cases fail on master.
  • The related integration files (test_alter.py, test_alter_rename.py, test_declare.py, test_fk_index.py, test_foreign_keys.py, test_json.py, test_multi_backend.py) and the unit suite pass locally.

Notes for reviewers

  • Partial indexes (with a WHERE clause) created outside DataJoint are listed like ordinary indexes on PostgreSQL. DataJoint does not declare them.

Merge order

  • alter() changes and renames attributes on PostgreSQL #1639 (alter() on PostgreSQL) also edits alter() in declare.py. git merge-tree of this branch with that PR reports no conflict: that PR changes other lines of alter(), and this PR changes only the index comparison line, which that PR leaves as it is. With both merged, test_heading_indexes.py and test_alter.py pass on both backends. Either can merge first.
  • fix(declare): declare JSON-path indexes on PostgreSQL #1684 (JSON-path indexes on PostgreSQL) does not conflict. A JSON-path index is an index with an expression, so with both merged it is left out of heading.indexes and describe() on both backends. That includes a mixed index such as unique index(item_id, data.name:char(20)), which was shown as UNIQUE INDEX (item_id) before the heading.py change. alter() with an unchanged definition raises NotImplementedError on such a table on both backends, because describe() cannot show the JSON path. test_describe in tests/integration/test_json.py is skipped for the same reason.
  • A read-only PostgreSQL login sees primary and foreign keys #1604 also rewrites queries in adapters/postgres.py to read the system catalogs. It does not conflict with this PR.

On PostgreSQL, get_indexes_sql returned indexname and indexdef from
pg_indexes, while Heading reads index_name, column_name, non_unique and
seq_in_index. heading.indexes was therefore always empty: describe()
dropped [unique] foreign keys and secondary indexes, and alter() with an
unchanged definition raised NotImplementedError on any table with a
secondary index.

The query now reads pg_index and pg_attribute and returns the same
columns as the MySQL query, one row per key column, ordered by index name
and position, without the primary-key index. An expression in an index
has no pg_attribute row, so it comes back with a NULL column_name, which
Heading skips as it does for MySQL's functional key parts.

Fixes #1699
…ex order

An index that mixes columns and expressions was listed by its plain columns
alone, so describe() showed a different index (a stricter one when unique)
and could hide a plain index on the same columns. The heading now leaves out
any index that contains an expression, on both backends.

alter() compared index lists in order, but describe() lists indexes by index
name and the names differ between backends. The comparison now ignores order.
Add a table that declares index(z) before index(a), so declared order
differs from index-name order on both backends. Reword the comment on
Ordered: its declared order differs from index-name order only on
PostgreSQL.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL: describe() drops [unique] foreign keys and secondary indexes, and alter() fails on tables that have them

1 participant