pgslice 0.4.5 → 0.4.6

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: 21d2ab39e5d79cc2477aedc2f458f5e42341c76387fcdd97dd7865cfaec777f8
4
- data.tar.gz: 9df6923ae6aba4054c6631a3d0c5c34c8de82214db0d0d6644e73002b9224702
3
+ metadata.gz: 30d1c5d3ed842d1250761b64f3da34a6e25e0ea17d7689d1eca9f1f0d4db1b8f
4
+ data.tar.gz: 82cbeccebcbdc6edeff9808d9013635d880aa8af20cb462722bdfe0fca5732a1
5
5
  SHA512:
6
- metadata.gz: eb51a60758f6179337c74052bac2ba433efc65ec211ccbcbf9175f9986df9e45f3f16560669a29fd6e9b3684377164dea622d5e39e26ac0f442b441e92953f72
7
- data.tar.gz: fc16788a99b3f307d99dd1d5230cb53e5746241e68cf3028454301f48cd711332b71d4716ab1210c07ca9a86daa07df342d009b306f24cc6245a9efbcb6fb32b
6
+ metadata.gz: 1db66bb9409d26d39952230c127d5edc900cf6070915d8a58ef4bec94a460d2db4588991fc2ed46e3c9205e904c424c6a379711c0ffc985e1723623f2eed3835
7
+ data.tar.gz: 60612ea69df38bcc43f27e492f93ca96f17dc9528236f3e107ea1e7990b5df372dd7d799ea1bdebd6ae3064903a63e9d086668208ba6d3e5958b6247d237df43
@@ -1,122 +1,127 @@
1
- ## 0.4.5
1
+ ## 0.4.6 (2020-05-29)
2
+
3
+ - Ensure correct order with multi-column primary keys
4
+ - Ensure fill always uses correct date range (bug introduced in 0.4.5)
5
+
6
+ ## 0.4.5 (2018-10-18)
2
7
 
3
8
  - Added support for Postgres 11 foreign key improvements
4
9
  - Improved versioning
5
10
 
6
- ## 0.4.4
11
+ ## 0.4.4 (2018-08-18)
7
12
 
8
13
  - Added partitioning by `year`
9
14
  - Fixed `--source-table` and `--dest-table` options
10
15
  - Added descriptions to options
11
16
 
12
- ## 0.4.3
17
+ ## 0.4.3 (2018-08-16)
13
18
 
14
19
  - Fixed sequence ownership
15
20
  - Improved help
16
21
 
17
- ## 0.4.2
22
+ ## 0.4.2 (2018-07-23)
18
23
 
19
24
  - Added support for Postgres 11 index improvements
20
25
  - Added support for all connection options
21
26
 
22
- ## 0.4.1
27
+ ## 0.4.1 (2018-04-30)
23
28
 
24
29
  - Better support for schemas
25
30
  - Use latest partition for schema
26
31
  - Added support for composite primary keys
27
32
 
28
- ## 0.4.0
33
+ ## 0.4.0 (2017-10-07)
29
34
 
30
35
  - Added support for declarative partitioning
31
36
  - Added support for foreign keys
32
37
 
33
- ## 0.3.6
38
+ ## 0.3.6 (2017-07-10)
34
39
 
35
40
  - Fixed drop trigger on `unprep` for non-lowercase tables
36
41
  - Fixed index creation for non-lowercase tables
37
42
 
38
- ## 0.3.5
43
+ ## 0.3.5 (2017-07-06)
39
44
 
40
45
  - Added support for non-lowercase tables and columns
41
46
 
42
- ## 0.3.4
47
+ ## 0.3.4 (2017-07-06)
43
48
 
44
49
  - Added `analyze` method
45
50
  - Fixed `fill` with `--dry-run` option
46
51
  - Better error message for tables without primary key
47
52
 
48
- ## 0.3.3
53
+ ## 0.3.3 (2017-03-22)
49
54
 
50
55
  - Fixed error when creating partitions
51
56
 
52
- ## 0.3.2
57
+ ## 0.3.2 (2016-12-15)
53
58
 
