PostgreSQL Source Code  git master
libpq-be.h File Reference
#include <sys/time.h>
#include <netinet/tcp.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  ClientConnectionInfo
 
struct  Port
 

Typedefs

typedef enum CAC_state CAC_state
 
typedef struct ClientConnectionInfo ClientConnectionInfo
 
typedef struct Port Port
 

Enumerations

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

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

PGDLLIMPORT ProtocolVersion FrontendProtocol
 
PGDLLIMPORT ClientConnectionInfo MyClientConnectionInfo
 

Typedef Documentation

◆ CAC_state

typedef enum CAC_state CAC_state

◆ ClientConnectionInfo

◆ 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 

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

62 {
63  CAC_OK,
69 } CAC_state;
CAC_state
Definition: libpq-be.h:62
@ CAC_TOOMANY
Definition: libpq-be.h:68
@ CAC_OK
Definition: libpq-be.h:63
@ CAC_RECOVERY
Definition: libpq-be.h:66
@ CAC_NOTCONSISTENT
Definition: libpq-be.h:67
@ CAC_STARTUP
Definition: libpq-be.h:64
@ CAC_SHUTDOWN
Definition: libpq-be.h:65

Function Documentation

◆ pq_getkeepalivescount()

int pq_getkeepalivescount ( Port port)

Definition at line 1778 of file pqcomm.c.

1779 {
1780 #ifdef TCP_KEEPCNT
1781  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1782  return 0;
1783 
1784  if (port->keepalives_count != 0)
1785  return port->keepalives_count;
1786 
1787  if (port->default_keepalives_count == 0)
1788  {
1789  socklen_t size = sizeof(port->default_keepalives_count);
1790 
1791  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1792  (char *) &port->default_keepalives_count,
1793  &size) < 0)
1794  {
1795  ereport(LOG,
1796  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1797  port->default_keepalives_count = -1; /* don't know */
1798  }
1799  }
1800 
1801  return port->default_keepalives_count;
1802 #else
1803  return 0;
1804 #endif
1805 }
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
static int port
Definition: pg_regress.c:109
unsigned int socklen_t
Definition: port.h:40

References ereport, errmsg(), LOG, and port.

Referenced by pq_setkeepalivescount(), and show_tcp_keepalives_count().

◆ pq_getkeepalivesidle()

int pq_getkeepalivesidle ( Port port)

Definition at line 1609 of file pqcomm.c.

1610 {
1611 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1612  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1613  return 0;
1614 
1615  if (port->keepalives_idle != 0)
1616  return port->keepalives_idle;
1617 
1618  if (port->default_keepalives_idle == 0)
1619  {
1620 #ifndef WIN32
1621  socklen_t size = sizeof(port->default_keepalives_idle);
1622 
1623  if (getsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1624  (char *) &port->default_keepalives_idle,
1625  &size) < 0)
1626  {
1627  ereport(LOG,
1628  (errmsg("%s(%s) failed: %m", "getsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1629  port->default_keepalives_idle = -1; /* don't know */
1630  }
1631 #else /* WIN32 */
1632  /* We can't get the defaults on Windows, so return "don't know" */
1633  port->default_keepalives_idle = -1;
1634 #endif /* WIN32 */
1635  }
1636 
1637  return port->default_keepalives_idle;
1638 #else
1639  return 0;
1640 #endif
1641 }

References ereport, errmsg(), LOG, and port.

Referenced by pq_setkeepalivesidle(), and show_tcp_keepalives_idle().

◆ pq_getkeepalivesinterval()

int pq_getkeepalivesinterval ( Port port)

Definition at line 1694 of file pqcomm.c.

1695 {
1696 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1697  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1698  return 0;
1699 
1700  if (port->keepalives_interval != 0)
1701  return port->keepalives_interval;
1702 
1703  if (port->default_keepalives_interval == 0)
1704  {
1705 #ifndef WIN32
1706  socklen_t size = sizeof(port->default_keepalives_interval);
1707 
1708  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1709  (char *) &port->default_keepalives_interval,
1710  &size) < 0)
1711  {
1712  ereport(LOG,
1713  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPINTVL")));
1714  port->default_keepalives_interval = -1; /* don't know */
1715  }
1716 #else
1717  /* We can't get the defaults on Windows, so return "don't know" */
1718  port->default_keepalives_interval = -1;
1719 #endif /* WIN32 */
1720  }
1721 
1722  return port->default_keepalives_interval;
1723 #else
1724  return 0;
1725 #endif
1726 }

References ereport, errmsg(), LOG, and port.

Referenced by pq_setkeepalivesinterval(), and show_tcp_keepalives_interval().

◆ pq_gettcpusertimeout()

int pq_gettcpusertimeout ( Port port)

Definition at line 1853 of file pqcomm.c.

1854 {
1855 #ifdef TCP_USER_TIMEOUT
1856  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1857  return 0;
1858 
1859  if (port->tcp_user_timeout != 0)
1860  return port->tcp_user_timeout;
1861 
1862  if (port->default_tcp_user_timeout == 0)
1863  {
1864  socklen_t size = sizeof(port->default_tcp_user_timeout);
1865 
1866  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1867  (char *) &port->default_tcp_user_timeout,
1868  &size) < 0)
1869  {
1870  ereport(LOG,
1871  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
1872  port->default_tcp_user_timeout = -1; /* don't know */
1873  }
1874  }
1875 
1876  return port->default_tcp_user_timeout;
1877 #else
1878  return 0;
1879 #endif
1880 }

