Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0c1682

Browse files
committed
Improve libpq documentation
1 parent7fdcd47 commitd0c1682

File tree

13 files changed

+94
-61
lines changed

13 files changed

+94
-61
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ PostgresPollingStatusType PQconnectPoll(PGconn *conn);
330330
socket underlying the database connection.
331331
Loop thus: If <function>PQconnectPoll(conn)</function> last returned
332332
<symbol>PGRES_POLLING_READING</symbol>, wait until the socket is ready to
333-
read (as indicated by <function>select()</>, <function>poll()</>, or
334-
similar systemfunction).
333+
read (as indicated by <function>PQselect()</> or
334+
<function>PQselectExtended()</>).
335335
Then call <function>PQconnectPoll(conn)</function> again.
336336
Conversely, if <function>PQconnectPoll(conn)</function> last returned
337337
<symbol>PGRES_POLLING_WRITING</symbol>, wait until the socket is ready
@@ -1879,6 +1879,42 @@ int PQsocket(const PGconn *conn);
18791879
</listitem>
18801880
</varlistentry>
18811881

1882+
<varlistentry id="libpq-pqselect">
1883+
<term><function>PQselect</function><indexterm><primary>PQselect</></></term>
1884+
<listitem>
1885+
<para>
1886+
Uses select(2) to monitor multiple file descriptors, waiting until one or
1887+
more of the file descriptors become "ready" for some class of I/O
1888+
operation (e.g., input possible).
1889+
1890+
<synopsis>
1891+
int PQselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout, int isRsocket);
1892+
</synopsis>
1893+
1894+
</para>
1895+
</listitem>
1896+
</varlistentry>
1897+
1898+
<varlistentry id="libpq-pqselectextended">
1899+
<term><function>PQselectExtended</function><indexterm><primary>PQselectExtended</></></term>
1900+
<listitem>
1901+
<para>
1902+
Uses select(2) or poll(2) to wait for input. The <literal>timeout</>
1903+
argument specifies the number of milliseconds that
1904+
<function>PQselectExtended</> should block waiting for a file descriptor
1905+
of the connection to become ready. Specifying a negative value
1906+
in <literal>timeout</> means an infinite timeout. Specifying a
1907+
<literal>timeout</> of zero causes <function>poll()</> to return
1908+
immediately.
1909+
1910+
<synopsis>
1911+
int PQselectExtended(const PGconn *conn, int timeout_ms);
1912+
</synopsis>
1913+
1914+
</para>
1915+
</listitem>
1916+
</varlistentry>
1917+
18821918
<varlistentry id="libpq-pqbackendpid">
18831919
<term><function>PQbackendPID</function><indexterm><primary>PQbackendPID</></></term>
18841920
<listitem>
@@ -4494,10 +4530,10 @@ int PQconsumeInput(PGconn *conn);
44944530
<function>PQconsumeInput</function> can be called even if the
44954531
application is not prepared to deal with a result or notification
44964532
just yet. The function will read available data and save it in
4497-
a buffer, thereby causing a <function>select()</function>
4533+
a buffer, thereby causing a <function>PQselect()</function>
44984534
read-ready indication to go away. The application can thus use
44994535
<function>PQconsumeInput</function> to clear the
4500-
<function>select()</function> condition immediately, and then
4536+
<function>PQselect()</function> condition immediately, and then
45014537
examine the results at leisure.
45024538
</para>
45034539
</listitem>
@@ -4534,10 +4570,10 @@ int PQisBusy(PGconn *conn);
45344570

