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 1846 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().

1847 {
1848 #ifdef TCP_KEEPCNT
1849  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1850  return 0;
1851 
1852  if (port->keepalives_count != 0)
1853  return port->keepalives_count;
1854 
1855  if (port->default_keepalives_count == 0)
1856  {
1857  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1858 
1859  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1860  (char *) &port->default_keepalives_count,
1861  &size) < 0)
1862  {
1863  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPCNT");
1864  port->default_keepalives_count = -1; /* don't know */
1865  }
1866  }
1867 
1868  return port->default_keepalives_count;
1869 #else
1870  return 0;
1871 #endif
1872 }
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:214

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1683 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().

1684 {
1685 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1686  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1687  return 0;
1688 
1689  if (port->keepalives_idle != 0)
1690  return port->keepalives_idle;
1691 
1692  if (port->default_keepalives_idle == 0)
1693  {
1694 #ifndef WIN32
1695  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1696 
1697  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1698  (char *) &port->default_keepalives_idle,
1699  &size) < 0)
1700  {
1701  elog(LOG, "getsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1702  port->default_keepalives_idle = -1; /* don't know */
1703  }
1704 #else /* WIN32 */
1705  /* We can't get the defaults on Windows, so return "don't know" */
1706  port->default_keepalives_idle = -1;
1707 #endif /* WIN32 */
1708  }
1709 
1710  return port->default_keepalives_idle;
1711 #else
1712  return 0;
1713 #endif
1714 }
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:214

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1765 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().

1766 {
1767 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1768  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1769  return 0;
1770 
1771  if (port->keepalives_interval != 0)
1772  return port->keepalives_interval;
1773 
1774  if (port->default_keepalives_interval == 0)
1775  {
1776 #ifndef WIN32
1777  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_interval);
1778 
1779  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1780  (char *) &port->default_keepalives_interval,
1781  &size) < 0)
1782  {
1783  elog(LOG, "getsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1784  port->default_keepalives_interval = -1; /* don't know */
1785  }
1786 #else
1787  /* We can't get the defaults on Windows, so return "don't know" */
1788  port->default_keepalives_interval = -1;
1789 #endif /* WIN32 */
1790  }
1791 
1792  return port->default_keepalives_interval;
1793 #else
1794  return 0;
1795 #endif
1796 }
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:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1918 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().

1919 {
1920 #ifdef TCP_USER_TIMEOUT
1921  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1922  return 0;
1923 
1924  if (port->tcp_user_timeout != 0)
1925  return port->tcp_user_timeout;
1926 
1927  if (port->default_tcp_user_timeout == 0)
1928  {
1929  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1930 
1931  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1932  (char *) &port->default_tcp_user_timeout,
1933  &size) < 0)
1934  {
1935  elog(LOG, "getsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1936  port->default_tcp_user_timeout = -1; /* don't know */
1937  }
1938  }
1939 
1940  return port->default_tcp_user_timeout;
1941 #else
1942  return 0;
1943 #endif
1944 }
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:214
int default_tcp_user_timeout
Definition: libpq-be.h:167

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1875 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().

1876 {
1877  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1878  return STATUS_OK;
1879 
1880 #ifdef TCP_KEEPCNT
1881  if (count == port->keepalives_count)
1882  return STATUS_OK;
1883 
1884  if (port->default_keepalives_count <= 0)
1885  {
1886  if (pq_getkeepalivescount(port) < 0)
1887  {
1888  if (count == 0)
1889  return STATUS_OK; /* default is set but unknown */
1890  else
1891  return STATUS_ERROR;
1892  }
1893  }
1894 
1895  if (count == 0)
1896  count = port->default_keepalives_count;
1897 
1898  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1899  (char *) &count, sizeof(count)) < 0)
1900  {
1901  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPCNT");
1902  return STATUS_ERROR;
1903  }
1904 
1905  port->keepalives_count = count;
1906 #else
1907  if (count != 0)
1908  {
1909  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPCNT");
1910  return STATUS_ERROR;
1911  }
1912 #endif
1913 
1914  return STATUS_OK;
1915 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1141
#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:1140
SockAddr laddr
Definition: libpq-be.h:125
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1846
#define elog(elevel,...)
Definition: elog.h:214

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1717 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().

