pg 1.2.3 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.appveyor.yml +42 -0
  4. data/.gems +6 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +137 -0
  7. data/.gitignore +22 -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 +14 -0
  15. data/History.md +876 -0
  16. data/Manifest.txt +0 -1
  17. data/README.ja.md +276 -0
  18. data/README.md +286 -0
  19. data/Rakefile +33 -135
  20. data/Rakefile.cross +12 -13
  21. data/certs/ged.pem +24 -0
  22. data/certs/larskanis-2022.pem +26 -0
  23. data/certs/larskanis-2023.pem +24 -0
  24. data/ext/errorcodes.def +12 -0
  25. data/ext/errorcodes.rb +0 -0
  26. data/ext/errorcodes.txt +4 -1
  27. data/ext/extconf.rb +100 -25
  28. data/ext/gvl_wrappers.c +4 -0
  29. data/ext/gvl_wrappers.h +23 -0
  30. data/ext/pg.c +72 -57
  31. data/ext/pg.h +28 -4
  32. data/ext/pg_binary_decoder.c +80 -1
  33. data/ext/pg_binary_encoder.c +225 -1
  34. data/ext/pg_coder.c +96 -33
  35. data/ext/pg_connection.c +996 -697
  36. data/ext/pg_copy_coder.c +351 -33
  37. data/ext/pg_errors.c +1 -1
  38. data/ext/pg_record_coder.c +50 -19
  39. data/ext/pg_result.c +177 -64
  40. data/ext/pg_text_decoder.c +29 -11
  41. data/ext/pg_text_encoder.c +29 -16
  42. data/ext/pg_tuple.c +83 -60
  43. data/ext/pg_type_map.c +44 -10
  44. data/ext/pg_type_map_all_strings.c +17 -3
  45. data/ext/pg_type_map_by_class.c +54 -27
  46. data/ext/pg_type_map_by_column.c +73 -31
  47. data/ext/pg_type_map_by_mri_type.c +48 -19
  48. data/ext/pg_type_map_by_oid.c +59 -27
  49. data/ext/pg_type_map_in_ruby.c +55 -21
  50. data/ext/pg_util.c +2 -2
  51. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  52. data/lib/pg/basic_type_map_for_queries.rb +198 -0
  53. data/lib/pg/basic_type_map_for_results.rb +104 -0
  54. data/lib/pg/basic_type_registry.rb +299 -0
  55. data/lib/pg/binary_decoder/date.rb +9 -0
  56. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  57. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  58. data/lib/pg/coder.rb +15 -13
  59. data/lib/pg/connection.rb +743 -83
  60. data/lib/pg/exceptions.rb +14 -1
  61. data/lib/pg/text_decoder/date.rb +18 -0
  62. data/lib/pg/text_decoder/inet.rb +9 -0
  63. data/lib/pg/text_decoder/json.rb +14 -0
  64. data/lib/pg/text_decoder/numeric.rb +9 -0
  65. data/lib/pg/text_decoder/timestamp.rb +30 -0
  66. data/lib/pg/text_encoder/date.rb +12 -0
  67. data/lib/pg/text_encoder/inet.rb +28 -0
  68. data/lib/pg/text_encoder/json.rb +14 -0
  69. data/lib/pg/text_encoder/numeric.rb +9 -0
  70. data/lib/pg/text_encoder/timestamp.rb +24 -0
  71. data/lib/pg/version.rb +4 -0
  72. data/lib/pg.rb +94 -39
  73. data/misc/openssl-pg-segfault.rb +31 -0
  74. data/misc/postgres/History.txt +9 -0
  75. data/misc/postgres/Manifest.txt +5 -0
  76. data/misc/postgres/README.txt +21 -0
  77. data/misc/postgres/Rakefile +21 -0
  78. data/misc/postgres/lib/postgres.rb +16 -0
  79. data/misc/ruby-pg/History.txt +9 -0
  80. data/misc/ruby-pg/Manifest.txt +5 -0
  81. data/misc/ruby-pg/README.txt +21 -0
  82. data/misc/ruby-pg/Rakefile +21 -0
  83. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  84. data/pg.gemspec +34 -0
  85. data/rakelib/task_extension.rb +46 -0
  86. data/sample/array_insert.rb +20 -0
  87. data/sample/async_api.rb +102 -0
  88. data/sample/async_copyto.rb +39 -0
  89. data/sample/async_mixed.rb +56 -0
  90. data/sample/check_conn.rb +21 -0
  91. data/sample/copydata.rb +71 -0
  92. data/sample/copyfrom.rb +81 -0
  93. data/sample/copyto.rb +19 -0
  94. data/sample/cursor.rb +21 -0
  95. data/sample/disk_usage_report.rb +177 -0
  96. data/sample/issue-119.rb +94 -0
  97. data/sample/losample.rb +69 -0
  98. data/sample/minimal-testcase.rb +17 -0
  99. data/sample/notify_wait.rb +72 -0
  100. data/sample/pg_statistics.rb +285 -0
  101. data/sample/replication_monitor.rb +222 -0
  102. data/sample/test_binary_values.rb +33 -0
  103. data/sample/wal_shipper.rb +434 -0
  104. data/sample/warehouse_partitions.rb +311 -0
  105. data/translation/.po4a-version +7 -0
  106. data/translation/po/all.pot +910 -0
  107. data/translation/po/ja.po +1047 -0
  108. data/translation/po4a.cfg +12 -0
  109. data.tar.gz.sig +0 -0
  110. metadata +142 -210
  111. metadata.gz.sig +0 -0
  112. data/ChangeLog +0 -0
  113. data/History.rdoc +0 -578
  114. data/README.ja.rdoc +0 -13
  115. data/README.rdoc +0 -213
  116. data/lib/pg/basic_type_mapping.rb +0 -522
  117. data/lib/pg/binary_decoder.rb +0 -23
  118. data/lib/pg/constants.rb +0 -12
  119. data/lib/pg/text_decoder.rb +0 -46
  120. data/lib/pg/text_encoder.rb +0 -59
  121. data/spec/data/expected_trace.out +0 -26
  122. data/spec/data/random_binary_data +0 -0
  123. data/spec/helpers.rb +0 -380
  124. data/spec/pg/basic_type_mapping_spec.rb +0 -630
  125. data/spec/pg/connection_spec.rb +0 -1949
  126. data/spec/pg/connection_sync_spec.rb +0 -41
  127. data/spec/pg/result_spec.rb +0 -681
  128. data/spec/pg/tuple_spec.rb +0 -333
  129. data/spec/pg/type_map_by_class_spec.rb +0 -138
  130. data/spec/pg/type_map_by_column_spec.rb +0 -226
  131. data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
  132. data/spec/pg/type_map_by_oid_spec.rb +0 -149
  133. data/spec/pg/type_map_in_ruby_spec.rb +0 -164
  134. data/spec/pg/type_map_spec.rb +0 -22
  135. data/spec/pg/type_spec.rb +0 -1123
  136. data/spec/pg_spec.rb +0 -50
