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_NOTCONSISTENT, 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_NOTCONSISTENT 
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 1780 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().

1781 {
1782 #ifdef TCP_KEEPCNT
1783  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1784  return 0;
1785 
1786  if (port->keepalives_count != 0)
1787  return port->keepalives_count;
1788 
1789  if (port->default_keepalives_count == 0)
1790  {
1791  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_count);
1792 
1793  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1794  (char *) &port->default_keepalives_count,
1795  &size) < 0)
1796  {
1797  ereport(LOG,
1798  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1799  port->default_keepalives_count = -1; /* don't know */
1800  }
1801  }
1802 
1803  return port->default_keepalives_count;
1804 #else
1805  return 0;
1806 #endif
1807 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:184
pgsocket sock
Definition: libpq-be.h:127
int keepalives_count
Definition: libpq-be.h:188
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

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

1612 {
1613 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1614  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1615  return 0;
1616 
1617  if (port->keepalives_idle != 0)
1618  return port->keepalives_idle;
1619 
1620  if (port->default_keepalives_idle == 0)
1621  {
1622 #ifndef WIN32
1623  ACCEPT_TYPE_ARG3 size = sizeof(port->default_keepalives_idle);
1624 
1625  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1626  (char *) &port->default_keepalives_idle,
1627  &size) < 0)
1628  {
1629  ereport(LOG,
1630  (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1631  port->default_keepalives_idle = -1; /* don't know */
1632  }
1633 #else /* WIN32 */
1634  /* We can't get the defaults on Windows, so return "don't know" */
1635  port->default_keepalives_idle = -1;
1636 #endif /* WIN32 */
1637  }
1638 
1639  return port->default_keepalives_idle;
1640 #else
1641  return 0;
1642 #endif
1643 }
int keepalives_idle
Definition: libpq-be.h:186
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
SockAddr laddr
Definition: libpq-be.h:130
int default_keepalives_idle
Definition: libpq-be.h:182
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

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

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

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

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

1856 {
1857 #ifdef TCP_USER_TIMEOUT
1858  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1859  return 0;
1860 
1861  if (port->tcp_user_timeout != 0)
1862  return port->tcp_user_timeout;
1863 
1864  if (port->default_tcp_user_timeout == 0)
1865  {
1866  ACCEPT_TYPE_ARG3 size = sizeof(port->default_tcp_user_timeout);
1867 
1868  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1869  (char *) &port->default_tcp_user_timeout,
1870  &size) < 0)
1871  {
1872  ereport(LOG,
1873  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
1874  port->default_tcp_user_timeout = -1; /* don't know */
1875  }
1876  }
1877 
1878  return port->default_tcp_user_timeout;
1879 #else
1880  return 0;
1881 #endif
1882 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:189
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int default_tcp_user_timeout
Definition: libpq-be.h:185

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

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

