-
Notifications
You must be signed in to change notification settings - Fork 24
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
Execute NOT NULL with ADD COLUMN in PG11+ #39
Execute NOT NULL with ADD COLUMN in PG11+ #39
Conversation
One of the performance improvements in Postgres 11 was executing an `ADD COLUMN ... NOT NULL` without needing a table rewrite. This is now a safe operation in PG11+, and we can avoid adding backwards-compatible safe version with a statement timeout that can fail in some cases. https://www.postgresql.org/docs/release/11.0/
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.
Amazing job, thanks 👏
I've apparently removed one of your comment, sorry about that
No worries! Thanks for taking a look! :) |
def add_column(table_name, column_name, type, **options) | ||
need_default_value_backfill = SafePgMigrations.pg_version_num < PG_11_VERSION_NUM | ||
apply_not_null_constraint_separately = SafePgMigrations.pg_version_num < PG_11_VERSION_NUM | ||
|
||
default = options.delete(:default) if need_default_value_backfill | ||
null = options.delete(:null) | ||
null = options.delete(:null) if apply_not_null_constraint_separately |
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.
Actually, now that null
is natively handled by ActiveRecord's add_column
, we do nothing if PG version is recent enough. We could simplify a lot this method:
def add_column(table_name, column_name, type, **options)
return super if SafePgMigrations.pg_version_num >= PG_11_VERSION_NUM
default = options.delete(:default)
null = options.delete(:null)
# ...
end
Would you like to handle this refactoring as well?
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.
That totally makes sense! I can make this change shortly.
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.
Done! I also added a few more tests for the older version of Postgres to cover a few more variations of arguments for the older versions since I was changing that logic a little more.
Let me know if we need to do anything else here!
This simplifies the if checks enough that we no longer have to ignore the Rubocop complaints about complexity.
@@ -10,10 +10,10 @@ module StatementInsurer | |||
end | |||
end | |||
|
|||
def add_column(table_name, column_name, type, **options) # rubocop:disable Metrics/CyclomaticComplexity | |||
need_default_value_backfill = SafePgMigrations.pg_version_num < PG_11_VERSION_NUM | |||
def add_column(table_name, column_name, type, **options) |
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.
That's great to see this method disable
removed
One of the performance improvements in Postgres 11 was executing an
ADD COLUMN ... NOT NULL
without needing a table rewrite. This is now a safe operation in PG11+, and we can avoid adding backwards-compatible safe version with a statement timeout that can fail in some cases.https://www.postgresql.org/docs/release/11.0/
Fixes #37.