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 1768 of file pqcomm.c.

1769 {
1770 #ifdef TCP_KEEPCNT
1771  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1772  return 0;
1773 
1774  if (port->keepalives_count != 0)
1775  return port->keepalives_count;
1776 
1777  if (port->default_keepalives_count == 0)
1778  {
1779  socklen_t size = sizeof(port->default_keepalives_count);
1780 
1781  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1782  (char *) &port->default_keepalives_count,
1783  &size) < 0)
1784  {
1785  ereport(LOG,
1786  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1787  port->default_keepalives_count = -1; /* don't know */
1788  }
1789  }
1790 
1791  return port->default_keepalives_count;
1792 #else
1793  return 0;
1794 #endif
1795 }
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define LOG
Definition: elog.h:27
#define ereport(elevel,...)
Definition: elog.h:145
static int port
Definition: pg_regress.c:90
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 1599 of file pqcomm.c.

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

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 1684 of file pqcomm.c.

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

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 1843 of file pqcomm.c.

1844 {
1845 #ifdef TCP_USER_TIMEOUT
1846  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1847  return 0;
1848 
1849  if (port->tcp_user_timeout != 0)
1850  return port->tcp_user_timeout;
1851 
1852  if (port->default_tcp_user_timeout == 0)
1853  {
1854  socklen_t size = sizeof(port->default_tcp_user_timeout);
1855 
1856  if (getsockopt(port->sock, IPPROTO_TCP, TCP_USER_TIMEOUT,
1857  (char *) &port->default_tcp_user_timeout,
1858  &size) < 0)
1859  {
1860  ereport(LOG,
1861  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_USER_TIMEOUT")));
1862  port->default_tcp_user_timeout = -1; /* don't know */
1863  }
1864  }
1865 
1866  return port->default_tcp_user_timeout;
1867 #else
1868  return 0;
1869 #endif
1870 }

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 1798 of file pqcomm.c.

1799 {
1800  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1801  return STATUS_OK;
1802 
1803 #ifdef TCP_KEEPCNT
1804  if (count == port->keepalives_count)
1805  return STATUS_OK;
1806 
1807  if (port->default_keepalives_count <= 0)
1808  {
1809  if (pq_getkeepalivescount(port) < 0)
1810  {
1811  if (count == 0)
1812  return STATUS_OK; /* default is set but unknown */
1813  else
1814  return STATUS_ERROR;
1815  }
1816  }
1817 
1818  if (count == 0)
1819  count = port->default_keepalives_count;
1820 
1821  if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1822  (char *) &count, sizeof(count)) < 0)
1823  {
1824  ereport(LOG,
1825  (errmsg("%s(%s) failed: %m", "setsockopt", "TCP_KEEPCNT")));
1826  return STATUS_ERROR;
1827  }
1828 
1829  port->keepalives_count = count;
1830 #else
1831  if (count != 0)
1832  {
1833  ereport(LOG,
1834  (errmsg("%s(%s) not supported", "setsockopt", "TCP_KEEPCNT")));
1835  return STATUS_ERROR;
1836  }
1837 #endif
1838 
1839  return STATUS_OK;
1840 }
#define STATUS_OK
Definition: c.h:1108
#define STATUS_ERROR
Definition: c.h:1109
int pq_getkeepalivescount(Port *port)
Definition: pqcomm.c:1768

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 1634 of file pqcomm.c.

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

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 1719 of file pqcomm.c.

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

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 1873 of file pqcomm.c.

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

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

Referenced by assign_tcp_user_timeout(), and StreamConnection().

Variable Documentation

◆ FrontendProtocol

◆ MyClientConnectionInfo