1811 {
1812  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1813  return STATUS_OK;
1814 
1815 #ifdef TCP_KEEPCNT
1816  if (count == port->keepalives_count)
1817  return STATUS_OK;
1818 
1819  if (port->default_keepalives_count <= 0)
1820  {
1821  if (pq_getkeepalivescount(port) < 0)
1822  {
1823  if (count == 0)
1824  return STATUS_OK; /* default is set but unknown */
1825  else
1826  return STATUS_ERROR;
1827  }
1828  }
1829 
1830  if (count == 0)
1831  count = port->default_keepalives_count;
1832 
1833  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1834  (char *) &count, sizeof(count)) < 0)
1835  {
1836  ereport(LOG,
1837  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
1838  return STATUS_ERROR;
1839  }
1840 
1841  port->keepalives_count = count;
1842 #else
1843  if (count != 0)
1844  {
1845  ereport(LOG,
1846  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1847  return STATUS_ERROR;
1848  }
1849 #endif
1850 
1851  return STATUS_OK;
1852 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
int default_keepalives_count
Definition: libpq-be.h:184
pgsocket sock
Definition: libpq-be.h:127
int keepalives_count
Definition: libpq-be.h:188
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1780
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

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

1647 {
1648  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1649  return STATUS_OK;
1650 
1651 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1652 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1653  if (idle == port->keepalives_idle)
1654  return STATUS_OK;
1655 
1656 #ifndef WIN32
1657  if (port->default_keepalives_idle <= 0)
1658  {
1659  if (pq_getkeepalivesidle(port) < 0)
1660  {
1661  if (idle == 0)
1662  return STATUS_OK; /* default is set but unknown */
1663  else
1664  return STATUS_ERROR;
1665  }
1666  }
1667 
1668  if (idle == 0)
1669  idle = port->default_keepalives_idle;
1670 
1671  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1672  (char *) &idle, sizeof(idle)) < 0)
1673  {
1674  ereport(LOG,
1675  (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1676  return STATUS_ERROR;
1677  }
1678 
1679  port->keepalives_idle = idle;
1680 #else /* WIN32 */
1681  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1682 #endif
1683 #else
1684  if (idle != 0)
1685  {
1686  ereport(LOG,
1687  (errmsg("setting the keepalive idle time is not supported")));
1688  return STATUS_ERROR;
1689  }
1690 #endif
1691 
1692  return STATUS_OK;
1693 }
int keepalives_idle
Definition: libpq-be.h:186
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
int default_keepalives_idle
Definition: libpq-be.h:182
#define ereport(elevel,...)
Definition: elog.h:157
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1611
int errmsg(const char *fmt,...)
Definition: elog.c:909
int keepalives_interval
Definition: libpq-be.h:187

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

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

1732 {
1733  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1734  return STATUS_OK;
1735 
1736 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1737  if (interval == port->keepalives_interval)
1738  return STATUS_OK;
1739 
1740 #ifndef WIN32
1741  if (port->default_keepalives_interval <= 0)
1742  {
1743  if (pq_getkeepalivesinterval(port) < 0)
1744  {
1745  if (interval == 0)
1746  return STATUS_OK; /* default is set but unknown */
1747  else
1748  return STATUS_ERROR;
1749  }
1750  }
1751 
1752  if (interval == 0)
1754 
1755  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1756  (char *) &interval, sizeof(interval)) < 0)
1757  {
1758  ereport(LOG,
1759  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
1760  return STATUS_ERROR;
1761  }
1762 
1763  port->keepalives_interval = interval;
1764 #else /* WIN32 */
1765  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1766 #endif
1767 #else
1768  if (interval != 0)
1769  {
1770  ereport(LOG,
1771  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
1772  return STATUS_ERROR;
1773  }
1774 #endif
1775 
1776  return STATUS_OK;
1777 }
int keepalives_idle
Definition: libpq-be.h:186
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1696
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
int default_keepalives_interval
Definition: libpq-be.h:183
pgsocket sock
Definition: libpq-be.h:127
#define IS_AF_UNIX(fam)
Definition: ip.h:24
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int keepalives_interval
Definition: libpq-be.h:187

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

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

1886 {
1887  if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
1888  return STATUS_OK;
1889 
1890 #ifdef TCP_USER_TIMEOUT
1891  if (timeout == port->tcp_user_timeout)
1892  return STATUS_OK;
1893 
1894  if (port->default_tcp_user_timeout <= 0)
1895  {
1896  if (pq_gettcpusertimeout(port) < 0)
1897  {
1898  if (timeout == 0)
1899  return STATUS_OK; /* default is set but unknown */
1900  else
1901  return STATUS_ERROR;
1902  }
1903  }
1904 
1905  if (timeout == 0)
1906  timeout = port->default_tcp_user_timeout;
1907 
1908  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1909  (char *) &timeout, sizeof(timeout)) < 0)
1910  {
1911  ereport(LOG,
1912  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
1913  return STATUS_ERROR;
1914  }
1915 
1916  port->tcp_user_timeout = timeout;
1917 #else
1918  if (timeout != 0)
1919  {
1920  ereport(LOG,
1921  (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
1922  return STATUS_ERROR;
1923  }
1924 #endif
1925 
1926  return STATUS_OK;
1927 }
struct sockaddr_storage addr
Definition: pqcomm.h:64
#define STATUS_ERROR
Definition: c.h:1171
#define LOG
Definition: elog.h:26
pgsocket sock
Definition: libpq-be.h:127
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1855
#define IS_AF_UNIX(fam)
Definition: ip.h:24
int tcp_user_timeout
Definition: libpq-be.h:189
#define STATUS_OK
Definition: c.h:1170
SockAddr laddr
Definition: libpq-be.h:130
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int default_tcp_user_timeout
Definition: libpq-be.h:185

Variable Documentation

◆ FrontendProtocol