54
59
  - Exit with error code on interrupt
55
60
  - Fixed `--start` option with `--swapped`
56
61
 
57
- ## 0.3.1
62
+ ## 0.3.1 (2016-12-13)
58
63
 
59
64
  - Fixed exception with `--no-partition` option
60
65
  - Use proper cast type in `fill` method for legacy `timestamptz` columns
61
66
 
62
- ## 0.3.0
67
+ ## 0.3.0 (2016-12-12)
63
68
 
64
69
  - Better query performance for `timestamptz` columns
65
70
  - Added support for schemas other than `public`
66
71
 
67
- ## 0.2.3
72
+ ## 0.2.3 (2016-10-10)
68
73
 
69
74
  - Added `--dest-table` option to `fill`
70
75
  - Fixed errors with `fill` when no partitions created
71
76
 
72
- ## 0.2.2
77
+ ## 0.2.2 (2016-10-06)
73
78
 
74
79
  - Set `lock_timeout` on `swap` to prevent bad things from happening
75
80
  - Friendlier error messages
76
81
 
77
- ## 0.2.1
82
+ ## 0.2.1 (2016-09-28)
78
83
 
79
84
  - Added `--where` option to `fill`
80
85
  - Fixed partition detection with `fill`
81
86
  - Fixed error for columns named `user` with `fill`
82
87
 
83
- ## 0.2.0
88
+ ## 0.2.0 (2016-09-22)
84
89
 
85
90
  - Switched to new trigger, which is about 20% faster
86
91
 
87
- ## 0.1.7
92
+ ## 0.1.7 (2016-09-14)
88
93
 
89
94
  - Added `--source-table` option to `fill`
90
95
 
91
- ## 0.1.6
96
+ ## 0.1.6 (2016-08-04)
92
97
 
93
98
  - Added `--no-partition` option to `prep`
94
99
  - Added `--url` option
95
100
 
96
- ## 0.1.5
101
+ ## 0.1.5 (2016-04-26)
97
102
 
98
103
  - Removed `activesupport` dependency for speed
99
104
  - Fixed `fill` for months
100
105
 
101
- ## 0.1.4
106
+ ## 0.1.4 (2016-04-24)
102
107
 
103
108
  - Added sequence ownership
104
109
  - Default to 0 for `--past` and `--future` options
105
110
  - Better `fill` with `--swapped`
106
111
 
107
- ## 0.1.3
112
+ ## 0.1.3 (2016-04-24)
108
113
 
109
114
  - Fixed table inheritance
110
115
 
111
- ## 0.1.2
116
+ ## 0.1.2 (2016-04-24)
112
117
 
113
118
  - Added `--dry-run` option
114
119
  - Print sql to stdout instead of stderr
115
120
 
116
- ## 0.1.1
121
+ ## 0.1.1 (2016-04-24)
117
122
 
118
123
  - Added sql commands to output
119
124
 
120
- ## 0.1.0
125
+ ## 0.1.0 (2016-04-24)
121
126
 
