pgslice 0.4.7 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/LICENSE.txt +1 -1
- data/README.md +66 -39
- data/lib/pgslice/cli/add_partitions.rb +1 -1
- data/lib/pgslice/cli/prep.rb +2 -2
- data/lib/pgslice/helpers.rb +1 -1
- data/lib/pgslice/version.rb +1 -1
- data/lib/pgslice.rb +11 -11
- metadata +9 -51
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38dff970617c4a96ee7b9234d345e841e0c496d02abbc1900a028a7f0cb46b87
|
4
|
+
data.tar.gz: 24aa5b2b515991c0310fc3dc8cf54c6a7f322b4ee8743bc5e745ca85a1831cec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f63af4550ae70f3fe03e3e4104a43652529d0da576f909c23512ea4a3fa12e2b7f3dd9c526821c69f4b01ec685c5f72d9c8988e5ff4ae74f391e846540c068f4
|
7
|
+
data.tar.gz: 405bb97384272d35e85fb0395e8ec367d1db62bdd7120cb0c9f03c9faa728c804256949fbc78eaf8c048b21aefe93106b3da60f8f9741e83dbb47aec23e64008
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Postgres partitioning as easy as pie. Works great for both new and existing tabl
|
|
4
4
|
|
5
5
|
:tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
|
6
6
|
|
7
|
-
[![Build Status](https://
|
7
|
+
[![Build Status](https://github.com/ankane/pgslice/workflows/build/badge.svg?branch=master)](https://github.com/ankane/pgslice/actions)
|
8
8
|
|
9
9
|
## Install
|
10
10
|
|
@@ -14,7 +14,7 @@ pgslice is a command line tool. To install, run:
|
|
14
14
|
gem install pgslice
|
15
15
|
```
|
16
16
|
|
17
|
-
This will give you the `pgslice` command. If installation fails, you may need to install [dependencies](#dependencies).
|
17
|
+
This will give you the `pgslice` command. You can also install it with [Homebrew](#homebrew) or [Docker](#docker). If installation fails, you may need to install [dependencies](#dependencies).
|
18
18
|
|
19
19
|
## Steps
|
20
20
|
|
@@ -108,17 +108,17 @@ pgslice add_partitions visits --intermediate --past 1 --future 1
|
|
108
108
|
```sql
|
109
109
|
BEGIN;
|
110
110
|
|
111
|
-
CREATE TABLE "public"."
|
111
|
+
CREATE TABLE "public"."visits_202108" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2021-08-01') TO ('2021-09-01');
|
112
112
|
|
113
|
-
ALTER TABLE "public"."
|
113
|
+
ALTER TABLE "public"."visits_202108" ADD PRIMARY KEY ("id");
|
114
114
|
|
115
|
-
CREATE TABLE "public"."
|
115
|
+
CREATE TABLE "public"."visits_202109" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2021-09-01') TO ('2021-10-01');
|
116
116
|
|
117
|
-
ALTER TABLE "public"."
|
117
|
+
ALTER TABLE "public"."visits_202109" ADD PRIMARY KEY ("id");
|
118
118
|
|
119
|
-
CREATE TABLE "public"."
|
119
|
+
CREATE TABLE "public"."visits_202110" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2021-10-01') TO ('2021-11-01');
|
120
120
|
|
121
|
-
ALTER TABLE "public"."
|
121
|
+
ALTER TABLE "public"."visits_202110" ADD PRIMARY KEY ("id");
|
122
122
|
|
123
123
|
COMMIT;
|
124
124
|
```
|
@@ -131,17 +131,17 @@ pgslice fill visits
|
|
131
131
|
/* 1 of 3 */
|
132
132
|
INSERT INTO "public"."visits_intermediate" ("id", "user_id", "ip", "created_at")
|
133
133
|
SELECT "id", "user_id", "ip", "created_at" FROM "public"."visits"
|
134
|
-
WHERE "id" > 0 AND "id" <= 10000 AND "created_at" >= '
|
134
|
+
WHERE "id" > 0 AND "id" <= 10000 AND "created_at" >= '2021-08-01'::date AND "created_at" < '2021-11-01'::date
|
135
135
|
|
136
136
|
/* 2 of 3 */
|
137
137
|
INSERT INTO "public"."visits_intermediate" ("id", "user_id", "ip", "created_at")
|
138
138
|
SELECT "id", "user_id", "ip", "created_at" FROM "public"."visits"
|
139
|
-
WHERE "id" > 10000 AND "id" <= 20000 AND "created_at" >= '
|
139
|
+
WHERE "id" > 10000 AND "id" <= 20000 AND "created_at" >= '2021-08-01'::date AND "created_at" < '2021-11-01'::date
|
140
140
|
|
141
141
|
/* 3 of 3 */
|
142
142
|
INSERT INTO "public"."visits_intermediate" ("id", "user_id", "ip", "created_at")
|
143
143
|
SELECT "id", "user_id", "ip", "created_at" FROM "public"."visits"
|
144
|
-
WHERE "id" > 20000 AND "id" <= 30000 AND "created_at" >= '
|
144
|
+
WHERE "id" > 20000 AND "id" <= 30000 AND "created_at" >= '2021-08-01'::date AND "created_at" < '2021-11-01'::date
|
145
145
|
```
|
146
146
|
|
147
147
|
```sh
|
@@ -149,11 +149,11 @@ pgslice analyze visits
|
|
149
149
|
```
|
150
150
|
|
151
151
|
```sql
|
152
|
-
ANALYZE VERBOSE "public"."
|
152
|
+
ANALYZE VERBOSE "public"."visits_202108";
|
153
153
|
|
154
|
-
ANALYZE VERBOSE "public"."
|
154
|
+
ANALYZE VERBOSE "public"."visits_202109";
|
155
155
|
|
156
|
-
ANALYZE VERBOSE "public"."
|
156
|
+
ANALYZE VERBOSE "public"."visits_202110";
|
157
157
|
|
158
158
|
ANALYZE VERBOSE "public"."visits_intermediate";
|
159
159
|
```
|
@@ -217,16 +217,25 @@ WHERE
|
|
217
217
|
Back up and drop older partitions each day, month, or year.
|
218
218
|
|
219
219
|
```sh
|
220
|
-
pg_dump -c -Fc -t <table>
|
221
|
-
psql -c "DROP TABLE <table>
|
220
|
+
pg_dump -c -Fc -t <table>_202109 $PGSLICE_URL > <table>_202109.dump
|
221
|
+
psql -c "DROP TABLE <table>_202109" $PGSLICE_URL
|
222
222
|
```
|
223
223
|
|
224
224
|
If you use [Amazon S3](https://aws.amazon.com/s3/) for backups, [s3cmd](https://github.com/s3tools/s3cmd) is a nice tool.
|
225
225
|
|
226
226
|
```sh
|
227
|
-
s3cmd put <table>
|
227
|
+
s3cmd put <table>_202109.dump s3://<s3-bucket>/<table>_202109.dump
|
228
228
|
```
|
229
229
|
|
230
|
+
## Schema Updates
|
231
|
+
|
232
|
+
Once a table is partitioned, make schema updates on the master table only (not partitions). This includes adding, removing, and modifying columns, as well as adding and removing indexes and foreign keys.
|
233
|
+
|
234
|
+
A few exceptions are:
|
235
|
+
|
236
|
+
- For Postgres 10, make index and foreign key updates on partitions only
|
237
|
+
- For Postgres < 10, make index and foreign key updates on the master table and all partitions
|
238
|
+
|
230
239
|
## Additional Commands
|
231
240
|
|
232
241
|
To undo prep (which will delete partitions), use:
|
@@ -241,6 +250,14 @@ To undo swap, use:
|
|
241
250
|
pgslice unswap <table>
|
242
251
|
```
|
243
252
|
|
253
|
+
## Additional Options
|
254
|
+
|
255
|
+
Set the tablespace when adding partitions
|
256
|
+
|
257
|
+
```sh
|
258
|
+
pgslice add_partitions <table> --tablespace fastspace
|
259
|
+
```
|
260
|
+
|
244
261
|
## App Considerations
|
245
262
|
|
246
263
|
This set up allows you to read and write with the original table name with no knowledge it’s partitioned. However, there are a few things to be aware of.
|
@@ -255,7 +272,7 @@ SELECT * FROM
|
|
255
272
|
WHERE
|
256
273
|
user_id = 123 AND
|
257
274
|
-- for performance
|
258
|
-
created_at >= '
|
275
|
+
created_at >= '2021-09-01' AND created_at < '2021-09-02'
|
259
276
|
```
|
260
277
|
|
261
278
|
For this to be effective, ensure `constraint_exclusion` is set to `partition` (default value) or `on`.
|
@@ -271,7 +288,9 @@ Before Postgres 10, if you use `INSERT` statements with a `RETURNING` clause (as
|
|
271
288
|
1. Insert directly into the partition
|
272
289
|
2. Get value before the insert with `SELECT nextval('sequence_name')` (for multiple rows, append `FROM generate_series(1, n)`)
|
273
290
|
|
274
|
-
##
|
291
|
+
## Frameworks
|
292
|
+
|
293
|
+
### Rails
|
275
294
|
|
276
295
|
For Postgres 10+, specify the primary key for partitioned models to ensure it’s returned.
|
277
296
|
|
@@ -291,6 +310,10 @@ class Visit < ApplicationRecord
|
|
291
310
|
end
|
292
311
|
```
|
293
312
|
|
313
|
+
### Other Frameworks
|
314
|
+
|
315
|
+
Please submit a PR if additional configuration is needed.
|
316
|
+
|
294
317
|
## One Off Tasks
|
295
318
|
|
296
319
|
You can also use pgslice to reduce the size of a table without partitioning by creating a new table, filling it with a subset of records, and swapping it in.
|
@@ -301,17 +324,9 @@ pgslice fill <table> --where "id > 1000" # use any conditions
|
|
301
324
|
pgslice swap <table>
|
302
325
|
```
|
303
326
|
|
304
|
-
##
|
327
|
+
## Triggers
|
305
328
|
|
306
|
-
|
307
|
-
|
308
|
-
To add, remove, or modify a column, make the update on the master table only.
|
309
|
-
|
310
|
-
To add or remove an index or foreign key:
|
311
|
-
|
312
|
-
- For Postgres 11+, make the update on the master table only.
|
313
|
-
- For Postgres 10, make the update on partitions only.
|
314
|
-
- For Postgres < 10, make the update on the master table and all partitions.
|
329
|
+
Triggers aren’t copied from the original table. You can set up triggers on the intermediate table if needed. Note that Postgres doesn’t support `BEFORE / FOR EACH ROW` triggers on partitioned tables.
|
315
330
|
|
316
331
|
## Declarative Partitioning
|
317
332
|
|
@@ -321,6 +336,27 @@ Postgres 10 introduces [declarative partitioning](https://www.postgresql.org/doc
|
|
321
336
|
|
322
337
|
Always make sure your [connection is secure](https://ankane.org/postgres-sslmode-explained) when connecting to a database over a network you don’t fully trust. Your best option is to connect over SSH or a VPN. Another option is to use `sslmode=verify-full`. If you don’t do this, your database credentials can be compromised.
|
323
338
|
|
339
|
+
## Additional Installation Methods
|
340
|
+
|
341
|
+
### Homebrew
|
342
|
+
|
343
|
+
With Homebrew, you can use:
|
344
|
+
|
345
|
+
```sh
|
346
|
+
brew install ankane/brew/pgslice
|
347
|
+
```
|
348
|
+
|
349
|
+
### Docker
|
350
|
+
|
351
|
+
Get the [Docker image](https://hub.docker.com/r/ankane/pgslice) with:
|
352
|
+
|
353
|
+
```sh
|
354
|
+
docker pull ankane/pgslice
|
355
|
+
alias pgslice="docker run --rm -e PGSLICE_URL ankane/pgslice"
|
356
|
+
```
|
357
|
+
|
358
|
+
This will give you the `pgslice` command.
|
359
|
+
|
324
360
|
## Dependencies
|
325
361
|
|
326
362
|
If installation fails, your system may be missing Ruby or libpq.
|
@@ -328,7 +364,7 @@ If installation fails, your system may be missing Ruby or libpq.
|
|
328
364
|
On Mac, run:
|
329
365
|
|
330
366
|
```sh
|
331
|
-
brew install
|
367
|
+
brew install libpq
|
332
368
|
```
|
333
369
|
|
334
370
|
On Ubuntu, run:
|
@@ -352,15 +388,6 @@ gem install specific_install
|
|
352
388
|
gem specific_install https://github.com/ankane/pgslice.git
|
353
389
|
```
|
354
390
|
|
355
|
-
## Docker
|
356
|
-
|
357
|
-
```sh
|
358
|
-
docker build -t pgslice .
|
359
|
-
alias pgslice="docker run --rm -e PGSLICE_URL pgslice"
|
360
|
-
```
|
361
|
-
|
362
|
-
This will give you the `pgslice` command.
|
363
|
-
|
364
391
|
## Reference
|
365
392
|
|
366
393
|
- [PostgreSQL Manual](https://www.postgresql.org/docs/current/static/ddl-partitioning.html)
|
@@ -27,7 +27,7 @@ module PgSlice
|
|
27
27
|
queries = []
|
28
28
|
|
29
29
|
if needs_comment
|
30
|
-
queries << "COMMENT ON TRIGGER #{quote_ident(trigger_name)} ON #{quote_table(table)}
|
30
|
+
queries << "COMMENT ON TRIGGER #{quote_ident(trigger_name)} ON #{quote_table(table)} IS 'column:#{field},period:#{period},cast:#{cast}';"
|
31
31
|
end
|
32
32
|
|
33
33
|
# today = utc date
|
data/lib/pgslice/cli/prep.rb
CHANGED
@@ -57,7 +57,7 @@ CREATE TABLE #{quote_table(intermediate_table)} (LIKE #{quote_table(table)} INCL
|
|
57
57
|
# add comment
|
58
58
|
cast = table.column_cast(column)
|
59
59
|
queries << <<-SQL
|
60
|
-
COMMENT ON TABLE #{quote_table(intermediate_table)}
|
60
|
+
COMMENT ON TABLE #{quote_table(intermediate_table)} IS 'column:#{column},period:#{period},cast:#{cast},version:#{version}';
|
61
61
|
SQL
|
62
62
|
else
|
63
63
|
queries << <<-SQL
|
@@ -87,7 +87,7 @@ CREATE TRIGGER #{quote_ident(trigger_name)}
|
|
87
87
|
|
88
88
|
cast = table.column_cast(column)
|
89
89
|
queries << <<-SQL
|
90
|
-
COMMENT ON TRIGGER #{quote_ident(trigger_name)} ON #{quote_table(intermediate_table)}
|
90
|
+
COMMENT ON TRIGGER #{quote_ident(trigger_name)} ON #{quote_table(intermediate_table)} IS 'column:#{column},period:#{period},cast:#{cast}';
|
91
91
|
SQL
|
92
92
|
end
|
93
93
|
|
data/lib/pgslice/helpers.rb
CHANGED
@@ -33,7 +33,7 @@ module PgSlice
|
|
33
33
|
params = CGI.parse(uri.query.to_s)
|
34
34
|
# remove schema
|
35
35
|
@schema = Array(params.delete("schema") || "public")[0]
|
36
|
-
uri.query = URI.encode_www_form(params)
|
36
|
+
uri.query = params.any? ? URI.encode_www_form(params) : nil
|
37
37
|
|
38
38
|
ENV["PGCONNECT_TIMEOUT"] ||= "1"
|
39
39
|
conn = PG::Connection.new(uri.to_s)
|
data/lib/pgslice/version.rb
CHANGED
data/lib/pgslice.rb
CHANGED
@@ -7,16 +7,16 @@ require "cgi"
|
|
7
7
|
require "time"
|
8
8
|
|
9
9
|
# modules
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
require_relative "pgslice/helpers"
|
11
|
+
require_relative "pgslice/table"
|
12
|
+
require_relative "pgslice/version"
|
13
13
|
|
14
14
|
# commands
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
require_relative "pgslice/cli"
|
16
|
+
require_relative "pgslice/cli/add_partitions"
|
17
|
+
require_relative "pgslice/cli/analyze"
|
18
|
+
require_relative "pgslice/cli/fill"
|
19
|
+
require_relative "pgslice/cli/prep"
|
20
|
+
require_relative "pgslice/cli/swap"
|
21
|
+
require_relative "pgslice/cli/unprep"
|
22
|
+
require_relative "pgslice/cli/unswap"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pgslice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -38,50 +38,8 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 0.18.2
|
41
|
-
|
42
|
-
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: minitest
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '0'
|
83
|
-
description:
|
84
|
-
email: andrew@chartkick.com
|
41
|
+
description:
|
42
|
+
email: andrew@ankane.org
|
85
43
|
executables:
|
86
44
|
- pgslice
|
87
45
|
extensions: []
|
@@ -107,7 +65,7 @@ homepage: https://github.com/ankane/pgslice
|
|
107
65
|
licenses:
|
108
66
|
- MIT
|
109
67
|
metadata: {}
|
110
|
-
post_install_message:
|
68
|
+
post_install_message:
|
111
69
|
rdoc_options: []
|
112
70
|
require_paths:
|
113
71
|
- lib
|
@@ -115,15 +73,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
73
|
requirements:
|
116
74
|
- - ">="
|
117
75
|
- !ruby/object:Gem::Version
|
118
|
-
version: '2.
|
76
|
+
version: '2.5'
|
119
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
78
|
requirements:
|
121
79
|
- - ">="
|
122
80
|
- !ruby/object:Gem::Version
|
123
81
|
version: '0'
|
124
82
|
requirements: []
|
125
|
-
rubygems_version: 3.1
|
126
|
-
signing_key:
|
83
|
+
rubygems_version: 3.4.1
|
84
|
+
signing_key:
|
127
85
|
specification_version: 4
|
128
86
|
summary: Postgres partitioning as easy as pie
|
129
87
|
test_files: []
|