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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 73d406b68757fa95efcc58a98249f9a8394cab3408a9ae14ef642983720f44b3
4
- data.tar.gz: 7952c9aaa1734c76260b76a59822a4065e6184ac01bf3b1916bd217f7b368403
3
+ metadata.gz: 38dff970617c4a96ee7b9234d345e841e0c496d02abbc1900a028a7f0cb46b87
4
+ data.tar.gz: 24aa5b2b515991c0310fc3dc8cf54c6a7f322b4ee8743bc5e745ca85a1831cec
5
5
  SHA512:
6
- metadata.gz: 057aeaddcf952f2bb99f657f290e03f2ec3bac2deffce0d7701a7549418d7d8211b2454cbc71039226b369fc5b355d7ad426df0c4ce032d660e8ce09ebe15eab
7
- data.tar.gz: a36f9db976f72cc656e289ff5af6ebb89657eb739fb6ca1bc4ccba652fdfa8b93894bc2e3fde5a9fdcd63d53a24574347a280af60157d124445a565810b9839c
6
+ metadata.gz: f63af4550ae70f3fe03e3e4104a43652529d0da576f909c23512ea4a3fa12e2b7f3dd9c526821c69f4b01ec685c5f72d9c8988e5ff4ae74f391e846540c068f4
7
+ data.tar.gz: 405bb97384272d35e85fb0395e8ec367d1db62bdd7120cb0c9f03c9faa728c804256949fbc78eaf8c048b21aefe93106b3da60f8f9741e83dbb47aec23e64008
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.5.0 (2023-01-29)
2
+
3
+ - Dropped support for Ruby < 2.5
4
+
5
+ ## 0.4.8 (2022-02-28)
6
+
7
+ - Fixed error with pg 1.3
8
+ - Reduced size of Docker image
9
+
1
10
  ## 0.4.7 (2020-08-14)
2
11
 
3
12
  - Added `--tablespace` option to `add_partitions`
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016-2020 Andrew Kane
3
+ Copyright (c) 2016-2023 Andrew Kane
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
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://travis-ci.org/ankane/pgslice.svg?branch=master)](https://travis-ci.org/ankane/pgslice)
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"."visits_201808" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2018-08-01') TO ('2018-09-01');
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"."visits_201808" ADD PRIMARY KEY ("id");
113
+ ALTER TABLE "public"."visits_202108" ADD PRIMARY KEY ("id");
114
114
 
115
- CREATE TABLE "public"."visits_201809" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2018-09-01') TO ('2018-10-01');
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"."visits_201809" ADD PRIMARY KEY ("id");
117
+ ALTER TABLE "public"."visits_202109" ADD PRIMARY KEY ("id");
118
118
 
119
- CREATE TABLE "public"."visits_201810" PARTITION OF "public"."visits_intermediate" FOR VALUES FROM ('2018-10-01') TO ('2018-11-01');
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"."visits_201808" ADD PRIMARY KEY ("id");
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" >= '2018-08-01'::date AND "created_at" < '2018-11-01'::date
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" >= '2018-08-01'::date AND "created_at" < '2018-11-01'::date
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" >= '2018-08-01'::date AND "created_at" < '2018-11-01'::date
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"."visits_201808";
152
+ ANALYZE VERBOSE "public"."visits_202108";
153
153
 
154
- ANALYZE VERBOSE "public"."visits_201809";
154
+ ANALYZE VERBOSE "public"."visits_202109";
155
155
 
156
- ANALYZE VERBOSE "public"."visits_201810";
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>_201809 $PGSLICE_URL > <table>_201809.dump
221
- psql -c "DROP TABLE <table>_201809" $PGSLICE_URL
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>_201809.dump s3://<s3-bucket>/<table>_201809.dump
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 >= '2018-09-01' AND created_at < '2018-09-02'
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
- ## Rails
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
- ## Schema Updates
327
+ ## Triggers
305
328
 
306
- Once a table is partitioned, here’s how to change the schema:
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 postgresql
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)} is 'column:#{field},period:#{period},cast:#{cast}';"
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
@@ -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)} is 'column:#{column},period:#{period},cast:#{cast},version:#{version}';
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)} is 'column:#{column},period:#{period},cast:#{cast}';
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
 
@@ -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)
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.4.7"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/pgslice.rb CHANGED
@@ -7,16 +7,16 @@ require "cgi"
7
7
  require "time"
8
8
 
9
9
  # modules
10
- require "pgslice/helpers"
11
- require "pgslice/table"
12
- require "pgslice/version"
10
+ require_relative "pgslice/helpers"
11
+ require_relative "pgslice/table"
12
+ require_relative "pgslice/version"
13
13
 
14
14
  # commands
15
- require "pgslice/cli"
16
- require "pgslice/cli/add_partitions"
17
- require "pgslice/cli/analyze"
18
- require "pgslice/cli/fill"
19
- require "pgslice/cli/prep"
20
- require "pgslice/cli/swap"
21
- require "pgslice/cli/unprep"
22
- require "pgslice/cli/unswap"
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.7
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: 2020-08-14 00:00:00.000000000 Z
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
- - !ruby/object:Gem::Dependency
42
- name: bundler
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.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.2
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: []