Paveł Tyślacki
@tbicr
https://en.wikipedia.org/wiki/Downtime:
The term downtime is used to refer to periods when a system is unavailable.
My suggestion: postgres one time response time increasing less than a few seconds is not a downtime.
Schema Migrations | Data Migrations | Business Logic |
---|---|---|
CREATE TABLE, DROP TABLE, ALTER TABLE, CREATE INDEX, DROP INDEX | SELECT, INSERT, UPDATE, DELETE | SELECT, INSERT, UPDATE, DELETE |
one thread | one thread | concurrent |
lock | operations |
---|---|
ACCESS EXCLUSIVE | CREATE SEQUENCE, DROP SEQUENCE, CREATE TABLE, DROP TABLE, ALTER TABLE, DROP INDEX |
SHARE | CREATE INDEX |
SHARE UPDATE EXCLUSIVE | CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY, ALTER TABLE VALIDATE CONSTRAINT |
lock | operations | conflict with locks | conflict with operations |
---|---|---|---|
ACCESS SHARE | SELECT | ACCESS EXCLUSIVE | ALTER TABLE, DROP INDEX |
ROW SHARE | SELECT FOR UPDATE | ACCESS EXCLUSIVE, EXCLUSIVE |
ALTER TABLE, DROP INDEX |
ROW EXCLUSIVE | INSERT, UPDATE, DELETE |
ACCESS EXCLUSIVE, EXCLUSIVE, SHARE ROW EXCLUSIVE, SHARE |
ALTER TABLE, DROP INDEX, CREATE INDEX |
Use timeouts:
Be careful with global statement_timeout for CREATE INDEX CONCURRENTLY or ALTER TABLE VALIDATE CONSTRAINT (disable statement_timeout for them).
Avoid too many operations in one transaction for schema migrations, especially for FOREIGN KEYS in active used tables (FOREIGN KEY constraint changes take ACCESS EXCLUSIVE lock for two tables).
How transactions work.
Isolation levels.
Row user and hidden data (xmin, xmax).
rewrite whole table, except case
when data stored same way and do not lose precision:
Use chunk changes for data migrations and business logic
CREATE SEQUENCE,
DROP SEQUENCE,
CREATE TABLE,
DROP TABLE
- do not have conflict with business logic,
except dropping tables with FOREIGN KEYs
ALTER TABLE ADD COLUMN (without
DEFAULT/NOT NULL/PRIMARY KEY/UNIQUE),
ALTER TABLE DROP COLUMN
- have ACCESS EXCLUSIVE lock, but change only schema
For DEFAULT/NOT NULL/PRIMARY KEY/UNIQUE add column and then add DEFAULT/constraint.
ALTER TABLE ADD COLUMN DEFAULT better in PG 11, but no luck with django.
ALTER TABLE ALTER COLUMN TYPE
- rewrite table, so better create new table and copy data, except:
ALTER TABLE ALTER COLUMN SET NOT NULL
- check whole table, can be replaced with CHECK IS NOT NULL
ALTER TABLE ALTER COLUMN DROP NOT NULL,
ALTER TABLE ALTER COLUMN SET DEFAULT,
ALTER TABLE ALTER COLUMN DROP DEFAULT
- have ACCESS EXCLUSIVE lock, but change only schema
ALTER TABLE ADD CONSTRAINT CHECK,
ALTER TABLE ADD CONSTRAINT FOREIGN KEY
- check whole table, but can be declared with NOT VALID keyword
and then validated with ALTER TABLE VALIDATE CONSTRAINT,
that do not have conflict with business logic
ALTER TABLE ADD CONSTRAINT CHECK NOT VALID,
ALTER TABLE ADD CONSTRAINT FOREIGN KEY NOT VALID,
ALTER TABLE DROP CONSTRAINT CHECK,
ALTER TABLE DROP CONSTRAINT FOREIGN KEY
- have ACCESS EXCLUSIVE lock, but change only schema
CREATE INDEX
- lock table for write,
but can be replaced with CREATE INDEX CONCURRENTLY,
that do not have conflict with business logic
DROP INDEX
- have ACCESS EXCLUSIVE lock, but change only schema,
also can be replaced with DROP INDEX CONCURRENTLY,
that do not have conflict with business logic
ALTER TABLE ADD CONSTRAINT PRIMARY KEY,
ALTER TABLE ADD CONSTRAINT UNIQUE
- create index under the hood,
but index can be created with CREATE INDEX CONCURRENTLY
and then constraints created with USING INDEX keyword
ALTER TABLE ADD CONSTRAINT PRIMARY KEY USING INDEX,
ALTER TABLE ADD CONSTRAINT UNIQUE USING INDEX,
ALTER TABLE DROP CONSTRAINT PRIMARY KEY,
ALTER TABLE DROP CONSTRAINT UNIQUE
- have ACCESS EXCLUSIVE lock, but change only schema
ALTER TABLE SET TABLESPACE
- physically move data,
so better create new table and copy data
ALTER TABLE RENAME TO,
ALTER TABLE RENAME COLUMN
- change only schema, but hard to write logic
that support schema before, on and after schema changes,
so better create new table/column and copy data
Create new table/column and copy data is a good solution when your DB does not support so cool migrations like postgres.