References ereport, errmsg(), LOG, and port.

Referenced by pq_settcpusertimeout(), and show_tcp_user_timeout().

◆ pq_setkeepalivescount()

int pq_setkeepalivescount ( int  count,
Port port 
)

Definition at line 1808 of file pqcomm.c.

1809 {
1810  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1811  return STATUS_OK;
1812 
1813 #ifdef TCP_KEEPCNT
1814  if (count == port->keepalives_count)
1815  return STATUS_OK;
1816 
1817  if (port->default_keepalives_count <= 0)
1818  {
1819  if (pq_getkeepalivescount(port) < 0)
1820  {
1821  if (count == 0)
1822  return STATUS_OK; /* default is set but unknown */
1823  else
1824  return STATUS_ERROR;
1825  }
1826  }
1827 
1828  if (count == 0)
1829  count = port->default_keepalives_count;
1830 
1831  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1832  (char *) &count, sizeof(count)) < 0)
1833  {
1834  ereport(LOG,
1835  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
1836  return STATUS_ERROR;
1837  }
1838 
1839  port->keepalives_count = count;
1840 #else
1841  if (count != 0)
1842  {
1843  ereport(LOG,
1844  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1845  return STATUS_ERROR;
1846  }
1847 #endif
1848 
1849  return STATUS_OK;
1850 }
#define STATUS_OK
Definition: c.h:1182
#define STATUS_ERROR
Definition: c.h:1183
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1778

References ereport, errmsg(), LOG, port, pq_getkeepalivescount(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_count(), and StreamConnection().

◆ pq_setkeepalivesidle()

int pq_setkeepalivesidle ( int  idle,
Port port 
)

Definition at line 1644 of file pqcomm.c.

1645 {
1646  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1647  return STATUS_OK;
1648 
1649 /* check SIO_KEEPALIVE_VALS here, not just WIN32, as some toolchains lack it */
1650 #if defined(PG_TCP_KEEPALIVE_IDLE) || defined(SIO_KEEPALIVE_VALS)
1651  if (idle == port->keepalives_idle)
1652  return STATUS_OK;
1653 
1654 #ifndef WIN32
1655  if (port->default_keepalives_idle <= 0)
1656  {
1657  if (pq_getkeepalivesidle(port) < 0)
1658  {
1659  if (idle == 0)
1660  return STATUS_OK; /* default is set but unknown */
1661  else
1662  return STATUS_ERROR;
1663  }
1664  }
1665 
1666  if (idle == 0)
1667  idle = port->default_keepalives_idle;
1668 
1669  if (setsockopt(port->sock, IPPROTO_TCP, PG_TCP_KEEPALIVE_IDLE,
1670  (char *) &idle, sizeof(idle)) < 0)
1671  {
1672  ereport(LOG,
1673  (errmsg("%s(%s) failed: %m", "setsockopt", PG_TCP_KEEPALIVE_IDLE_STR)));
1674  return STATUS_ERROR;
1675  }
1676 
1677  port->keepalives_idle = idle;
1678 #else /* WIN32 */
1679  return pq_setkeepaliveswin32(port, idle, port->keepalives_interval);
1680 #endif
1681 #else
1682  if (idle != 0)
1683  {
1684  ereport(LOG,
1685  (errmsg("setting the keepalive idle time is not supported")));
1686  return STATUS_ERROR;
1687  }
1688 #endif
1689 
1690  return STATUS_OK;
1691 }
int pq_getkeepalivesidle(Port *port)
Definition: pqcomm.c:1609

References ereport, errmsg(), LOG, port, pq_getkeepalivesidle(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_idle(), and StreamConnection().

◆ pq_setkeepalivesinterval()

int pq_setkeepalivesinterval ( int  interval,
Port port 
)

Definition at line 1729 of file pqcomm.c.

1730 {
1731  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1732  return STATUS_OK;
1733 
1734 #if defined(TCP_KEEPINTVL) || defined(SIO_KEEPALIVE_VALS)
1735  if (interval == port->keepalives_interval)
1736  return STATUS_OK;
1737 
1738 #ifndef WIN32
1739  if (port->default_keepalives_interval <= 0)
1740  {
1741  if (pq_getkeepalivesinterval(port) < 0)
1742  {
1743  if (interval == 0)
1744  return STATUS_OK; /* default is set but unknown */
1745  else
1746  return STATUS_ERROR;
1747  }
1748  }
1749 
1750  if (interval == 0)
1751  interval = port->default_keepalives_interval;
1752 
1753  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPINTVL,
1754  (char *) &interval, sizeof(interval)) < 0)
1755  {
1756  ereport(LOG,
1757  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPINTVL")));
1758  return STATUS_ERROR;
1759  }
1760 
1761  port->keepalives_interval = interval;
1762 #else /* WIN32 */
1763  return pq_setkeepaliveswin32(port, port->keepalives_idle, interval);
1764 #endif
1765 #else
1766  if (interval != 0)
1767  {
1768  ereport(LOG,
1769  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPINTVL")));
1770  return STATUS_ERROR;
1771  }
1772 #endif
1773 
1774  return STATUS_OK;
1775 }
int pq_getkeepalivesinterval(Port *port)
Definition: pqcomm.c:1694

References ereport, errmsg(), LOG, port, pq_getkeepalivesinterval(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_keepalives_interval(), and StreamConnection().

◆ pq_settcpusertimeout()

int pq_settcpusertimeout ( int  timeout,
Port port 
)

Definition at line 1883 of file pqcomm.c.

1884 {
1885  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1886  return STATUS_OK;
1887 
1888 #ifdef TCP_USER_TIMEOUT
1889  if (timeout == port->tcp_user_timeout)
1890  return STATUS_OK;
1891 
1892  if (port->default_tcp_user_timeout <= 0)
1893  {
1894  if (pq_gettcpusertimeout(port) < 0)
1895  {
1896  if (timeout == 0)
1897  return STATUS_OK; /* default is set but unknown */
1898  else
1899  return STATUS_ERROR;
1900  }
1901  }
1902 
1903  if (timeout == 0)
1904  timeout = port->default_tcp_user_timeout;
1905 
1906  if (setsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1907  (char *) &timeout, sizeof(timeout)) < 0)
1908  {
1909  ereport(LOG,
1910  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_USER_TIMEOUT")));
1911  return STATUS_ERROR;
1912  }
1913 
1914  port->tcp_user_timeout = timeout;
1915 #else
1916  if (timeout != 0)
1917  {
1918  ereport(LOG,
1919  (errmsg("%s(%s) not supported", "setsockopt", "TCP_USER_TIMEOUT")));
1920  return STATUS_ERROR;
1921  }
1922 #endif
1923 
1924  return STATUS_OK;
1925 }
int pq_gettcpusertimeout(Port *port)
Definition: pqcomm.c:1853

References ereport, errmsg(), LOG, port, pq_gettcpusertimeout(), STATUS_ERROR, and STATUS_OK.

Referenced by assign_tcp_user_timeout(), and StreamConnection().

Variable Documentation

◆ FrontendProtocol

◆ MyClientConnectionInfo