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

Execute NOT NULL with ADD COLUMN in PG11+ #39

Merged

Conversation

katzenbar
Copy link
Contributor

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.

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/
@doctolib doctolib deleted a comment from katzenbar Mar 31, 2021
Copy link
Contributor

@ThHareau ThHareau left a 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

@katzenbar
Copy link
Contributor Author

No worries! Thanks for taking a look! :)

Comment on lines 14 to 19
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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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

@ThHareau ThHareau merged commit 40c68d7 into doctolib:master Apr 6, 2021
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.

add_column, null: false should be executed in one statement
2 participants