Show a sensible value in pg_settings.unit for GUC_UNIT_XSEGS variables.

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: <CAG-jOyA=iNFhN+yB4vfvqh688B7Tr5SArbYcFUAjZi=0Exp-Lg@mail.gmail.com>
This commit is contained in:
Tom Lane 2016-10-03 16:40:05 -04:00
parent 814b9e9b8e
commit 6bc811c992
2 changed files with 16 additions and 8 deletions

View File

@ -8016,20 +8016,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";
@ -8040,7 +8043,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;
}

View 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)