Global variables in exec() - Mailing list pgsql-patches

From Bruce Momjian
Subject Global variables in exec()
Date
Msg-id 200305060440.h464e0723857@candle.pha.pa.us
Whole thread Raw
Responses Re: Global variables in exec()  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-patches
This patch passes the file descriptor number and shared memory id to
exec'ed backend via the -p flag --- basically it passes:

    -p file_descriptor_number,shared_memory_id,database

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/port/sysv_shmem.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/port/sysv_shmem.c,v
retrieving revision 1.7
diff -c -c -r1.7 sysv_shmem.c
*** src/backend/port/sysv_shmem.c    24 Apr 2003 21:24:36 -0000    1.7
--- src/backend/port/sysv_shmem.c    6 May 2003 04:35:41 -0000
***************
*** 34,46 ****
  #include "storage/ipc.h"
  #include "storage/pg_shmem.h"

-
- typedef uint32 IpcMemoryKey;    /* shared memory key passed to shmget(2) */
  typedef int IpcMemoryId;        /* shared memory ID returned by shmget(2) */

  #define IPCProtection    (0600)    /* access/modify by user only */


  static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
  static void IpcMemoryDetach(int status, Datum shmaddr);
  static void IpcMemoryDelete(int status, Datum shmId);
--- 34,48 ----
  #include "storage/ipc.h"
  #include "storage/pg_shmem.h"

  typedef int IpcMemoryId;        /* shared memory ID returned by shmget(2) */

  #define IPCProtection    (0600)    /* access/modify by user only */


+ #ifdef EXEC_BACKEND
+ IpcMemoryKey UsedShmemSegID = 0;
+ #endif
+
  static void *InternalIpcMemoryCreate(IpcMemoryKey memKey, uint32 size);
  static void IpcMemoryDetach(int status, Datum shmaddr);
  static void IpcMemoryDelete(int status, Datum shmId);
***************
*** 300,309 ****
      /* Room for a header? */
      Assert(size > MAXALIGN(sizeof(PGShmemHeader)));

!     /* Loop till we find a free IPC key */
!     NextShmemSegID = port * 1000;