@@ -0,0 +1,102 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+
5
+ # This is a example of how to use the asynchronous API to query the
6
+ # server without blocking other threads. It's intentionally low-level;
7
+ # if you hooked up the PG::Connection#socket to some kind of reactor, you
8
+ # could make this much nicer.
9
+
10
+ TIMEOUT = 5.0 # seconds to wait for an async operation to complete
11
+
12
+ # Print 'x' continuously to demonstrate that other threads aren't
13
+ # blocked while waiting for the connection, for the query to be sent,
14
+ # for results, etc. You might want to sleep inside the loop or
15
+ # comment this out entirely for cleaner output.
16
+ progress_thread = Thread.new { loop { print 'x' } }
17
+
18
+ # Output progress messages
19
+ def output_progress( msg )
20
+ puts "\n>>> #{msg}\n"
21
+ end
22
+
23
+ # Start the connection
24
+ output_progress "Starting connection..."
25
+ conn = PG::Connection.connect_start( :dbname => 'test' ) or
26
+ abort "Unable to create a new connection!"
27
+ abort "Connection failed: %s" % [ conn.error_message ] if
28
+ conn.status == PG::CONNECTION_BAD
29
+
30
+ # Track the progress of the connection, waiting for the socket to become readable/writable
31
+ # before polling it
32
+ poll_status = PG::PGRES_POLLING_WRITING
33
+ until poll_status == PG::PGRES_POLLING_OK ||
34
+ poll_status == PG::PGRES_POLLING_FAILED
35
+
36
+ # If the socket needs to read, wait 'til it becomes readable to poll again
37
+ case poll_status
38
+ when PG::PGRES_POLLING_READING
39
+ output_progress " waiting for socket to become readable"
40
+ select( [conn.socket_io], nil, nil, TIMEOUT ) or
41
+ raise "Asynchronous connection timed out!"
42
+
43
+ # ...and the same for when the socket needs to write
44
+ when PG::PGRES_POLLING_WRITING
45
+ output_progress " waiting for socket to become writable"
46
+ select( nil, [conn.socket_io], nil, TIMEOUT ) or
47
+ raise "Asynchronous connection timed out!"
48
+ end
49
+
50
+ # Output a status message about the progress
51
+ case conn.status
52
+ when PG::CONNECTION_STARTED
53
+ output_progress " waiting for connection to be made."
54
+ when PG::CONNECTION_MADE
55
+ output_progress " connection OK; waiting to send."
56
+ when PG::CONNECTION_AWAITING_RESPONSE
57
+ output_progress " waiting for a response from the server."
58
+ when PG::CONNECTION_AUTH_OK
59
+ output_progress " received authentication; waiting for backend start-up to finish."
60
+ when PG::CONNECTION_SSL_STARTUP
61
+ output_progress " negotiating SSL encryption."
62
+ when PG::CONNECTION_SETENV
63
+ output_progress " negotiating environment-driven parameter settings."
64
+ when PG::CONNECTION_NEEDED
65
+ output_progress " internal state: connect() needed."
66
+ end
67
+
68
+ # Check to see if it's finished or failed yet
69
+ poll_status = conn.connect_poll
70
+ end
71
+
72
+ abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == PG::CONNECTION_OK
73
+
74
+ output_progress "Sending query"
75
+ conn.send_query( "SELECT * FROM pg_stat_activity" )
76
+
77
+ # Fetch results until there aren't any more
78
+ loop do
79
+ output_progress " waiting for a response"
80
+
81
+ # Buffer any incoming data on the socket until a full result is ready.
82
+ conn.consume_input
83
+ while conn.is_busy
84
+ select( [conn.socket_io], nil, nil, TIMEOUT ) or
85
+ raise "Timeout waiting for query response."
86
+ conn.consume_input
87
+ end
88
+
89
+ # Fetch the next result. If there isn't one, the query is finished
90
+ result = conn.get_result or break
91
+
92
+ puts "\n\nQuery result:\n%p\n" % [ result.values ]
93
+ end
94
+
95
+ output_progress "Done."
96
+ conn.finish
97
+
98
+ if defined?( progress_thread )
99
+ progress_thread.kill
100
+ progress_thread.join
101
+ end
102
+
@@ -0,0 +1,39 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+ require 'stringio'
5
+
6
+ # Using COPY asynchronously
7
+
8
+ $stderr.puts "Opening database connection ..."
9
+ conn = PG.connect( :dbname => 'test' )
10
+ conn.setnonblocking( true )
11
+
12
+ socket = conn.socket_io
13
+
14
+ $stderr.puts "Running COPY command ..."
15
+ buf = ''
16
+ conn.transaction do
17
+ conn.send_query( "COPY logs TO STDOUT WITH csv" )
18
+ buf = nil
19
+
20
+ # #get_copy_data returns a row if there's a whole one to return, false
21
+ # if there isn't one but the COPY is still running, or nil when it's
22
+ # finished.
23
+ begin
24
+ $stderr.puts "COPY loop"
25
+ conn.consume_input
26
+ while conn.is_busy
27
+ $stderr.puts " ready loop"
28
+ select( [socket], nil, nil, 5.0 ) or
29
+ raise "Timeout (5s) waiting for query response."
30
+ conn.consume_input
31
+ end
32
+
33
+ buf = conn.get_copy_data
34
+ $stdout.puts( buf ) if buf
35
+ end until buf.nil?
36
+ end
37
+
38
+ conn.finish
39
+
@@ -0,0 +1,56 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+
5
+ $stdout.sync = true
6
+
7
+ # This is a example of how to mix and match synchronous and async APIs. In this case,
8
+ # the connection to the server is made syncrhonously, and then queries are
9
+ # asynchronous.
10
+
11
+ TIMEOUT = 5.0 # seconds to wait for an async operation to complete
12
+ CONN_OPTS = {
13
+ :host => 'localhost',
14
+ :dbname => 'test',
15
+ }
16
+
17
+ # Output progress messages
18
+ def output_progress( msg )
19
+ puts ">>> #{msg}\n"
20
+ end
21
+
22
+ # Start the (synchronous) connection
23
+ output_progress "Starting connection..."
24
+ conn = PG.connect( CONN_OPTS ) or abort "Unable to create a new connection!"
25
+
26
+ abort "Connect failed: %s" % [ conn.error_message ] unless conn.status == PG::CONNECTION_OK
27
+
28
+ # Now grab a reference to the underlying socket to select() on while the query is running
29
+ socket = conn.socket_io
30
+
31
+ # Send the (asynchronous) query
32
+ output_progress "Sending query"
33
+ conn.send_query( "SELECT * FROM pg_stat_activity" )
34
+
35
+ # Fetch results until there aren't any more
36
+ loop do
37
+ output_progress " waiting for a response"
38
+
39
+ # Buffer any incoming data on the socket until a full result is ready.
40
+ conn.consume_input
41
+ while conn.is_busy
42
+ output_progress " waiting for data to be available on %p..." % [ socket ]
43
+ select( [socket], nil, nil, TIMEOUT ) or
44
+ raise "Timeout waiting for query response."
45
+ conn.consume_input
46
+ end
47
+
48
+ # Fetch the next result. If there isn't one, the query is finished
49
+ result = conn.get_result or break
50
+
51
+ output_progress "Query result:\n%p\n" % [ result.values ]
52
+ end
53
+
54
+ output_progress "Done."
55
+ conn.finish
56
+
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+ # vim: set nosta noet ts=4 sw=4:
3
+ # encoding: utf-8
4
+
5
+ require 'pg'
6
+
7
+ # This is a minimal example of a function that can test an existing PG::Connection and
8
+ # reset it if necessary.
9
+
10
+ def check_connection( conn )
11
+ begin
12
+ conn.exec( "SELECT 1" )
13
+ rescue PG::Error => err
14
+ $stderr.puts "%p while testing connection: %s" % [ err.class, err.message ]
15
+ conn.reset
16
+ end
17
+ end
18
+
19
+ conn = PG.connect( dbname: 'test' )
20
+ check_connection( conn )
21
+
@@ -0,0 +1,71 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+ require 'stringio'
5
+
6
+ $stderr.puts "Opening database connection ..."
7
+ conn = PG.connect( dbname: 'test' )
8
+
9
+ conn.exec( <<END_SQL )
10
+ DROP TABLE IF EXISTS logs;
11
+ CREATE TABLE logs (
12
+ client_ip inet,
13
+ username text,
14
+ ts timestamp,
15
+ request text,
16
+ status smallint,
17
+ bytes int
18
+ );
19
+ END_SQL
20
+
21
+ csv_io = StringIO.new( <<"END_DATA" )
22
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /manual/ HTTP/1.1",404,205
23
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /favicon.ico HTTP/1.1",404,209
24
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /favicon.ico HTTP/1.1",404,209
25
+ "127.0.0.1","","30/Aug/2010:08:22:29 -0700","GET /manual/ HTTP/1.1",200,11094
26
+ "127.0.0.1","","30/Aug/2010:08:22:38 -0700","GET /manual/index.html HTTP/1.1",200,725
27
+ "127.0.0.1","","30/Aug/2010:08:27:56 -0700","GET /manual/ HTTP/1.1",200,11094
28
+ "127.0.0.1","","30/Aug/2010:08:27:57 -0700","GET /manual/ HTTP/1.1",200,11094
29
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/index.html HTTP/1.1",200,7709
30
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/feather.gif HTTP/1.1",200,6471
31
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/left.gif HTTP/1.1",200,60
32
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual.css HTTP/1.1",200,18674
33
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual-print.css HTTP/1.1",200,13200
34
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/favicon.ico HTTP/1.1",200,1078
35
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual-loose-100pc.css HTTP/1.1",200,3065
36
+ "127.0.0.1","","30/Aug/2010:08:28:14 -0700","OPTIONS * HTTP/1.0",200,0
37
+ "127.0.0.1","","30/Aug/2010:08:28:15 -0700","OPTIONS * HTTP/1.0",200,0
38
+ "127.0.0.1","","30/Aug/2010:08:28:47 -0700","GET /manual/mod/directives.html HTTP/1.1",200,33561
39
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/mod/mpm_common.html HTTP/1.1",200,67683
40
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/images/down.gif HTTP/1.1",200,56
41
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/images/up.gif HTTP/1.1",200,57
42
+ "127.0.0.1","","30/Aug/2010:09:19:58 -0700","GET /manual/mod/mod_log_config.html HTTP/1.1",200,28307
43
+ "127.0.0.1","","30/Aug/2010:09:20:19 -0700","GET /manual/mod/core.html HTTP/1.1",200,194144
44
+ "127.0.0.1","","30/Aug/2010:16:02:56 -0700","GET /manual/ HTTP/1.1",200,11094
45
+ "127.0.0.1","","30/Aug/2010:16:03:00 -0700","GET /manual/ HTTP/1.1",200,11094
46
+ "127.0.0.1","","30/Aug/2010:16:06:16 -0700","GET /manual/mod/mod_dir.html HTTP/1.1",200,10583
47
+ "127.0.0.1","","30/Aug/2010:16:06:44 -0700","GET /manual/ HTTP/1.1",200,7709
48
+ END_DATA
49
+
50
+ ### You can test the error case from the database side easily by
51
+ ### changing one of the numbers at the end of one of the above rows to
52
+ ### something non-numeric like "-".
53
+
54
+ $stderr.puts "Running COPY command with data ..."
55
+ buf = ''
56
+ conn.transaction do
57
+ res = conn.copy_data( "COPY logs FROM STDIN WITH csv" ) do
58
+ $stderr.print "Sending lines... "
59
+ csv_io.each_line.with_index do |buf, i|
60
+ $stderr.print "#{i + 1} "
61
+ conn.put_copy_data( buf )
62
+ end
63
+ $stderr.puts "done."
64
+ end
65
+ $stderr.puts "Result of COPY is: %s" % [ res.res_status(res.result_status) ]
66
+ $stderr.puts " tuples copied: %p" % [ res.cmd_tuples ]
67
+ end
68
+
69
+
70
+ conn.finish
71
+
@@ -0,0 +1,81 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+ require 'stringio'
5
+
6
+ $stderr.puts "Opening database connection ..."
7
+ conn = PG.connect( :dbname => 'test' )
8
+
9
+ conn.exec( <<END_SQL )
10
+ DROP TABLE IF EXISTS logs;
11
+ CREATE TABLE logs (
12
+ client_ip inet,
13
+ username text,
14
+ ts timestamp,
15
+ request text,
16
+ status smallint,
17
+ bytes int
18
+ );
19
+ END_SQL
20
+
21
+ copy_data = StringIO.new( <<"END_DATA" )
22
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /manual/ HTTP/1.1",404,205
23
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /favicon.ico HTTP/1.1",404,209
24
+ "127.0.0.1","","30/Aug/2010:08:21:24 -0700","GET /favicon.ico HTTP/1.1",404,209
25
+ "127.0.0.1","","30/Aug/2010:08:22:29 -0700","GET /manual/ HTTP/1.1",200,11094
26
+ "127.0.0.1","","30/Aug/2010:08:22:38 -0700","GET /manual/index.html HTTP/1.1",200,725
27
+ "127.0.0.1","","30/Aug/2010:08:27:56 -0700","GET /manual/ HTTP/1.1",200,11094
28
+ "127.0.0.1","","30/Aug/2010:08:27:57 -0700","GET /manual/ HTTP/1.1",200,11094
29
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/index.html HTTP/1.1",200,7709
30
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/feather.gif HTTP/1.1",200,6471
31
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/left.gif HTTP/1.1",200,60
32
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual.css HTTP/1.1",200,18674
33
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual-print.css HTTP/1.1",200,13200
34
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/images/favicon.ico HTTP/1.1",200,1078
35
+ "127.0.0.1","","30/Aug/2010:08:28:06 -0700","GET /manual/style/css/manual-loose-100pc.css HTTP/1.1",200,3065
36
+ "127.0.0.1","","30/Aug/2010:08:28:14 -0700","OPTIONS * HTTP/1.0",200,0
37
+ "127.0.0.1","","30/Aug/2010:08:28:15 -0700","OPTIONS * HTTP/1.0",200,0
38
+ "127.0.0.1","","30/Aug/2010:08:28:47 -0700","GET /manual/mod/directives.html HTTP/1.1",200,33561
39
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/mod/mpm_common.html HTTP/1.1",200,67683
40
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/images/down.gif HTTP/1.1",200,56
41
+ "127.0.0.1","","30/Aug/2010:08:28:53 -0700","GET /manual/images/up.gif HTTP/1.1",200,57
42
+ "127.0.0.1","","30/Aug/2010:09:19:58 -0700","GET /manual/mod/mod_log_config.html HTTP/1.1",200,28307
43
+ "127.0.0.1","","30/Aug/2010:09:20:19 -0700","GET /manual/mod/core.html HTTP/1.1",200,194144
44
+ "127.0.0.1","","30/Aug/2010:16:02:56 -0700","GET /manual/ HTTP/1.1",200,11094
45
+ "127.0.0.1","","30/Aug/2010:16:03:00 -0700","GET /manual/ HTTP/1.1",200,11094
46
+ "127.0.0.1","","30/Aug/2010:16:06:16 -0700","GET /manual/mod/mod_dir.html HTTP/1.1",200,10583
47
+ "127.0.0.1","","30/Aug/2010:16:06:44 -0700","GET /manual/ HTTP/1.1",200,7709
48
+ END_DATA
49
+
50
+ ### You can test the error case from the database side easily by
51
+ ### changing one of the numbers at the end of one of the above rows to
52
+ ### something non-numeric like "-".
53
+
54
+ $stderr.puts "Running COPY command with data ..."
55
+ buf = ''
56
+ conn.transaction do
57
+ conn.exec( "COPY logs FROM STDIN WITH csv" )
58
+ begin
59
+ while copy_data.read( 256, buf )
60
+ ### Uncomment this to test error-handling for exceptions from the reader side:
61
+ # raise Errno::ECONNRESET, "socket closed while reading"
62
+ $stderr.puts " sending %d bytes of data..." % [ buf.length ]
63
+ until conn.put_copy_data( buf )
64
+ $stderr.puts " waiting for connection to be writable..."
65
+ sleep 0.1
66
+ end
67
+ end
68
+ rescue Errno => err
69
+ errmsg = "%s while reading copy data: %s" % [ err.class.name, err.message ]
70
+ conn.put_copy_end( errmsg )
71
+ else
72
+ conn.put_copy_end
73
+ while res = conn.get_result
74
+ $stderr.puts "Result of COPY is: %s" % [ res.res_status(res.result_status) ]
75
+ end
76
+ end
77
+ end
78
+
79
+
80
+ conn.finish
81
+
data/sample/copyto.rb ADDED
@@ -0,0 +1,19 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+ require 'stringio'
5
+
6
+ # An example of how to stream data to your local host from the database as CSV.
7
+
8
+ $stderr.puts "Opening database connection ..."
9
+ conn = PG.connect( :dbname => 'test' )
10
+
11
+ $stderr.puts "Running COPY command ..."
12
+ buf = ''
13
+ conn.transaction do
14
+ conn.exec( "COPY logs TO STDOUT WITH csv" )
15
+ $stdout.puts( buf ) while buf = conn.get_copy_data
16
+ end
17
+
18
+ conn.finish
19
+
data/sample/cursor.rb ADDED
@@ -0,0 +1,21 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+
5
+ # An example of how to use SQL cursors. This is mostly a straight port of
6
+ # the cursor portion of testlibpq.c from src/test/examples.
7
+
8
+ $stderr.puts "Opening database connection ..."
9
+ conn = PG.connect( :dbname => 'test' )
10
+
11
+ #
12
+ conn.transaction do
13
+ conn.exec( "DECLARE myportal CURSOR FOR select * from pg_database" )
14
+ res = conn.exec( "FETCH ALL IN myportal" )
15
+
16
+ puts res.fields.collect {|fname| "%-15s" % [fname] }.join( '' )
17
+ res.values.collect do |row|
18
+ puts row.collect {|col| "%-15s" % [col] }.join( '' )
19
+ end
20
+ end
21
+
@@ -0,0 +1,177 @@
1
+ # -*- ruby -*-
2
+ # vim: set noet nosta sw=4 ts=4 :
3
+ #
4
+ # Quickly dump size information for a given database.
5
+ # Top twenty objects, and size per schema.
6
+ #
7
+ # Mahlon E. Smith <mahlon@martini.nu>
8
+ #
9
+ # Based on work by Jeff Davis <ruby@j-davis.com>.
10
+ #
11
+
12
+
13
+ require 'ostruct'
14
+ require 'optparse'
15
+ require 'etc'
16
+ require 'pg'
17
+
18
+ SCRIPT_VERSION = %q$Id$
19
+
20
+
21
+ ### Gather data and output it to $stdout.
22
+ ###
23
+ def report( opts )
24
+ db = PG.connect(
25
+ :dbname => opts.database,
26
+ :host => opts.host,
27
+ :port => opts.port,
28
+ :user => opts.user,
29
+ :password => opts.pass,
30
+ :sslmode => 'prefer'
31
+ )
32
+
33
+ # -----------------------------------------
34
+
35
+ db_info = db.exec %Q{
36
+ SELECT
37
+ count(oid) AS num_relations,
38
+ pg_size_pretty(pg_database_size('#{opts.database}')) AS dbsize
39
+ FROM
40
+ pg_class
41
+ }
42
+
43
+ puts '=' * 70
44
+ puts "Disk usage information for %s: (%d relations, %s total)" % [
45
+ opts.database,
46
+ db_info[0]['num_relations'],
47
+ db_info[0]['dbsize']
48
+ ]
49
+ puts '=' * 70
50
+
51
+ # -----------------------------------------
52
+
53
+ top_twenty = db.exec %q{
54
+ SELECT
55
+ relname AS name,
56
+ relkind AS kind,
57
+ pg_size_pretty(pg_relation_size(pg_class.oid)) AS size
58
+ FROM
59
+ pg_class
60
+ ORDER BY
61
+ pg_relation_size(pg_class.oid) DESC
62
+ LIMIT 20
63
+ }
64
+
65
+ puts 'Top twenty objects by size:'
66
+ puts '-' * 70
67
+ top_twenty.each do |row|
68
+ type = case row['kind']
69
+ when 'i'; 'index'
70
+ when 't'; 'toast'
71
+ when 'r'; 'table'
72
+ when 'S'; 'sequence'
73
+ else; '???'
74
+ end
75
+
76
+ puts "%40s %10s (%s)" % [ row['name'], row['size'], type ]
77
+ end
78
+ puts '-' * 70
79
+
80
+ # -----------------------------------------
81
+
82
+ schema_sizes = db.exec %q{
83
+ SELECT
84
+ table_schema,
85
+ pg_size_pretty( CAST( SUM(pg_total_relation_size(table_schema || '.' || table_name)) AS bigint)) AS size
86
+ FROM
87
+ information_schema.tables
88
+ GROUP BY
89
+ table_schema
90
+ ORDER BY
91
+ CAST( SUM(pg_total_relation_size(table_schema || '.' || table_name)) AS bigint ) DESC
92
+ }
93
+
94
+
95
+ puts 'Size per schema:'
96
+ puts '-' * 70
97
+ schema_sizes.each do |row|
98
+ puts "%20s %10s" % [ row['table_schema'], row['size'] ]
99
+ end
100
+ puts '-' * 70
101
+ puts
102
+
103
+ db.finish
104
+ end
105
+
106
+
107
+ ### Parse command line arguments. Return a struct of global options.
108
+ ###
109
+ def parse_args( args )
110
+ options = OpenStruct.new
111
+ options.database = Etc.getpwuid( Process.uid ).name
112
+ options.host = '127.0.0.1'
113
+ options.port = 5432
114
+ options.user = Etc.getpwuid( Process.uid ).name
115
+ options.sslmode = 'prefer'
116
+ options.interval = 5
117
+
118
+ opts = OptionParser.new do |opts|
119
+ opts.banner = "Usage: #{$0} [options]"
120
+
121
+ opts.separator ''
122
+ opts.separator 'Connection options:'
123
+
124
+ opts.on( '-d', '--database DBNAME',
125
+ "specify the database to connect to (default: \"#{options.database}\")" ) do |db|
126
+ options.database = db
127
+ end
128
+
129
+ opts.on( '-h', '--host HOSTNAME', 'database server host' ) do |host|
130
+ options.host = host
131
+ end
132
+
133
+ opts.on( '-p', '--port PORT', Integer,
134
+ "database server port (default: \"#{options.port}\")" ) do |port|
135
+ options.port = port
136
+ end
137
+
138
+ opts.on( '-U', '--user NAME',
139
+ "database user name (default: \"#{options.user}\")" ) do |user|
140
+ options.user = user
141
+ end
142
+
143
+ opts.on( '-W', 'force password prompt' ) do |pw|
144
+ print 'Password: '
145
+ begin
146
+ system 'stty -echo'
147
+ options.pass = gets.chomp
148
+ ensure
149
+ system 'stty echo'
150
+ puts
151
+ end
152
+ end
153
+
154
+ opts.separator ''
155
+ opts.separator 'Other options:'
156
+
157
+ opts.on_tail( '--help', 'show this help, then exit' ) do
158
+ $stderr.puts opts
159
+ exit
160
+ end
161
+
162
+ opts.on_tail( '--version', 'output version information, then exit' ) do
163
+ puts SCRIPT_VERSION
164
+ exit
165
+ end
166
+ end
167
+
168
+ opts.parse!( args )
169
+ return options
170
+ end
171
+
172
+
173
+ if __FILE__ == $0
174
+ opts = parse_args( ARGV )
175
+ report( opts )
176
+ end
177
+
@@ -0,0 +1,94 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'pg'
4
+
5
+ # This is another example of how to use COPY FROM, this time as a
6
+ # minimal test case used to try to figure out what was going on in
7
+ # an issue submitted from a user:
8
+ #
9
+ # https://bitbucket.org/ged/ruby-pg/issue/119
10
+ #
11
+
12
+ conn = PG.connect( dbname: 'test' )
13
+ table_name = 'issue_119'
14
+ field_list = %w[name body_weight brain_weight]
15
+ method = 0
16
+ options = { truncate: true }
17
+ sql_parameters = ''
18
+
19
+ conn.set_error_verbosity( PG::PQERRORS_VERBOSE )
20
+ conn.exec( "DROP TABLE IF EXISTS #{table_name}" )
21
+ conn.exec( "CREATE TABLE #{table_name} ( id SERIAL, name TEXT, body_weight REAL, brain_weight REAL )" )
22
+
23
+ text = <<-END_DATA
24
+ Mountain beaver 1.35 465
25
+ Cow 465 423
26
+ Grey wolf 36.33 119.5
27
+ Goat 27.66 115
28
+ Guinea pig 1.04 5.5
29
+ Dipliodocus 11700 50
30
+ Asian elephant 2547 4603
31
+ Donkey 187.1 419
32
+ Horse 521 655
33
+ Potar monkey 10 115
34
+ Cat 3.3 25.6
35
+ Giraffe 529 680
36
+ Gorilla 207 406
37
+ Human 62 1320
38
+ African elephant 6654 5712
39
+ Triceratops 9400 70
40
+ Rhesus monkey 6.8 179
41
+ Kangaroo 35 56
42
+ Golden hamster 0.12 1
43
+ Mouse 0.023 0.4
44
+ Rabbit 2.5 12.1
45
+ Sheep 55.5 175
46
+ Jaguar 100 157
47
+ Chimpanzee 52.16 440
48
+ Brachiosaurus 87000 154.5
49
+ Mole 0.122 3
50
+ Pig 192 18
51
+ END_DATA
52
+
53
+ #ActiveRecord::Base.connection_pool.with_connection do |conn|
54
+ conn.transaction do
55
+ rc = conn #.raw_connection
56
+ rc.exec "TRUNCATE TABLE #{table_name};" if options[:truncate]
57
+ sql = "COPY #{table_name} (#{field_list.join(',')}) FROM STDIN #{sql_parameters} "
58
+ p sql
59
+ rc.exec(sql)
60
+ errmsg = nil # scope this outside of the rescue below so it's visible later
61
+ begin
62
+ if method == 1
63
+ rc.put_copy_data text + "\\.\n"
64
+ else
65
+ text.each_line { |line| rc.put_copy_data(line) }
66
+ end
67
+ rescue Errno => err
68
+ errmsg = "%s while reading copy data: %s" % [err.class.name, err.message]
69
+ puts "an error occurred"
70
+ end
71
+
72
+ if errmsg
73
+ rc.put_copy_end(errmsg)
74
+ puts "ERROR #{errmsg}"
75
+ else
76
+ rc.put_copy_end
77
+ end
78
+
79
+ while res = rc.get_result
80
+ st = res.res_status( res.result_status )
81
+ puts "Result of COPY is: %s" % [ st ]
82
+ if res.result_status != PG::PGRES_COPY_IN
83
+ puts res.error_message
84
+ end
85
+ end
86
+ puts "end"
87
+ end #transaction
88
+ #end #connection
89
+
90
+ conn.exec( "SELECT name, brain_weight FROM #{table_name}" ) do |res|
91
+ p res.values
92
+ end
93
+
94
+