Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Further code review for pg_stat_ssl patch.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 27 Jul 2015 20:29:14 +0000 (16:29 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 27 Jul 2015 20:29:14 +0000 (16:29 -0400)
Fix additional bogosity in commit 9029f4b37406b21a.  Include the
BackendSslStatusBuffer in the BackendStatusShmemSize calculation,
avoid ugly and error-prone casts to char* and back, put related
code stanzas into a consistent order (and fix a couple of previous
instances of that sin).  All cosmetic except for the size oversight.

src/backend/postmaster/pgstat.c

index 887095def06661e3d4471fdecdcc683ae6558768..ab018c474140a39dd9f568168047c2133a576fc8 100644 (file)
@@ -2478,8 +2478,8 @@ pgstat_fetch_global(void)
 
 static PgBackendStatus *BackendStatusArray = NULL;
 static PgBackendStatus *MyBEEntry = NULL;
-static char *BackendClientHostnameBuffer = NULL;
 static char *BackendAppnameBuffer = NULL;
+static char *BackendClientHostnameBuffer = NULL;
 static char *BackendActivityBuffer = NULL;
 static Size BackendActivityBufferSize = 0;
 #ifdef USE_SSL
@@ -2495,13 +2495,22 @@ BackendStatusShmemSize(void)
 {
    Size        size;
 
+   /* BackendStatusArray: */
    size = mul_size(sizeof(PgBackendStatus), MaxBackends);
+   /* BackendAppnameBuffer: */
+   size = add_size(size,
+                   mul_size(NAMEDATALEN, MaxBackends));
+   /* BackendClientHostnameBuffer: */
    size = add_size(size,
                    mul_size(NAMEDATALEN, MaxBackends));
+   /* BackendActivityBuffer: */
    size = add_size(size,
                    mul_size(pgstat_track_activity_query_size, MaxBackends));
+#ifdef USE_SSL
+   /* BackendSslStatusBuffer: */
    size = add_size(size,
-                   mul_size(NAMEDATALEN, MaxBackends));
+                   mul_size(sizeof(PgBackendSSLStatus), MaxBackends));
+#endif
    return size;
 }
 
@@ -2566,26 +2575,6 @@ CreateSharedBackendStatus(void)
        }
    }
 
-#ifdef USE_SSL
-   /* Create or attach to the shared SSL status buffer */
-   size = mul_size(sizeof(PgBackendSSLStatus), MaxBackends);
-   BackendSslStatusBuffer = (PgBackendSSLStatus *)
-       ShmemInitStruct("Backend SSL Status Buffer", size, &found);
-
-   if (!found)
-   {
-       MemSet(BackendSslStatusBuffer, 0, size);
-
-       /* Initialize st_sslstatus pointers. */
-       buffer = (char *) BackendSslStatusBuffer;
-       for (i = 0; i < MaxBackends; i++)
-       {
-           BackendStatusArray[i].st_sslstatus = (PgBackendSSLStatus *) buffer;
-           buffer += sizeof(PgBackendSSLStatus);
-       }
-   }
-#endif
-
    /* Create or attach to the shared activity buffer */
    BackendActivityBufferSize = mul_size(pgstat_track_activity_query_size,
                                         MaxBackends);
@@ -2606,6 +2595,28 @@ CreateSharedBackendStatus(void)
            buffer += pgstat_track_activity_query_size;
        }
    }
+
+#ifdef USE_SSL
+   /* Create or attach to the shared SSL status buffer */
+   size = mul_size(sizeof(PgBackendSSLStatus), MaxBackends);
+   BackendSslStatusBuffer = (PgBackendSSLStatus *)
+       ShmemInitStruct("Backend SSL Status Buffer", size, &found);
+
+   if (!found)
+   {
+       PgBackendSSLStatus *ptr;
+
+       MemSet(BackendSslStatusBuffer, 0, size);
+
+       /* Initialize st_sslstatus pointers. */
+       ptr = BackendSslStatusBuffer;
+       for (i = 0; i < MaxBackends; i++)
+       {
+           BackendStatusArray[i].st_sslstatus = ptr;
+           ptr++;
+       }
+   }
+#endif
 }
 
 
@@ -2932,11 +2943,11 @@ pgstat_read_current_status(void)
    volatile PgBackendStatus *beentry;
    LocalPgBackendStatus *localtable;
    LocalPgBackendStatus *localentry;
+   char       *localappname,
+              *localactivity;
 #ifdef USE_SSL
    PgBackendSSLStatus *localsslstatus;
 #endif
-   char       *localappname,
-              *localactivity;
    int         i;
 
    Assert(!pgStatRunningInCollector);
@@ -2951,15 +2962,15 @@ pgstat_read_current_status(void)
    localappname = (char *)
        MemoryContextAlloc(pgStatLocalContext,
                           NAMEDATALEN * MaxBackends);
+   localactivity = (char *)
+       MemoryContextAlloc(pgStatLocalContext,
+                          pgstat_track_activity_query_size * MaxBackends);
 #ifdef USE_SSL
    localsslstatus = (PgBackendSSLStatus *)
        MemoryContextAlloc(pgStatLocalContext,
                           sizeof(PgBackendSSLStatus) * MaxBackends);
 #endif
 
-   localactivity = (char *)
-       MemoryContextAlloc(pgStatLocalContext,
-                          pgstat_track_activity_query_size * MaxBackends);
    localNumBackends = 0;
 
    beentry = BackendStatusArray;