PostgreSQL Source Code  git master
libpq-be.h File Reference
#include <sys/time.h>
#include "datatype/timestamp.h"
#include "libpq/hba.h"
#include "libpq/pqcomm.h"
Include dependency graph for libpq-be.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  Port
 

Typedefs

typedef enum CAC_state CAC_state
 
typedef struct Port Port
 

Enumerations

enum  CAC_state {
  CAC_OK, CAC_STARTUP, CAC_SHUTDOWN, CAC_RECOVERY,
  CAC_TOOMANY, CAC_WAITBACKUP
}
 

Functions

int pq_getkeepalivesidle (Port *port)
 
int pq_getkeepalivesinterval (Port *port)
 
int pq_getkeepalivescount (Port *port)
 
int pq_gettcpusertimeout (Port *port)
 
int pq_setkeepalivesidle (int idle, Port *port)
 
int pq_setkeepalivesinterval (int interval, Port *port)
 
int pq_setkeepalivescount (int count, Port *port)
 
int pq_settcpusertimeout (int timeout, Port *port)
 

Variables

ProtocolVersion FrontendProtocol
 

Typedef Documentation

◆ CAC_state

typedef enum CAC_state CAC_state

◆ Port

typedef struct Port Port

Enumeration Type Documentation

◆ CAC_state

enum CAC_state
Enumerator
CAC_OK 
CAC_STARTUP 
CAC_SHUTDOWN 
CAC_RECOVERY 
CAC_TOOMANY 
CAC_WAITBACKUP 

Definition at line 71 of file libpq-be.h.

Function Documentation

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1860 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, elog, IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

1861 {
1862 #ifdef TCP_KEEPCNT
1863  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1864  return 0;
1865 
1866  if (port->keepalives_count != 0)
1867  return port->keepalives_count;
1868 
1869  if (port->default_keepalives_count == 0)
1870  {
1871  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1872 
1873  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1874  (char *) &port->default_keepalives_count,
1875  &size) < 0)
1876  {
1877  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPCNT");
1878  port->default_keepalives_count = -1; /* don't know */
1879  }
1880  }
1881 
1882  return port->default_keepalives_count;
1883 #else
1884  return 0;
1885 #endif
1886 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:166
pgsocket sock
Definition: libpq-be.h:122
int keepalives_count
Definition: libpq-be.h:170
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1697 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, elog, IS_AF_UNIX, Port::keepalives_idle, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

1698 {
1699 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1700  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1701  return 0;
1702 
1703  if (port->keepalives_idle != 0)
1704  return port->keepalives_idle;
1705 
1706  if (port->default_keepalives_idle == 0)
1707  {
1708 #ifndef WIN32
1709  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1710 
1711  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1712  (char *) &port->default_keepalives_idle,
1713  &size) < 0)
1714  {
1715  elog(LOG, "getsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1716  port->default_keepalives_idle = -1; /* don't know */
1717  }
1718 #else /* WIN32 */
1719  /* We can't get the defaults on Windows, so return "don't know" */
1720  port->default_keepalives_idle = -1;
1721 #endif /* WIN32 */
1722  }
1723 
1724  return port->default_keepalives_idle;
1725 #else
1726  return 0;
1727 #endif
1728 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
#define elog(elevel,...)
Definition: elog.h:226

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1779 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, elog, IS_AF_UNIX, Port::keepalives_interval, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

1780 {
1781 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1782  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1783  return 0;
1784 
1785  if (port->keepalives_interval != 0)
1786  return port->keepalives_interval;
1787 
1788  if (port->default_keepalives_interval == 0)
1789  {
1790 #ifndef WIN32
1791  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1792 
1793  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1794  (char *) &port->default_keepalives_interval,
1795  &size) < 0)
1796  {
1797  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1798  port->default_keepalives_interval = -1; /* don't know */
1799  }
1800 #else
1801  /* We can't get the defaults on Windows, so return "don't know" */
1802  port->default_keepalives_interval = -1;
1803 #endif /* WIN32 */
1804  }
1805 
1806  return port->default_keepalives_interval;
1807 #else
1808  return 0;
1809 #endif
1810 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:165
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1932 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, elog, IS_AF_UNIX, Port::laddr, LOG, Port::sock, and Port::tcp_user_timeout.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

