Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ module PG
src + " int con(){ return PGRES_PIPELINE_SYNC; }"
end
have_func 'PQsetChunkedRowsMode', 'libpq-fe.h' # since PostgreSQL-17
have_func 'PQfullProtocolVersion', 'libpq-fe.h' # since PostgreSQL-18
have_func 'timegm'
have_func 'rb_io_wait' # since ruby-3.0
have_func 'rb_io_descriptor' # since ruby-3.1
Expand Down
28 changes: 28 additions & 0 deletions ext/pg_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,31 @@ pgconn_protocol_version(VALUE self)
return INT2NUM(protocol_version);
}

#ifdef HAVE_PQFULLPROTOCOLVERSION
/*
* call-seq:
* conn.full_protocol_version -> Integer
*
* Interrogates the frontend/backend protocol being used, including minor version.
*
* Applications might wish to use this function to determine whether certain features are supported.
* The result is the major version multiplied by 10000 added to the minor version, e.g. 30002 for version 3.2.
* The protocol version will not change after connection startup is complete, but it could theoretically change during a connection reset.
* The 3.0 protocol is supported by PostgreSQL server versions 7.4 and above.
*
* PG::ConnectionBad is raised if the connection is bad.
*/
static VALUE
pgconn_full_protocol_version(VALUE self)
{
int protocol_version = PQfullProtocolVersion(pg_get_pgconn(self));
if (protocol_version == 0) {
pg_raise_conn_error( rb_eConnectionBad, self, "PQfullProtocolVersion() can't get protocol version");
}
return INT2NUM(protocol_version);
}
#endif

/*
* call-seq:
* conn.server_version -> Integer
Expand Down Expand Up @@ -4733,6 +4758,9 @@ init_pg_connection(void)
rb_define_method(rb_cPGconn, "transaction_status", pgconn_transaction_status, 0);
rb_define_method(rb_cPGconn, "parameter_status", pgconn_parameter_status, 1);
rb_define_method(rb_cPGconn, "protocol_version", pgconn_protocol_version, 0);
#ifdef HAVE_PQFULLPROTOCOLVERSION
rb_define_method(rb_cPGconn, "full_protocol_version", pgconn_full_protocol_version, 0);
#endif
rb_define_method(rb_cPGconn, "server_version", pgconn_server_version, 0);
rb_define_method(rb_cPGconn, "error_message", pgconn_error_message, 0);
rb_define_method(rb_cPGconn, "socket", pgconn_socket, 0);
Expand Down
1 change: 1 addition & 0 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ def set_etc_hosts(hostaddr, hostname)
config.filter_run_excluding( :postgresql_12 ) if PG.library_version < 120000
config.filter_run_excluding( :postgresql_14 ) if PG.library_version < 140000
config.filter_run_excluding( :postgresql_17 ) if PG.library_version < 170000
config.filter_run_excluding( :postgresql_18 ) if PG.library_version < 180000
config.filter_run_excluding( :unix_socket ) if RUBY_PLATFORM=~/mingw|mswin/i
config.filter_run_excluding( :scheduler ) if RUBY_VERSION < "3.0" || (RUBY_PLATFORM =~ /mingw|mswin/ && RUBY_VERSION < "3.1") || !Fiber.respond_to?(:scheduler)
config.filter_run_excluding( :scheduler_address_resolve ) if RUBY_VERSION < "3.1"
Expand Down
12 changes: 12 additions & 0 deletions spec/pg/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,18 @@
end
end

context :protocol_version, :postgresql_18 do
it "should retrieve the wrie protocol version" do
expect( @conn.full_protocol_version ).to eq 30000
end

it "should raise an error on a bad connection" do
conn = PG::Connection.connect_start( @conninfo )
conn.finish
expect{ conn.full_protocol_version }.to raise_error(PG::ConnectionBad)
end
end

it "allows a query to be cancelled" do
start = Time.now
@conn.set_notice_processor do |notice|
Expand Down
Loading