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_SUPERUSER
}
 

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_SUPERUSER 

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

Function Documentation

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1865 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_count, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

1866 {
1867 #ifdef TCP_KEEPCNT
1868  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1869  return 0;
1870 
1871  if (port->keepalives_count != 0)
1872  return port->keepalives_count;
1873 
1874  if (port->default_keepalives_count == 0)
1875  {
1876  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1877 
1878  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1879  (char *) &port->default_keepalives_count,
1880  &size) < 0)
1881  {
1882  ereport(LOG,
1883  (errmsg("getsockopt(%s) failed: %m", "TCP_KEEPCNT")));
1884  port->default_keepalives_count = -1; /* don't know */
1885  }
1886  }
1887 
1888  return port->default_keepalives_count;
1889 #else
1890  return 0;
1891 #endif
1892 }
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 ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1696 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_idle, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

1697 {
1698 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1699  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1700  return 0;
1701 
1702  if (port->keepalives_idle != 0)
1703  return port->keepalives_idle;
1704 
1705  if (port->default_keepalives_idle == 0)
1706  {
1707 #ifndef WIN32
1708  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1709 
1710  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1711  (char *) &port->default_keepalives_idle,
1712  &size) < 0)
1713  {
1714  ereport(LOG,
1715  (errmsg("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 ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1781 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, ereport, errmsg(), IS_AF_UNIX, Port::keepalives_interval, Port::laddr, LOG, and Port::sock.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

1782 {
1783 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1784  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1785  return 0;
1786 
1787  if (port->keepalives_interval != 0)
1788  return port->keepalives_interval;
1789 
1790  if (port->default_keepalives_interval == 0)
1791  {
1792 #ifndef WIN32
1793  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1794 
1795  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1796  (char *) &port->default_keepalives_interval,
1797  &size) < 0)
1798  {
1799  ereport(LOG,
1800  (errmsg("getsockopt(%s) failed: %m", "TCP_KEEPINTVL")));
1801  port->default_keepalives_interval = -1; /* don't know */
1802  }
1803 #else
1804  /* We can't get the defaults on Windows, so return "don't know" */
1805  port->default_keepalives_interval = -1;
1806 #endif /* WIN32 */
1807  }
1808 
1809  return port->default_keepalives_interval;
1810 #else
1811  return 0;
1812 #endif
1813 }
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 ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1940 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, ereport, errmsg(), IS_AF_UNIX, Port::laddr, LOG, Port::sock, and Port::tcp_user_timeout.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

1941 {
1942 #ifdef TCP_USER_TIMEOUT
1943  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1944  return 0;
1945 
1946  if (port->tcp_user_timeout != 0)
1947  return port->tcp_user_timeout;
1948 
1949  if (port->default_tcp_user_timeout == 0)
1950  {
1951  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1952 
1953  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1954  (char *) &port->default_tcp_user_timeout,
1955  &size) < 0)
1956  {
1957  ereport(LOG,
1958  (errmsg("getsockopt(%s) failed: %m", "TCP_USER_TIMEOUT")));
1959  port->default_tcp_user_timeout = -1; /* don't know */
1960  }
1961  }
1962 
1963  return port->default_tcp_user_timeout;
1964 #else
1965  return 0;
1966 #endif
1967 }
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 ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1895 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_count, ereport, errmsg(), 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().

