Show a sensible value in pg_settings.unit for GUC_UNIT_XSEGS variables.
authorTom Lane
Mon, 3 Oct 2016 20:40:06 +0000 (16:40 -0400)
committerTom Lane
Mon, 3 Oct 2016 20:40:26 +0000 (16:40 -0400)
Commit 88e982302 invented GUC_UNIT_XSEGS for min_wal_size and max_wal_size,
but neglected to make it display sensibly in pg_settings.unit (by adding a
case to the switch in GetConfigOptionByNum).  Fix that, and adjust said
switch to throw a run-time error the next time somebody forgets.

In passing, avoid using a static buffer for the output string --- the rest
of this function pstrdup's from a local buffer, and I see no very good
reason why the units code should do it differently and less safely.

Per report from Otar Shavadze.  Back-patch to 9.5 where the new unit type
was added.

Report: 

src/backend/utils/misc/guc.c
src/include/utils/guc.h

index bc9d33f6ff1bb04e847a961f9fb342117b441448..2414b2f1c9a03d70cc40022064af4e710bd22ddc 100644 (file)
@@ -7998,20 +7998,23 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
    /* unit */
    if (conf->vartype == PGC_INT)
    {
-       static char buf[8];
-
        switch (conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME))
        {
            case GUC_UNIT_KB:
                values[2] = "kB";
                break;
            case GUC_UNIT_BLOCKS:
-               snprintf(buf, sizeof(buf), "%dkB", BLCKSZ / 1024);
-               values[2] = buf;
+               snprintf(buffer, sizeof(buffer), "%dkB", BLCKSZ / 1024);
+               values[2] = pstrdup(buffer);
                break;
            case GUC_UNIT_XBLOCKS:
-               snprintf(buf, sizeof(buf), "%dkB", XLOG_BLCKSZ / 1024);
-               values[2] = buf;
+               snprintf(buffer, sizeof(buffer), "%dkB", XLOG_BLCKSZ / 1024);
+               values[2] = pstrdup(buffer);
+               break;
+           case GUC_UNIT_XSEGS:
+               snprintf(buffer, sizeof(buffer), "%dMB",
+                        XLOG_SEG_SIZE / (1024 * 1024));
+               values[2] = pstrdup(buffer);
                break;
            case GUC_UNIT_MS:
                values[2] = "ms";
@@ -8022,7 +8025,12 @@ GetConfigOptionByNum(int varnum, const char **values, bool *noshow)
            case GUC_UNIT_MIN:
                values[2] = "min";
                break;
+           case 0:
+               values[2] = NULL;
+               break;
            default:
+               elog(ERROR, "unrecognized GUC units value: %d",
+                    conf->flags & (GUC_UNIT_MEMORY | GUC_UNIT_TIME));
                values[2] = NULL;
                break;
        }
index e1de1a5d0653bdb72ecbdb6a92207f3e5e4fa3ea..0bf9f21067824daed538bb6ba477ddfda7686e78 100644 (file)
@@ -219,12 +219,12 @@ typedef enum
 #define GUC_UNIT_BLOCKS            0x2000  /* value is in blocks */
 #define GUC_UNIT_XBLOCKS       0x3000  /* value is in xlog blocks */
 #define GUC_UNIT_XSEGS         0x4000  /* value is in xlog segments */
-#define GUC_UNIT_MEMORY            0xF000  /* mask for KB, BLOCKS, XBLOCKS */
+#define GUC_UNIT_MEMORY            0xF000  /* mask for size-related units */
 
 #define GUC_UNIT_MS               0x10000  /* value is in milliseconds */
 #define GUC_UNIT_S            0x20000  /* value is in seconds */
 #define GUC_UNIT_MIN          0x30000  /* value is in minutes */
-#define GUC_UNIT_TIME         0xF0000  /* mask for MS, S, MIN */
+#define GUC_UNIT_TIME         0xF0000  /* mask for time-related units */
 
 #define GUC_UNIT               (GUC_UNIT_MEMORY | GUC_UNIT_TIME)