|
| 1 | +## 用pg_migrate实现PostgreSQL online DDL with table rewrite |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2024-01-04 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , PolarDB , DuckDB , online DDL , 排他锁 , pg_migrate , pg_repack |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | + |
| 16 | +https://github.com/phillbaker/pg_migrate |
| 17 | + |
| 18 | +pg_migrate is a PostgreSQL extension and CLI which lets you make schema changes to tables and indexes. Unlike ALTER TABLE it works online, without holding a long lived exclusive lock on the processed tables during the migration. It builds a copy of the target table and swaps them. |
| 19 | + |
| 20 | +Forked from the excellent pg_repack project (https://reorg.github.io/pg_repack). |
| 21 | + |
| 22 | +## Examples |
| 23 | +Change the type of a column |
| 24 | +``` |
| 25 | +pg_migrate --table=my_table --alter='ALTER COLUMN id TYPE bigint' # Add --execute to run |
| 26 | +``` |
| 27 | + |
| 28 | +Add a column with a default (non-nullable) |
| 29 | +``` |
| 30 | +pg_migrate --table=my_table --alter='ADD COLUMN foo integer NOT NULL DEFAULT 42' # Add --execute to run |
| 31 | +``` |
| 32 | + |
| 33 | +## Known Limitations |
| 34 | +- Unique constraints are converted into unique indexes, they are equivalent in Postgres. However, this may be an unexpected change. |
| 35 | +- Index names on the target table and foreign key constraints are changed during the migration. |
| 36 | + - If the generated names are > 63 characters, this will likely break |
| 37 | +- If the target table is used in views, those objects will continue to reference the original table - this is not supported currently. |
| 38 | + - If the target table is used in stored procedures, those functions are stored as text so are not linked through object IDs and will reference the migrated table. |
| 39 | +- DDL to drop columns or add columns without a default is not currently supported |
| 40 | +- Hosted PG databases (RDS, Cloud SQL) are not supported because they do not allow installing custom extensions. |
| 41 | + |
| 42 | + |
| 43 | +## Demo |
| 44 | +``` |
| 45 | +cd /tmp |
| 46 | +git clone --depth 1 https://github.com/phillbaker/pg_migrate |
| 47 | +cd pg_migrate/ |
| 48 | +USE_PGXS=1 make install |
| 49 | +``` |
| 50 | + |
| 51 | +``` |
| 52 | +root@56000550f873:/tmp/pg_migrate# psql |
| 53 | +psql (14.10 (Debian 14.10-1.pgdg110+1)) |
| 54 | +Type "help" for help. |
| 55 | + |
| 56 | +postgres=# create extension pg_migrate ; |
| 57 | +CREATE EXTENSION |
| 58 | +postgres=# \q |
| 59 | +``` |
| 60 | + |
| 61 | +``` |
| 62 | +root@56000550f873:/tmp/pg_migrate# pg_migrate --help |
| 63 | +pg_migrate migrates a PostgreSQL table avoiding long locks. |
| 64 | + |
| 65 | +Usage: |
| 66 | + pg_migrate [OPTION]... [DBNAME] |
| 67 | +Options: |
| 68 | + -t, --table=TABLE table to target |
| 69 | + -d, --database=DATABASE database in which the table lives |
| 70 | + -s, --tablespace=TBLSPC move table to a new tablespace |
| 71 | + -a, --alter=ALTER SQL of the alter statement |
| 72 | + -N, --execute whether to run the migration |
| 73 | + -j, --jobs=NUM Use this many parallel jobs for each table |
| 74 | + -T, --wait-timeout=SECS timeout to cancel other backends on conflict |
| 75 | + -D, --no-kill-backend don't kill other backends when timed out |
| 76 | + -k, --no-superuser-check skip superuser checks in client |
| 77 | + |
| 78 | +Connection options: |
| 79 | + -d, --dbname=DBNAME database to connect |
| 80 | + -h, --host=HOSTNAME database server host or socket directory |
| 81 | + -p, --port=PORT database server port |
| 82 | + -U, --username=USERNAME user name to connect as |
| 83 | + -w, --no-password never prompt for password |
| 84 | + -W, --password force password prompt |
| 85 | + |
| 86 | +Generic options: |
| 87 | + -e, --echo echo queries |
| 88 | + -E, --elevel=LEVEL set output message level |
| 89 | + --help show this help, then exit |
| 90 | + --version output version information, then exit |
| 91 | + |
| 92 | +Read the website for details: <https://github.com/phillbaker/pg_migrate>. |
| 93 | +Report bugs to <https://github.com/phillbaker/pg_migrate/issues>. |
| 94 | +``` |
| 95 | + |
| 96 | + |
| 97 | +#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216") |
| 98 | + |
| 99 | + |
| 100 | +#### [PolarDB 开源数据库](https://openpolardb.com/home "57258f76c37864c6e6d23383d05714ea") |
| 101 | + |
| 102 | + |
| 103 | +#### [PolarDB 学习图谱](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4") |
| 104 | + |
| 105 | + |
| 106 | +#### [购买PolarDB云服务折扣活动进行中, 55元起](https://www.aliyun.com/activity/new/polardb-yunparter?userCode=bsb3t4al "e0495c413bedacabb75ff1e880be465a") |
| 107 | + |
| 108 | + |
| 109 | +#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8") |
| 110 | + |
| 111 | + |
| 112 | +#### [德哥 / digoal's Github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 113 | + |
| 114 | + |
| 115 | +#### [About 德哥](https://github.com/digoal/blog/blob/master/me/readme.md "a37735981e7704886ffd590565582dd0") |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + |
0 commit comments