From 3c05ee8234ce5954983ca6455d8b6aa23665342e Mon Sep 17 00:00:00 2001 From: Dmitry Yemanov Date: Thu, 26 Nov 2026 12:31:27 +0300 Subject: [PATCH 1/5] Tablespace support for gstat --- doc/README.tablespaces | 26 +- src/common/IntlParametersBlock.cpp | 2 + src/common/classes/ClumpletReader.cpp | 1 + src/include/firebird/impl/consts_pub.h | 2 + src/include/firebird/impl/msg/gstat.h | 4 + src/include/gen/Firebird.pas | 2 + src/jrd/svc.cpp | 1 + src/utilities/fbsvcmgr/fbsvcmgr.cpp | 2 + src/utilities/gstat/dba.epp | 371 +++++++++++++++++++++---- src/utilities/gstat/dbaswi.h | 3 + 10 files changed, 354 insertions(+), 60 deletions(-) diff --git a/doc/README.tablespaces b/doc/README.tablespaces index 9ad1660e475..ea1c86f8c0a 100644 --- a/doc/README.tablespaces +++ b/doc/README.tablespaces @@ -131,9 +131,33 @@ UTILITIES 3. ISQL SHOW {TABLESPACES | TABLESPACE } + Displays a list of all tablespaces names in alphabetical order or information about the specified tablespace. - 4. Replication + 4. GSTAT + -ts [tablespace ...] + Reports the tablespaces and limits the analysis to the objects located in them. + Without tablespace names all user-defined tablespaces are reported. + The names are case sensitive, "PRIMARY" can be used to reference the main database file, + and an unknown name is reported as an error. + The switch combines with the other ones: -ts selects the locations, -t and -sch select + the objects, and both are applied. + + Before the analysis a "Tablespaces:" section lists every reported tablespace, in ascending + order of their IDs, with the file where it is located and the tables and indices it contains. + + A tablespace containing no objects is marked as empty. The tables are listed even when their + data pages are not analyzed (for example, with -ts -i), because their location is known + from the metadata. When no tablespace matches the selection, the section is not printed. + + A table whose data is located outside the selected tablespaces is still reported when one of + its indices matches the selection, but its data pages are not analyzed. + + The same is available through the services API, using the action_db_stats options + sts_tablespace (can be repeated) and sts_user_tablespaces, the latter being + equivalent to -ts without names. + + 5. Replication There is an apply_tablespaces_ddl parameter for replication, it may be configured for the replica side. If this parameter is disabled, CREATE/ALTER/DROP TSABLESPACE statements will be ignored on the replica and tablespace-related clauses in CREATE/ALTER TABLE/INDEX statements will also be ignored. diff --git a/src/common/IntlParametersBlock.cpp b/src/common/IntlParametersBlock.cpp index 7830b40a181..b561cf1dae1 100644 --- a/src/common/IntlParametersBlock.cpp +++ b/src/common/IntlParametersBlock.cpp @@ -324,6 +324,8 @@ IntlParametersBlock::TagType IntlSpbStart::checkTag(UCHAR tag, const char** tagN return TAG_STRING; FB_IPB_TAG(isc_spb_sts_schema); return TAG_STRING; + FB_IPB_TAG(isc_spb_sts_tablespace); + return TAG_STRING; FB_IPB_TAG(isc_spb_command_line); return TAG_COMMAND_LINE; } diff --git a/src/common/classes/ClumpletReader.cpp b/src/common/classes/ClumpletReader.cpp index 473aee40775..e0d4f834d39 100644 --- a/src/common/classes/ClumpletReader.cpp +++ b/src/common/classes/ClumpletReader.cpp @@ -428,6 +428,7 @@ ClumpletReader::ClumpletType ClumpletReader::getClumpletType(UCHAR tag) const case isc_spb_command_line: case isc_spb_sts_table: case isc_spb_sts_schema: + case isc_spb_sts_tablespace: return StringSpb; case isc_spb_options: return IntSpb; diff --git a/src/include/firebird/impl/consts_pub.h b/src/include/firebird/impl/consts_pub.h index 4e96a0b1468..d7404bf0e94 100644 --- a/src/include/firebird/impl/consts_pub.h +++ b/src/include/firebird/impl/consts_pub.h @@ -623,6 +623,7 @@ #define isc_spb_sts_table 64 #define isc_spb_sts_schema 65 +#define isc_spb_sts_tablespace 66 #define isc_spb_sts_data_pages 0x01 #define isc_spb_sts_db_log 0x02 @@ -633,6 +634,7 @@ //#define isc_spb_sts_table 0x40 #define isc_spb_sts_nocreation 0x80 #define isc_spb_sts_encryption 0x100 +#define isc_spb_sts_user_tablespaces 0x200 /***********************************/ /* Server configuration key values */ diff --git a/src/include/firebird/impl/msg/gstat.h b/src/include/firebird/impl/msg/gstat.h index 1d54a54604a..630acb7186a 100644 --- a/src/include/firebird/impl/msg/gstat.h +++ b/src/include/firebird/impl/msg/gstat.h @@ -65,3 +65,7 @@ FB_IMPL_MSG_NO_SYMBOL(GSTAT, 64, " Level @1: @2, total length: @3, blob p FB_IMPL_MSG_NO_SYMBOL(GSTAT, 65, " -sch schemaname (case sensitive)") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 66, "option -sch needs a schema name") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 67, "option -sch got a too long schema name @1") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 68, " -ts tablespacename (case sensitive)") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 69, "option -ts accepts several tablespace names only if used after ") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 70, "tablespace @1 not found") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 71, "option -ts got a too long tablespace name @1") diff --git a/src/include/gen/Firebird.pas b/src/include/gen/Firebird.pas index fe1a38d1486..087f3c5f5e9 100644 --- a/src/include/gen/Firebird.pas +++ b/src/include/gen/Firebird.pas @@ -4624,6 +4624,7 @@ IPerformanceStatsImpl = class(IPerformanceStats) isc_spb_num_db = byte(6); isc_spb_sts_table = byte(64); isc_spb_sts_schema = byte(65); + isc_spb_sts_tablespace = byte(66); isc_spb_sts_data_pages = $01; isc_spb_sts_db_log = $02; isc_spb_sts_hdr_pages = $04; @@ -4632,6 +4633,7 @@ IPerformanceStatsImpl = class(IPerformanceStats) isc_spb_sts_record_versions = $20; isc_spb_sts_nocreation = $80; isc_spb_sts_encryption = $100; + isc_spb_sts_user_tablespaces = $200; isc_spb_nbk_level = byte(5); isc_spb_nbk_file = byte(6); isc_spb_nbk_direct = byte(7); diff --git a/src/jrd/svc.cpp b/src/jrd/svc.cpp index 42760c5743e..e5831e38bcd 100644 --- a/src/jrd/svc.cpp +++ b/src/jrd/svc.cpp @@ -2876,6 +2876,7 @@ bool Service::process_switches(ClumpletReader& spb, string& switches) { case isc_spb_sts_table: case isc_spb_sts_schema: + case isc_spb_sts_tablespace: if (!get_action_svc_parameter(spb.getClumpTag(), dba_in_sw_table, switches)) { return false; diff --git a/src/utilities/fbsvcmgr/fbsvcmgr.cpp b/src/utilities/fbsvcmgr/fbsvcmgr.cpp index 89fd0abd052..a0ee65dc846 100644 --- a/src/utilities/fbsvcmgr/fbsvcmgr.cpp +++ b/src/utilities/fbsvcmgr/fbsvcmgr.cpp @@ -538,11 +538,13 @@ constexpr SvcSwitches statisticsOptions[] = {"sts_nocreation", putOption, 0, isc_spb_sts_nocreation, 0}, {"sts_schema", putStringArgument, 0, isc_spb_sts_schema, 0}, {"sts_table", putStringArgument, 0, isc_spb_sts_table, 0}, + {"sts_tablespace", putStringArgument, 0, isc_spb_sts_tablespace, 0}, {"sts_data_pages", putOption, 0, isc_spb_sts_data_pages, 0}, {"sts_hdr_pages", putOption, 0, isc_spb_sts_hdr_pages, 0}, {"sts_idx_pages", putOption, 0, isc_spb_sts_idx_pages, 0}, {"sts_sys_relations", putOption, 0, isc_spb_sts_sys_relations, 0}, {"sts_encryption", putOption, 0, isc_spb_sts_encryption, 0}, + {"sts_user_tablespaces", putOption, 0, isc_spb_sts_user_tablespaces, 0}, {0, 0, 0, 0, 0} }; diff --git a/src/utilities/gstat/dba.epp b/src/utilities/gstat/dba.epp index 8abd3a80dfa..349ce34080a 100644 --- a/src/utilities/gstat/dba.epp +++ b/src/utilities/gstat/dba.epp @@ -232,17 +232,48 @@ static void print_help(); #include "../utilities/gstat/dba_proto.h" +namespace +{ + + // Check the length of a name specified for the -t, -sch or -ts switch. + // Names longer than the metadata can store are reported truncated. + + void checkNameLength(const char* name, USHORT msg) + { + if (strlen(name) <= MAX_SQL_IDENTIFIER_LEN) + return; + + char shortName[MAX_SQL_IDENTIFIER_LEN + 5]; + fb_utils::copy_terminate(shortName, name, MAX_SQL_IDENTIFIER_SIZE); + memcpy(shortName + MAX_SQL_IDENTIFIER_LEN, "...\0", 4); + dba_error(msg, SafeArg() << shortName); + } + +} // namespace + + struct dba_mem { char* memory; dba_mem* mem_next; }; -struct TableSpace +struct Tablespace { ULONG id = INVALID_PAGE_SPACE; MetaString name; dba_fil* file = nullptr; + + // Used for SortedArray::find + static ULONG generate(const Tablespace* tablespace) { return tablespace->id; } +}; + +// Tablespace explicitly specified by the user with the -ts switch + +struct SpecifiedTablespace +{ + ULONG id = INVALID_PAGE_SPACE; + MetaString name; }; // threading declarations for thread data @@ -251,13 +282,16 @@ class tdba final : public ThreadData { public: explicit tdba(Firebird::UtilSvc* us) - : ThreadData(tddDBA), uSvc(us), tablespaces(us->getPool()) + : ThreadData(tddDBA), uSvc(us), tablespaces(us->getPool()), + specifiedTablespaces(us->getPool()) { dba_status = dba_status_vector; } Firebird::UtilSvc* uSvc; - Firebird::Array tablespaces; + Firebird::Array tablespaces; + Firebird::Array specifiedTablespaces; + bool sw_tablespace = false; // the -ts switch is in effect dba_rel* relations = nullptr; dba_schema* schemas = nullptr; USHORT page_size = 0; @@ -276,18 +310,211 @@ public: { return (tdba*) ThreadData::getSpecific(); } + static inline void putSpecific(tdba* &tddba, tdba* thd_context) { tddba = thd_context; tddba->ThreadData::putSpecific(); } + static inline void restoreSpecific() { ThreadData::restoreSpecific(); } + + // Tablespace support: handling of the -ts switch, id lookup, filtering of + // the objects it selects and the "Tablespaces:" report + + void specifyTablespace(const char* name); + ULONG findTablespaceId(const char* name) const; + bool checkTablespaceFilter(ULONG pageSpaceId) const; + void printRelationHeader(const dba_rel* relation) const; + void printTablespacesInfo() const; + +private: + bool printTablespaceTables(ULONG tablespaceId) const; + bool printTablespaceIndices(ULONG tablespaceId) const; + void printTablespaceInfo(const Tablespace& tablespace) const; }; +// Remember a tablespace specified with the -ts switch. Its id is resolved later, +// when RDB$TABLESPACES is read. + +void tdba::specifyTablespace(const char* name) +{ + checkNameLength(name, 71); + // msg 71: option -ts got a too long tablespace name @1 + + const MetaString tsName(name); + + for (const auto& tablespace : specifiedTablespaces) + { + if (tablespace.name == tsName) + return; // the same tablespace may be specified more than once + } + + specifiedTablespaces.add({INVALID_PAGE_SPACE, tsName}); +} + + +// Find the id of a tablespace by its name. Objects that have no tablespace +// assigned are always located in the primary tablespace. + +ULONG tdba::findTablespaceId(const char* name) const +{ + if (!name) + return DB_PAGE_SPACE; + + for (const auto& tablespace : tablespaces) + { + if (tablespace.name == name) + return tablespace.id; + } + + // The metadata refers to a tablespace that doesn't exist. Better report the + // object as located in an unknown tablespace than attribute it to a real one. + + fb_assert(false); + return INVALID_PAGE_SPACE; +} + + +// Check if the objects located in the given tablespace should be analyzed and +// printed when the -ts switch is in effect. When no tablespace names are given, +// everything but the primary tablespace is included. + +bool tdba::checkTablespaceFilter(ULONG pageSpaceId) const +{ + if (!sw_tablespace) + return true; + + if (specifiedTablespaces.isEmpty()) + return (pageSpaceId != DB_PAGE_SPACE); + + for (const auto& tablespace : specifiedTablespaces) + { + if (tablespace.id == pageSpaceId) + return true; + } + + return false; +} + + +// Print the header of a relation + +void tdba::printRelationHeader(const dba_rel* relation) const +{ + uSvc->printf(false, "%s (%d)\n", + QualifiedMetaString(relation->rel_name, relation->rel_schema).toQuotedString().c_str(), + relation->rel_id); +} + + +// Print the tables located in the given tablespace. Relations named with -t but +// missing in the database are skipped, only the ones read from the metadata have +// a tablespace assigned. + +bool tdba::printTablespaceTables(ULONG tablespaceId) const +{ + bool printed = false; + + for (const dba_rel* relation = relations; relation; relation = relation->rel_next) + { + if (relation->rel_id == -1 || relation->rel_pagespace_id != tablespaceId) + continue; + + if (!printed) + { + uSvc->printf(false, " Tables:\n"); + printed = true; + } + + uSvc->printf(false, "\t%s\n", + QualifiedMetaString(relation->rel_name, relation->rel_schema) + .toQuotedString().c_str()); + } + + return printed; +} + + +// Print the indices located in the given tablespace + +bool tdba::printTablespaceIndices(ULONG tablespaceId) const +{ + bool printed = false; + + for (const dba_rel* relation = relations; relation; relation = relation->rel_next) + { + for (const dba_idx* index = relation->rel_indexes; index; index = index->idx_next) + { + if (index->idx_pagespace_id != tablespaceId) + continue; + + if (!printed) + { + uSvc->printf(false, " Indices:\n"); + printed = true; + } + + uSvc->printf(false, "\t%s (%s)\n", + QualifiedMetaString(index->idx_name, relation->rel_schema) + .toQuotedString().c_str(), + QualifiedMetaString(relation->rel_name, relation->rel_schema) + .toQuotedString().c_str()); + } + } + + return printed; +} + + +// Print a tablespace along with the objects it contains + +void tdba::printTablespaceInfo(const Tablespace& tablespace) const +{ + uSvc->printf(false, "%s (%u)\n", tablespace.name.c_str(), tablespace.id); + uSvc->printf(false, " Full path: %s\n", tablespace.file->fil_string); + + // The tablespace of a table is known from its metadata, therefore the tables + // are reported even when their data pages are not analyzed + + const bool printedTables = printTablespaceTables(tablespace.id); + const bool printedIndices = printTablespaceIndices(tablespace.id); + + if (!printedTables && !printedIndices) + uSvc->printf(false, " Tablespace is empty\n"); + + uSvc->printf(false, "\n"); +} + + +// Print the tablespaces selected with the -ts switch or all of them + +void tdba::printTablespacesInfo() const +{ + // Keep the tablespaces sorted by their ids to make the output predictable + SortedArray, ULONG, Tablespace> + selectedTablespaces(uSvc->getPool()); + + for (const auto& tablespace : tablespaces) + { + if (checkTablespaceFilter(tablespace.id)) + selectedTablespaces.add(&tablespace); + } + + if (!selectedTablespaces.hasData()) + return; + + uSvc->printf(false, "\nTablespaces:\n"); + + for (const auto& tablespace : selectedTablespaces) + printTablespaceInfo(*tablespace); +} + + [[noreturn]] void inline dba_exit(int code, tdba* tddba) { tddba->exit_code = code; @@ -583,14 +810,7 @@ int gstat(Firebird::UtilSvc* uSvc) sw_relation = true; while (argv < end && **argv != '-') { - if (strlen(*argv) > MAX_SQL_IDENTIFIER_LEN) - { - char tbname[MAX_SQL_IDENTIFIER_LEN + 5]; - fb_utils::copy_terminate(tbname, *argv, MAX_SQL_IDENTIFIER_SIZE); - memcpy(tbname + MAX_SQL_IDENTIFIER_LEN, "...\0", 4); - dba_error(42, SafeArg() << tbname); // option -t got a too long table name @1 - break; - } + checkNameLength(*argv, 42); // option -t got a too long table name @1 dba_rel* relation = (dba_rel*) alloc(sizeof(struct dba_rel)); strcpy(relation->rel_name, *argv++); @@ -603,7 +823,7 @@ int gstat(Firebird::UtilSvc* uSvc) *next = relation; // CVC: If the db name wasn't given yet, only one table name can be specified - // after -t to avoid ambiguity. + // after -t to avoid ambiguity if (!name) break; } @@ -613,14 +833,7 @@ int gstat(Firebird::UtilSvc* uSvc) sw_schema = true; while (argv < end && **argv != '-') { - if (strlen(*argv) > MAX_SQL_IDENTIFIER_LEN) - { - char schname[MAX_SQL_IDENTIFIER_LEN + 5]; - fb_utils::copy_terminate(schname, *argv, MAX_SQL_IDENTIFIER_SIZE); - memcpy(schname + MAX_SQL_IDENTIFIER_LEN, "...\0", 4); - dba_error(67, SafeArg() << schname); // option -sch got a too long schema name @1 - break; - } + checkNameLength(*argv, 67); // option -sch got a too long schema name @1 dba_schema* schema = (dba_schema*) alloc(sizeof(struct dba_schema)); strcpy(schema->name, *argv++); @@ -632,7 +845,20 @@ int gstat(Firebird::UtilSvc* uSvc) *next = schema; // If the db name wasn't given yet, only one schema name can be specified - // after -s to avoid ambiguity. + // after -s to avoid ambiguity + if (!name) + break; + } + break; + + case IN_SW_DBA_TABLESPACE: + tddba->sw_tablespace = true; + while (argv < end && **argv != '-') + { + tddba->specifyTablespace(*argv++); + + // If the db name wasn't given yet, only one tablespace name can be specified + // after -ts to avoid ambiguity if (!name) break; } @@ -859,7 +1085,38 @@ int gstat(Firebird::UtilSvc* uSvc) isc_req_handle request3 = 0; isc_req_handle request4 = 0; isc_req_handle request5 = 0; - isc_req_handle request6 = 0; + + // Open the tablespace files and resolve the tablespaces specified with -ts + + FOR(TRANSACTION_HANDLE transact1 REQUEST_HANDLE request5) + TS IN RDB$TABLESPACES + { + const char* const tsName = fb_utils::exact_name(TS.RDB$TABLESPACE_NAME); + const char* const tsFileName = fb_utils::exact_name(TS.RDB$FILE_NAME); + db_open(TS.RDB$TABLESPACE_ID, tsName, tsFileName); + + for (auto& tablespace : tddba->specifiedTablespaces) + { + if (tablespace.name == tsName) + tablespace.id = TS.RDB$TABLESPACE_ID; + } + } + END_FOR + ON_ERROR + dba_exit(FINI_ERROR, tddba); + END_ERROR + + if (tddba->sw_tablespace) + { + for (const auto& tablespace : tddba->specifiedTablespaces) + { + if (tablespace.id == INVALID_PAGE_SPACE) + { + dba_error(70, SafeArg() << tablespace.name.c_str()); + // msg 70: tablespace @1 not found + } + } + } FOR(TRANSACTION_HANDLE transact1 REQUEST_HANDLE request1) X IN RDB$RELATIONS @@ -946,18 +1203,9 @@ int gstat(Firebird::UtilSvc* uSvc) dba_exit(FINI_ERROR, tddba); END_ERROR - relation->rel_pagespace_id = DB_PAGE_SPACE; - - FOR(TRANSACTION_HANDLE transact1 REQUEST_HANDLE request5) - TS IN RDB$TABLESPACES - WITH TS.RDB$TABLESPACE_NAME = X.RDB$TABLESPACE_NAME - { - relation->rel_pagespace_id = TS.RDB$TABLESPACE_ID; - } - END_FOR - ON_ERROR - dba_exit(FINI_ERROR, tddba); - END_ERROR + const char* const relationTsName = X.RDB$TABLESPACE_NAME.NULL ? + nullptr : fb_utils::exact_name(X.RDB$TABLESPACE_NAME); + relation->rel_pagespace_id = tddba->findTablespaceId(relationTsName); if (sw_index) { @@ -970,12 +1218,20 @@ int gstat(Firebird::UtilSvc* uSvc) if (Y.RDB$INDEX_INACTIVE) continue; + const char* const indexTsName = Y.RDB$TABLESPACE_NAME.NULL ? + nullptr : fb_utils::exact_name(Y.RDB$TABLESPACE_NAME); + const ULONG indexPagespaceId = tddba->findTablespaceId(indexTsName); + + if (!tddba->checkTablespaceFilter(indexPagespaceId)) + continue; + dba_idx* index = (dba_idx*) alloc(sizeof(struct dba_idx)); index->idx_next = relation->rel_indexes; relation->rel_indexes = index; strcpy(index->idx_name, Y.RDB$INDEX_NAME); fb_utils::exact_name(index->idx_name); index->idx_id = Y.RDB$INDEX_ID - 1; + index->idx_pagespace_id = indexPagespaceId; } END_FOR ON_ERROR @@ -1012,18 +1268,6 @@ int gstat(Firebird::UtilSvc* uSvc) dba_exit(FINI_ERROR, tddba); END_ERROR - FOR(TRANSACTION_HANDLE transact1 REQUEST_HANDLE request6) - TS IN RDB$TABLESPACES - { - const char* const tsName = fb_utils::exact_name(TS.RDB$TABLESPACE_NAME); - const char* const fileName = fb_utils::exact_name(TS.RDB$FILE_NAME); - db_open(TS.RDB$TABLESPACE_ID, tsName, fileName); - } - END_FOR - ON_ERROR - dba_exit(FINI_ERROR, tddba); - END_ERROR - auto getTablespaceName = [&tddba](ULONG id) -> const char* { for (const auto& tablespace : tddba->tablespaces) @@ -1051,9 +1295,6 @@ int gstat(Firebird::UtilSvc* uSvc) if (request5) isc_release_request(status_vector, &request5); - if (request6) - isc_release_request(status_vector, &request6); - COMMIT transact1; ON_ERROR dba_exit(FINI_ERROR, tddba); @@ -1088,6 +1329,8 @@ int gstat(Firebird::UtilSvc* uSvc) } } + tddba->printTablespacesInfo(); + dba_print(false, 10); // msg 10: \nAnalyzing database pages ...\n @@ -1102,9 +1345,9 @@ int gstat(Firebird::UtilSvc* uSvc) continue; } - if (sw_data) { + if (sw_data && tddba->checkTablespaceFilter(relation->rel_pagespace_id)) analyze_data(relation, sw_record); - } + for (dba_idx* index = relation->rel_indexes; index; index = index->idx_next) { checkForShutdown(tddba); @@ -1116,16 +1359,13 @@ int gstat(Firebird::UtilSvc* uSvc) for (const dba_rel* relation = tddba->relations; relation; relation = relation->rel_next) { - if (relation->rel_id == -1) { + if (relation->rel_id == -1) continue; - } - - uSvc->printf(false, "%s (%d)\n", - QualifiedMetaString(relation->rel_name, relation->rel_schema).toQuotedString().c_str(), - relation->rel_id); - if (sw_data) + if (sw_data && tddba->checkTablespaceFilter(relation->rel_pagespace_id)) { + tddba->printRelationHeader(relation); + uSvc->printf(false, " Tablespace: %s\n", getTablespaceName(relation->rel_pagespace_id)); dba_print(false, 11, SafeArg() << relation->rel_pointer_page << relation->rel_index_root); @@ -1227,8 +1467,20 @@ int gstat(Firebird::UtilSvc* uSvc) dba_print(false, 13); // msg 13: " Fill distribution:" print_distribution("\t", relation->rel_fill_distribution); + uSvc->printf(false, "\n"); + } + else if (!tddba->sw_tablespace) + { + // Without -ts the table name is printed even when its data is not analyzed + tddba->printRelationHeader(relation); + uSvc->printf(false, "\n"); + } + else if (relation->rel_indexes) + { + // With -ts a table is reported even when only its indexes, already filtered + // when they were collected, are located in the selected tablespaces + tddba->printRelationHeader(relation); } - uSvc->printf(false, "\n"); for (const dba_idx* index = relation->rel_indexes; index; index = index->idx_next) { @@ -2348,4 +2600,5 @@ static void print_help() dba_print(true, in_sw_tab->in_sw_msg); } dba_print(true, 43); // option -t accepts... + dba_print(true, 69); // option -ts accepts... } diff --git a/src/utilities/gstat/dbaswi.h b/src/utilities/gstat/dbaswi.h index 9cdfde3d9b8..73f5136a246 100644 --- a/src/utilities/gstat/dbaswi.h +++ b/src/utilities/gstat/dbaswi.h @@ -52,6 +52,7 @@ inline constexpr int IN_SW_DBA_ENCRYPTION = 15; // analyze pages encryption inline constexpr int IN_SW_DBA_HELP = 16; // show help inline constexpr int IN_SW_DBA_ROLE = 17; // SQL role inline constexpr int IN_SW_DBA_SCHEMA = 18; // analyze specific schemas +inline constexpr int IN_SW_DBA_TABLESPACE = 19; // tablespace statistics inline constexpr static struct Switches::in_sw_tab_t dba_in_sw_table[] = { @@ -69,6 +70,8 @@ inline constexpr static struct Switches::in_sw_tab_t dba_in_sw_table[] = {IN_SW_DBA_SCHEMA, isc_spb_sts_schema, "SCHEMA", 0,0,0, false, false, 65, 1, NULL}, // msg 65: -sch schemaname (case sensitive) {IN_SW_DBA_RELATION, isc_spb_sts_table, "TABLE", 0,0,0, false, false, 35, 1, NULL}, // msg 35: -t tablename {IN_SW_DBA_RELATION, isc_spb_sts_table, "TABLE", 0,0,0, false, true, 0, 1, NULL}, // no msg: let run old buggy code + {IN_SW_DBA_TABLESPACE, isc_spb_sts_tablespace, "TS", 0,0,0, false, false, 68, 2, NULL}, // msg 68: -ts tablespacename (case sensitive) + {IN_SW_DBA_TABLESPACE, isc_spb_sts_user_tablespaces, "TS", 0,0,0, false, true, 0, 2, NULL}, // no msg: -ts without tablespace names {IN_SW_DBA_ROLE, 0, "ROLE", 0,0,0, false, false, 57, 1, NULL}, // msg 57: -role SQL role name // special switch to avoid including creation date, only for tests (no message) {IN_SW_DBA_NOCREATION, isc_spb_sts_nocreation, "NOCREATION", 0,0,0, false, true, 0, 1, NULL}, // msg (ignored) -n suppress creation date From 5e7ff34f75a2811ceab7853109b63161d58908fd Mon Sep 17 00:00:00 2001 From: Dmitry Yemanov Date: Thu, 3 Dec 2026 20:55:55 +0300 Subject: [PATCH 2/5] Report ambiguity between object name and database name --- doc/README.tablespaces | 3 +++ src/include/firebird/impl/msg/gstat.h | 1 + src/utilities/gstat/dba.epp | 22 +++++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/doc/README.tablespaces b/doc/README.tablespaces index ea1c86f8c0a..bb353a07e3b 100644 --- a/doc/README.tablespaces +++ b/doc/README.tablespaces @@ -140,6 +140,9 @@ UTILITIES Without tablespace names all user-defined tablespaces are reported. The names are case sensitive, "PRIMARY" can be used to reference the main database file, and an unknown name is reported as an error. + Tablespace names are accepted only after the database name, like the names of -t and -sch. + Used before it, the switch takes the next argument as a name and reports an error if that + leaves no database name. The switch combines with the other ones: -ts selects the locations, -t and -sch select the objects, and both are applied. diff --git a/src/include/firebird/impl/msg/gstat.h b/src/include/firebird/impl/msg/gstat.h index 630acb7186a..7e1fbb2971f 100644 --- a/src/include/firebird/impl/msg/gstat.h +++ b/src/include/firebird/impl/msg/gstat.h @@ -69,3 +69,4 @@ FB_IMPL_MSG_NO_SYMBOL(GSTAT, 68, " -ts tablespacename ") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 70, "tablespace @1 not found") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 71, "option -ts got a too long tablespace name @1") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 72, "found ambiguity between the object name and the database name for option -@1@") diff --git a/src/utilities/gstat/dba.epp b/src/utilities/gstat/dba.epp index 349ce34080a..f95285b7298 100644 --- a/src/utilities/gstat/dba.epp +++ b/src/utilities/gstat/dba.epp @@ -711,6 +711,11 @@ int gstat(Firebird::UtilSvc* uSvc) const Switches switches(dba_in_sw_table, FB_NELEM(dba_in_sw_table), false, true); const char* name = NULL; + // The last switch that took a word as a name while the database name wasn't given + // yet. That word could as well have been the database name, and the ambiguity + // becomes an error only if none is found by the end of the command line. + const char* ambiguousSwitch = NULL; + const TEXT* const* const end = argv + argc; ++argv; while (argv < end) @@ -825,7 +830,10 @@ int gstat(Firebird::UtilSvc* uSvc) // CVC: If the db name wasn't given yet, only one table name can be specified // after -t to avoid ambiguity if (!name) + { + ambiguousSwitch = str + 1; break; + } } break; @@ -845,9 +853,12 @@ int gstat(Firebird::UtilSvc* uSvc) *next = schema; // If the db name wasn't given yet, only one schema name can be specified - // after -s to avoid ambiguity + // after -sch to avoid ambiguity if (!name) + { + ambiguousSwitch = str + 1; break; + } } break; @@ -860,7 +871,10 @@ int gstat(Firebird::UtilSvc* uSvc) // If the db name wasn't given yet, only one tablespace name can be specified // after -ts to avoid ambiguity if (!name) + { + ambiguousSwitch = str + 1; break; + } } break; @@ -885,6 +899,12 @@ int gstat(Firebird::UtilSvc* uSvc) if (!name) { + if (ambiguousSwitch) + { + // msg 72: found ambiguity between the object name and the database name + dba_error(72, SafeArg() << ambiguousSwitch); + } + dba_error(2); // msg 2: please retry, giving a database name } From 1ccf649f57b8486cc9ac230e29839bd2e62823b7 Mon Sep 17 00:00:00 2001 From: Dmitry Yemanov Date: Thu, 3 Dec 2026 21:17:37 +0300 Subject: [PATCH 3/5] Fix the typo in the message text --- src/include/firebird/impl/msg/gstat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/include/firebird/impl/msg/gstat.h b/src/include/firebird/impl/msg/gstat.h index 7e1fbb2971f..29d147e2e5a 100644 --- a/src/include/firebird/impl/msg/gstat.h +++ b/src/include/firebird/impl/msg/gstat.h @@ -69,4 +69,4 @@ FB_IMPL_MSG_NO_SYMBOL(GSTAT, 68, " -ts tablespacename ") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 70, "tablespace @1 not found") FB_IMPL_MSG_NO_SYMBOL(GSTAT, 71, "option -ts got a too long tablespace name @1") -FB_IMPL_MSG_NO_SYMBOL(GSTAT, 72, "found ambiguity between the object name and the database name for option -@1@") +FB_IMPL_MSG_NO_SYMBOL(GSTAT, 72, "found ambiguity between the object name and the database name for option -@1") From c10bd6b122403c82f45092ffac26db4105e8ab87 Mon Sep 17 00:00:00 2001 From: Dmitry Yemanov Date: Sat, 5 Dec 2026 15:44:52 +0300 Subject: [PATCH 4/5] Add total/allocated size information to the tablespace output --- doc/README.tablespaces | 5 +- src/utilities/gstat/dba.epp | 122 +++++++++++++++++++++++++++++++----- 2 files changed, 109 insertions(+), 18 deletions(-) diff --git a/doc/README.tablespaces b/doc/README.tablespaces index bb353a07e3b..46bd1e7015d 100644 --- a/doc/README.tablespaces +++ b/doc/README.tablespaces @@ -147,7 +147,10 @@ UTILITIES the objects, and both are applied. Before the analysis a "Tablespaces:" section lists every reported tablespace, in ascending - order of their IDs, with the file where it is located and the tables and indices it contains. + order of their IDs, with the file where it is located, its size and the tables and indices it + contains. The size is given in pages: the total number of the pages in the file and the number + of the allocated ones, that is, the pages used by the system (the header, inventory and SCN + pages) and by the objects. A tablespace containing no objects is marked as empty. The tables are listed even when their data pages are not analyzed (for example, with -ts -i), because their location is known diff --git a/src/utilities/gstat/dba.epp b/src/utilities/gstat/dba.epp index f95285b7298..824e0b98b78 100644 --- a/src/utilities/gstat/dba.epp +++ b/src/utilities/gstat/dba.epp @@ -205,6 +205,7 @@ static ULONG analyze_fragments(dba_rel*, const rhdf*); static ULONG analyze_versions(dba_rel*, const rhdf*); static void analyze_index(const dba_rel*, dba_idx*); static ULONG lastUsedPage(ULONG); +static bool getAllocatedPages(ULONG, ULONG, ULONG&); #if (defined WIN_NT) [[noreturn]] static void db_error(SLONG); @@ -263,6 +264,7 @@ struct Tablespace ULONG id = INVALID_PAGE_SPACE; MetaString name; dba_fil* file = nullptr; + SINT64 file_size = 0; // Size of the tablespace file in bytes // Used for SortedArray::find static ULONG generate(const Tablespace* tablespace) { return tablespace->id; } @@ -297,7 +299,7 @@ public: USHORT page_size = 0; USHORT dp_per_pp = 0; USHORT max_records = 0; - ULONG page_number = 0; + PageNumber page_number; // Page cached in global_buffer, if any pag* buffer1 = nullptr; pag* buffer2 = nullptr; pag* global_buffer = nullptr; @@ -478,6 +480,25 @@ void tdba::printTablespaceInfo(const Tablespace& tablespace) const uSvc->printf(false, "%s (%u)\n", tablespace.name.c_str(), tablespace.id); uSvc->printf(false, " Full path: %s\n", tablespace.file->fil_string); + // The total number of the pages is taken from the size of the file, the number + // of the allocated ones - from the page inventory pages of the tablespace. The + // allocated pages include the system pages and the pages of all the objects, + // not only the ones selected with the -t, -sch or -ts switches + + const ULONG totalPages = + (tablespace.file_size > 0) ? (ULONG) (tablespace.file_size / page_size) : 0; + + ULONG allocatedPages = 0; + if (getAllocatedPages(tablespace.id, page_size, allocatedPages)) + { + uSvc->printf(false, " Total pages: %u, allocated pages: %u\n", + totalPages, allocatedPages); + } + else + { + uSvc->printf(false, " Total pages: %u\n", totalPages); + } + // The tablespace of a table is known from its metadata, therefore the tables // are reported even when their data pages are not analyzed @@ -963,7 +984,7 @@ int gstat(Firebird::UtilSvc* uSvc) alignas(DIRECT_IO_BLOCK_SIZE) SCHAR temp[MAX(RAW_HEADER_SIZE, DIRECT_IO_BLOCK_SIZE)]; tddba->page_size = MAX(RAW_HEADER_SIZE, DIRECT_IO_BLOCK_SIZE); tddba->global_buffer = (pag*) temp; - tddba->page_number = -1; + tddba->page_number.clear(); const header_page* header = (const header_page*) db_read(HEADER_PAGE_NUMBER); if (!Ods::isSupported(header)) @@ -995,7 +1016,7 @@ int gstat(Firebird::UtilSvc* uSvc) tddba->buffer2 = (pag*) (buff + tddba->page_size); tddba->global_buffer = (pag*) (buff + tddba->page_size * 2); } - tddba->page_number = -1; + tddba->page_number.clear(); // Print header page @@ -1635,7 +1656,10 @@ static ULONG lastUsedPage(ULONG pageSize) while (true) { const pag* page = db_read(PageNumber(DB_PAGE_SPACE, pipLast), true); - if (page->pag_type != pag_pages) + + // The last argument of db_read() allows it to return NULL instead of + // reporting an error when the page is beyond the end of the file + if (!page || page->pag_type != pag_pages) { dba_print(true, 61, SafeArg() << pipLast); // msg 61: " Expected page inventory page @1" @@ -1677,6 +1701,63 @@ static ULONG lastUsedPage(ULONG pageSize) } +// Get the number of the pages allocated in the given page space, walking its PIP chain + +static bool getAllocatedPages(ULONG pageSpaceId, ULONG pageSize, ULONG& allocatedPages) +{ + const ULONG pagesPerPIP = Ods::pagesPerPIP(pageSize); + const ULONG bytesBitPIP = Ods::bytesBitPIP(pageSize); + + allocatedPages = 0; + ULONG pipNumber = FIRST_PIP_PAGE; + + while (true) + { + const pag* const page = db_read(PageNumber(pageSpaceId, pipNumber), true); + + // The inventory of an encrypted database can't be read, only the total + // number of the pages is reported in this case + if (page && (page->pag_flags & Ods::crypted_page)) + return false; + + if (!page || page->pag_type != pag_pages) + { + dba_print(true, 61, SafeArg() << pipNumber); + // msg 61: " Expected page inventory page @1" + return false; + } + + const page_inv_page* const pip = (const page_inv_page*) page; + + // A cleared bit of the inventory means an allocated page, a set one means + // a free page. The pages beyond the high-water mark of the inventory have + // never been allocated, therefore their bits are not checked. + + const ULONG usedPages = MIN(pip->pip_used, pagesPerPIP); + + for (ULONG bit = 0; bit < usedPages; bit++) + { + if (!(pip->pip_bits[bit / 8] & (1 << (bit % 8)))) + allocatedPages++; + } + + // The next inventory page is the last page of the range of the current one, + // hence the chain ends when that page is free or when the range of the current + // inventory page is not fully allocated + + if (pip->pip_used != pagesPerPIP || (pip->pip_bits[bytesBitPIP - 1] & 0x80)) + break; + + if (pipNumber == FIRST_PIP_PAGE) + pipNumber = pagesPerPIP - 1; + else + pipNumber += pagesPerPIP; + } + + return true; +} + + static char* alloc(size_t size) { /************************************** @@ -2230,7 +2311,7 @@ static void db_close(dba_fil* file) TEXT s[128]; tdba* tddba = tdba::getSpecific(); - tddba->page_number = -1; + tddba->page_number.clear(); if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, @@ -2302,7 +2383,11 @@ static void db_open(ULONG pageSpaceId, const MetaString& name, const PathName& f db_error(GetLastError()); } - tddba->tablespaces.add({pageSpaceId, name, file}); + // Remember the size of the file to report the size of the tablespace + LARGE_INTEGER fileSize; + const SINT64 size = GetFileSizeEx(file->fil_desc, &fileSize) ? fileSize.QuadPart : 0; + + tddba->tablespaces.add({pageSpaceId, name, file, size}); } @@ -2323,12 +2408,11 @@ static const pag* db_read(const PageNumber& page_number, bool ok_enc) if (tddba->uSvc->finished()) dba_exit(FINI_OK, tddba); - ULONG pageNum = page_number.getPageNum(); - - if (tddba->page_number == pageNum) + if (tddba->page_number == page_number) return tddba->global_buffer; - tddba->page_number = pageNum; + tddba->page_number = page_number; + ULONG pageNum = page_number.getPageNum(); dba_fil* file = nullptr; for (const auto& tablespace : tddba->tablespaces) @@ -2415,7 +2499,7 @@ static void db_close(dba_fil* file) * **************************************/ tdba* tddba = tdba::getSpecific(); - tddba->page_number = -1; + tddba->page_number.clear(); // FIXME: The strerror() function returns the appropriate description // string, or an unknown error message if the error code is unknown. @@ -2465,7 +2549,12 @@ static void db_open(ULONG pageSpaceId, const MetaString& name, const PathName& f db_error(errno); } - tddba->tablespaces.add({pageSpaceId, name, file}); + // Remember the size of the file to report the size of the tablespace + SINT64 size = os_utils::lseek(file->fil_desc, 0, SEEK_END); + if (size < 0) + size = 0; + + tddba->tablespaces.add({pageSpaceId, name, file, size}); } @@ -2483,12 +2572,11 @@ static const pag* db_read(const PageNumber& page_number, bool ok_enc) **************************************/ tdba* tddba = tdba::getSpecific(); - ULONG pageNum = page_number.getPageNum(); - - if (tddba->page_number == pageNum) + if (tddba->page_number == page_number) return tddba->global_buffer; - tddba->page_number = pageNum; + tddba->page_number = page_number; + ULONG pageNum = page_number.getPageNum(); dba_fil* file = nullptr; for (const auto& tablespace : tddba->tablespaces) @@ -2556,7 +2644,7 @@ static const pag* db_read(const PageNumber& page_number, bool ok_enc) * **************************************/ tdba* tddba = tdba::getSpecific(); - tddba->page_number = -1; + tddba->page_number.clear(); tddba->uSvc->getStatusAccessor().setServiceStatus(GSTAT_MSG_FAC, errcode, arg); if (!tddba->uSvc->isService()) From 1cf1e9790f922e48a9b9c244d2dd442437258c85 Mon Sep 17 00:00:00 2001 From: Dmitry Yemanov Date: Sun, 6 Dec 2026 16:38:45 +0300 Subject: [PATCH 5/5] Correction for the ambiguity error --- src/utilities/gstat/dba.epp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/utilities/gstat/dba.epp b/src/utilities/gstat/dba.epp index 824e0b98b78..b03fa751a5f 100644 --- a/src/utilities/gstat/dba.epp +++ b/src/utilities/gstat/dba.epp @@ -732,10 +732,11 @@ int gstat(Firebird::UtilSvc* uSvc) const Switches switches(dba_in_sw_table, FB_NELEM(dba_in_sw_table), false, true); const char* name = NULL; - // The last switch that took a word as a name while the database name wasn't given - // yet. That word could as well have been the database name, and the ambiguity - // becomes an error only if none is found by the end of the command line. - const char* ambiguousSwitch = NULL; + // The switch that took a word as a name while the database name wasn't given yet. + // Only -ts can be ambiguous here, because -t and -sch require an object name, so + // the word can't be a database name. The ambiguity becomes an error only if the + // database name is not found by the end of the command line. + const char* ambiguousSwitch = nullptr; const TEXT* const* const end = argv + argc; ++argv; @@ -851,10 +852,7 @@ int gstat(Firebird::UtilSvc* uSvc) // CVC: If the db name wasn't given yet, only one table name can be specified // after -t to avoid ambiguity if (!name) - { - ambiguousSwitch = str + 1; break; - } } break; @@ -876,10 +874,7 @@ int gstat(Firebird::UtilSvc* uSvc) // If the db name wasn't given yet, only one schema name can be specified // after -sch to avoid ambiguity if (!name) - { - ambiguousSwitch = str + 1; break; - } } break; @@ -930,10 +925,10 @@ int gstat(Firebird::UtilSvc* uSvc) } if (sw_relation && !tddba->relations) - dba_error(41); // option -t needs a table name. + dba_error(41); // option -t needs a table name if (sw_schema && !tddba->schemas) - dba_error(66); // option -sch needs a schema name. + dba_error(66); // option -sch needs a schema name if (!sw_data && !sw_index) sw_data = sw_index = true;