122
127
  - First release
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016-2018 Andrew Kane
3
+ Copyright (c) 2016-2020 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
@@ -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.
17
+ This will give you the `pgslice` command. If installation fails, you may need to install [dependencies](#dependencies).
18
18
 
19
19
  ## Steps
20
20
 
@@ -34,16 +34,14 @@ This will give you the `pgslice` command.
34
34
 
35
35
  Period can be `day`, `month`, or `year`.
36
36
 
37
- This creates a table named `<table>_intermediate` with the appropriate trigger for partitioning.
37
+ This creates a partitioned table named `<table>_intermediate`.
38
38
 
39
- 4. Add partitions
39
+ 4. Add partitions to the intermediate table
40
40
 
41
41
  ```sh
42
42
  pgslice add_partitions <table> --intermediate --past 3 --future 3
43
43
  ```
44
44
 
45
- This creates child tables that inherit from the intermediate table.
46
-
47
45
  Use the `--past` and `--future` options to control the number of partitions.
48
46
 
49
47
  5. *Optional, for tables with data* - Fill the partitions in batches with data from the original table
@@ -80,7 +78,7 @@ This will give you the `pgslice` command.
80
78
 
81
79
  ```sql
82
80
  pg_dump -c -Fc -t <table>_retired $PGSLICE_URL > <table>_retired.dump
83
- psql -c "DROP <table>_retired" $PGSLICE_URL
81
+ psql -c "DROP TABLE <table>_retired" $PGSLICE_URL
84
82
  ```
85
83
 
86
84
  ## Sample Output
@@ -220,7 +218,7 @@ Back up and drop older partitions each day, month, or year.
220
218
 
221
219
  ```sh
222
220
  pg_dump -c -Fc -t <table>_201809 $PGSLICE_URL > <table>_201809.dump
223
- psql -c "DROP <table>_201809" $PGSLICE_URL
221
+ psql -c "DROP TABLE <table>_201809" $PGSLICE_URL
224
222
  ```
225
223
 
226
224
  If you use [Amazon S3](https://aws.amazon.com/s3/) for backups, [s3cmd](https://github.com/s3tools/s3cmd) is a nice tool.
@@ -273,6 +271,26 @@ Before Postgres 10, if you use `INSERT` statements with a `RETURNING` clause (as
273
271
  1. Insert directly into the partition
274
272
  2. Get value before the insert with `SELECT nextval('sequence_name')` (for multiple rows, append `FROM generate_series(1, n)`)
275
273
 
274
+ ## Rails
275
+
276
+ For Postgres 10+, specify the primary key for partitioned models to ensure it’s returned.
277
+
278
+ ```ruby
279
+ class Visit < ApplicationRecord
280
+ self.primary_key = "id"
281
+ end
282
+ ```
283
+
284
+ Before Postgres 10, preload the value.
285
+
286
+ ```ruby
287
+ class Visit < ApplicationRecord
288
+ before_create do
289
+ self.id ||= self.class.connection.select_all("SELECT nextval('#{self.class.sequence_name}')").first["nextval"]
290
+ end
291
+ end
292
+ ```
293
+
276
294
  ## One Off Tasks
277
295
 
278
296
  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.
@@ -287,8 +305,13 @@ pgslice swap
287
305
 
288
306
  Once a table is partitioned, here’s how to change the schema:
289
307
 
290
- - To add, remove, or modify a column, make the update on the master table only.
291
- - To add or remove an index or foreign key, make the update on the master table. For Postgres < 11, make the update on all partitions as well.
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.
292
315
 
293
316
  ## Declarative Partitioning
294
317
 
@@ -296,7 +319,23 @@ Postgres 10 introduces [declarative partitioning](https://www.postgresql.org/doc
296
319
 
297
320
  ## Data Protection
298
321
 
299
- Always make sure your connection is secure when connecting to your 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.
322
+ 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
+
324
+ ## Dependencies
325
+
326
+ If installation fails, your system may be missing Ruby or libpq.
327
+
328
+ On Mac, run:
329
+
330
+ ```sh
331
+ brew install postgresql
332
+ ```
333
+
334
+ On Ubuntu, run:
335
+
336
+ ```sh
337
+ sudo apt-get install ruby-dev libpq-dev build-essential
338
+ ```
300
339
 
301
340
  ## Upgrading
302
341
 
@@ -331,6 +370,7 @@ This will give you the `pgslice` command.
331
370
 
332
371
  Also check out:
333
372
 
373
+ - [Dexter](https://github.com/ankane/dexter) - The automatic indexer for Postgres
334
374
  - [PgHero](https://github.com/ankane/pghero) - A performance dashboard for Postgres
335
375
  - [pgsync](https://github.com/ankane/pgsync) - Sync Postgres data to your local machine
336
376
 
@@ -343,7 +383,7 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
343
383
  - Write, clarify, or fix documentation
344
384
  - Suggest or add new features
345
385
 
346
- To run tests, do:
386
+ To get started with development and testing:
347
387
 
348
388
  ```sh
349
389
  git clone https://github.com/ankane/pgslice.git
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # handle interrupts
4
+ trap("SIGINT") { abort }
5
+
3
6
  require "pgslice"
4
- begin
5
- PgSlice::CLI.start
6
- rescue Interrupt
7
- abort
8
- end
7
+ PgSlice::CLI.start
@@ -1,7 +1,9 @@
1
1
  # dependencies
2
- require "cgi"
3
2
  require "pg"
4
3
  require "thor"
4
+
5
+ # stdlib
6
+ require "cgi"
5
7
  require "time"
6
8
 
7
9
  # modules
@@ -14,7 +14,7 @@ module PgSlice
14
14
  map %w[--version -v] => :version
15
15
 
16
16
  def self.exit_on_failure?
17
- true
17
+ ENV["PGSLICE_ENV"] != "test"
18
18
  end
19
19
 
20
20
  def initialize(*args)
@@ -33,6 +33,7 @@ module PgSlice
33
33
  WHERE s.relkind = 'S'
34
34
  AND n.nspname = $1
35
35
  AND t.relname = $2
36
+ ORDER BY s.relname ASC
36
37
  SQL
37
38
  execute(query, [schema, name])
38
39
  end
@@ -41,12 +42,15 @@ module PgSlice
41
42
  execute("SELECT pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = #{regclass} AND contype ='f'").map { |r| r["pg_get_constraintdef"] }
42
43
  end
43
44
 
44
- # http://stackoverflow.com/a/20537829
45
+ # https://stackoverflow.com/a/20537829
46
+ # TODO can simplify with array_position in Postgres 9.5+
45
47
  def primary_key
46
48
  query = <<-SQL
47
49
  SELECT
48
50
  pg_attribute.attname,
49
- format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
51
+ format_type(pg_attribute.atttypid, pg_attribute.atttypmod),
52
+ pg_attribute.attnum,
53
+ pg_index.indkey
50
54
  FROM
51
55
  pg_index, pg_class, pg_attribute, pg_namespace
52
56
  WHERE
@@ -58,7 +62,8 @@ module PgSlice
58
62
  pg_attribute.attnum = any(pg_index.indkey) AND
59
63
  indisprimary
60
64
  SQL
61
- execute(query, [schema, name]).map { |r| r["attname"] }
65
+ rows = execute(query, [schema, name])
66
+ rows.sort_by { |r| r["indkey"].split(" ").index(r["attnum"]) }.map { |r| r["attname"] }
62
67
  end
63
68
 
64
69
  def index_defs
@@ -117,6 +122,7 @@ module PgSlice
117
122
  WHERE
118
123
  nmsp_parent.nspname = $1 AND
119
124
  parent.relname = $2
125
+ ORDER BY child.relname ASC
120
126
  SQL
121
127
  execute(query, [schema, name]).map { |r| Table.new(r["schema"], r["name"]) }
122
128
  end
@@ -144,7 +150,7 @@ module PgSlice
144
150
  function_def = execute("SELECT pg_get_functiondef(oid) FROM pg_proc WHERE proname = $1", [trigger_name])[0]
145
151
  return [] unless function_def
146
152
  function_def = function_def["pg_get_functiondef"]
147
- sql_format = SQL_FORMAT.find { |_, f| function_def.include?("'#{f}'") }
153
+ sql_format = Helpers::SQL_FORMAT.find { |_, f| function_def.include?("'#{f}'") }
148
154
  return [] unless sql_format
149
155
  period = sql_format[0]
150
156
  field = /to_char\(NEW\.(\w+),/.match(function_def)[1]
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.4.5"
2
+ VERSION = "0.4.6"
3
3
  end
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.5
4
+ version: 0.4.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-18 00:00:00.000000000 Z
11
+ date: 2020-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -122,8 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0'
124
124
  requirements: []
125
- rubyforge_project:
126
- rubygems_version: 2.7.7
125
+ rubygems_version: 3.1.2
127
126
  signing_key:
128
127
  specification_version: 4
129
128
  summary: Postgres partitioning as easy as pie