cipherstash-pg 1.0.0.beta.1-arm64-darwin-22 → 1.0.0.beta.4-arm64-darwin-22
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.appveyor.yml +42 -0
- data/.gems +6 -0
- data/.gemtest +0 -0
- data/.github/workflows/binary-gems.yml +117 -0
- data/.github/workflows/source-gem.yml +137 -0
- data/.gitignore +19 -0
- data/.hgsigs +34 -0
- data/.hgtags +41 -0
- data/.irbrc +23 -0
- data/.pryrc +23 -0
- data/.tm_properties +21 -0
- data/.travis.yml +49 -0
- data/Gemfile +3 -3
- data/Gemfile.lock +45 -0
- data/{History.rdoc → History.md} +168 -153
- data/README.ja.md +266 -0
- data/README.md +272 -0
- data/Rakefile +65 -104
- data/Rakefile.cross +298 -0
- data/certs/larskanis-2023.pem +24 -0
- data/cipherstash-pg.gemspec +0 -0
- data/lib/2.7/pg_ext.bundle +0 -0
- data/lib/3.0/pg_ext.bundle +0 -0
- data/lib/3.1/pg_ext.bundle +0 -0
- data/lib/3.2/pg_ext.bundle +0 -0
- data/lib/cipherstash-pg/basic_type_map_based_on_result.rb +11 -0
- data/lib/cipherstash-pg/basic_type_map_for_queries.rb +113 -0
- data/lib/cipherstash-pg/basic_type_map_for_results.rb +30 -0
- data/lib/cipherstash-pg/basic_type_registry.rb +206 -0
- data/lib/cipherstash-pg/binary_decoder.rb +21 -0
- data/lib/cipherstash-pg/coder.rb +82 -0
- data/lib/cipherstash-pg/connection.rb +467 -0
- data/lib/cipherstash-pg/constants.rb +3 -0
- data/lib/cipherstash-pg/exceptions.rb +19 -0
- data/lib/cipherstash-pg/result.rb +22 -0
- data/lib/cipherstash-pg/text_decoder.rb +43 -0
- data/lib/cipherstash-pg/text_encoder.rb +67 -0
- data/lib/cipherstash-pg/tuple.rb +24 -0
- data/lib/cipherstash-pg/type_map_by_column.rb +11 -0
- data/lib/cipherstash-pg/version.rb +3 -0
- data/lib/cipherstash-pg.rb +56 -11
- data/lib/libpq.5.dylib +0 -0
- data/misc/openssl-pg-segfault.rb +15 -25
- data/misc/postgres/Rakefile +13 -20
- data/misc/postgres/lib/postgres.rb +10 -14
- data/misc/ruby-pg/Rakefile +13 -20
- data/misc/ruby-pg/lib/ruby/pg.rb +10 -14
- data/rakelib/task_extension.rb +17 -31
- data/sample/array_insert.rb +7 -20
- data/sample/async_api.rb +54 -96
- data/sample/async_copyto.rb +20 -35
- data/sample/async_mixed.rb +22 -50
- data/sample/check_conn.rb +8 -20
- data/sample/copydata.rb +18 -68
- data/sample/copyfrom.rb +26 -78
- data/sample/copyto.rb +10 -16
- data/sample/cursor.rb +9 -19
- data/sample/disk_usage_report.rb +89 -174
- data/sample/issue-119.rb +45 -93
- data/sample/losample.rb +48 -66
- data/sample/minimal-testcase.rb +6 -17
- data/sample/notify_wait.rb +21 -67
- data/sample/pg_statistics.rb +100 -281
- data/sample/replication_monitor.rb +119 -218
- data/sample/test_binary_values.rb +14 -30
- data/sample/wal_shipper.rb +199 -431
- data/sample/warehouse_partitions.rb +157 -307
- data/translation/.po4a-version +7 -0
- data/translation/po/all.pot +875 -0
- data/translation/po/ja.po +868 -0
- data/translation/po4a.cfg +9 -0
- metadata +50 -28
- data/README.ja.rdoc +0 -13
- data/README.rdoc +0 -233
- data/lib/pg/basic_type_map_based_on_result.rb +0 -47
- data/lib/pg/basic_type_map_for_queries.rb +0 -193
- data/lib/pg/basic_type_map_for_results.rb +0 -81
- data/lib/pg/basic_type_registry.rb +0 -301
- data/lib/pg/binary_decoder.rb +0 -23
- data/lib/pg/coder.rb +0 -104
- data/lib/pg/connection.rb +0 -878
- data/lib/pg/constants.rb +0 -12
- data/lib/pg/exceptions.rb +0 -18
- data/lib/pg/result.rb +0 -43
- data/lib/pg/text_decoder.rb +0 -46
- data/lib/pg/text_encoder.rb +0 -59
- data/lib/pg/tuple.rb +0 -30
- data/lib/pg/type_map_by_column.rb +0 -16
- data/lib/pg/version.rb +0 -4
- data/lib/pg.rb +0 -55
@@ -1,222 +1,123 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
# Nagios/Mon/Monit/Zabbix/whatever, or wrapping it in a display loop,
|
8
|
-
# and is suitable for both WAL shipping or streaming forms of replication.
|
9
|
-
#
|
10
|
-
# Mahlon E. Smith <mahlon@martini.nu>
|
11
|
-
#
|
12
|
-
# First argument is the master server, all other arguments are treated
|
13
|
-
# as slave machines.
|
14
|
-
#
|
15
|
-
# db_replication.monitor db-master.example.com ...
|
16
|
-
#
|
17
|
-
|
18
|
-
require 'ostruct'
|
19
|
-
require 'optparse'
|
20
|
-
require 'pathname'
|
21
|
-
require 'etc'
|
22
|
-
require 'pg'
|
23
|
-
require 'pp'
|
24
|
-
|
25
|
-
|
26
|
-
### A class to encapsulate the PG handles.
|
27
|
-
###
|
1
|
+
require("ostruct")
|
2
|
+
require("optparse")
|
3
|
+
require("pathname")
|
4
|
+
require("etc")
|
5
|
+
require("cipherstash-pg")
|
6
|
+
require("pp")
|
28
7
|
class PGMonitor
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
|
85
|
-
#########
|
86
|
-
protected
|
87
|
-
#########
|
88
|
-
|
89
|
-
### Ask the master for the current xlog information, to compare
|
90
|
-
### to slaves. Returns true on success. On failure, populates
|
91
|
-
### the failures array and returns false.
|
92
|
-
###
|
93
|
-
def get_current_wal
|
94
|
-
master_db = PG.connect(
|
95
|
-
:dbname => self.opts.database,
|
96
|
-
:host => self.master,
|
97
|
-
:port => self.opts.port,
|
98
|
-
:user => self.opts.user,
|
99
|
-
:password => self.opts.pass,
|
100
|
-
:sslmode => 'prefer'
|
101
|
-
)
|
102
|
-
|
103
|
-
self.current_wal[ :segbytes ] = master_db.exec( 'SHOW wal_segment_size' ).
|
104
|
-
getvalue( 0, 0 ).sub( /\D+/, '' ).to_i << 20
|
105
|
-
|
106
|
-
current = master_db.exec( 'SELECT pg_current_xlog_location()' ).getvalue( 0, 0 )
|
107
|
-
self.current_wal[ :segment ], self.current_wal[ :offset ] = current.split( /\// )
|
108
|
-
|
109
|
-
master_db.close
|
110
|
-
return true
|
111
|
-
|
112
|
-
# If we can't get any of the info from the master, then there is no
|
113
|
-
# point in a comparison with slaves.
|
114
|
-
#
|
115
|
-
rescue => err
|
116
|
-
self.failures << { :host => self.master,
|
117
|
-
:error => 'Unable to retrieve required info from the master (%s)' % [ err.message ] }
|
118
|
-
|
119
|
-
return false
|
120
|
-
end
|
121
|
-
|
122
|
-
|
123
|
-
### Given an +xlog+ position from a slave server, return
|
124
|
-
### the number of bytes the slave needs to replay before it
|
125
|
-
### is caught up to the master.
|
126
|
-
###
|
127
|
-
def find_lag( xlog )
|
128
|
-
s_segment, s_offset = xlog.split( /\// )
|
129
|
-
m_segment = self.current_wal[ :segment ]
|
130
|
-
m_offset = self.current_wal[ :offset ]
|
131
|
-
m_segbytes = self.current_wal[ :segbytes ]
|
132
|
-
|
133
|
-
return (( m_segment.hex - s_segment.hex ) * m_segbytes) + ( m_offset.hex - s_offset.hex )
|
134
|
-
end
|
135
|
-
|
8
|
+
VERSION = "Id"
|
9
|
+
|
10
|
+
LAG_ALERT = 32
|
11
|
+
|
12
|
+
def initialize(opts, hosts)
|
13
|
+
@opts = opts
|
14
|
+
@master = hosts.shift
|
15
|
+
@slaves = hosts
|
16
|
+
@current_wal = {}
|
17
|
+
@failures = []
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader(:opts, :current_wal, :master, :slaves, :failures)
|
21
|
+
|
22
|
+
def check
|
23
|
+
@failures = []
|
24
|
+
return unless self.get_current_wal
|
25
|
+
self.slaves.each do |slave|
|
26
|
+
begin
|
27
|
+
(slave_db = CipherStashPG.connect(:dbname => self.opts.database, :host => slave, :port => self.opts.port, :user => self.opts.user, :password => self.opts.pass, :sslmode => "prefer")
|
28
|
+
xlog = slave_db.exec("SELECT pg_last_xlog_receive_location()").getvalue(0, 0)
|
29
|
+
slave_db.close
|
30
|
+
lag_in_megs = ((self.find_lag(xlog).to_f / 1024) / 1024).abs
|
31
|
+
if (lag_in_megs >= LAG_ALERT) then
|
32
|
+
(failures << { :host => slave, :error => ("%0.2fMB behind the master." % [lag_in_megs]) })
|
33
|
+
end)
|
34
|
+
rescue => err
|
35
|
+
(failures << { :host => slave, :error => err.message })
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def get_current_wal
|
43
|
+
(master_db = CipherStashPG.connect(:dbname => self.opts.database, :host => self.master, :port => self.opts.port, :user => self.opts.user, :password => self.opts.pass, :sslmode => "prefer")
|
44
|
+
self.current_wal[:segbytes] = (master_db.exec("SHOW wal_segment_size").getvalue(0, 0).sub(/\D+/, "").to_i << 20)
|
45
|
+
current = master_db.exec("SELECT pg_current_xlog_location()").getvalue(0, 0)
|
46
|
+
self.current_wal[:segment], self.current_wal[:offset] = current.split(/\//)
|
47
|
+
master_db.close
|
48
|
+
return true)
|
49
|
+
rescue => err
|
50
|
+
(self.failures << { :host => self.master, :error => ("Unable to retrieve required info from the master (%s)" % [err.message]) })
|
51
|
+
return false
|
52
|
+
end
|
53
|
+
|
54
|
+
def find_lag(xlog)
|
55
|
+
s_segment, s_offset = xlog.split(/\//)
|
56
|
+
m_segment = self.current_wal[:segment]
|
57
|
+
m_offset = self.current_wal[:offset]
|
58
|
+
m_segbytes = self.current_wal[:segbytes]
|
59
|
+
return (((m_segment.hex - s_segment.hex) * m_segbytes) + (m_offset.hex - s_offset.hex))
|
60
|
+
end
|
136
61
|
end
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
end
|
183
|
-
|
184
|
-
opts.separator ''
|
185
|
-
opts.separator 'Other options:'
|
186
|
-
|
187
|
-
opts.on_tail( '--help', 'show this help, then exit' ) do
|
188
|
-
$stderr.puts opts
|
189
|
-
exit
|
190
|
-
end
|
191
|
-
|
192
|
-
opts.on_tail( '--version', 'output version information, then exit' ) do
|
193
|
-
puts PGMonitor::VERSION
|
194
|
-
exit
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
opts.parse!( args )
|
199
|
-
return options
|
62
|
+
def parse_args(args)
|
63
|
+
options = OpenStruct.new
|
64
|
+
options.database = "postgres"
|
65
|
+
options.port = 5432
|
66
|
+
options.user = Etc.getpwuid(Process.uid).name
|
67
|
+
options.sslmode = "prefer"
|
68
|
+
opts = OptionParser.new do |opts|
|
69
|
+
opts.banner = "Usage: #{$0} [options] <master> <slave> [slave2, slave3...]"
|
70
|
+
opts.separator("")
|
71
|
+
opts.separator("Connection options:")
|
72
|
+
opts.on("-d", "--database DBNAME", "specify the database to connect to (default: \"#{options.database}\")") do |db|
|
73
|
+
options.database = db
|
74
|
+
end
|
75
|
+
opts.on("-h", "--host HOSTNAME", "database server host") do |host|
|
76
|
+
options.host = host
|
77
|
+
end
|
78
|
+
opts.on("-p", "--port PORT", Integer, "database server port (default: \"#{options.port}\")") do |port|
|
79
|
+
options.port = port
|
80
|
+
end
|
81
|
+
opts.on("-U", "--user NAME", "database user name (default: \"#{options.user}\")") do |user|
|
82
|
+
options.user = user
|
83
|
+
end
|
84
|
+
opts.on("-W", "force password prompt") do |pw|
|
85
|
+
print("Password: ")
|
86
|
+
begin
|
87
|
+
(system("stty -echo")
|
88
|
+
options.pass = $stdin.gets.chomp)
|
89
|
+
ensure
|
90
|
+
(system("stty echo")
|
91
|
+
puts)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
opts.separator("")
|
95
|
+
opts.separator("Other options:")
|
96
|
+
opts.on_tail("--help", "show this help, then exit") do
|
97
|
+
$stderr.puts(opts)
|
98
|
+
exit
|
99
|
+
end
|
100
|
+
opts.on_tail("--version", "output version information, then exit") do
|
101
|
+
puts(PGMonitor::VERSION)
|
102
|
+
exit
|
103
|
+
end
|
104
|
+
end
|
105
|
+
opts.parse!(args)
|
106
|
+
return options
|
200
107
|
end
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
puts "%s: %s" % [ bad[ :host ], bad[ :error ] ]
|
217
|
-
end
|
218
|
-
exit 1
|
219
|
-
end
|
108
|
+
if ("(string)" == $0) then
|
109
|
+
opts = parse_args(ARGV)
|
110
|
+
if (ARGV.length < 2) then
|
111
|
+
raise(ArgumentError, "At least two PostgreSQL servers are required.")
|
112
|
+
end
|
113
|
+
mon = PGMonitor.new(opts, ARGV)
|
114
|
+
mon.check
|
115
|
+
if mon.failures.empty? then
|
116
|
+
puts("All is well!")
|
117
|
+
exit(0)
|
118
|
+
else
|
119
|
+
puts("Database replication delayed or broken.")
|
120
|
+
mon.failures.each { |bad| puts(("%s: %s" % [bad[:host], bad[:error]])) }
|
121
|
+
exit(1)
|
122
|
+
end
|
220
123
|
end
|
221
|
-
|
222
|
-
|
@@ -1,33 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
db = PG.connect( :dbname => 'test' )
|
6
|
-
db.exec "DROP TABLE IF EXISTS test"
|
7
|
-
db.exec "CREATE TABLE test (a INTEGER, b BYTEA)"
|
8
|
-
|
1
|
+
require("cipherstash-pg")
|
2
|
+
db = CipherStashPG.connect(:dbname => "test")
|
3
|
+
db.exec("DROP TABLE IF EXISTS test")
|
4
|
+
db.exec("CREATE TABLE test (a INTEGER, b BYTEA)")
|
9
5
|
a = 42
|
10
6
|
b = [1, 2, 3]
|
11
|
-
db.exec
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
res.fformat( i ).zero? ? "string" : "binary",
|
22
|
-
res.ftable( i ),
|
23
|
-
]
|
24
|
-
end
|
25
|
-
|
26
|
-
res.each do |row|
|
27
|
-
puts "a = #{row['a'].inspect}"
|
28
|
-
puts "a (unpacked) = #{row['a'].unpack('N*').inspect}"
|
29
|
-
puts "b = #{row['b'].unpack('N*').inspect}"
|
30
|
-
end
|
7
|
+
db.exec("INSERT INTO test(a, b) VALUES($1::int, $2::bytea)", [a, { :value => b.pack("N*"), :format => 1 }])
|
8
|
+
db.exec("SELECT a::int, b::bytea FROM test LIMIT 1", [], 1) do |res|
|
9
|
+
res.nfields.times do |i|
|
10
|
+
puts(("Field %d is: %s, a %s (%s) column from table %p" % [i, res.fname(i), db.exec("SELECT format_type($1,$2)", [res.ftype(i), res.fmod(1)]).getvalue(0, 0), res.fformat(i).zero? ? ("string") : ("binary"), res.ftable(i)]))
|
11
|
+
end
|
12
|
+
res.each do |row|
|
13
|
+
puts("a = #{row["a"].inspect}")
|
14
|
+
puts("a (unpacked) = #{row["a"].unpack("N*").inspect}")
|
15
|
+
puts("b = #{row["b"].unpack("N*").inspect}")
|
16
|
+
end
|
31
17
|
end
|
32
|
-
|
33
|
-
|