Postgres terminology: client vs connection - postgresql

In Postgres, is there a one-to-one relationship between a client and a connection? In other word, is a client always one connection and no client can open more than one connection?
For example, when Postgres says:
org.postgresql.util.PSQLException: FATAL: sorry, too many clients already.
is that equivalent to "too many connections already"?
Also, as far as I understand, Postgres uses one process for each client. So does this mean that each process is used for one connection only?

Refer - https://www.postgresql.org/docs/9.6/static/connect-estab.html
PostgreSQL is implemented using a simple "process per user"
client/server model. In this model there is one client process
connected to exactly one server process. As we do not know ahead of
time how many connections will be made, we have to use a master
process that spawns a new server process every time a connection is
requested.
So yes, one server process serves one connection.

You can have as many connections from a single client (machine, application) as the server can manage. The server can support a given number of connections, whether or not these come from different clients (machine, application) is irrelevant to the server.
The connection is made to the postmaster process that is listening on the port that PG is configured to listen to (5432 by default). When a connection is established (after authentication), the server spawns a process which is used exclusively by a single client. That client can make multiple connections to the same server, for instance to connect to different databases, or the same database using different credentials, etc.

Related

How to monitor/list outgoing connections to foreign servers in postgres?

I currently use the native foreign data wrapper extension to connect to some other postgres instances (sharding architecture). Now I would like to understand how those underlying outgoing connections are managed by the fdw. I read in the documentation that libpq is used to handle the connections. There is no connection pooling in place but those connections are cached.
This connection is kept and re-used for subsequent queries in the same session.
I would like to list those connections to monitor those? We can list incoming connections via SELECT * FROM pg_stat_activity; - can we do something similar for outgoing connections?
The connections to the foreign server are kept open until the database session ends. To monitor that, set log_connections and log_disconnections to on on the target PostgreSQL server.

How to get the local port of a jdbc connection?

As far as I know when one establishes multiple Connection objects via JDBC to one database then each connection occupies a separate port on the machine where the Connection is established (they all connect to one port on the server where the DBMS is running).
I tried to extract the port that corresponds to the Connection objects. Unfortunately I did not find any way to do so.
Background: I'm doing performance analysis where I setup multiple clients which issue queries on the db. I'm logging the execution time of queries on the database server. In the resulting log I have - among others - information about the connection who initiated the query e.g. localhost.localdomain:44760 I hope it is possible to use this information to map each query to the client or more precisely the Connection object who initiated the query (which is my ultimate goal and serves analysis purposes).
Just run this select through the JDBC connection:
select inet_client_port()
More functions like that are in the manual:
http://www.postgresql.org/docs/current/static/functions-info.html

Sql Server Times Out Twice - Connects on 3rd Attempt

I have a WinForms application installed on multiple PCs in an office, and a SQL Server 2012 Express database on the server to which the client application connects.
Each machine fails to connect on the first two attempts giving an error -
Timeout Expired: The timeout period elapsed prior to the completion of the operation or the server is not responding.
However, it always works on the 3rd attempt on all machines!
The server is SBS 2008, the machines are running Windows 7.
The issue was I had used a Named Instanced on SQL Server which by default uses dynamic ports. Hence each connection attempt used a different Port, and each time I was asking the server administrator to allow additional ports. The successful log ins where simply because the dynamic port chosen just so happened to be one previously allowed.
The answer was to use SQL Server Configuration Manager to remove the dynamic port setting and specify a single port to use for all connection attempts, and ensure firewalls etc had an exception for that particular port.

Difference between server connection and server instance?

I was using MySQL Workbench and I am not able to figure out the difference between the following:
1. Server instance
2. Connection to server
In general I want to know if we can use Open Connection to start querying without creating
a server instance of the connection we are trying to connect. Are these two things independent?
You need one or two connections depending on what you wanna do with your server. For MySQL work (i.e. running queries) you need a MySQL connection. For server work (e.g. shutting the MySQL server down or manage other aspects that require shell access) you need a second connection (which is called a Server Instance).
Beginning with MySQL Workbench 6.0 we merged both connection settings into one interface.

Two (or more) socket client connections on one machine

I have a simple node.js client and server programs running on one machine and when I try to connect to the server with second instance of client program simultaneously I get EADDRINUSE, Address already in use error. Is it possible to have two or more TCP based socket client connections (created with createConnection) to one server (created with createServer) on the same machine, or only one client program can be connected to the server at the same time?
Yes, it's possible. Infact, very common. Many applications open dozens, or hundreds of connections to the same server. It sounds like your client program is binding on a port. Only the server should be binding on a port. You should verify.
The client will usually use a random port between 1024-65535, as assigned by your OS. You don't need to worry about it. Since the client is initiating a connection to the server, the server port must unique to one program. Which is why your problem signifies you are trying to start the server twice. Please see http://www.tcpipguide.com/free/t_TCPIPClientEphemeralPortsandClientServerApplicatio.htm