cipherstash-pg 1.0.0.beta.1-x86_64-darwin-22 → 1.0.0.beta.4-x86_64-darwin-22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +137 -0
  7. data/.gitignore +19 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/Gemfile +3 -3
  15. data/Gemfile.lock +45 -0
  16. data/{History.rdoc → History.md} +168 -153
  17. data/README.ja.md +266 -0
  18. data/README.md +272 -0
  19. data/Rakefile +65 -104
  20. data/Rakefile.cross +298 -0
  21. data/certs/larskanis-2023.pem +24 -0
  22. data/cipherstash-pg.gemspec +0 -0
  23. data/lib/2.7/pg_ext.bundle +0 -0
  24. data/lib/3.0/pg_ext.bundle +0 -0
  25. data/lib/3.1/pg_ext.bundle +0 -0
  26. data/lib/3.2/pg_ext.bundle +0 -0
  27. data/lib/cipherstash-pg/basic_type_map_based_on_result.rb +11 -0
  28. data/lib/cipherstash-pg/basic_type_map_for_queries.rb +113 -0
  29. data/lib/cipherstash-pg/basic_type_map_for_results.rb +30 -0
  30. data/lib/cipherstash-pg/basic_type_registry.rb +206 -0
  31. data/lib/cipherstash-pg/binary_decoder.rb +21 -0
  32. data/lib/cipherstash-pg/coder.rb +82 -0
  33. data/lib/cipherstash-pg/connection.rb +467 -0
  34. data/lib/cipherstash-pg/constants.rb +3 -0
  35. data/lib/cipherstash-pg/exceptions.rb +19 -0
  36. data/lib/cipherstash-pg/result.rb +22 -0
  37. data/lib/cipherstash-pg/text_decoder.rb +43 -0
  38. data/lib/cipherstash-pg/text_encoder.rb +67 -0
  39. data/lib/cipherstash-pg/tuple.rb +24 -0
  40. data/lib/cipherstash-pg/type_map_by_column.rb +11 -0
  41. data/lib/cipherstash-pg/version.rb +3 -0
  42. data/lib/cipherstash-pg.rb +56 -11
  43. data/lib/libpq.5.dylib +0 -0
  44. data/misc/openssl-pg-segfault.rb +15 -25
  45. data/misc/postgres/Rakefile +13 -20
  46. data/misc/postgres/lib/postgres.rb +10 -14
  47. data/misc/ruby-pg/Rakefile +13 -20
  48. data/misc/ruby-pg/lib/ruby/pg.rb +10 -14
  49. data/rakelib/task_extension.rb +17 -31
  50. data/sample/array_insert.rb +7 -20
  51. data/sample/async_api.rb +54 -96
  52. data/sample/async_copyto.rb +20 -35
  53. data/sample/async_mixed.rb +22 -50
  54. data/sample/check_conn.rb +8 -20
  55. data/sample/copydata.rb +18 -68
  56. data/sample/copyfrom.rb +26 -78
  57. data/sample/copyto.rb +10 -16
  58. data/sample/cursor.rb +9 -19
  59. data/sample/disk_usage_report.rb +89 -174
  60. data/sample/issue-119.rb +45 -93
  61. data/sample/losample.rb +48 -66
  62. data/sample/minimal-testcase.rb +6 -17
  63. data/sample/notify_wait.rb +21 -67
  64. data/sample/pg_statistics.rb +100 -281
  65. data/sample/replication_monitor.rb +119 -218
  66. data/sample/test_binary_values.rb +14 -30
  67. data/sample/wal_shipper.rb +199 -431
  68. data/sample/warehouse_partitions.rb +157 -307
  69. data/translation/.po4a-version +7 -0
  70. data/translation/po/all.pot +875 -0
  71. data/translation/po/ja.po +868 -0
  72. data/translation/po4a.cfg +9 -0
  73. metadata +50 -28
  74. data/README.ja.rdoc +0 -13
  75. data/README.rdoc +0 -233
  76. data/lib/pg/basic_type_map_based_on_result.rb +0 -47
  77. data/lib/pg/basic_type_map_for_queries.rb +0 -193
  78. data/lib/pg/basic_type_map_for_results.rb +0 -81
  79. data/lib/pg/basic_type_registry.rb +0 -301
  80. data/lib/pg/binary_decoder.rb +0 -23
  81. data/lib/pg/coder.rb +0 -104
  82. data/lib/pg/connection.rb +0 -878
  83. data/lib/pg/constants.rb +0 -12
  84. data/lib/pg/exceptions.rb +0 -18
  85. data/lib/pg/result.rb +0 -43
  86. data/lib/pg/text_decoder.rb +0 -46
  87. data/lib/pg/text_encoder.rb +0 -59
  88. data/lib/pg/tuple.rb +0 -30
  89. data/lib/pg/type_map_by_column.rb +0 -16
  90. data/lib/pg/version.rb +0 -4
  91. data/lib/pg.rb +0 -55