1718 {
1719  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1720  return STATUS_OK;
1721 
1722 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1723 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1724  if (idle == port->keepalives_idle)
1725  return STATUS_OK;
1726 
1727 #ifndef WIN32
1728  if (port->default_keepalives_idle <= 0)
1729  {
1730  if (pq_getkeepalivesidle(port) < 0)
1731  {
1732  if (idle == 0)
1733  return STATUS_OK; /* default is set but unknown */
1734  else
1735  return STATUS_ERROR;
1736  }
1737  }
1738 
1739  if (idle == 0)
1740  idle = port->default_keepalives_idle;
1741 
1742  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1743  (char *) &idle, sizeof(idle)) < 0)
1744  {
1745  elog(LOG, "setsockopt(%s) failed: %m", PG_TCP_KEEPALIVE_IDLE_STR);
1746  return STATUS_ERROR;
1747  }
1748 
1749  port->keepalives_idle = idle;
1750 #else /* WIN32 */
1751  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1752 #endif
1753 #else
1754  if (idle != 0)
1755  {
1756  elog(LOG, "setting the keepalive idle time is not supported");
1757  return STATUS_ERROR;
1758  }
1759 #endif
1760 
1761  return STATUS_OK;
1762 }
int keepalives_idle
Definition: libpq-be.h:168
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1141
#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:1140
SockAddr laddr
Definition: libpq-be.h:125
int default_keepalives_idle
Definition: libpq-be.h:164
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1683
#define elog(elevel,...)
Definition: elog.h:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1799 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().

1800 {
1801  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1802  return STATUS_OK;
1803 
1804 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1805  if (interval == port->keepalives_interval)
1806  return STATUS_OK;
1807 
1808 #ifndef WIN32
1809  if (port->default_keepalives_interval <= 0)
1810  {
1811  if (pq_getkeepalivesinterval(port) < 0)
1812  {
1813  if (interval == 0)
1814  return STATUS_OK; /* default is set but unknown */
1815  else
1816  return STATUS_ERROR;
1817  }
1818  }
1819 
1820  if (interval == 0)
1822 
1823  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1824  (char *) &interval, sizeof(interval)) < 0)
1825  {
1826  elog(LOG, "setsockopt(%s) failed: %m", "TCP_KEEPINTVL");
1827  return STATUS_ERROR;
1828  }
1829 
1830  port->keepalives_interval = interval;
1831 #else /* WIN32 */
1832  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1833 #endif
1834 #else
1835  if (interval != 0)
1836  {
1837  elog(LOG, "setsockopt(%s) not supported", "TCP_KEEPINTVL");
1838  return STATUS_ERROR;
1839  }
1840 #endif
1841 
1842  return STATUS_OK;
1843 }
int keepalives_idle
Definition: libpq-be.h:168
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1765
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1141
#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:1140
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int keepalives_interval
Definition: libpq-be.h:169

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1947 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().

1948 {
1949  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1950  return STATUS_OK;
1951 
1952 #ifdef TCP_USER_TIMEOUT
1953  if (timeout == port->tcp_user_timeout)
1954  return STATUS_OK;
1955 
1956  if (port->default_tcp_user_timeout <= 0)
1957  {
1958  if (pq_gettcpusertimeout(port) < 0)
1959  {
1960  if (timeout == 0)
1961  return STATUS_OK; /* default is set but unknown */
1962  else
1963  return STATUS_ERROR;
1964  }
1965  }
1966 
1967  if (timeout == 0)
1968  timeout = port->default_tcp_user_timeout;
1969 
1970  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1971  (char *) &timeout, sizeof(timeout)) < 0)
1972  {
1973  elog(LOG, "setsockopt(%s) failed: %m", "TCP_USER_TIMEOUT");
1974  return STATUS_ERROR;
1975  }
1976 
1977  port->tcp_user_timeout = timeout;
1978 #else
1979  if (timeout != 0)
1980  {
1981  elog(LOG, "setsockopt(%s) not supported", "TCP_USER_TIMEOUT");
1982  return STATUS_ERROR;
1983  }
1984 #endif
1985 
1986  return STATUS_OK;
1987 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1141
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:122
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1918
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:171
#define STATUS_OK
Definition: c.h:1140
SockAddr laddr
Definition: libpq-be.h:125
#define elog(elevel,...)
Definition: elog.h:214
int default_tcp_user_timeout
Definition: libpq-be.h:167

Variable Documentation

◆ FrontendProtocol