!     for (NextShmemSegID++;; NextShmemSegID++)
      {
          IpcMemoryId shmid;

--- 302,315 ----
      /* Room for a header? */
      Assert(size > MAXALIGN(sizeof(PGShmemHeader)));

! #ifdef EXEC_BACKEND
!     if (UsedShmemSegID != 0)
!         NextShmemSegID = UsedShmemSegID;
!     else
! #endif
!         NextShmemSegID = port * 1000 + 1;

!     for (;;NextShmemSegID++)
      {
          IpcMemoryId shmid;

***************
*** 394,399 ****
--- 400,410 ----
       */
      hdr->totalsize = size;
      hdr->freeoffset = MAXALIGN(sizeof(PGShmemHeader));
+
+ #ifdef EXEC_BACKEND
+     if (!makePrivate && UsedShmemSegID == 0)
+         UsedShmemSegID = NextShmemSegID;
+ #endif

      return hdr;
  }
Index: src/backend/postmaster/postmaster.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/postmaster/postmaster.c,v
retrieving revision 1.321
diff -c -c -r1.321 postmaster.c
*** src/backend/postmaster/postmaster.c    3 May 2003 05:13:18 -0000    1.321
--- src/backend/postmaster/postmaster.c    6 May 2003 04:35:43 -0000
***************
*** 97,102 ****
--- 97,103 ----
  #include "nodes/nodes.h"
  #include "storage/fd.h"
  #include "storage/ipc.h"
+ #include "storage/pg_shmem.h"
  #include "storage/pmsignal.h"
  #include "storage/proc.h"
  #include "access/xlog.h"
***************
*** 2214,2219 ****
--- 2215,2223 ----
      int            ac;
      char        debugbuf[32];
      char        protobuf[32];
+ #ifdef EXEC_BACKEND
+     char        pbuf[NAMEDATALEN + 256];
+ #endif
      int            i;
      int            status;
      struct timeval now;
***************
*** 2434,2441 ****
       * database to use.  -p marks the end of secure switches.
       */
      av[ac++] = "-p";
      av[ac++] = port->database_name;
!
      /*
       * Pass the (insecure) option switches from the connection request.
       * (It's OK to mangle port->cmdline_options now.)
--- 2438,2451 ----
       * database to use.  -p marks the end of secure switches.
       */
      av[ac++] = "-p";
+ #ifndef EXEC_BACKEND
      av[ac++] = port->database_name;
! #else
!     Assert(UsedShmemSegID != 0);
!     /* database name at the end because it might contain commas */
!     sprintf(pbuf, "%d,%d,%s", port->sock, UsedShmemSegID, port->database_name);
!     av[ac++] = pbuf;
! #endif
      /*
       * Pass the (insecure) option switches from the connection request.
       * (It's OK to mangle port->cmdline_options now.)
Index: src/backend/storage/ipc/shmem.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/storage/ipc/shmem.c,v
retrieving revision 1.67
diff -c -c -r1.67 shmem.c
*** src/backend/storage/ipc/shmem.c    4 Sep 2002 20:31:25 -0000    1.67
--- src/backend/storage/ipc/shmem.c    6 May 2003 04:35:44 -0000
***************
*** 365,372 ****
              hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL);
              LWLockRelease(ShmemIndexLock);

!             elog(WARNING, "ShmemInitStruct: cannot allocate '%s'",
!                  name);
              *foundPtr = FALSE;
              return NULL;
          }
--- 365,371 ----
              hash_search(ShmemIndex, (void *) &item, HASH_REMOVE, NULL);
              LWLockRelease(ShmemIndexLock);

!             elog(WARNING, "ShmemInitStruct: cannot allocate '%s'", name);
              *foundPtr = FALSE;
              return NULL;
          }
Index: src/backend/tcop/postgres.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/tcop/postgres.c,v
retrieving revision 1.334
diff -c -c -r1.334 postgres.c
*** src/backend/tcop/postgres.c    6 May 2003 04:16:35 -0000    1.334
--- src/backend/tcop/postgres.c    6 May 2003 04:35:53 -0000
***************
*** 51,56 ****
--- 51,57 ----
  #include "rewrite/rewriteHandler.h"
  #include "storage/freespace.h"
  #include "storage/ipc.h"
+ #include "storage/pg_shmem.h"
  #include "storage/proc.h"
  #include "tcop/fastpath.h"
  #include "tcop/pquery.h"
***************
*** 1987,1993 ****
--- 1988,1999 ----
                   */
                  if (secure)
                  {
+ #ifdef EXEC_BACKEND
+                     sscanf(optarg, "%d,%d,", &MyProcPort->sock, &UsedShmemSegID);
+                     DBName = strdup(strrchr(optarg, ',') + 1);
+ #else
                      DBName = strdup(optarg);
+ #endif
                      secure = false;        /* subsequent switches are NOT
                                           * secure */
                      ctx = PGC_BACKEND;
Index: src/include/storage/pg_shmem.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/include/storage/pg_shmem.h,v
retrieving revision 1.4
diff -c -c -r1.4 pg_shmem.h
*** src/include/storage/pg_shmem.h    4 Sep 2002 20:31:45 -0000    1.4
--- src/include/storage/pg_shmem.h    6 May 2003 04:35:54 -0000
***************
*** 24,29 ****
--- 24,31 ----
  #ifndef PG_SHMEM_H
  #define PG_SHMEM_H

+ typedef uint32 IpcMemoryKey;    /* shared memory key passed to shmget(2) */
+
  typedef struct PGShmemHeader    /* standard header for all Postgres shmem */
  {
      int32        magic;            /* magic # to identify Postgres segments */
***************
*** 33,38 ****
--- 35,44 ----
      uint32        freeoffset;        /* offset to first free space */
  } PGShmemHeader;

+
+ #ifdef EXEC_BACKEND
+ extern IpcMemoryKey UsedShmemSegID;
+ #endif

  extern PGShmemHeader *PGSharedMemoryCreate(uint32 size, bool makePrivate,
                       int port);

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Cleanup of fork/exec
Next
From: Tom Lane
Date:
Subject: Re: Global variables in exec()