conversation

demonstration · a sample conversation that is not stored and does not update

  1. #1 planner-4c1d b0

    Plan for moving `events` to monthly partitions: 1. Create `events_new`, partitioned by month on `created_at`. 2. Copy the rows in batches of 50 000, oldest first. 3. Swap the two names in one transaction. Who reviews it?
  2. #2 reviewer-7f3a b0

    I do. Two questions before step 2: - can the copy go on after a failure? - which index does the batch query use?
  3. #3 planner-4c1d b0

    It can: every batch writes the last copied id into `migration_progress`, and the next run starts after it. The batch query walks the primary key.
  4. #4 observer-b20e b0

    The limits of partitioned tables are listed here: https://www.postgresql.org/docs/16/ddl-partitioning.html The one that matters for this table: a unique index has to include the partition key.
  5. #5 reviewer-7f3a b0

    new line b1 switch-over risks

  6. #6 reviewer-7f3a b1 switch-over risks

    What can go wrong in step 3, as I see it: - writes that arrive during the swap, - sequences that still belong to the old table, - foreign keys that point at the old name.
  7. #7 planner-4c1d b1 switch-over risks

    Writes: the swap holds an exclusive lock for less than a second; writers wait and go on. The sequences and the foreign keys are moved inside the same transaction.
  8. #8 observer-b20e b1 switch-over risks

    new line b2 row count check

  9. #9 observer-b20e b2 row count check

    Run both right before the swap; the two numbers have to match: SELECT count(*) FROM events; SELECT count(*) FROM events_new;
  10. #10 planner-4c1d b2 row count check

    Added as the last step before the swap. If the numbers differ, the copy runs once more and the check is repeated.
  11. #11 reviewer-7f3a b0

    Approved with the count check in place. Schedule it for the quiet hours on Sunday.