@@ -1,222 +1,123 @@
1
- # -*- ruby -*-
2
- # vim: set noet nosta sw=4 ts=4 :
3
- #
4
- # Get the current WAL segment and offset from a master postgresql
5
- # server, and compare slave servers to see how far behind they
6
- # are in MB. This script should be easily modified for use with
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
- VERSION = %q$Id$
31
-
32
- # When to consider a slave as 'behind', measured in WAL segments.
33
- # The default WAL segment size is 16, so we'll alert after
34
- # missing two WAL files worth of data.
35
- #
36
- LAG_ALERT = 32
37
-
38
- ### Create a new PGMonitor object.
39
- ###
40
- def initialize( opts, hosts )
41
- @opts = opts
42
- @master = hosts.shift
43
- @slaves = hosts
44
- @current_wal = {}
45
- @failures = []
46
- end
47
-
48
- attr_reader :opts, :current_wal, :master, :slaves, :failures
49
-
50
-
51
- ### Perform the connections and check the lag.
52
- ###
53
- def check
54
- # clear prior failures, get current xlog info
55
- @failures = []
56
- return unless self.get_current_wal
57
-
58
- # check all slaves
59
- self.slaves.each do |slave|
60
- begin
61
- slave_db = PG.connect(
62
- :dbname => self.opts.database,
63
- :host => slave,
64
- :port => self.opts.port,
65
- :user => self.opts.user,
66
- :password => self.opts.pass,
67
- :sslmode => 'prefer'
68
- )
69
-
70
- xlog = slave_db.exec( 'SELECT pg_last_xlog_receive_location()' ).getvalue( 0, 0 )
71
- slave_db.close
72
-
73
- lag_in_megs = ( self.find_lag( xlog ).to_f / 1024 / 1024 ).abs
74
- if lag_in_megs >= LAG_ALERT
75
- failures << { :host => slave,
76
- :error => "%0.2fMB behind the master." % [ lag_in_megs ] }
77
- end
78
- rescue => err
79
- failures << { :host => slave, :error => err.message }
80
- end
81
- end
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
- ### Parse command line arguments. Return a struct of global options.
140
- ###
141
- def parse_args( args )
142
- options = OpenStruct.new
143
- options.database = 'postgres'
144
- options.port = 5432
145
- options.user = Etc.getpwuid( Process.uid ).name
146
- options.sslmode = 'prefer'
147
-
148
- opts = OptionParser.new do |opts|
149
- opts.banner = "Usage: #{$0} [options] <master> <slave> [slave2, slave3...]"
150
-
151
- opts.separator ''
152
- opts.separator 'Connection options:'
153
-
154
- opts.on( '-d', '--database DBNAME',
155
- "specify the database to connect to (default: \"#{options.database}\")" ) do |db|
156
- options.database = db
157
- end
158
-
159
- opts.on( '-h', '--host HOSTNAME', 'database server host' ) do |host|
160
- options.host = host
161
- end
162
-
163
- opts.on( '-p', '--port PORT', Integer,
164
- "database server port (default: \"#{options.port}\")" ) do |port|
165
- options.port = port
166
- end
167
-
168
- opts.on( '-U', '--user NAME',
169
- "database user name (default: \"#{options.user}\")" ) do |user|
170
- options.user = user
171
- end
172
-
173
- opts.on( '-W', 'force password prompt' ) do |pw|
174
- print 'Password: '
175
- begin
176
- system 'stty -echo'
177
- options.pass = $stdin.gets.chomp
178
- ensure
179
- system 'stty echo'
180
- puts
181
- end
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
- if __FILE__ == $0
205
- opts = parse_args( ARGV )
206
- raise ArgumentError, "At least two PostgreSQL servers are required." if ARGV.length < 2
207
- mon = PGMonitor.new( opts, ARGV )
208
-
209
- mon.check
210
- if mon.failures.empty?
211
- puts "All is well!"
212
- exit 0
213
- else
214
- puts "Database replication delayed or broken."
215
- mon.failures.each do |bad|
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
- # -*- ruby -*-1.9.1
2
-
3
- require 'pg'
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 "INSERT INTO test(a, b) VALUES($1::int, $2::bytea)",
12
- [a, {:value => b.pack('N*'), :format => 1}]
13
-
14
- db.exec( "SELECT a::int, b::bytea FROM test LIMIT 1", [], 1 ) do |res|
15
-
16
- res.nfields.times do |i|
17
- puts "Field %d is: %s, a %s (%s) column from table %p" % [
18
- i,
19
- res.fname( i ),
20
- db.exec( "SELECT format_type($1,$2)", [res.ftype(i), res.fmod(1)] ).getvalue(0,0),
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
-