pg_online_schema_change 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +4 -0
- data/.rubocop.yml +99 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +88 -0
- data/LICENSE.txt +21 -0
- data/README.md +212 -0
- data/Rakefile +12 -0
- data/bin/console +8 -0
- data/bin/pg-online-schema-change +6 -0
- data/bin/setup +8 -0
- data/diagrams/how-it-works.excalidraw +1401 -0
- data/diagrams/how-it-works.png +0 -0
- data/docker-compose.yml +9 -0
- data/lib/pg_online_schema_change/cli.rb +44 -0
- data/lib/pg_online_schema_change/client.rb +49 -0
- data/lib/pg_online_schema_change/functions.rb +62 -0
- data/lib/pg_online_schema_change/helper.rb +21 -0
- data/lib/pg_online_schema_change/orchestrate.rb +263 -0
- data/lib/pg_online_schema_change/query.rb +302 -0
- data/lib/pg_online_schema_change/replay.rb +122 -0
- data/lib/pg_online_schema_change/store.rb +18 -0
- data/lib/pg_online_schema_change/version.rb +5 -0
- data/lib/pg_online_schema_change.rb +33 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6580f86cf1fc33883f10391990817fb8c885b03eb4ecd1cd3095d46fc2a6bff5
|
4
|
+
data.tar.gz: 28b702f57765278ed7cdf9f7590c2bbf957d2debe8ace180bacd2a3f2fad98e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b761947d35092a7479589ef2b1bc4f79290e5172d83e1f24bd23af37e4ecdfaf7e26a0d0b91dafed3ad68b6e3012dfcd9ff8f54c861f1270037e9582e1f9db7b
|
7
|
+
data.tar.gz: a4f8f44660858b0e3fb5d74847e8f4e46e46b5d5237cbea669dcb9880dd3cb6deae9c5b598120169769e57cdb07e67f72cfc0edd6d72ef8c46d8bc9f5284ded6
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
- rubocop-packaging
|
4
|
+
- rubocop-performance
|
5
|
+
- rubocop-rake
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
DisplayCopNames: true
|
9
|
+
DisplayStyleGuide: true
|
10
|
+
NewCops: enable
|
11
|
+
TargetRubyVersion: 2.6
|
12
|
+
Exclude:
|
13
|
+
- "tmp/**/*"
|
14
|
+
- "vendor/**/*"
|
15
|
+
|
16
|
+
Layout/HashAlignment:
|
17
|
+
EnforcedColonStyle:
|
18
|
+
- table
|
19
|
+
- key
|
20
|
+
EnforcedHashRocketStyle:
|
21
|
+
- table
|
22
|
+
- key
|
23
|
+
|
24
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
25
|
+
EnforcedStyle: no_space
|
26
|
+
|
27
|
+
Metrics/AbcSize:
|
28
|
+
Max: 20
|
29
|
+
Exclude:
|
30
|
+
- "test/**/*"
|
31
|
+
|
32
|
+
Metrics/BlockLength:
|
33
|
+
Exclude:
|
34
|
+
- "*.gemspec"
|
35
|
+
- "Rakefile"
|
36
|
+
|
37
|
+
Metrics/ClassLength:
|
38
|
+
Exclude:
|
39
|
+
- "test/**/*"
|
40
|
+
|
41
|
+
Metrics/MethodLength:
|
42
|
+
Max: 18
|
43
|
+
Exclude:
|
44
|
+
- "test/**/*"
|
45
|
+
|
46
|
+
Metrics/ParameterLists:
|
47
|
+
Max: 6
|
48
|
+
|
49
|
+
Naming/MemoizedInstanceVariableName:
|
50
|
+
Enabled: false
|
51
|
+
|
52
|
+
Naming/VariableNumber:
|
53
|
+
Enabled: false
|
54
|
+
|
55
|
+
Rake/Desc:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/BarePercentLiterals:
|
59
|
+
EnforcedStyle: percent_q
|
60
|
+
|
61
|
+
Style/ClassAndModuleChildren:
|
62
|
+
Enabled: false
|
63
|
+
|
64
|
+
Style/Documentation:
|
65
|
+
Enabled: false
|
66
|
+
|
67
|
+
Style/DoubleNegation:
|
68
|
+
Enabled: false
|
69
|
+
|
70
|
+
Style/EmptyMethod:
|
71
|
+
Enabled: false
|
72
|
+
|
73
|
+
Style/FrozenStringLiteralComment:
|
74
|
+
Enabled: false
|
75
|
+
|
76
|
+
Style/NumericPredicate:
|
77
|
+
Enabled: false
|
78
|
+
|
79
|
+
Style/StringLiterals:
|
80
|
+
EnforcedStyle: double_quotes
|
81
|
+
|
82
|
+
Style/TrivialAccessors:
|
83
|
+
AllowPredicates: true
|
84
|
+
|
85
|
+
Style/TrailingCommaInArguments:
|
86
|
+
EnforcedStyleForMultiline: comma
|
87
|
+
|
88
|
+
Style/TrailingCommaInArrayLiteral:
|
89
|
+
EnforcedStyleForMultiline: comma
|
90
|
+
|
91
|
+
Style/TrailingCommaInHashLiteral:
|
92
|
+
EnforcedStyleForMultiline: comma
|
93
|
+
|
94
|
+
Style/SpaceAroundEqualsInParameterDefault:
|
95
|
+
EnforcedStyle: space
|
96
|
+
|
97
|
+
Style/MultilineHashBraceLayout:
|
98
|
+
Enabled: true
|
99
|
+
EnforcedStyle: symmetrical
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.0.0
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
## [0.1.0] - 2022-02-16
|
2
|
+
|
3
|
+
Initial release
|
4
|
+
|
5
|
+
pg-online-schema-change (`pg-osc`) is a tool for making schema changes (any `ALTER` statements) in Postgres tables with minimal locks, thus helping achieve zero downtime schema changes against production workloads.
|
6
|
+
|
7
|
+
`pg-osc` uses the concept of shadow table to perform schema changes. At a high level, it copies the contents from a primary table to a shadow table, performs the schema change on the shadow table and swaps the table names in the end while preserving all changes to the primary table using triggers (via audit table).
|
8
|
+
|
9
|
+
Checkout [Readme](https://github.com/shayonj/pg-online-schema-change#readme) for more details.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at shayon@loom.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gemspec
|
6
|
+
|
7
|
+
gem "ougai", "~> 2.0.0"
|
8
|
+
gem "pg", "~> 1.0"
|
9
|
+
gem "pg_query", "~> 2.1.2"
|
10
|
+
gem "pry"
|
11
|
+
gem "rake", "~> 13.0"
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
gem "rubocop", "~> 1.23.0"
|
14
|
+
gem "rubocop-packaging", "~> 0.5.1"
|
15
|
+
gem "rubocop-performance", "~> 1.12.0"
|
16
|
+
gem "rubocop-rake", "~> 0.6.0"
|
17
|
+
gem "rubocop-rspec", "~> 2.7.0"
|
18
|
+
gem "thor", "~> 1.1.0"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pg_online_schema_change (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
coderay (1.1.3)
|
11
|
+
diff-lcs (1.5.0)
|
12
|
+
google-protobuf (3.19.2)
|
13
|
+
google-protobuf (3.19.2-x86_64-linux)
|
14
|
+
method_source (1.0.0)
|
15
|
+
oj (3.13.10)
|
16
|
+
ougai (2.0.0)
|
17
|
+
oj (~> 3.10)
|
18
|
+
parallel (1.21.0)
|
19
|
+
parser (3.0.3.2)
|
20
|
+
ast (~> 2.4.1)
|
21
|
+
pg (1.2.3)
|
22
|
+
pg_query (2.1.2)
|
23
|
+
google-protobuf (>= 3.17.1)
|
24
|
+
pry (0.14.1)
|
25
|
+
coderay (~> 1.1)
|
26
|
+
method_source (~> 1.0)
|
27
|
+
rainbow (3.0.0)
|
28
|
+
rake (13.0.6)
|
29
|
+
regexp_parser (2.2.0)
|
30
|
+
rexml (3.2.5)
|
31
|
+
rspec (3.10.0)
|
32
|
+
rspec-core (~> 3.10.0)
|
33
|
+
rspec-expectations (~> 3.10.0)
|
34
|
+
rspec-mocks (~> 3.10.0)
|
35
|
+
rspec-core (3.10.1)
|
36
|
+
rspec-support (~> 3.10.0)
|
37
|
+
rspec-expectations (3.10.1)
|
38
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
39
|
+
rspec-support (~> 3.10.0)
|
40
|
+
rspec-mocks (3.10.2)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.10.0)
|
43
|
+
rspec-support (3.10.3)
|
44
|
+
rubocop (1.23.0)
|
45
|
+
parallel (~> 1.10)
|
46
|
+
parser (>= 3.0.0.0)
|
47
|
+
rainbow (>= 2.2.2, < 4.0)
|
48
|
+
regexp_parser (>= 1.8, < 3.0)
|
49
|
+
rexml
|
50
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
51
|
+
ruby-progressbar (~> 1.7)
|
52
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
53
|
+
rubocop-ast (1.15.1)
|
54
|
+
parser (>= 3.0.1.1)
|
55
|
+
rubocop-packaging (0.5.1)
|
56
|
+
rubocop (>= 0.89, < 2.0)
|
57
|
+
rubocop-performance (1.12.0)
|
58
|
+
rubocop (>= 1.7.0, < 2.0)
|
59
|
+
rubocop-ast (>= 0.4.0)
|
60
|
+
rubocop-rake (0.6.0)
|
61
|
+
rubocop (~> 1.0)
|
62
|
+
rubocop-rspec (2.7.0)
|
63
|
+
rubocop (~> 1.19)
|
64
|
+
ruby-progressbar (1.11.0)
|
65
|
+
thor (1.1.0)
|
66
|
+
unicode-display_width (2.1.0)
|
67
|
+
|
68
|
+
PLATFORMS
|
69
|
+
arm64-darwin-20
|
70
|
+
x86_64-linux
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
ougai (~> 2.0.0)
|
74
|
+
pg (~> 1.0)
|
75
|
+
pg_online_schema_change!
|
76
|
+
pg_query (~> 2.1.2)
|
77
|
+
pry
|
78
|
+
rake (~> 13.0)
|
79
|
+
rspec (~> 3.0)
|
80
|
+
rubocop (~> 1.23.0)
|
81
|
+
rubocop-packaging (~> 0.5.1)
|
82
|
+
rubocop-performance (~> 1.12.0)
|
83
|
+
rubocop-rake (~> 0.6.0)
|
84
|
+
rubocop-rspec (~> 2.7.0)
|
85
|
+
thor (~> 1.1.0)
|
86
|
+
|
87
|
+
BUNDLED WITH
|
88
|
+
2.3.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Shayon Mukherjee
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
# pg-online-schema-change / pg-osc
|
2
|
+
[![CircleCI](https://circleci.com/gh/shayonj/pg-online-schema-change/tree/main.svg?style=shield)](https://circleci.com/gh/shayonj/pg-online-schema-change/tree/main)
|
3
|
+
|
4
|
+
pg-online-schema-change (`pg-osc`) is a tool for making schema changes (any `ALTER` statements) in Postgres tables with minimal locks, thus helping achieve zero downtime schema changes against production workloads.
|
5
|
+
|
6
|
+
`pg-osc` uses the concept of shadow table to perform schema changes. At a high level, it copies the contents from a primary table to a shadow table, performs the schema change on the shadow table and swaps the table names in the end while preserving all changes to the primary table using triggers (via audit table).
|
7
|
+
|
8
|
+
`pg-osc` is inspired by the design and workings of tools like `pg_repack` and `pt-online-schema-change` (MySQL). Read more below on [how does it work](#how-does-it-work), [prominent features](#prominent-features), the [caveats](#caveats) and [examples](#examples)
|
9
|
+
|
10
|
+
⚠️ Proceed with caution when using this on production like workloads. Best to try on similar setup or staging like environment first. Read on below for some examples and caveats.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'pg_online_schema_change'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle install
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install pg_online_schema_change
|
27
|
+
|
28
|
+
## Requirements
|
29
|
+
- PostgreSQL 9.6 and later
|
30
|
+
- Ruby 2.6 and later
|
31
|
+
- Database user should have permissions for `TRIGGER` and/or a `SUPERUSER`
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
```
|
36
|
+
Usage:
|
37
|
+
pg-online-schema-change perform -a, --alter-statement=ALTER_STATEMENT -d, --dbname=DBNAME -h, --host=HOST -p, --port=N -s, --schema=SCHEMA -u, --username=USERNAME -w, --password=PASSWORD
|
38
|
+
|
39
|
+
Options:
|
40
|
+
-a, --alter-statement=ALTER_STATEMENT # The ALTER statement to perform the schema change
|
41
|
+
-s, --schema=SCHEMA # The schema in which the table is
|
42
|
+
# Default: public
|
43
|
+
-d, --dbname=DBNAME # Name of the database
|
44
|
+
-h, --host=HOST # Server host where the Database is located
|
45
|
+
-u, --username=USERNAME # Username for the Database
|
46
|
+
-p, --port=N # Port for the Database
|
47
|
+
# Default: 5432
|
48
|
+
-w, --password=PASSWORD # Password for the Database
|
49
|
+
-v, [--verbose], [--no-verbose] # Emit logs in debug mode
|
50
|
+
-f, [--drop], [--no-drop] # Drop the original table in the end after the swap
|
51
|
+
-k, [--kill-backends], [--no-kill-backends] # Kill other competing queries/backends when trying to acquire lock for the shadow table creation and swap. It will wait for --wait-time-for-lock duration before killing backends and try upto 3 times.
|
52
|
+
-w, [--wait-time-for-lock=N] # Time to wait before killing backends to acquire lock and/or retrying upto 3 times. It will kill backends if --kill-backends is true, otherwise try upto 3 times and exit if it cannot acquire a lock.
|
53
|
+
# Default: 10
|
54
|
+
-c, [--copy-statement=COPY_STATEMENT] # Takes a .sql file location where you can provide a custom query to be played (ex: backfills) when pg-osc copies data from the primary to the shadow table. More examples in README.
|
55
|
+
```
|
56
|
+
|
57
|
+
```
|
58
|
+
Usage:
|
59
|
+
pg-online-schema-change --version, -v
|
60
|
+
|
61
|
+
print the version
|
62
|
+
```
|
63
|
+
## How does it work
|
64
|
+
|
65
|
+
- **Primary table**: A table against which a potential schema change is to be run
|
66
|
+
- **Shadow table**: A copy of an existing primary table
|
67
|
+
- **Audit table**: A table to store any updates/inserts/delete on a primary table
|
68
|
+
|
69
|
+
![how-it-works](diagrams/how-it-works.png)
|
70
|
+
|
71
|
+
|
72
|
+
1. Create an audit table to record changes made to the parent table.
|
73
|
+
2. Acquire a brief `ACCESS EXCLUSIVE` lock to add a trigger on the parent table (for inserts, updates, deletes) to the audit table.
|
74
|
+
3. Create a new shadow table and run ALTER/migration on the shadow table.
|
75
|
+
4. Copy all rows from the old table.
|
76
|
+
5. Build indexes on the new table.
|
77
|
+
6. Replay all changes accumulated in the audit table against the shadow table.
|
78
|
+
- Delete rows in the audit table as they are replayed.
|
79
|
+
7. Once the delta (remaining rows) is ~20 rows, acquire an `ACCESS EXCLUSIVE` lock against the parent table within a transaction and:
|
80
|
+
- swap table names (shadow table <> parent table).
|
81
|
+
- update references in other tables (FKs) by dropping and re-creating the FKs with a `NOT VALID`.
|
82
|
+
8. Runs `ANALYZE` on the new table.
|
83
|
+
9. Validates all FKs that were added with `NOT VALID`.
|
84
|
+
10. Drop parent (now old) table (OPTIONAL).
|
85
|
+
|
86
|
+
## Prominent features
|
87
|
+
- `pg-osc` supports when a column is being added, dropped or renamed with no data loss.
|
88
|
+
- `pg-osc` acquires minimal locks throughout the process (read more below on the caveats).
|
89
|
+
- Copies over indexes and Foreign keys.
|
90
|
+
- Optionally drop or retain old tables in the end.
|
91
|
+
- Backfill old/new columns as data is copied from primary table to shadow table, and then perform the swap. [Example](#backfill-data)
|
92
|
+
- **TBD**: Ability to reverse the change with no data loss. [tracking issue](https://github.com/shayonj/pg-online-schema-change/issues/14)
|
93
|
+
|
94
|
+
## Examples
|
95
|
+
|
96
|
+
### Renaming a column
|
97
|
+
```
|
98
|
+
pg-online-schema-change perform \
|
99
|
+
--alter-statement 'ALTER TABLE books RENAME COLUMN email TO new_email' \
|
100
|
+
--dbname "postgres" \
|
101
|
+
--host "localhost" \
|
102
|
+
--username "jamesbond" \
|
103
|
+
--password "" \
|
104
|
+
```
|
105
|
+
|
106
|
+
### Multiple ALTER statements
|
107
|
+
```
|
108
|
+
pg-online-schema-change perform \
|
109
|
+
--alter-statement 'ALTER TABLE books ADD COLUMN "purchased" BOOLEAN DEFAULT FALSE; ALTER TABLE books RENAME COLUMN email TO new_email;' \
|
110
|
+
--dbname "postgres" \
|
111
|
+
--host "localhost" \
|
112
|
+
--username "jamesbond" \
|
113
|
+
--password "" \
|
114
|
+
--drop
|
115
|
+
```
|
116
|
+
|
117
|
+
### Kill other backends after 5s
|
118
|
+
If the operation is being performed on a busy table, you can use `pg-osc`'s `kill-backend` functionality to kill other backends that may be competing with the `pg-osc` operation to acquire a lock for a brief while. The `ACCESS EXCLUSIVE` lock acquired by `pg-osc` is only held for a brief while and released after. You can tune how long `pg-osc` should wait before killing other backends (or if at all `pg-osc` should kill backends in the first place).
|
119
|
+
|
120
|
+
```
|
121
|
+
pg-online-schema-change perform \
|
122
|
+
--alter-statement 'ALTER TABLE books ADD COLUMN "purchased" BOOLEAN DEFAULT FALSE;' \
|
123
|
+
--dbname "postgres" \
|
124
|
+
--host "localhost" \
|
125
|
+
--username "jamesbond" \
|
126
|
+
--password "" \
|
127
|
+
--wait-time-for-lock=5 \
|
128
|
+
--kill-backends \
|
129
|
+
--drop
|
130
|
+
```
|
131
|
+
### Backfill data
|
132
|
+
When inserting data into the shadow table, instead of just copying all columns and rows from the primary table, you can pass in a custom sql file to perform the copy and do any additional work. For instance - backfilling certain columns. By providing the `copy-statement`, `pg-osc` will instead play the query to perform the copy operation.
|
133
|
+
|
134
|
+
**IMPORTANT NOTES:**
|
135
|
+
- It is possible to violate a constraint accidentally or not copy data, **so proceed with caution**.
|
136
|
+
- You must use OUTER JOINs when joining in the custom SQL, or you will **lose rows** which do not match the joined table.
|
137
|
+
- The `ALTER` statement can change the table's structure, **so proceed with caution**.
|
138
|
+
- Preserve `%{shadow_table}` as that will be replaced with the destination of the shadow table.
|
139
|
+
- Users are **STRONGLY URGED** to test and validate results before using in production!
|
140
|
+
|
141
|
+
```sql
|
142
|
+
-- file: /src/query.sql
|
143
|
+
INSERT INTO %{shadow_table}(foo, bar, baz, rental_id, tenant_id)
|
144
|
+
SELECT a.foo,a.bar,a.baz,a.rental_id,r.tenant_id AS tenant_id
|
145
|
+
FROM ONLY examples a
|
146
|
+
LEFT OUTER JOIN rentals r
|
147
|
+
ON a.rental_id = r.id
|
148
|
+
```
|
149
|
+
|
150
|
+
```
|
151
|
+
pg-online-schema-change perform \
|
152
|
+
--alter-statement 'ALTER TABLE books ADD COLUMN "tenant_id" VARCHAR;' \
|
153
|
+
--dbname "postgres" \
|
154
|
+
--host "localhost" \
|
155
|
+
--username "jamesbond" \
|
156
|
+
--password "" \
|
157
|
+
--copy-statement "/src/query.sql" \
|
158
|
+
--drop
|
159
|
+
```
|
160
|
+
### Caveats
|
161
|
+
- A primary key should exist on the table; without it, `pg-osc` will raise an exception
|
162
|
+
- This is because - currently there is no other way to uniquely identify rows during replay.
|
163
|
+
- `pg-osc` will acquire `ACCESS EXCLUSIVE` lock on the parent table twice during the operation.
|
164
|
+
- First, when setting up the triggers and the shadow table.
|
165
|
+
- Next, when performing the swap and updating FK references.
|
166
|
+
- Note: If `kill-backends` is passed, it will attempt to terminate any competing operations during both times.
|
167
|
+
- By design, `pg-osc` doesn't kill any other DDLs being performed. It's best to not run any DDLs against the parent table during the operation.
|
168
|
+
- Due to the nature of duplicating a table, there needs to be enough space on the disk to support the operation.
|
169
|
+
- Index, constraints and sequence names will be altered and lose their original naming.
|
170
|
+
- Can be fixed in future releases. Feel free to open a feature req.
|
171
|
+
- Triggers are not carried over.
|
172
|
+
- Can be fixed in future releases. Feel free to open a feature req.
|
173
|
+
- Foreign keys are dropped & re-added to referencing tables with a `NOT VALID`. A follow on `VALIDATE CONSTRAINT` is run.
|
174
|
+
- Ensures that integrity is maintained and re-introducing FKs doesn't acquire additional locks, hence the `NOT VALID`.
|
175
|
+
## Development
|
176
|
+
|
177
|
+
- Install ruby 3.0
|
178
|
+
```
|
179
|
+
\curl -sSL https://get.rvm.io | bash
|
180
|
+
|
181
|
+
rvm install 3.0.0
|
182
|
+
|
183
|
+
rvm use 3.0.0
|
184
|
+
```
|
185
|
+
|
186
|
+
- Spin up postgres via Docker Compose - `docker compose up`
|
187
|
+
- `bundle exec rspec` to run the tests.
|
188
|
+
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
189
|
+
|
190
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
191
|
+
|
192
|
+
## Releasing
|
193
|
+
|
194
|
+
- Bump version in `version.rb`
|
195
|
+
- `git tag v0.1.0`
|
196
|
+
- `git push origin --tags`
|
197
|
+
- `gem build pg_online_schema_change.gemspec`
|
198
|
+
- `gem push pg_online_schema_change-0.1.0.gem`
|
199
|
+
- Update `CHANGELOG.md`
|
200
|
+
- Create a new release - https://github.com/shayonj/pg-online-schema-change/releases/new
|
201
|
+
|
202
|
+
## Contributing
|
203
|
+
|
204
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shayonj/pg-online-schema-change.
|
205
|
+
|
206
|
+
## License
|
207
|
+
|
208
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
209
|
+
|
210
|
+
## Code of Conduct
|
211
|
+
|
212
|
+
Everyone interacting in the PgOnlineSchemaChange project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/shayonj/pg-online-schema-change/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED