Fetching Server configured port from C Module

Started by Mike Roestalmost 13 years ago6 messagesgeneral
Jump to latest
#1Mike Roest
mike.roest@replicon.com

Hi There,
I'm having a bit of an issue finding a C function to fetch the
configured server port from a C module.

We have written a C module to allow for remote clients to call a function
to run pg_dump/pg_restore remotely but create files locally on the db
server.

Currently it works fine if the client connects through a network socket as
we're using inet_server_port to get the port to pass onto pg_dump/restore.
But if the client is connected through a unix socket (actually a remote
client connecting to pgbouncer which is connecting to postgres though the
unix socket) inet_server_port is null. I've looked for a function that we
can use to get the configured server port but haven't had any luck.

I could hard code the port in the module when we build it but it would be
nice to be able to change the configured postgres port and not have to
rebuild the module.

Anyone have any suggestions?

I've posted the code for our backup module here:

http://pastebin.com/wQ6VidWn

#2Bruce Momjian
bruce@momjian.us
In reply to: Mike Roest (#1)
Re: Fetching Server configured port from C Module

On Wed, Apr 17, 2013 at 01:08:18PM -0600, Mike Roest wrote:

Hi There,
I'm having a bit of an issue finding a C function to fetch the configured
server port from a C module.

We have written a C module to allow for remote clients to call a function to
run pg_dump/pg_restore remotely but create files locally on the db server.

Currently it works fine if the client connects through a network socket as
we're using inet_server_port to get the port to pass onto pg_dump/restore. But
if the client is connected through a unix socket (actually a remote client
connecting to pgbouncer which is connecting to postgres though the unix socket)
inet_server_port is null. I've looked for a function that we can use to get
the configured server port but haven't had any luck.

I could hard code the port in the module when we build it but it would be nice
to be able to change the configured postgres port and not have to rebuild the
module.

Well, there are technically no _ports_ in unix-domain sockets. However,
the TCP port number is used to construct the socket file; I think you
can use the simple "port" server-side variable for this; does this help
you?

test=> SELECT setting FROM pg_settings WHERE name = 'port';
setting
---------
5432
(1 row)

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3John R Pierce
pierce@hogranch.com
In reply to: Mike Roest (#1)
Re: Fetching Server configured port from C Module

On 4/17/2013 12:08 PM, Mike Roest wrote:

I could hard code the port in the module when we build it but it would
be nice to be able to change the configured postgres port and not have
to rebuild the module.

Anyone have any suggestions?

SHOW PORT;

?

works in 9.2, anyways.

--
john r pierce 37N 122W
somewhere on the middle of the left coast

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#4Mike Roest
mike.roest@replicon.com
In reply to: John R Pierce (#3)
Re: Fetching Server configured port from C Module

SHOW PORT;

test=> SELECT setting FROM pg_settings WHERE name = 'port';
setting
---------
5432

Both of these are from a query context. This is in a C module, I suppose I
could run a query but there has to be a direct C function to get this data.

#5Bruce Momjian
bruce@momjian.us
In reply to: Mike Roest (#4)
Re: Fetching Server configured port from C Module

On Wed, Apr 17, 2013 at 01:32:00PM -0600, Mike Roest wrote:

SHOW PORT;

test=> SELECT setting FROM pg_settings WHERE name = 'port';
setting
---------
5432

Both of these are from a query context. This is in a C module, I suppose I
could run a query but there has to be a direct C function to get this data.

Well, you could look at how to access the PostPortNumber global
variable.

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#6Mike Roest
mike.roest@replicon.com
In reply to: Bruce Momjian (#5)
Re: Fetching Server configured port from C Module

Perfect thanks Bruce that worked.

I just extern'd PostPortNumber in my module and everything seems to be
working.

--Mike