by kroolik · 5y ago · 0 comments
The interesting part is when you actually get to pay the debt back. Yes, this is not free lunch. You move fast, but you have to eventually pay the cost of the underlying storage containing all the historical schemas combined at once.
This is the difference between carefully planned schema migrations and abstraction-based migrations. The former takes more time planning and executing, but the end state is the reference schema. The latter keeps on extending the underlying schema and building projections over it. Over time, the system accumulates tech debt in the form of ever-expanding base schema that contains all the historical columns.
The point is not to let old clients work indefinitely.
In other versions of this idea, all views/procedures are prefixed with some revision-number but that looks messy.
That purging is what has a lot of operational complexity. And renaming a column. All the rest is zero-downtime in PG already.
We rarely drop columns due to this, but haven't found it to be a huge issue. If we need to, we'll wait until the last supported version that has the column in its schema is retired. Then we'll add an explicit "drop column" to our schema file for the next version.
In the simplest form, when you add a column to some schema, it should be materialized in the base schema and exposed via the migration view.
The problems start when you add and remove 20 columns because even though they are no longer visible in migration schemas, they take up space in the base schema
Add Xn+1: create new views without column. When no Xn clients remain: drop column from tables and drop schema Xn.
The point as I see it is to not break live client connections which expect the column to exist.
You can use views to make migrations that were previously tricky zero-downtime.
If that's not the case, then I mist've read the article wrong!
Edit: although when I think of it - if you want to eventually materialize old migration schemas into the base schema, you need to do the rename, too. Which is not zero-downtime because of new migration schemas that do the renaming automatically. Meaning changing views' definition, meaning lots of locking.
So, you still need maintenance windows to merge all the changes. Just not on every change. Otherwise the base schema will then eventually be completely out of sync and contain tons of old, unused columns.
Now, when you remove a column, you just create a new view without it being available. But the column in the base schema must stay because: you support the old schema, and because you want to avoid the operational complexity of physically removing it, right?
Now, you can obviously split a table into a list of tables each storing the pk and some column value. But you have just invented columnar storage in a row-based rdbms :D
A different approach is to have scheduled maintenance window, for example quarterly or every 6 month when you materialize all the changes at once. The amortized operational cost is way lower :)
Until you have no current clients left, you can't delete the old view or remove underlying columns that that old view requires. Once they have, you can execute that phase.