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

1785 {
1786 #ifdef TCP_KEEPCNT
1787  if (port == NULL || port->laddr.addr.ss_family == AF_UNIX)
1788  return 0;
1789 
1790  if (port->keepalives_count != 0)
1791  return port->keepalives_count;
1792 
1793  if (port->default_keepalives_count == 0)
1794  {
1795  socklen_t size = sizeof(port->default_keepalives_count);
1796 
1797  if (getsockopt(port->sock, IPPROTO_TCP, TCP_KEEPCNT,
1798  (char *) &port->default_keepalives_count,
1799  &size) < 0)
1800  {
1801  ereport(LOG,
1802  (errmsg("%s(%s) failed: %m", "getsockopt", "TCP_KEEPCNT")));
1803  port->default_keepalives_count = -1; /* don't know */
1804  }
1805  }
1806 
1807  return port->default_keepalives_count;
1808 #else
1809  return 0;
1810 #endif
1811 }
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define LOG
Definition: elog.h:31
#define ereport(elevel,...)
Definition: elog.h:149
static int port
Definition: pg_regress.c:116
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 1615 of file pqcomm.c.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Referenced by assign_tcp_user_timeout(), and StreamConnection().

Variable Documentation

◆ FrontendProtocol

◆ MyClientConnectionInfo