1896 {
1897  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1898  return STATUS_OK;
1899 
1900 #ifdef TCP_KEEPCNT
1901  if (count == port->keepalives_count)
1902  return STATUS_OK;
1903 
1904  if (port->default_keepalives_count <= 0)
1905  {
1906  if (pq_getkeepalivescount(port) < 0)
1907  {
1908  if (count == 0)
1909  return STATUS_OK; /* default is set but unknown */
1910  else
1911  return STATUS_ERROR;
1912  }
1913  }
1914 
1915  if (count == 0)
1916  count = port->default_keepalives_count;
1917 
1918  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1919  (char *) &count, sizeof(count)) < 0)
1920  {
1921  ereport(LOG,
1922  (errmsg("setsockopt(%s) failed: %m", "TCP_KEEPCNT")));
1923  return STATUS_ERROR;
1924  }
1925 
1926  port->keepalives_count = count;
1927 #else
1928  if (count != 0)
1929  {
1930  ereport(LOG,
1931  (errmsg("setsockopt(%s) not supported", "TCP_KEEPCNT")));
1932  return STATUS_ERROR;
1933  }
1934 #endif
1935 
1936  return STATUS_OK;
1937 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1159
#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:1158
SockAddr laddr
Definition: libpq-be.h:125
#define ereport(elevel,...)
Definition: elog.h:155
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1865
int errmsg(const char *fmt,...)
Definition: elog.c:915

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1731 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_idle, ereport, errmsg(), 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  ereport(LOG,
1760  (errmsg("setsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR)));
1761  return STATUS_ERROR;
1762  }
1763 
1764  port->keepalives_idle = idle;
1765 #else /* WIN32 */
1766  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1767 #endif
1768 #else
1769  if (idle != 0)
1770  {
1771  ereport(LOG,
1772  (errmsg("setting the keepalive idle time is not supported")));
1773  return STATUS_ERROR;
1774  }
1775 #endif
1776 
1777  return STATUS_OK;
1778 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1159
#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:1158
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
#define ereport(elevel,...)
Definition: elog.h:155
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1696
int errmsg(const char *fmt,...)
Definition: elog.c:915
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1816 of file pqcomm.c.

References SockAddr::addr, Port::default_keepalives_interval, ereport, errmsg(), 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().

1817 {
1818  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1819  return STATUS_OK;
1820 
1821 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1822  if (interval == port->keepalives_interval)
1823  return STATUS_OK;
1824 
1825 #ifndef WIN32
1826  if (port->default_keepalives_interval <= 0)
1827  {
1828  if (pq_getkeepalivesinterval(port) < 0)
1829  {
1830  if (interval == 0)
1831  return STATUS_OK; /* default is set but unknown */
1832  else
1833  return STATUS_ERROR;
1834  }
1835  }
1836 
1837  if (interval == 0)
1839 
1840  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1841  (char *) &interval, sizeof(interval)) < 0)
1842  {
1843  ereport(LOG,
1844  (errmsg("setsockopt(%s) failed: %m", "TCP_KEEPINTVL")));
1845  return STATUS_ERROR;
1846  }
1847 
1848  port->keepalives_interval = interval;
1849 #else /* WIN32 */
1850  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1851 #endif
1852 #else
1853  if (interval != 0)
1854  {
1855  ereport(LOG,
1856  (errmsg("setsockopt(%s) not supported", "TCP_KEEPINTVL")));
1857  return STATUS_ERROR;
1858  }
1859 #endif
1860 
1861  return STATUS_OK;
1862 }
int keepalives_idle
Definition: libpq-be.h:168
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1781
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1159
#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:1158
SockAddr laddr
Definition: libpq-be.h:125
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1970 of file pqcomm.c.

References SockAddr::addr, Port::default_tcp_user_timeout, ereport, errmsg(), 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().

1971 {
1972  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1973  return STATUS_OK;
1974 
1975 #ifdef TCP_USER_TIMEOUT
1976  if (timeout == port->tcp_user_timeout)
1977  return STATUS_OK;
1978 
1979  if (port->default_tcp_user_timeout <= 0)
1980  {
1981  if (pq_gettcpusertimeout(port) < 0)
1982  {
1983  if (timeout == 0)
1984  return STATUS_OK; /* default is set but unknown */
1985  else
1986  return STATUS_ERROR;
1987  }
1988  }
1989 
1990  if (timeout == 0)
1991  timeout = port->default_tcp_user_timeout;
1992 
1993  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1994  (char *) &timeout, sizeof(timeout)) < 0)
1995  {
1996  ereport(LOG,
1997  (errmsg("setsockopt(%s) failed: %m", "TCP_USER_TIMEOUT")));
1998  return STATUS_ERROR;
1999  }
2000 
2001  port->tcp_user_timeout = timeout;
2002 #else
2003  if (timeout != 0)
2004  {
2005  ereport(LOG,
2006  (errmsg("setsockopt(%s) not supported", "TCP_USER_TIMEOUT")));
2007  return STATUS_ERROR;
2008  }
2009 #endif
2010 
2011  return STATUS_OK;
2012 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1159
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1940
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
#define STATUS_OK
Definition: c.h:1158
SockAddr laddr
Definition: libpq-be.h:125
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:915
int default_tcp_user_timeout
Definition: libpq-be.h:167

Variable Documentation

◆ FrontendProtocol