Accept "B" in all memory-unit GUCs, and improve error messages.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 23 May 2018 07:02:40 +0000 (10:02 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Wed, 23 May 2018 07:22:26 +0000 (10:22 +0300)
Commit 6e7baa3227 added support for "B" unit, for specifying config options
in bytes. However, it was only accepted in GUC_UNIT_BYTE settings,
wal_segment_size and track_activity_query_size, and not e.g. in work_mem.
This patch makes it consistent, so that "B" accepted in all the same
contexts where "kB", "MB", and so forth are accepted.

Add "B" to the list of accepted units in the error hint, along with "kB",
"MB", etc.

Add an entry in the conversion table for "TB" to "B" conversion. A terabyte
is out of range for any GUC_UNIT_BYTE option, so you always get an "out of
range" error with that, but without it, you get a confusing error message
that claims that "TB" is not an accepted unit, with a hint that nevertheless
lists "TB" as an accepted unit.

Reviewed-by: Alexander Korotkov, Andres Freund
Discussion: https://www.postgresql.org/message-id/1bfe7f4a-7e22-aa6e-7b37-f4d222ed2d67@iki.fi

src/backend/utils/misc/guc.c

index 7cd2d2d80ef650ef9da0216e59e02c772b7c9826..ee1444c427fb60ed552a79709f6acbd0b495ed89 100644 (file)
@@ -705,7 +705,7 @@ typedef struct
    char        unit[MAX_UNIT_LEN + 1]; /* unit, as a string, like "kB" or
                                         * "min" */
    int         base_unit;      /* GUC_UNIT_XXX */
-   int         multiplier;     /* If positive, multiply the value with this
+   int64       multiplier;     /* If positive, multiply the value with this
                                 * for unit -> base_unit conversion.  If
                                 * negative, divide (with the absolute value) */
 } unit_conversion;
@@ -718,10 +718,16 @@ typedef struct
 #error XLOG_BLCKSZ must be between 1KB and 1MB
 #endif
 
-static const char *memory_units_hint = gettext_noop("Valid units for this parameter are \"kB\", \"MB\", \"GB\", and \"TB\".");
+static const char *memory_units_hint = gettext_noop("Valid units for this parameter are \"B\", \"kB\", \"MB\", \"GB\", and \"TB\".");
 
 static const unit_conversion memory_unit_conversion_table[] =
 {
+   /*
+    * TB -> bytes conversion always overflows 32-bit integer, so this always
+    * produces an error.  Include it nevertheless for completeness, and so
+    * that you get an "out of range" error, rather than "invalid unit".
+    */
+   {"TB", GUC_UNIT_BYTE, INT64CONST(1024) * 1024 * 1024 * 1024},
    {"GB", GUC_UNIT_BYTE, 1024 * 1024 * 1024},
    {"MB", GUC_UNIT_BYTE, 1024 * 1024},
    {"kB", GUC_UNIT_BYTE, 1024},
@@ -731,21 +737,25 @@ static const unit_conversion memory_unit_conversion_table[] =
    {"GB", GUC_UNIT_KB, 1024 * 1024},
    {"MB", GUC_UNIT_KB, 1024},
    {"kB", GUC_UNIT_KB, 1},
+   {"B", GUC_UNIT_KB, -1024},
 
    {"TB", GUC_UNIT_MB, 1024 * 1024},
    {"GB", GUC_UNIT_MB, 1024},
    {"MB", GUC_UNIT_MB, 1},
    {"kB", GUC_UNIT_MB, -1024},
+   {"B", GUC_UNIT_MB, -(1024 * 1024)},
 
    {"TB", GUC_UNIT_BLOCKS, (1024 * 1024 * 1024) / (BLCKSZ / 1024)},
    {"GB", GUC_UNIT_BLOCKS, (1024 * 1024) / (BLCKSZ / 1024)},
    {"MB", GUC_UNIT_BLOCKS, 1024 / (BLCKSZ / 1024)},
    {"kB", GUC_UNIT_BLOCKS, -(BLCKSZ / 1024)},
+   {"B", GUC_UNIT_BLOCKS, -BLCKSZ},
 
    {"TB", GUC_UNIT_XBLOCKS, (1024 * 1024 * 1024) / (XLOG_BLCKSZ / 1024)},
    {"GB", GUC_UNIT_XBLOCKS, (1024 * 1024) / (XLOG_BLCKSZ / 1024)},
    {"MB", GUC_UNIT_XBLOCKS, 1024 / (XLOG_BLCKSZ / 1024)},
    {"kB", GUC_UNIT_XBLOCKS, -(XLOG_BLCKSZ / 1024)},
+   {"B", GUC_UNIT_XBLOCKS, -XLOG_BLCKSZ},
 
    {""}                        /* end of table marker */
 };