1933 {
1934 #ifdef TCP_USER_TIMEOUT
1935  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1936  return 0;
1937 
1938  if (port->tcp_user_timeout != 0)
1939  return port->tcp_user_timeout;
1940 
1941  if (port->default_tcp_user_timeout == 0)
1942  {
1943  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1944 
1945  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1946  (char *) &port->default_tcp_user_timeout,
1947  &size) < 0)
1948  {
1949  elog(LOG, "getsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1950  port->default_tcp_user_timeout = -1; /* don't know */
1951  }
1952  }
1953 
1954  return port->default_tcp_user_timeout;
1955 #else
1956  return 0;
1957 #endif
1958 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1889 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, elog, IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, pq_getkeepalivescount(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_count(), and StreamConnection().

1890 {
1891  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1892  return STATUS_OK;
1893 
1894 #ifdef TCP_KEEPCNT
1895  if (count == port->keepalives_count)
1896  return STATUS_OK;
1897 
1898  if (port->default_keepalives_count <= 0)
1899  {
1900  if (pq_getkeepalivescount(port) < 0)
1901  {
1902  if (count == 0)
1903  return STATUS_OK; /* default is set but unknown */
1904  else
1905  return STATUS_ERROR;
1906  }
1907  }
1908 
1909  if (count == 0)
1910  count = port->default_keepalives_count;
1911 
1912  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1913  (char *) &count, sizeof(count)) < 0)
1914  {
1915  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPCNT");
1916  return STATUS_ERROR;
1917  }
1918 
1919  port->keepalives_count = count;
1920 #else
1921  if (count != 0)
1922  {
1923  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPCNT");
1924  return STATUS_ERROR;
1925  }
1926 #endif
1927 
1928  return STATUS_OK;
1929 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:166
pgsocket sock
Definition: libpq-be.h:122
int keepalives_count
Definition: libpq-be.h:170
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1089
SockAddr laddr
Definition: libpq-be.h:125
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1860
#define elog(elevel,...)
Definition: elog.h:226

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1731 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, elog, IS_AF_UNIX, Port::keepalives_idle, Port::keepalives_interval, Port::laddr, LOG, pq_getkeepalivesidle(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_idle(), and StreamConnection().

1732 {
1733  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1734  return STATUS_OK;
1735 
1736 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1737 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1738  if (idle == port->keepalives_idle)
1739  return STATUS_OK;
1740 
1741 #ifndef WIN32
1742  if (port->default_keepalives_idle <= 0)
1743  {
1744  if (pq_getkeepalivesidle(port) < 0)
1745  {
1746  if (idle == 0)
1747  return STATUS_OK; /* default is set but unknown */
1748  else
1749  return STATUS_ERROR;
1750  }
1751  }
1752 
1753  if (idle == 0)
1754  idle = port->default_keepalives_idle;
1755 
1756  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1757  (char *) &idle, sizeof(idle)) < 0)
1758  {
1759  elog(LOG, "setsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1760  return STATUS_ERROR;
1761  }
1762 
1763  port->keepalives_idle = idle;
1764 #else /* WIN32 */
1765  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1766 #endif
1767 #else
1768  if (idle != 0)
1769  {
1770  elog(LOG, "setting the keepalive idle time is not supported");
1771  return STATUS_ERROR;
1772  }
1773 #endif
1774 
1775  return STATUS_OK;
1776 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1089
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1697
#define elog(elevel,...)
Definition: elog.h:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1813 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, elog, IS_AF_UNIX, Port::keepalives_idle, Port::keepalives_interval, Port::laddr, LOG, pq_getkeepalivesinterval(), Port::sock, STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_interval(), and StreamConnection().

1814 {
1815  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1816  return STATUS_OK;
1817 
1818 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1819  if (interval == port->keepalives_interval)
1820  return STATUS_OK;
1821 
1822 #ifndef WIN32
1823  if (port->default_keepalives_interval <= 0)
1824  {
1825  if (pq_getkeepalivesinterval(port) < 0)
1826  {
1827  if (interval == 0)
1828  return STATUS_OK; /* default is set but unknown */
1829  else
1830  return STATUS_ERROR;
1831  }
1832  }
1833 
1834  if (interval == 0)
1836 
1837  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1838  (char *) &interval, sizeof(interval)) < 0)
1839  {
1840  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1841  return STATUS_ERROR;
1842  }
1843 
1844  port->keepalives_interval = interval;
1845 #else /* WIN32 */
1846  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1847 #endif
1848 #else
1849  if (interval != 0)
1850  {
1851  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPINTVL");
1852  return STATUS_ERROR;
1853  }
1854 #endif
1855 
1856  return STATUS_OK;
1857 }
int keepalives_idle
Definition: libpq-be.h:168
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1779
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:165
pgsocket sock
Definition: libpq-be.h:122
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1089
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1961 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, elog, IS_AF_UNIX, Port::laddr, LOG, pq_gettcpusertimeout(), Port::sock, STATUS_ERROR, STATUS_OK, and Port::tcp_user_timeout.

Referenced by assign_tcp_user_timeout(), and StreamConnection().

1962 {
1963  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1964  return STATUS_OK;
1965 
1966 #ifdef TCP_USER_TIMEOUT
1967  if (timeout == port->tcp_user_timeout)
1968  return STATUS_OK;
1969 
1970  if (port->default_tcp_user_timeout <= 0)
1971  {
1972  if (pq_gettcpusertimeout(port) < 0)
1973  {
1974  if (timeout == 0)
1975  return STATUS_OK; /* default is set but unknown */
1976  else
1977  return STATUS_ERROR;
1978  }
1979  }
1980 
1981  if (timeout == 0)
1982  timeout = port->default_tcp_user_timeout;
1983 
1984  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1985  (char *) &timeout, sizeof(timeout)) < 0)
1986  {
1987  elog(LOG, "setsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1988  return STATUS_ERROR;
1989  }
1990 
1991  port->tcp_user_timeout = timeout;
1992 #else
1993  if (timeout != 0)
1994  {
1995  elog(LOG, "setsockopt(%s) not supported", "TCP_USER_TIMEOUT");
1996  return STATUS_ERROR;
1997  }
1998 #endif
1999 
2000  return STATUS_OK;
2001 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1090
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1932
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
#define STATUS_OK
Definition: c.h:1089
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:226
int default_tcp_user_timeout
Definition: libpq-be.h:167

Variable Documentation

◆ FrontendProtocol