Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql/postgres: fixed inspection of multicolumn indexes #715

Merged
merged 1 commit into from
Apr 19, 2022

Conversation

svstanev
Copy link
Contributor

I have an unique index like this:

create unique index if not exists u_entity_name on entity (
  entity_type,
  coalesce(parent_id,0),
  name
);

which when inspected become like this:

index "u_entity_name" {
    unique = true
    type   = BTREE
    on {
      expr = "COALESCE(parent_id, (0)::bigint)"
    }
    on {
      expr = "COALESCE(parent_id, (0)::bigint)"
    }
    on {
      expr = "COALESCE(parent_id, (0)::bigint)"
    }
  }

Expected:

index "u_entity_name" {
    unique = true
    type   = BTREE
    on {
      column = column.entity_type
    }
    on {
      expr = "COALESCE(parent_id, (0)::bigint)"
    }
    on {
      column = column.name
    }
  }

Reason: incorrect index metadata query.

Comment on lines 390 to 394
switch {
case sqlx.ValidString(expr):
part.X = &schema.RawExpr{
X: expr.String,
}
case sqlx.ValidString(column):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change is needed?

Copy link
Contributor Author

@svstanev svstanev Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each row of the indexesQuery returns an index part/column in a multicolumn index. As per the suggested query each part always has a valid expression but column only if it is created with a column name:

LEFT JOIN pg_attribute a ON (a.attrelid, a.attnum) = (idx.indrelid, idx.key)

If you mean why I have replaced the switch statement with an if that's because at that moment it seemed like the more obvious way to express the precedence of the column over the expression. Of course the switch also evaluates the cases in a fixed order so it's would work just fine in this case.

Copy link
Member

@a8m a8m Apr 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. If you don't mind, please revert to the switch statement and I'll merge it.

Comment on lines +779 to +792
pg_get_indexdef(idx.indexrelid, idx.ord, false) AS expression,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'desc') AS desc,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'nulls_first') AS nulls_first,
pg_index_column_has_property(idx.indexrelid, a.attnum, 'nulls_last') AS nulls_last,
obj_description(to_regclass($1 || i.relname)::oid) AS comment
FROM
pg_index idx
(
select
*,
generate_series(1,array_length(i.indkey,1)) as ord,
unnest(i.indkey) AS key
from pg_index i
) idx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Copy link
Member

@a8m a8m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@a8m
Copy link
Member

a8m commented Apr 19, 2022

Thanks for the contribution @svstanev 🙏🏻

@a8m a8m merged commit 906cbf9 into ariga:master Apr 19, 2022
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.

2 participants