45354571
<para>
45364572
A typical application using these functions will have a main loop that
4537-
uses <function>select()</function> or <function>poll()</> to wait for
4573+
uses <function>PQselect()</function> or <function>PQselectExtended()</> to wait for
45384574
all the conditions that it must respond to. One of the conditions
45394575
will be input available from the server, which in terms of
4540-
<function>select()</function> means readable data on the file
4576+
<function>PQselect()</function> means readable data on the file
45414577
descriptor identified by <function>PQsocket</function>. When the main
45424578
loop detects input ready, it should call
45434579
<function>PQconsumeInput</function> to read the input. It can then
@@ -5084,10 +5120,11 @@ typedef struct pgNotify
50845120
useful commands to execute is to call
50855121
<function>PQconsumeInput</function>, then check
50865122
<function>PQnotifies</function>. You can use
5087-
<function>select()</function> to wait for data to arrive from the
5123+
<function>PQselect()</function> or <function>PQselectExtended()</function>
5124+
to wait for data to arrive from the
50885125
server, thereby using no <acronym>CPU</acronym> power unless there is
50895126
something to do. (See <function>PQsocket</function> to obtain the file
5090-
descriptor number to use with <function>select()</function>.) Note that
5127+
descriptor number to use with <function>PQselect()</function>.) Note that
50915128
this will work OK whether you submit commands with
50925129
<function>PQsendQuery</function>/<function>PQgetResult</function> or
50935130
simply use <function>PQexec</function>. You should, however, remember
@@ -8347,12 +8384,10 @@ main(int argc, char **argv)
83478384
while (nnotifies < 4)
83488385
{
83498386
/*
8350-
* Sleep until something happens on the connection. We use select(2)
8351-
* to wait for input, but you could also use poll() or similar
8352-
* facilities.
8387+
* Sleep until something happens on the connection. We use
8388+
* PQselectExtended to wait for input.
83538389
*/
83548390
int sock;
8355-
fd_set input_mask;
83568391

83578392
sock = PQsocket(conn);
83588393

@@ -8362,9 +8397,9 @@ main(int argc, char **argv)
83628397
FD_ZERO(&input_mask);
83638398
FD_SET(sock, &input_mask);
83648399

8365-
if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
8400+
if (PQselectExtended(conn, -1) < 0)
83668401
{
8367-
fprintf(stderr, "select() failed: %s\n", strerror(errno));
8402+
fprintf(stderr, "PQselectExtended() failed: %s\n", strerror(errno));
83688403
exit_nicely(conn);
83698404
}
83708405

‎src/backend/postmaster/postmaster.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@
105105
#include"libpq/pqsignal.h"
106106
#include"miscadmin.h"
107107
#include"pg_getopt.h"
108-
#include"pg_socket.h"
109108
#include"pgstat.h"
110109
#include"postmaster/autovacuum.h"
111110
#include"postmaster/bgworker_internals.h"

‎src/backend/replication/walreceiver.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@
6767
#include"utils/resowner.h"
6868
#include"utils/timestamp.h"
6969

70+
#ifdefWITH_RSOCKET
71+
#include"pg_socket.h"
72+
#endif
73+
7074

7175
/* GUC variables */
7276
intwal_receiver_status_interval;

‎src/bin/pg_basebackup/pg_recvlogical.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include<unistd.h>
1818

1919
/* local includes */
20-
#include"pg_socket.h"
2120
#include"streamutil.h"
2221

2322
#include"access/xlog_internal.h"

‎src/bin/pg_basebackup/receivelog.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include<unistd.h>
1919

2020
/* local includes */
21-
#include"pg_socket.h"
2221
#include"receivelog.h"
2322
#include"streamutil.h"
2423

‎src/bin/pgbench/pgbench.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#endif
5454

5555
#include"pgbench.h"
56-
#include"pg_socket.h"
5756

5857
#defineERRCODE_IN_FAILED_SQL_TRANSACTION "25P02"
5958
#defineERRCODE_T_R_SERIALIZATION_FAILURE "40001"

‎src/bin/scripts/vacuumdb.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include"common.h"
1616
#include"fe_utils/simple_list.h"
1717
#include"fe_utils/string_utils.h"
18-
#include"pg_socket.h"
1918

2019

2120
#defineERRCODE_UNDEFINED_TABLE "42P01"

‎src/include/pg_socket.h

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@
1313
#ifndefPG_SOCKET_H
1414
#definePG_SOCKET_H
1515

16-
#ifndefFRONTEND
17-
#include"postgres.h"
18-
#else
19-
#include"postgres_fe.h"
20-
#endif
21-
2216
#include"pg_config.h"
2317

18+
#ifndefWIN32
2419
#include<sys/socket.h>
2520
#include<sys/time.h>
2621
#include<sys/types.h>
@@ -35,46 +30,44 @@
3530
#ifdefHAVE_SYS_SELECT_H
3631
#include<sys/select.h>
3732
#endif
33+
#else/* WIN32 */
34+
#include"win32.h"
35+
#endif/* WIN32 */
3836

3937
#ifdefWITH_RSOCKET
4038

41-
#include"port.h"
42-
4339
/* Rsocket function pointers */
4440
typedefstructPgSocketCall
4541
{
46-
pgsocket(*socket) (intdomain,inttype,intprotocol);
47-
int(*bind) (pgsocketsocket,conststructsockaddr*addr,
42+
int(*socket) (intdomain,inttype,intprotocol);
43+
int(*bind) (intsocket,conststructsockaddr*addr,
4844
socklen_taddrlen);
49-
int(*listen) (pgsocketsocket,intbacklog);
50-
int(*accept) (pgsocketsocket,structsockaddr*addr,
45+
int(*listen) (intsocket,intbacklog);
46+
int(*accept) (intsocket,structsockaddr*addr,
5147
socklen_t*addrlen);
52-
int(*connect) (pgsocketsocket,conststructsockaddr*addr,
48+
int(*connect) (intsocket,conststructsockaddr*addr,
5349
socklen_taddrlen);
54-
int(*close) (pgsocketsocket);
50+
int(*close) (intsocket);
5551

56-
ssize_t(*recv) (pgsocketsocket,void*buf,size_tlen,intflags);
57-
ssize_t(*send) (pgsocketsocket,constvoid*buf,size_tlen,
52+
ssize_t(*recv) (intsocket,void*buf,size_tlen,intflags);
53+
ssize_t(*send) (intsocket,constvoid*buf,size_tlen,
5854
intflags);
59-
ssize_t(*sendmsg) (pgsocketsocket,conststructmsghdr*msg,
60-
intflags);
55+
ssize_t(*sendmsg) (intsocket,conststructmsghdr*msg,intflags);
6156

6257
#ifdefHAVE_POLL
6358
int(*poll) (structpollfd*fds,nfds_tnfds,inttimeout);
6459
#endif
65-
int(*select) (pgsocketnfds,fd_set*readfds,fd_set*writefds,
60+
int(*select) (intnfds,fd_set*readfds,fd_set*writefds,
6661
fd_set*exceptfds,structtimeval*timeout);
6762

68-
int(*getsockname) (pgsocketsocket,structsockaddr*addr,
63+
int(*getsockname) (intsocket,structsockaddr*addr,
6964
socklen_t*addrlen);
70-
int(*setsockopt) (pgsocketsocket,intlevel,intoptname,
65+
int(*setsockopt) (intsocket,intlevel,intoptname,
7166
constvoid*optval,socklen_toptlen);
72-
int(*getsockopt) (pgsocketsocket,intlevel,intoptname,
67+
int(*getsockopt) (intsocket,intlevel,intoptname,
7368
void*optval,socklen_t*optlen);
7469

75-
#if !defined(WIN32)
76-
int(*fcntl) (pgsocketsocket,intcmd, .../* arg */ );
77-
#endif
70+
int(*fcntl) (intsocket,intcmd, .../* arg */ );
7871
}PgSocketCall;
7972

8073
externPgSocketCall*rcalls;
@@ -109,7 +102,7 @@ extern PgSocketCall *rcalls;
109102

110103
#definepg_closesocket(socket,isRsocket) \
111104
((isRsocket) ? rcalls->close(socket) : \
112-
close(socket))
105+
closesocket(socket))
113106

114107
#definepg_recv(socket,buf,len,flags,isRsocket) \
115108
((isRsocket) ? rcalls->recv(socket, buf, len, flags) : \
@@ -145,10 +138,15 @@ extern PgSocketCall *rcalls;
145138
((isRsocket) ? rcalls->getsockopt(socket, level, optname, optval, optlen) : \
146139
getsockopt(socket, level, optname, optval, optlen))
147140

141+
/* port/pg_rsocket.c */
142+
externvoidinitialize_rsocket(void);
143+
148144
#else
149145

146+
#if !defined(WIN32)
150147
#definepg_fcntl(fd,flag,value,isRsocket) \
151148
fcntl(fd, flag, value)
149+
#endif
152150

153151
#definepg_socket(domain,type,protocol,isRsocket) \
154152
socket(domain, type, protocol)
@@ -166,16 +164,18 @@ extern PgSocketCall *rcalls;
166164
connect(socket, addr, addrlen)
167165

168166
#definepg_closesocket(socket,isRsocket) \
169-
close(socket)
167+
closesocket(socket)
170168

171169
#definepg_recv(socket,buf,len,flags,isRsocket) \
172170
recv(socket, buf, len, flags)
173171

174172
#definepg_send(socket,buf,len,flags,isRsocket) \
175173
send(socket, buf, len, flags)
176174

175+
#if !defined(WIN32)
177176
#definepg_sendmsg(socket,msg,flags,isRsocket) \
178177
sendmsg(socket, msg, flags)
178+
#endif
179179

180180
#ifdefHAVE_POLL
181181
#definepg_poll(fds,nfds,timeout,isRsocket) \

‎src/include/port.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,4 @@ extern char *escape_single_quotes_ascii(const char *src);
489489
/* port/wait_error.c */
490490
externchar*wait_result_to_str(intexit_status);
491491

492-
/* port/pg_rsocket.c */
493-
externvoidinitialize_rsocket(void);
494-
495492
#endif/* PG_PORT_H */

‎src/interfaces/libpq/fe-auth.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include"fe-auth.h"
4343
#include"libpq/scram.h"
4444
#include"libpq/md5.h"
45+
#include"pg_socket.h"
4546

4647

4748
#ifdefENABLE_GSS

‎src/interfaces/libpq/fe-connect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6636,8 +6636,8 @@ PQselectExtended(const PGconn *conn, int timeout_ms)
66366636
ptr_timeout=&timeout;
66376637
}
66386638

6639-
ret=pg_select(PQsocket(conn)+1,&input_mask,
6640-
NULL,NULL,ptr_timeout,PQisRsocket(conn));
6639+
returnpg_select(PQsocket(conn)+1,&input_mask,
6640+
NULL,NULL,ptr_timeout,PQisRsocket(conn));
66416641
#endif/* HAVE_POLL */
66426642
}
66436643

‎src/interfaces/libpq/libpq-fe.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ extern"C"
2222

2323
#include<stdio.h>
2424

25+
#ifndefWIN32
26+
#include<sys/time.h>
27+
#else
28+
#include<winsock2.h>
29+
#endif
30+
2531
/*
2632
* postgres_ext.h defines the backend's externally visible types,
2733
* such as Oid.
@@ -326,7 +332,7 @@ extern intPQserverVersion(const PGconn *conn);
326332
externchar*PQerrorMessage(constPGconn*conn);
327333
externintPQsocket(constPGconn*conn);
328334
externintPQisRsocket(constPGconn*conn);
329-
externintPQselect(pgsocketnfds,fd_set*readfds,fd_set*writefds,
335+
externintPQselect(intnfds,fd_set*readfds,fd_set*writefds,
330336
fd_set*exceptfds,structtimeval*timeout,intisRsocket);
331337
externintPQselectExtended(constPGconn*conn,inttimeout_ms);
332338
externintPQbackendPID(constPGconn*conn);

‎src/test/examples/testlibpq2.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,19 @@ main(int argc, char **argv)
9999
while (nnotifies<4)
100100
{
101101
/*
102-
* Sleep until something happens on the connection. We use select(2)
103-
* to wait for input, but you could also use poll() or similar
104-
* facilities.
102+
* Sleep until something happens on the connection. We use
103+
* PQselectExtended to wait for input.
105104
*/
106105
intsock;
107-
fd_setinput_mask;
108106

109107
sock=PQsocket(conn);
110108

111109
if (sock<0)
112110
break;/* shouldn't happen */
113111

114-
FD_ZERO(&input_mask);
115-
FD_SET(sock,&input_mask);
116-
117-
if (select(sock+1,&input_mask,NULL,NULL,NULL)<0)
112+
if (PQselectExtended(conn,-1)<0)
118113
{
119-
fprintf(stderr,"select() failed: %s\n",strerror(errno));
114+
fprintf(stderr,"PQselectExtended() failed: %s\n",strerror(errno));
120115
exit_nicely(conn);
121116
}
122117

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp