Re: [BUGS] BUG #14244: wrong suffix for pg_size_pretty()

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: thomas(dot)berger(at)1und1(dot)de
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [BUGS] BUG #14244: wrong suffix for pg_size_pretty()
Date: 2016-07-30 00:18:38
Message-ID: 20160730001838.GA22405@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Tue, Jul 12, 2016 at 01:36:38PM +0000, thomas(dot)berger(at)1und1(dot)de wrote:
> The following bug has been logged on the website:
>
> Bug reference: 14244
> Logged by: Thomas Berger
> Email address: thomas(dot)berger(at)1und1(dot)de
> PostgreSQL version: 9.5.3
> Operating system: any
> Description:
>
> pg_size_pretty uses the suffix "kB" (kilobyte, 10^3 byte), but the returned
> value is "KB", or "KiB" ( kibibyte, 2^10 byte). This is missleading and
> should be fixed. See also https://en.wikipedia.org/wiki/Kibibyte
>
> =# select pg_size_pretty(1024000::bigint);
> pg_size_pretty
> ----------------
> 1000 kB

(Thread moved to hackers.)

The Postgres docs specify that kB is based on 1024 or 2^10:

https://www.postgresql.org/docs/9.6/static/functions-admin.html

Note: The units kB, MB, GB and TB used by the functions
pg_size_pretty and pg_size_bytes are defined using powers of 2 rather
than powers of 10, so 1kB is 1024 bytes, 1MB is 10242 = 1048576 bytes,
and so on.

These prefixes were introduced to GUC variable specification in 2006:

commit b517e653489f733893d61e7a84c118325394471c
Author: Peter Eisentraut <peter_e(at)gmx(dot)net>
Date: Thu Jul 27 08:30:41 2006 +0000

Allow units to be specified with configuration settings.

and added to postgresql.conf:

# Memory units: kB = kilobytes Time units: ms = milliseconds
# MB = megabytes s = seconds
# GB = gigabytes min = minutes
# TB = terabytes h = hours
# d = days

and the units were copied when pg_size_pretty() was implemented. These
units are based on the International System of Units (SI)/metric.
However, the SI system is power-of-10-based, and we just re-purposed
them to be 1024 or 2^10-based.

However, that is not the end of the story. Things have moved forward
since 2006 and there is now firm support for either KB or KiB to be
1024-based units. This blog post explains the current state of prefix
specification:

http://pchelp.ricmedia.com/kilobytes-megabytes-gigabytes-terabytes-explained/

and here is a summary for 1000/1024-based units:

Kilobyte (Binary, JEDEC) KB 1024
Kilobyte (Decimal, Metric) kB 1000
Kibibyte (Binary, IEC) KiB 1024

You will notice that none of these list kB as 1024, which explains this
bug report.

Yes, we have redefined kB, and documented its use in postgresql.conf and
pg_size_pretty(), but it does not match any recognized standard.

I am thinking Postgres 10 would be a good time to switch to KB as a
1024-based prefix. Unfortunately, there is no similar fix for MB, GB,
etc. 'm' is 'milli' so there we never used mB, so in JEDEC and Metric,
MB is ambiguous as 1000-based or 1024-based.

IEC does give us a unique specification for 'mega', MiB, and GiB, which
might be what we want to use, but that might be too big a change, and I
rarely see those.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ As you are, so once was I. As I am, so you will be. +
+ Ancient Roman grave inscription +

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Bruce Momjian 2016-07-30 00:26:25 Re: [BUGS] BUG #14244: wrong suffix for pg_size_pretty()
Previous Message David G. Johnston 2016-07-29 14:00:32 Re: BUG #14269: Postgres JDBC driver should be able to connect using protocol version 2 to server versions 9.5+

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2016-07-30 00:26:25 Re: [BUGS] BUG #14244: wrong suffix for pg_size_pretty()
Previous Message Tomas Vondra 2016-07-29 23:46:45 Re: [Patch] Temporary tables that do not bloat pg_catalog (a.k.a fast temp tables)