-
Notifications
You must be signed in to change notification settings - Fork 278
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
Conversation
sql/postgres/inspect.go
Outdated
switch { | ||
case sqlx.ValidString(expr): | ||
part.X = &schema.RawExpr{ | ||
X: expr.String, | ||
} | ||
case sqlx.ValidString(column): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
ba96651
to
0ef7fae
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
Thanks for the contribution @svstanev 🙏🏻 |
I have an unique index like this:
which when inspected become like this:
Expected:
Reason: incorrect index metadata query.