Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2dee5e6

Browse files
committed
Port recent changes from EE
1 parent ddbebeb commit 2dee5e6

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

src/backend/port/send_sock.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ pg_send_sock(pgsocket chan, pgsocket sock, pid_t pid)
8282
memcpy(CMSG_DATA(cmsg), &sock, sizeof(sock));
8383
msg.msg_controllen = cmsg->cmsg_len;
8484

85-
if (sendmsg(chan, &msg, 0) < 0)
86-
return PGINVALID_SOCKET;
85+
while (sendmsg(chan, &msg, 0) < 0)
86+
{
87+
if (errno != EINTR)
88+
return PGINVALID_SOCKET;
89+
}
8790

8891
return 0;
8992
#endif
@@ -142,8 +145,11 @@ pg_recv_sock(pgsocket chan)
142145
msg.msg_control = c_buffer;
143146
msg.msg_controllen = sizeof(c_buffer);
144147

145-
if (recvmsg(chan, &msg, 0) < 0)
146-
return PGINVALID_SOCKET;
148+
while (recvmsg(chan, &msg, 0) < 0)
149+
{
150+
if (errno != EINTR)
151+
return PGINVALID_SOCKET;
152+
}
147153

148154
cmsg = CMSG_FIRSTHDR(&msg);
149155
if (!cmsg)

src/backend/storage/file/fd.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
* the number of open files. (This appears to be true on most if not
117117
* all platforms as of Feb 2004.)
118118
*/
119-
#define NUM_RESERVED_FDS 10
119+
#define NUM_RESERVED_FDS 20
120120

121121
/*
122122
* If we have fewer than this many usable FDs after allowing for the reserved
@@ -276,7 +276,6 @@ static int nextTempTableSpace = 0;
276276
* Insert - put a file at the front of the Lru ring
277277
* LruInsert - put a file at the front of the Lru ring and open it
278278
* ReleaseLruFile - Release an fd by closing the last entry in the Lru ring
279-
* ReleaseLruFiles - Release fd(s) until we're under the max_safe_fds limit
280279
* AllocateVfd - grab a free (or new) file record (from VfdArray)
281280
* FreeVfd - free a file record
282281
*
@@ -304,7 +303,6 @@ static void LruDelete(File file);
304303
static void Insert(File file);
305304
static int LruInsert(File file);
306305
static bool ReleaseLruFile(void);
307-
static void ReleaseLruFiles(void);
308306
static File AllocateVfd(void);
309307
static void FreeVfd(File file);
310308

@@ -1176,7 +1174,7 @@ ReleaseLruFile(void)
11761174
* Release kernel FDs as needed to get under the max_safe_fds limit.
11771175
* After calling this, it's OK to try to open another file.
11781176
*/
1179-
static void
1177+
void
11801178
ReleaseLruFiles(void)
11811179
{
11821180
while (nfile + numAllocatedDescs >= max_safe_fds)

src/backend/storage/ipc/ipc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ atexit_callback(void)
304304
void
305305
on_proc_exit(pg_on_exit_callback function, Datum arg)
306306
{
307+
int i = on_proc_exit_index;
308+
309+
while (--i >= 0)
310+
{
311+
if (on_proc_exit_list[i].function == function && on_proc_exit_list[i].arg == arg)
312+
return;
313+
}
307314
if (on_proc_exit_index >= MAX_ON_EXITS)
308315
ereport(FATAL,
309316
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),

src/backend/tcop/postgres.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,10 +3675,13 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx,
36753675
#endif
36763676
}
36773677

3678-
#define ACTIVE_SESSION_MAGIC 0xDEFA1234U
3679-
#define REMOVED_SESSION_MAGIC 0xDEADDEEDU
3678+
#define ACTIVE_SESSION_MAGIC 0xDEFA1234U
3679+
#define REMOVED_SESSION_MAGIC 0xDEADDEEDU
3680+
#define MIN_FREE_FDS 10
3681+
#define DESCRIPTORS_PER_SESSION 2
36803682

36813683
static int nActiveSessions = 0;
3684+
static int maxActiveSessions = 0;
36823685

36833686
static SessionContext *
36843687
CreateSession(void)
@@ -3694,6 +3697,23 @@ CreateSession(void)
36943697
session->magic = ACTIVE_SESSION_MAGIC;
36953698
session->eventPos = -1;
36963699
nActiveSessions += 1;
3700+
if (nActiveSessions > maxActiveSessions)
3701+
{
3702+
int new_max_safe_fds = max_safe_fds - (nActiveSessions - maxActiveSessions)*DESCRIPTORS_PER_SESSION;
3703+
if (new_max_safe_fds >= MIN_FREE_FDS)
3704+
{
3705+
max_safe_fds = new_max_safe_fds;
3706+
/* Ensure that we have enough free descriptors to establish new session.
3707+
* Unlike fd.c, which throws away lest recently used file descriptors
3708+
* only when open() call is failed, we prefer more conservative (pessimistic)
3709+
* aporach here.
3710+
*/
3711+
ReleaseLruFiles();
3712+
}
3713+
else
3714+
elog(WARNING, "Too few free file desriptors %d for %d sessions", new_max_safe_fds, maxActiveSessions);
3715+
maxActiveSessions = nActiveSessions;
3716+
}
36973717
return session;
36983718
}
36993719

