public inbox for [email protected]help / color / mirror / Atom feed
[PATCH v3 1/3] Allow CREATE INDEX CONCURRENTLY on partitioned table 9+ messages / 3 participants [nested] [flat]
* [PATCH v3 1/3] Allow CREATE INDEX CONCURRENTLY on partitioned table @ 2020-06-06 22:42 Justin Pryzby <[email protected]> 0 siblings, 0 replies; 9+ messages in thread From: Justin Pryzby @ 2020-06-06 22:42 UTC (permalink / raw) Note, this effectively reverts 050098b14, so take care to not reintroduce the bug it fixed. --- doc/src/sgml/ref/create_index.sgml | 9 ---- src/backend/commands/indexcmds.c | 63 ++++++++++++++++---------- src/test/regress/expected/indexing.out | 26 +++++++++-- src/test/regress/sql/indexing.sql | 12 +++-- 4 files changed, 71 insertions(+), 39 deletions(-) diff --git a/doc/src/sgml/ref/create_index.sgml b/doc/src/sgml/ref/create_index.sgml index ff87b2d28f..e1298b8523 100644 --- a/doc/src/sgml/ref/create_index.sgml +++ b/doc/src/sgml/ref/create_index.sgml @@ -657,15 +657,6 @@ Indexes: cannot. </para> - <para> - Concurrent builds for indexes on partitioned tables are currently not - supported. However, you may concurrently build the index on each - partition individually and then finally create the partitioned index - non-concurrently in order to reduce the time where writes to the - partitioned table will be locked out. In this case, building the - partitioned index is a metadata only operation. - </para> - </refsect2> </refsect1> diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 2baca12c5f..42c905867c 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -652,17 +652,6 @@ DefineIndex(Oid relationId, partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE; if (partitioned) { - /* - * Note: we check 'stmt->concurrent' rather than 'concurrent', so that - * the error is thrown also for temporary tables. Seems better to be - * consistent, even though we could do it on temporary table because - * we're not actually doing it concurrently. - */ - if (stmt->concurrent) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot create index on partitioned table \"%s\" concurrently", - RelationGetRelationName(rel)))); if (stmt->excludeOpNames) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), @@ -1139,6 +1128,10 @@ DefineIndex(Oid relationId, CreateComments(indexRelationId, RelationRelationId, 0, stmt->idxcomment); + /* save lockrelid and locktag for below */ + heaprelid = rel->rd_lockInfo.lockRelId; + SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId); + if (partitioned) { /* @@ -1149,8 +1142,17 @@ DefineIndex(Oid relationId, */ if (!stmt->relation || stmt->relation->inh) { + /* + * Need to close the relation before recursing into children, so + * copy needed data into a longlived context. + */ + + MemoryContext ind_context = AllocSetContextCreate(PortalContext, "CREATE INDEX", + ALLOCSET_DEFAULT_SIZES); + MemoryContext oldcontext = MemoryContextSwitchTo(ind_context); PartitionDesc partdesc = RelationGetPartitionDesc(rel); int nparts = partdesc->nparts; + char *relname = pstrdup(RelationGetRelationName(rel)); Oid *part_oids = palloc(sizeof(Oid) * nparts); bool invalidate_parent = false; TupleDesc parentDesc; @@ -1160,8 +1162,10 @@ DefineIndex(Oid relationId, nparts); memcpy(part_oids, partdesc->oids, sizeof(Oid) * nparts); + parentDesc = CreateTupleDescCopy(RelationGetDescr(rel)); + table_close(rel, NoLock); + MemoryContextSwitchTo(oldcontext); - parentDesc = RelationGetDescr(rel); opfamOids = palloc(sizeof(Oid) * numberOfKeyAttributes); for (i = 0; i < numberOfKeyAttributes; i++) opfamOids[i] = get_opclass_family(classObjectId[i]); @@ -1196,18 +1200,20 @@ DefineIndex(Oid relationId, ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("cannot create unique index on partitioned table \"%s\"", - RelationGetRelationName(rel)), + relname), errdetail("Table \"%s\" contains partitions that are foreign tables.", - RelationGetRelationName(rel)))); + relname))); table_close(childrel, lockmode); continue; } + oldcontext = MemoryContextSwitchTo(ind_context); childidxs = RelationGetIndexList(childrel); attmap = build_attrmap_by_name(RelationGetDescr(childrel), parentDesc); + MemoryContextSwitchTo(oldcontext); foreach(cell, childidxs) { @@ -1333,6 +1339,8 @@ DefineIndex(Oid relationId, createdConstraintId, is_alter_table, check_rights, check_not_in_use, skip_build, quiet); + if (concurrent) + PushActiveSnapshot(GetTransactionSnapshot()); } pgstat_progress_update_param(PROGRESS_CREATEIDX_PARTITIONS_DONE, @@ -1363,23 +1371,37 @@ DefineIndex(Oid relationId, table_close(pg_index, RowExclusiveLock); heap_freetuple(newtup); } + } else + table_close(rel, NoLock); + + /* + * CIC needs to mark a partitioned table as VALID, which itself + * requires setting READY, which is unset for CIC (even though + * it's meaningless for an index without storage). + */ + if (concurrent) + { + PopActiveSnapshot(); + CommitTransactionCommand(); + StartTransactionCommand(); + index_set_state_flags(indexRelationId, INDEX_CREATE_SET_READY); + + CommandCounterIncrement(); + index_set_state_flags(indexRelationId, INDEX_CREATE_SET_VALID); } /* * Indexes on partitioned tables are not themselves built, so we're * done here. */ - table_close(rel, NoLock); if (!OidIsValid(parentIndexId)) pgstat_progress_end_command(); return address; } + table_close(rel, NoLock); if (!concurrent) { - /* Close the heap and we're done, in the non-concurrent case */ - table_close(rel, NoLock); - /* If this is the top-level index, we're done. */ if (!OidIsValid(parentIndexId)) pgstat_progress_end_command(); @@ -1387,11 +1409,6 @@ DefineIndex(Oid relationId, return address; } - /* save lockrelid and locktag for below, then close rel */ - heaprelid = rel->rd_lockInfo.lockRelId; - SET_LOCKTAG_RELATION(heaplocktag, heaprelid.dbId, heaprelid.relId); - table_close(rel, NoLock); - /* * For a concurrent build, it's important to make the catalog entries * visible to other transactions before we start to build the index. That diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out index f78865ef81..85d3e37ae1 100644 --- a/src/test/regress/expected/indexing.out +++ b/src/test/regress/expected/indexing.out @@ -50,11 +50,29 @@ select relname, relkind, relhassubclass, inhparent::regclass (8 rows) drop table idxpart; --- Some unsupported features +-- CIC on partitioned table create table idxpart (a int, b int, c text) partition by range (a); -create table idxpart1 partition of idxpart for values from (0) to (10); -create index concurrently on idxpart (a); -ERROR: cannot create index on partitioned table "idxpart" concurrently +create table idxpart1 partition of idxpart for values from (0) to (10) partition by range(a); +create table idxpart11 partition of idxpart1 for values from (0) to (10) partition by range(a); +create table idxpart2 partition of idxpart for values from (10) to (20); +create index concurrently on idxpart (a); -- partitioned +create index concurrently on idxpart1 (a); -- partitioned and partition +create index concurrently on idxpart11 (a); -- partitioned and partition, with no leaves +create index concurrently on idxpart2 (a); -- leaf +\d idxpart1 + Partitioned table "public.idxpart1" + Column | Type | Collation | Nullable | Default +--------+---------+-----------+----------+--------- + a | integer | | | + b | integer | | | + c | text | | | +Partition of: idxpart FOR VALUES FROM (0) TO (10) +Partition key: RANGE (a) +Indexes: + "idxpart1_a_idx" btree (a) + "idxpart1_a_idx1" btree (a) +Number of partitions: 1 (Use \d+ to list them.) + drop table idxpart; -- Verify bugfix with query on indexed partitioned table with no partitions -- https://postgr.es/m/[email protected] diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql index 35d159f41b..56a117d75f 100644 --- a/src/test/regress/sql/indexing.sql +++ b/src/test/regress/sql/indexing.sql @@ -29,10 +29,16 @@ select relname, relkind, relhassubclass, inhparent::regclass where relname like 'idxpart%' order by relname; drop table idxpart; --- Some unsupported features +-- CIC on partitioned table create table idxpart (a int, b int, c text) partition by range (a); -create table idxpart1 partition of idxpart for values from (0) to (10); -create index concurrently on idxpart (a); +create table idxpart1 partition of idxpart for values from (0) to (10) partition by range(a); +create table idxpart11 partition of idxpart1 for values from (0) to (10) partition by range(a); +create table idxpart2 partition of idxpart for values from (10) to (20); +create index concurrently on idxpart (a); -- partitioned +create index concurrently on idxpart1 (a); -- partitioned and partition +create index concurrently on idxpart11 (a); -- partitioned and partition, with no leaves +create index concurrently on idxpart2 (a); -- leaf +\d idxpart1 drop table idxpart; -- Verify bugfix with query on indexed partitioned table with no partitions -- 2.17.0 --SCOJXUq1iwCn05li Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v3-0002-Implement-REINDEX-of-partitioned-tables-indexes.patch" ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-02-21 23:47 Peter Eisentraut <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: Peter Eisentraut @ 2023-02-21 23:47 UTC (permalink / raw) To: Justin Pryzby <[email protected]>; +Cc: pgsql-hackers On 20.02.23 15:34, Justin Pryzby wrote: > On Mon, Feb 20, 2023 at 07:44:15AM +0100, Peter Eisentraut wrote: >> This patch adds support for the unit "B" to pg_size_pretty(). This makes it > > It seems like what it actually does is to support "B" in pg_size_bytes() > - is that what you meant ? yes > pg_size_pretty() already supports "bytes", so this doesn't actually make > sizes any more pretty, or evidently change its output at all. Right, this is for the input side. >> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c >> index dbd404101f..9ecd5428c3 100644 >> --- a/src/backend/utils/adt/dbsize.c >> +++ b/src/backend/utils/adt/dbsize.c >> @@ -49,6 +49,7 @@ struct size_pretty_unit >> /* When adding units here also update the error message in pg_size_bytes */ >> static const struct size_pretty_unit size_pretty_units[] = { >> {"bytes", 10 * 1024, false, 0}, >> + {"B", 10 * 1024, false, 0}, > > This adds a duplicate line (unitbits=0) where no other existing line > uses duplicates. If that's intentional, I think it deserves a comment > highlighting that it's an /*alias*/, and about why that does the right > thing, either here about or in the commit message. I have added a comment about that. From 6b3a155260e2da5338f7cb6a1d729a0d34e3935a Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Wed, 22 Feb 2023 00:44:45 +0100 Subject: [PATCH v2] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 2 +- src/backend/utils/adt/dbsize.c | 10 ++++++++-- src/test/regress/expected/dbsize.out | 15 ++++++++------- src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index e09e289a43..15a5a98b0a 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27162,7 +27162,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, B, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..cab7834e8a 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -46,9 +46,15 @@ struct size_pretty_unit * unit */ }; -/* When adding units here also update the error message in pg_size_bytes */ +/* + * When adding units here also update the error message in pg_size_bytes. + * + * Aliases (with the same unitbits) are allowed. pg_size_pretty uses the + * first one among them. + */ static const struct size_pretty_unit size_pretty_units[] = { {"bytes", 10 * 1024, false, 0}, + {"B", 10 * 1024, false, 0}, {"kB", 20 * 1024 - 1, true, 10}, {"MB", 20 * 1024 - 1, true, 20}, {"GB", 20 * 1024 - 1, true, 30}, @@ -813,7 +819,7 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: 8028e294b456bf1539649527fd98b3985fa4752a -- 2.39.2 Attachments: [text/plain] v2-0001-Add-support-for-unit-B-to-pg_size_bytes.patch (5.8K, ../../[email protected]/2-v2-0001-Add-support-for-unit-B-to-pg_size_bytes.patch) download | inline diff: From 6b3a155260e2da5338f7cb6a1d729a0d34e3935a Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Wed, 22 Feb 2023 00:44:45 +0100 Subject: [PATCH v2] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 2 +- src/backend/utils/adt/dbsize.c | 10 ++++++++-- src/test/regress/expected/dbsize.out | 15 ++++++++------- src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index e09e289a43..15a5a98b0a 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27162,7 +27162,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, B, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..cab7834e8a 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -46,9 +46,15 @@ struct size_pretty_unit * unit */ }; -/* When adding units here also update the error message in pg_size_bytes */ +/* + * When adding units here also update the error message in pg_size_bytes. + * + * Aliases (with the same unitbits) are allowed. pg_size_pretty uses the + * first one among them. + */ static const struct size_pretty_unit size_pretty_units[] = { {"bytes", 10 * 1024, false, 0}, + {"B", 10 * 1024, false, 0}, {"kB", 20 * 1024 - 1, true, 10}, {"MB", 20 * 1024 - 1, true, 20}, {"GB", 20 * 1024 - 1, true, 30}, @@ -813,7 +819,7 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: 8028e294b456bf1539649527fd98b3985fa4752a -- 2.39.2 ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-02-22 02:39 David Rowley <[email protected]> parent: Peter Eisentraut <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: David Rowley @ 2023-02-22 02:39 UTC (permalink / raw) To: Peter Eisentraut <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On Wed, 22 Feb 2023 at 12:47, Peter Eisentraut <[email protected]> wrote: > >> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c > >> index dbd404101f..9ecd5428c3 100644 > >> --- a/src/backend/utils/adt/dbsize.c > >> +++ b/src/backend/utils/adt/dbsize.c > >> @@ -49,6 +49,7 @@ struct size_pretty_unit > >> /* When adding units here also update the error message in pg_size_bytes */ > >> static const struct size_pretty_unit size_pretty_units[] = { > >> {"bytes", 10 * 1024, false, 0}, > >> + {"B", 10 * 1024, false, 0}, > > > > This adds a duplicate line (unitbits=0) where no other existing line > > uses duplicates. If that's intentional, I think it deserves a comment > > highlighting that it's an /*alias*/, and about why that does the right > > thing, either here about or in the commit message. > > I have added a comment about that. hmm. I didn't really code pg_size_pretty with aliases in mind. I don't think you can do this. There's code in pg_size_pretty() and pg_size_pretty_numeric() that'll not work correctly. We look ahead to the next unit to check if there is one so we know we must use this unit if there are no other units to convert to. Let's assume someone in the future reads your comment about aliases and thinks we can just go and add an alias for any unit. Here we'll add PiB for PB. diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..8e22969a76 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -54,6 +54,7 @@ static const struct size_pretty_unit size_pretty_units[] = { {"GB", 20 * 1024 - 1, true, 30}, {"TB", 20 * 1024 - 1, true, 40}, {"PB", 20 * 1024 - 1, true, 50}, + {"PiB", 20 * 1024 - 1, true, 50}, {NULL, 0, false, 0} }; testing it, I see: postgres=# select pg_size_pretty(10000::numeric * 1024*1024*1024*1024*1024); pg_size_pretty ---------------- 10000 PB (1 row) postgres=# select pg_size_pretty(20000::numeric * 1024*1024*1024*1024*1024); pg_size_pretty ---------------- 20000 PiB (1 row) I think we'll likely get complaints about PB being used sometimes and PiB being used at other times. I think you'll need to find another way to make the aliases work. Maybe another array with the name and an int to reference the corresponding index in size_pretty_units. David ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-02-27 08:34 Peter Eisentraut <[email protected]> parent: David Rowley <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: Peter Eisentraut @ 2023-02-27 08:34 UTC (permalink / raw) To: David Rowley <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On 22.02.23 03:39, David Rowley wrote: > hmm. I didn't really code pg_size_pretty with aliases in mind. I don't > think you can do this. There's code in pg_size_pretty() and > pg_size_pretty_numeric() that'll not work correctly. We look ahead to > the next unit to check if there is one so we know we must use this > unit if there are no other units to convert to. > I think you'll need to find another way to make the aliases work. > Maybe another array with the name and an int to reference the > corresponding index in size_pretty_units. Ok, here is a new patch with a separate table of aliases. (Might look like overkill, but I think the "PiB" etc. example you had could actually be a good use case for this as well.) From 4e493128adddc2656f3f139b2ca402f0d13721ba Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Mon, 27 Feb 2023 09:28:25 +0100 Subject: [PATCH v3] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 2 +- src/backend/utils/adt/dbsize.c | 34 +++++++++++++++++++++++++--- src/test/regress/expected/dbsize.out | 15 ++++++------ src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 0cbdf63632..718d0cb550 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27176,7 +27176,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, B, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..338e990aeb 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -57,6 +57,19 @@ static const struct size_pretty_unit size_pretty_units[] = { {NULL, 0, false, 0} }; +/* Additional unit aliases acceted by pg_size_bytes */ +struct size_bytes_unit_alias +{ + const char *alias; + const char *base; +}; + +/* When adding units here also update the error message in pg_size_bytes */ +static const struct size_bytes_unit_alias size_bytes_aliases[] = { + {"B", "bytes"}, + {NULL, NULL} +}; + /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 db_dir_size(const char *path) @@ -801,9 +814,22 @@ pg_size_bytes(PG_FUNCTION_ARGS) { /* Parse the unit case-insensitively */ if (pg_strcasecmp(strptr, unit->name) == 0) - { - multiplier = ((int64) 1) << unit->unitbits; break; + } + + /* If not found, look in table of aliases */ + if (unit->name == NULL) + { + for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++) + { + if (pg_strcasecmp(strptr, a->alias) == 0) + { + for (unit = size_pretty_units; unit->name != NULL; unit++) + { + if (pg_strcasecmp(a->base, unit->name) == 0) + break; + } + } } } @@ -813,7 +839,9 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + + multiplier = ((int64) 1) << unit->unitbits; if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: a6cd1fc692eff708fd42c72b03f756fa1860530e -- 2.39.2 Attachments: [text/plain] v3-0001-Add-support-for-unit-B-to-pg_size_bytes.patch (6.4K, ../../[email protected]/2-v3-0001-Add-support-for-unit-B-to-pg_size_bytes.patch) download | inline diff: From 4e493128adddc2656f3f139b2ca402f0d13721ba Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Mon, 27 Feb 2023 09:28:25 +0100 Subject: [PATCH v3] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 2 +- src/backend/utils/adt/dbsize.c | 34 +++++++++++++++++++++++++--- src/test/regress/expected/dbsize.out | 15 ++++++------ src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 0cbdf63632..718d0cb550 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27176,7 +27176,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, B, kB, MB, GB or TB as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..338e990aeb 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -57,6 +57,19 @@ static const struct size_pretty_unit size_pretty_units[] = { {NULL, 0, false, 0} }; +/* Additional unit aliases acceted by pg_size_bytes */ +struct size_bytes_unit_alias +{ + const char *alias; + const char *base; +}; + +/* When adding units here also update the error message in pg_size_bytes */ +static const struct size_bytes_unit_alias size_bytes_aliases[] = { + {"B", "bytes"}, + {NULL, NULL} +}; + /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 db_dir_size(const char *path) @@ -801,9 +814,22 @@ pg_size_bytes(PG_FUNCTION_ARGS) { /* Parse the unit case-insensitively */ if (pg_strcasecmp(strptr, unit->name) == 0) - { - multiplier = ((int64) 1) << unit->unitbits; break; + } + + /* If not found, look in table of aliases */ + if (unit->name == NULL) + { + for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++) + { + if (pg_strcasecmp(strptr, a->alias) == 0) + { + for (unit = size_pretty_units; unit->name != NULL; unit++) + { + if (pg_strcasecmp(a->base, unit->name) == 0) + break; + } + } } } @@ -813,7 +839,9 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + + multiplier = ((int64) 1) << unit->unitbits; if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: a6cd1fc692eff708fd42c72b03f756fa1860530e -- 2.39.2 ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-03-02 19:58 David Rowley <[email protected]> parent: Peter Eisentraut <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: David Rowley @ 2023-03-02 19:58 UTC (permalink / raw) To: Peter Eisentraut <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On Mon, 27 Feb 2023 at 21:34, Peter Eisentraut <[email protected]> wrote: > > On 22.02.23 03:39, David Rowley wrote: > > I think you'll need to find another way to make the aliases work. > > Maybe another array with the name and an int to reference the > > corresponding index in size_pretty_units. > > Ok, here is a new patch with a separate table of aliases. (Might look > like overkill, but I think the "PiB" etc. example you had could actually > be a good use case for this as well.) I think I'd prefer to see the size_bytes_unit_alias struct have an index into size_pretty_units[] array. i.e: struct size_bytes_unit_alias { const char *alias; /* aliased unit name */ const int unit_index; /* corresponding size_pretty_units element */ }; then the pg_size_bytes code can be simplified to: /* If not found, look in the table of aliases */ if (unit->name == NULL) { for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++) { if (pg_strcasecmp(strptr, a->alias) == 0) { unit = &size_pretty_units[a->unit_index]; break; } } } which saves having to have the additional and slower nested loop code. Apart from that, the patch looks fine. David ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-03-06 08:13 Peter Eisentraut <[email protected]> parent: David Rowley <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: Peter Eisentraut @ 2023-03-06 08:13 UTC (permalink / raw) To: David Rowley <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On 02.03.23 20:58, David Rowley wrote: > On Mon, 27 Feb 2023 at 21:34, Peter Eisentraut > <[email protected]> wrote: >> >> On 22.02.23 03:39, David Rowley wrote: >>> I think you'll need to find another way to make the aliases work. >>> Maybe another array with the name and an int to reference the >>> corresponding index in size_pretty_units. >> >> Ok, here is a new patch with a separate table of aliases. (Might look >> like overkill, but I think the "PiB" etc. example you had could actually >> be a good use case for this as well.) > > I think I'd prefer to see the size_bytes_unit_alias struct have an > index into size_pretty_units[] array. i.e: Ok, done that way. (I had thought about that, but I was worried that that would be too error-prone to maintain. But I suppose the tables don't change that often, and test cases would easily catch mistakes.) I also updated the documentation a bit more. From bb0fb6eb3364195838a9c7e387ee4237c8cd30b4 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Mon, 6 Mar 2023 09:10:50 +0100 Subject: [PATCH v4] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 9 +++++--- src/backend/utils/adt/dbsize.c | 33 ++++++++++++++++++++++++---- src/test/regress/expected/dbsize.out | 15 +++++++------ src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 97b3f1c1a6..fa5f60cf4c 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27166,8 +27166,11 @@ <title>Database Object Size Functions</title> <returnvalue>bigint</returnvalue> </para> <para> - Converts a size in human-readable format (as returned - by <function>pg_size_pretty</function>) into bytes. + Converts a size in human-readable format (as returned by + <function>pg_size_pretty</function>) into bytes. Valid units are + <literal>bytes</literal>, <literal>B</literal>, <literal>kB</literal>, + <literal>MB</literal>, <literal>GB</literal>, <literal>TB</literal>, + and <literal>PB</literal>. </para></entry> </row> @@ -27185,7 +27188,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, kB, MB, GB, TB, etc. as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..8d5ca41c8b 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -46,7 +46,7 @@ struct size_pretty_unit * unit */ }; -/* When adding units here also update the error message in pg_size_bytes */ +/* When adding units here also update the docs and the error message in pg_size_bytes */ static const struct size_pretty_unit size_pretty_units[] = { {"bytes", 10 * 1024, false, 0}, {"kB", 20 * 1024 - 1, true, 10}, @@ -57,6 +57,19 @@ static const struct size_pretty_unit size_pretty_units[] = { {NULL, 0, false, 0} }; +/* Additional unit aliases acceted by pg_size_bytes */ +struct size_bytes_unit_alias +{ + const char *alias; + int unit_index; /* corresponding size_pretty_units element */ +}; + +/* When adding units here also update the docs and the error message in pg_size_bytes */ +static const struct size_bytes_unit_alias size_bytes_aliases[] = { + {"B", 0}, + {NULL} +}; + /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 db_dir_size(const char *path) @@ -801,9 +814,19 @@ pg_size_bytes(PG_FUNCTION_ARGS) { /* Parse the unit case-insensitively */ if (pg_strcasecmp(strptr, unit->name) == 0) - { - multiplier = ((int64) 1) << unit->unitbits; break; + } + + /* If not found, look in table of aliases */ + if (unit->name == NULL) + { + for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++) + { + if (pg_strcasecmp(strptr, a->alias) == 0) + { + unit = &size_pretty_units[a->unit_index]; + break; + } } } @@ -813,7 +836,9 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + + multiplier = ((int64) 1) << unit->unitbits; if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: 46d490ac19a7ca93a5c0f47e5a0e759b5385a8ae -- 2.39.2 Attachments: [text/plain] v4-0001-Add-support-for-unit-B-to-pg_size_bytes.patch (7.4K, ../../[email protected]/2-v4-0001-Add-support-for-unit-B-to-pg_size_bytes.patch) download | inline diff: From bb0fb6eb3364195838a9c7e387ee4237c8cd30b4 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <[email protected]> Date: Mon, 6 Mar 2023 09:10:50 +0100 Subject: [PATCH v4] Add support for unit "B" to pg_size_bytes() This makes it consistent with the units support in GUC. Discussion: https://www.postgresql.org/message-id/flat/0106914a-9eb5-22be-40d8-652cc88c827d%40enterprisedb.com --- doc/src/sgml/func.sgml | 9 +++++--- src/backend/utils/adt/dbsize.c | 33 ++++++++++++++++++++++++---- src/test/regress/expected/dbsize.out | 15 +++++++------ src/test/regress/sql/dbsize.sql | 2 +- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 97b3f1c1a6..fa5f60cf4c 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -27166,8 +27166,11 @@ <title>Database Object Size Functions</title> <returnvalue>bigint</returnvalue> </para> <para> - Converts a size in human-readable format (as returned - by <function>pg_size_pretty</function>) into bytes. + Converts a size in human-readable format (as returned by + <function>pg_size_pretty</function>) into bytes. Valid units are + <literal>bytes</literal>, <literal>B</literal>, <literal>kB</literal>, + <literal>MB</literal>, <literal>GB</literal>, <literal>TB</literal>, + and <literal>PB</literal>. </para></entry> </row> @@ -27185,7 +27188,7 @@ <title>Database Object Size Functions</title> </para> <para> Converts a size in bytes into a more easily human-readable format with - size units (bytes, kB, MB, GB or TB as appropriate). Note that the + size units (bytes, kB, MB, GB, TB, etc. as appropriate). Note that the units are powers of 2 rather than powers of 10, so 1kB is 1024 bytes, 1MB is 1024<superscript>2</superscript> = 1048576 bytes, and so on. </para></entry> diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dbd404101f..8d5ca41c8b 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -46,7 +46,7 @@ struct size_pretty_unit * unit */ }; -/* When adding units here also update the error message in pg_size_bytes */ +/* When adding units here also update the docs and the error message in pg_size_bytes */ static const struct size_pretty_unit size_pretty_units[] = { {"bytes", 10 * 1024, false, 0}, {"kB", 20 * 1024 - 1, true, 10}, @@ -57,6 +57,19 @@ static const struct size_pretty_unit size_pretty_units[] = { {NULL, 0, false, 0} }; +/* Additional unit aliases acceted by pg_size_bytes */ +struct size_bytes_unit_alias +{ + const char *alias; + int unit_index; /* corresponding size_pretty_units element */ +}; + +/* When adding units here also update the docs and the error message in pg_size_bytes */ +static const struct size_bytes_unit_alias size_bytes_aliases[] = { + {"B", 0}, + {NULL} +}; + /* Return physical size of directory contents, or 0 if dir doesn't exist */ static int64 db_dir_size(const char *path) @@ -801,9 +814,19 @@ pg_size_bytes(PG_FUNCTION_ARGS) { /* Parse the unit case-insensitively */ if (pg_strcasecmp(strptr, unit->name) == 0) - { - multiplier = ((int64) 1) << unit->unitbits; break; + } + + /* If not found, look in table of aliases */ + if (unit->name == NULL) + { + for (const struct size_bytes_unit_alias *a = size_bytes_aliases; a->alias != NULL; a++) + { + if (pg_strcasecmp(strptr, a->alias) == 0) + { + unit = &size_pretty_units[a->unit_index]; + break; + } } } @@ -813,7 +836,9 @@ pg_size_bytes(PG_FUNCTION_ARGS) (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid size: \"%s\"", text_to_cstring(arg)), errdetail("Invalid size unit: \"%s\".", strptr), - errhint("Valid units are \"bytes\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + errhint("Valid units are \"bytes\", \"B\", \"kB\", \"MB\", \"GB\", \"TB\", and \"PB\"."))); + + multiplier = ((int64) 1) << unit->unitbits; if (multiplier > 1) { diff --git a/src/test/regress/expected/dbsize.out b/src/test/regress/expected/dbsize.out index d8d6686b5f..f1121a87aa 100644 --- a/src/test/regress/expected/dbsize.out +++ b/src/test/regress/expected/dbsize.out @@ -81,12 +81,13 @@ SELECT size, pg_size_pretty(size), pg_size_pretty(-1 * size) FROM -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); size | pg_size_bytes ----------+-------------------- 1 | 1 123bytes | 123 + 256 B | 256 1kB | 1024 1MB | 1048576 1 GB | 1073741824 @@ -95,7 +96,7 @@ SELECT size, pg_size_bytes(size) FROM 3000 TB | 3298534883328000 1e6 MB | 1048576000000 99 PB | 111464090777419776 -(10 rows) +(11 rows) -- case-insensitive units are supported SELECT size, pg_size_bytes(size) FROM @@ -153,15 +154,15 @@ SELECT size, pg_size_bytes(size) FROM SELECT pg_size_bytes('1 AB'); ERROR: invalid size: "1 AB" DETAIL: Invalid size unit: "AB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A'); ERROR: invalid size: "1 AB A" DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('1 AB A '); ERROR: invalid size: "1 AB A " DETAIL: Invalid size unit: "AB A". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('9223372036854775807.9'); ERROR: bigint out of range SELECT pg_size_bytes('1e100'); @@ -171,7 +172,7 @@ ERROR: value overflows numeric format SELECT pg_size_bytes('1 byte'); -- the singular "byte" is not supported ERROR: invalid size: "1 byte" DETAIL: Invalid size unit: "byte". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes(''); ERROR: invalid size: "" SELECT pg_size_bytes('kb'); @@ -189,6 +190,6 @@ ERROR: invalid size: ".+912" SELECT pg_size_bytes('+912+ kB'); ERROR: invalid size: "+912+ kB" DETAIL: Invalid size unit: "+ kB". -HINT: Valid units are "bytes", "kB", "MB", "GB", "TB", and "PB". +HINT: Valid units are "bytes", "B", "kB", "MB", "GB", "TB", and "PB". SELECT pg_size_bytes('++123 kB'); ERROR: invalid size: "++123 kB" diff --git a/src/test/regress/sql/dbsize.sql b/src/test/regress/sql/dbsize.sql index 7df865271b..b34cf33385 100644 --- a/src/test/regress/sql/dbsize.sql +++ b/src/test/regress/sql/dbsize.sql @@ -29,7 +29,7 @@ -- pg_size_bytes() tests SELECT size, pg_size_bytes(size) FROM - (VALUES ('1'), ('123bytes'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), + (VALUES ('1'), ('123bytes'), ('256 B'), ('1kB'), ('1MB'), (' 1 GB'), ('1.5 GB '), ('1TB'), ('3000 TB'), ('1e6 MB'), ('99 PB')) x(size); -- case-insensitive units are supported base-commit: 46d490ac19a7ca93a5c0f47e5a0e759b5385a8ae -- 2.39.2 ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-03-06 08:27 David Rowley <[email protected]> parent: Peter Eisentraut <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: David Rowley @ 2023-03-06 08:27 UTC (permalink / raw) To: Peter Eisentraut <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On Mon, 6 Mar 2023 at 21:13, Peter Eisentraut <[email protected]> wrote: > > On 02.03.23 20:58, David Rowley wrote: > > I think I'd prefer to see the size_bytes_unit_alias struct have an > > index into size_pretty_units[] array. i.e: > > Ok, done that way. (I had thought about that, but I was worried that > that would be too error-prone to maintain. But I suppose the tables > don't change that often, and test cases would easily catch mistakes.) Patch looks pretty good. I just see a small spelling mistake in: +/* Additional unit aliases acceted by pg_size_bytes */ > I also updated the documentation a bit more. I see I must have forgotten to add PB to the docs when pg_size_pretty had that unit added. I guess you added the "etc" to fix that? I'm wondering if that's the right choice. You modified the comment above size_pretty_units[] to remind us to update the docs when adding units, but the docs now say "etc", so do we need to? I'd likely have gone with just adding "PB" to the docs, that way it's pretty clear that new units need to be mentioned in the docs. David ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-03-07 20:22 Peter Eisentraut <[email protected]> parent: David Rowley <[email protected]> 0 siblings, 1 reply; 9+ messages in thread From: Peter Eisentraut @ 2023-03-07 20:22 UTC (permalink / raw) To: David Rowley <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On 06.03.23 09:27, David Rowley wrote: > On Mon, 6 Mar 2023 at 21:13, Peter Eisentraut > <[email protected]> wrote: >> >> On 02.03.23 20:58, David Rowley wrote: >>> I think I'd prefer to see the size_bytes_unit_alias struct have an >>> index into size_pretty_units[] array. i.e: >> >> Ok, done that way. (I had thought about that, but I was worried that >> that would be too error-prone to maintain. But I suppose the tables >> don't change that often, and test cases would easily catch mistakes.) > > Patch looks pretty good. I just see a small spelling mistake in: > > +/* Additional unit aliases acceted by pg_size_bytes */ > >> I also updated the documentation a bit more. > > I see I must have forgotten to add PB to the docs when pg_size_pretty > had that unit added. I guess you added the "etc" to fix that? I'm > wondering if that's the right choice. You modified the comment above > size_pretty_units[] to remind us to update the docs when adding units, > but the docs now say "etc", so do we need to? I'd likely have gone > with just adding "PB" to the docs, that way it's pretty clear that new > units need to be mentioned in the docs. Ok, I have fixed the original documentation to that effect and backpatched it. The remaining patch has been updated accordingly and committed also. ^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: Add support for unit "B" to pg_size_pretty() @ 2023-03-07 21:25 David Rowley <[email protected]> parent: Peter Eisentraut <[email protected]> 0 siblings, 0 replies; 9+ messages in thread From: David Rowley @ 2023-03-07 21:25 UTC (permalink / raw) To: Peter Eisentraut <[email protected]>; +Cc: Justin Pryzby <[email protected]>; pgsql-hackers On Wed, 8 Mar 2023 at 09:22, Peter Eisentraut <[email protected]> wrote: > Ok, I have fixed the original documentation to that effect and > backpatched it. Thanks for fixing that. David ^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2023-03-07 21:25 UTC | newest] Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2020-06-06 22:42 [PATCH v3 1/3] Allow CREATE INDEX CONCURRENTLY on partitioned table Justin Pryzby <[email protected]> 2023-02-21 23:47 Re: Add support for unit "B" to pg_size_pretty() Peter Eisentraut <[email protected]> 2023-02-22 02:39 ` Re: Add support for unit "B" to pg_size_pretty() David Rowley <[email protected]> 2023-02-27 08:34 ` Re: Add support for unit "B" to pg_size_pretty() Peter Eisentraut <[email protected]> 2023-03-02 19:58 ` Re: Add support for unit "B" to pg_size_pretty() David Rowley <[email protected]> 2023-03-06 08:13 ` Re: Add support for unit "B" to pg_size_pretty() Peter Eisentraut <[email protected]> 2023-03-06 08:27 ` Re: Add support for unit "B" to pg_size_pretty() David Rowley <[email protected]> 2023-03-07 20:22 ` Re: Add support for unit "B" to pg_size_pretty() Peter Eisentraut <[email protected]> 2023-03-07 21:25 ` Re: Add support for unit "B" to pg_size_pretty() David Rowley <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox