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

Commit 5e7a5c9

Browse files
committed
Pass shared memory address on command line to exec'ed backend.
Allow backends to attached to specified shared memory address.
1 parent 4e08d35 commit 5e7a5c9

File tree

5 files changed

+78
-47
lines changed

5 files changed

+78
-47
lines changed

src/backend/bootstrap/bootstrap.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.155 2003/05/06 23:34:55 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/bootstrap/bootstrap.c,v 1.156 2003/05/08 14:49:03 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -286,10 +286,13 @@ BootstrapMain(int argc, char *argv[])
286286
case 'p':
287287
{
288288
/* indicates fork from postmaster */
289-
char *p;
290289
#ifdef EXEC_BACKEND
291-
sscanf(optarg, "%d,", &UsedShmemSegID);
290+
char *p;
291+
292+
sscanf(optarg, "%d,%p,", &UsedShmemSegID, &UsedShmemSegAddr);
292293
p = strchr(optarg, ',');
294+
if (p)
295+
p = strchr(p+1, ',');
293296
if (p)
294297
dbname = strdup(p+1);
295298
#else

src/backend/port/sysv_shmem.c

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.8 2003/05/06 23:34:55 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/port/sysv_shmem.c,v 1.9 2003/05/08 14:49:03 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -39,9 +39,8 @@ typedef int IpcMemoryId; /* shared memory ID returned by shmget(2) */
3939
#define IPCProtection (0600) /* access/modify by user only */
4040

4141

42-
#ifdef EXEC_BACKEND
4342
IpcMemoryKey UsedShmemSegID = 0;
44-
#endif
43+
void *UsedShmemSegAddr = NULL;
4544

4645
static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
4746
static void IpcMemoryDetach(int status, Datum shmaddr);
@@ -282,7 +281,7 @@ PrivateMemoryDelete(int status, Datum memaddr)
282281
*
283282
* Create a shared memory segment of the given size and initialize its
284283
* standard header. Also, register an on_shmem_exit callback to release
285-
* the storage.
284+
* the storage. For an exec'ed backend, it just attaches.
286285
*
287286
* Dead Postgres segments are recycled if found, but we do not fail upon
288287
* collision with non-Postgres shmem segments. The idea here is to detect and
@@ -302,11 +301,9 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
302301
/* Room for a header? */
303302
Assert(size > MAXALIGN(sizeof(PGShmemHeader)));
304303

305-
#ifdef EXEC_BACKEND
306-
if (UsedShmemSegID != 0)
304+
if (ExecBackend && UsedShmemSegID != 0)
307305
NextShmemSegID = UsedShmemSegID;
308306
else
309-
#endif
310307
NextShmemSegID = port * 1000 + 1;
311308

312309
for (;;NextShmemSegID++)
@@ -320,40 +317,59 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
320317
break;
321318
}
322319

323-
/* Try to create new segment */
324-
memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
325-
if (memAddress)
326-
break; /* successful create and attach */
320+
/* If attach to fixed address, only try once */
321+
if (ExecBackend && UsedShmemSegAddr != NULL && NextShmemSegID != UsedShmemSegID)
322+
{
323+
fprintf(stderr, "Unable to attach to memory at fixed address: shmget(key=%d, addr=%p) failed: %s\n",
324+
(int) UsedShmemSegID, UsedShmemSegAddr, strerror(errno));
325+
proc_exit(1);
326+
}
327+
328+
if (!ExecBackend || UsedShmemSegAddr == NULL)
329+
{
330+
/* Try to create new segment */
331+
memAddress = InternalIpcMemoryCreate(NextShmemSegID, size);
332+
if (memAddress)
333+
break; /* successful create and attach */
334+
}
327335

328336
/* See if it looks to be leftover from a dead Postgres process */
329337
shmid = shmget(NextShmemSegID, sizeof(PGShmemHeader), 0);
330338
if (shmid < 0)
331339
continue; /* failed: must be some other app's */
332340

333-
#if defined(solaris) && defined(__sparc__)
334341
/* use intimate shared memory on SPARC Solaris */
335-
memAddress = shmat(shmid, 0, SHM_SHARE_MMU);
342+
memAddress = shmat(shmid, UsedShmemSegAddr,
343+
#if defined(solaris) && defined(__sparc__)
344+
SHM_SHARE_MMU
336345
#else
337-
memAddress = shmat(shmid, 0, 0);
346+
0
338347
#endif
348+
);
339349

340350
if (memAddress == (void *) -1)
341351
continue; /* failed: must be some other app's */
352+
342353
hdr = (PGShmemHeader *) memAddress;
343354
if (hdr->magic != PGShmemMagic)
344355
{
345356
shmdt(memAddress);
346357
continue; /* segment belongs to a non-Postgres app */
347358
}
348359

360+
/* Successfully attached to shared memory, which is all we wanted */
361+
if (ExecBackend && UsedShmemSegAddr != NULL)
362+
break;
363+
364+
/* Check shared memory and possibly remove and recreate */
365+
349366
/*
350-
* If the creator PID is my own PID or does not belong to any
351-
* extant process, it's safe to zap it.
367+
* If I am not the creator and it belongs to an extant process,
368+
* continue.
352369
*/
353370
if (hdr->creatorPID != getpid())
354371
{
355-
if (kill(hdr->creatorPID, 0) == 0 ||
356-
errno != ESRCH)
372+
if (kill(hdr->creatorPID, 0) == 0 || errno != ESRCH)
357373
{
358374
shmdt(memAddress);
359375
continue; /* segment belongs to a live process */
@@ -385,26 +401,31 @@ PGSharedMemoryCreate(uint32 size, bool makePrivate, int port)
385401
*/
386402
}
387403

388-
/*
389-
* OK, we created a new segment. Mark it as created by this process.
390-
* The order of assignments here is critical so that another Postgres
391-
* process can't see the header as valid but belonging to an invalid
392-
* PID!
393-
*/
394404
hdr = (PGShmemHeader *) memAddress;
395-
hdr->creatorPID = getpid();
396-
hdr->magic = PGShmemMagic;
397405

398-
/*
399-
* Initialize space allocation status for segment.
400-
*/
401-
hdr->totalsize = size;
402-
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
406+
if (!ExecBackend || makePrivate || UsedShmemSegAddr == NULL)
407+
{
408+
/*
409+
* OK, we created a new segment. Mark it as created by this process.
410+
* The order of assignments here is critical so that another Postgres
411+
* process can't see the header as valid but belonging to an invalid
412+
* PID!
413+
*/
414+
hdr->creatorPID = getpid();
415+
hdr->magic = PGShmemMagic;
416+
417+
/*
418+
* Initialize space allocation status for segment.
419+
*/
420+
hdr->totalsize = size;
421+
hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
422+
}
403423

404-
#ifdef EXEC_BACKEND
405-
if (!makePrivate && UsedShmemSegID == 0)
424+
if (ExecBackend && !makePrivate && UsedShmemSegAddr == NULL)
425+
{
426+
UsedShmemSegAddr = memAddress;
406427
UsedShmemSegID = NextShmemSegID;
407-
#endif
428+
}
408429

409430
return hdr;
410431
}

src/backend/postmaster/postmaster.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.322 2003/05/06 23:34:55 momjian Exp $
40+
* $Header: /cvsroot/pgsql/src/backend/postmaster/postmaster.c,v 1.323 2003/05/08 14:49:03 momjian Exp $
4141
*
4242
* NOTES
4343
*
@@ -2439,9 +2439,10 @@ BackendFinalize(Port *port)
24392439
*/
24402440
av[ac++] = "-p";
24412441
#ifdef EXEC_BACKEND
2442-
Assert(UsedShmemSegID != 0);
2442+
Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
24432443
/* database name at the end because it might contain commas */
2444-
snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%s", port->sock, UsedShmemSegID, port->database_name);
2444+
snprintf(pbuf, NAMEDATALEN + 256, "%d,%d,%p,%s", port->sock,
2445+
UsedShmemSegID, UsedShmemSegAddr, port->database_name);
24452446
av[ac++] = pbuf;
24462447
#else
24472448
av[ac++] = port->database_name;
@@ -2776,9 +2777,10 @@ SSDataBase(int xlop)
27762777

27772778
av[ac++] = "-p";
27782779
#ifdef EXEC_BACKEND
2779-
Assert(UsedShmemSegID != 0);
2780+
Assert(UsedShmemSegID != 0 && UsedShmemSegAddr != NULL);
27802781
/* database name at the end because it might contain commas */
2781-
snprintf(pbuf, NAMEDATALEN + 256, "%d,%s", UsedShmemSegID, "template1");
2782+
snprintf(pbuf, NAMEDATALEN + 256, "%d,%p,%s", UsedShmemSegID,
2783+
UsedShmemSegAddr, "template1");
27822784
av[ac++] = pbuf;
27832785
#else
27842786
av[ac++] = "template1";

src/backend/tcop/postgres.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.338 2003/05/06 23:34:55 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/tcop/postgres.c,v 1.339 2003/05/08 14:49:04 momjian Exp $
1212
*
1313
* NOTES
1414
* this is the "main" module of the postgres backend and
@@ -2025,13 +2025,17 @@ PostgresMain(int argc, char *argv[], const char *username)
20252025
*/
20262026
if (secure)
20272027
{
2028-
char *p;
20292028
#ifdef EXEC_BACKEND
2030-
sscanf(optarg, "%d,%d,", &MyProcPort->sock, &UsedShmemSegID);
2029+
char *p;
2030+
2031+
sscanf(optarg, "%d,%d,%p,", &MyProcPort->sock,
2032+
&UsedShmemSegID, &UsedShmemSegAddr);
20312033
/* Grab dbname as last param */
20322034
p = strchr(optarg, ',');
20332035
if (p)
20342036
p = strchr(p+1, ',');
2037+
if (p)
2038+
p = strchr(p+1, ',');
20352039
if (p)
20362040
dbname = strdup(p+1);
20372041
#else
@@ -2393,7 +2397,7 @@ PostgresMain(int argc, char *argv[], const char *username)
23932397
if (!IsUnderPostmaster)
23942398
{
23952399
puts("\nPOSTGRES backend interactive interface ");
2396-
puts("$Revision: 1.338 $ $Date: 2003/05/06 23:34:55 $\n");
2400+
puts("$Revision: 1.339 $ $Date: 2003/05/08 14:49:04 $\n");
23972401
}
23982402

23992403
/*

src/include/storage/pg_shmem.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
20-
* $Id: pg_shmem.h,v 1.5 2003/05/06 23:34:56 momjian Exp $
20+
* $Id: pg_shmem.h,v 1.6 2003/05/08 14:49:04 momjian Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -38,6 +38,7 @@ typedef struct PGShmemHeader /* standard header for all Postgres shmem */
3838

3939
#ifdef EXEC_BACKEND
4040
extern IpcMemoryKey UsedShmemSegID;
41+
extern void *UsedShmemSegAddr;
4142
#endif
4243

4344
extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,

0 commit comments

Comments
 (0)