@@ -3723,15 +3743,15 @@ SwitchToSession(SessionContext *session)
37233743
RestoreSessionGUCs(ActiveSession);
37243744
ActiveSession = session;
37253745

3726-
MyProcPort = ActiveSession->port;
3727-
SetTempNamespaceState(ActiveSession->tempNamespace,
3728-
ActiveSession->tempToastNamespace);
3746+
MyProcPort = session->port;
3747+
SetTempNamespaceState(session->tempNamespace,
3748+
session->tempToastNamespace);
37293749
pq_set_current_state(session->port->pqcomm_state, session->port,
37303750
session->eventSet);
37313751
whereToSendOutput = DestRemote;
37323752

3733-
RestoreSessionGUCs(ActiveSession);
3734-
LoadSessionVariables(ActiveSession);
3753+
RestoreSessionGUCs(session);
3754+
LoadSessionVariables(session);
37353755
}
37363756

37373757
static void
@@ -4369,11 +4389,12 @@ PostgresMain(int argc, char *argv[],
43694389
pgsocket sock;
43704390
MemoryContext oldcontext;
43714391

4392+
session = CreateSession();
4393+
43724394
sock = pg_recv_sock(SessionPoolSock);
43734395
if (sock == PGINVALID_SOCKET)
43744396
elog(ERROR, "Failed to receive session socket: %m");
43754397

4376-
session = CreateSession();
43774398

43784399
/* Initialize port and wait event set for this session */
43794400
oldcontext = MemoryContextSwitchTo(session->memory);
@@ -4436,11 +4457,10 @@ PostgresMain(int argc, char *argv[],
44364457
pq_sendint(&buf, (int32) MyProcPid, 4);
44374458
pq_sendint(&buf, (int32) MyCancelKey, 4);
44384459
pq_endmessage(&buf);
4439-
44404460
/* Need not flush since ReadyForQuery will do it. */
4441-
send_ready_for_query = true;
44424461

4443-
continue;
4462+
ReadyForQuery(whereToSendOutput);
4463+
goto ChooseSession;
44444464
}
44454465
else
44464466
{

src/include/storage/fd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ extern int durable_rename(const char *oldfile, const char *newfile, int loglevel
138138
extern int durable_unlink(const char *fname, int loglevel);
139139
extern int durable_link_or_rename(const char *oldfile, const char *newfile, int loglevel);
140140
extern void SyncDataDirectory(void);
141+
extern void ReleaseLruFiles(void);
141142

142143
/* Filename components */
143144
#define PG_TEMP_FILES_DIR "pgsql_tmp"

0 commit comments

Comments
 (0)