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

1775 {
1776 #ifdef TCP_KEEPCNT
1777  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1778  return 0;
1779 
1780  if (port->keepalives_count != 0)
1781  return port->keepalives_count;
1782 
1783  if (port->default_keepalives_count == 0)
1784  {
1785  socklen_t size = sizeof(port->default_keepalives_count);
1786 
1787  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1788  (char *) &port->default_keepalives_count,
1789  &size) < 0)
1790  {
1791  ereport(LOG,
1792  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1793  port->default_keepalives_count = -1; /* don't know */
1794  }
1795  }
1796 
1797  return port->default_keepalives_count;
1798 #else
1799  return 0;
1800 #endif
1801 }
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: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 1605 of file pqcomm.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Referenced by assign_tcp_user_timeout(), and StreamConnection().

Variable Documentation

◆ FrontendProtocol

◆ MyClientConnectionInfo