Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Quick links:

## [Unreleased]

### Added

- **Database Health is now a visual dashboard.** The Database Health action opens a panel with status tiles (cache hit ratio, transaction ID age, connections, longest open transaction, size), a short "What to do" list ordered by severity, and one card per check (table bloat, unused indexes, foreign keys without an index, blocking locks, slowest statements) with bars and one-click Copy SQL. Status always shows a word and a symbol as well as a colour. A check that cannot run (for example a missing permission) is shown as unavailable while the others still load. *Open as notebook* in the header gives the previous step-by-step notebook report.

### Changed

- **Info and one-off actions open in a temporary notebook.** The database health report, schema, table and object operations, properties views, table profile / activity / index usage / definition, column statistics, maintenance, list views, and the Create / Edit / Drop / Truncate / Quick Clone helpers now open in a new untitled notebook instead of appending to your saved notebook for that database. Close the tab to discard it (VS Code may ask whether to save; choose Don't Save). Query Tool, New Notebook, View Data, Call Function / Procedure, Insert / Update Data and every Scripts submenu entry still go to your saved notebook. Note that *Drop Database* from the Scripts submenu now opens in a temporary notebook too, because it is the same command as the top-level Drop Database.
Expand Down
4 changes: 2 additions & 2 deletions src/activation/commandSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { cmdImportConnectionFromDatabaseUrl } from '../commands/importConnection
import { cmdSmartPasteConnection } from '../commands/smartPasteConnection';
import { showIndexProperties, copyIndexName, generateDropIndexScript, generateReindexScript, generateScriptCreate, analyzeIndexUsage, generateAlterIndexScript, addIndexComment, cmdIndexOperations, cmdAddIndex } from '../commands/indexes';
import { cmdAddObjectInDatabase, cmdCreateDatabase, cmdDatabaseOperations, cmdDeleteDatabase, cmdDisconnectDatabase as cmdDisconnectDatabaseLegacy, cmdGenerateCreateScript, cmdMaintenanceDatabase, cmdPsqlTool, cmdQueryTool, cmdScriptAlterDatabase, cmdShowConfiguration } from '../commands/database';
import { cmdDatabaseHealth } from '../commands/health';
import { DatabaseHealthPanel } from '../providers/DatabaseHealthPanel';
import { cmdDropExtension, cmdEnableExtension, cmdExtensionOperations, cmdRefreshExtension } from '../commands/extensions';
import { cmdCreateForeignTable, cmdDropForeignTable, cmdEditForeignTable, cmdForeignTableOperations, cmdRefreshForeignTable, cmdShowForeignTableProperties, cmdViewForeignTableData } from '../commands/foreignTables';
import { cmdForeignDataWrapperOperations, cmdShowForeignDataWrapperProperties, cmdCreateForeignServer, cmdForeignServerOperations, cmdShowForeignServerProperties, cmdDropForeignServer, cmdCreateUserMapping, cmdUserMappingOperations, cmdShowUserMappingProperties, cmdDropUserMapping, cmdRefreshForeignDataWrapper, cmdRefreshForeignServer, cmdRefreshUserMapping } from '../commands/foreignDataWrappers';
Expand Down Expand Up @@ -757,7 +757,7 @@ export function getCommandSpecs(
},
{
command: 'postgres-explorer.databaseHealth',
callback: async (item: DatabaseTreeItem) => await cmdDatabaseHealth(item, context)
callback: async (item: DatabaseTreeItem) => await DatabaseHealthPanel.open(item, context)
},
{
command: 'postgres-explorer.queryTool',
Expand Down
29 changes: 28 additions & 1 deletion src/commands/sql/health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ function schemaPredicate(column: string, schema?: string): string {
}

export const HealthSQL = {
/**
* One row of typed numbers for the Health dashboard: size, connections, cache hit ratio,
* transaction ID age and the longest open transaction. Unlike overview(), nothing is pre-formatted.
*/
summary(): string {
return `SELECT current_database()::text AS db_name,
pg_database_size(current_database())::bigint AS db_size_bytes,
(SELECT count(*) FROM pg_stat_activity WHERE datname = current_database())::int AS connections_total,
(SELECT count(*) FROM pg_stat_activity
WHERE datname = current_database() AND state = 'active' AND pid <> pg_backend_pid())::int AS connections_active,
(SELECT count(*) FROM pg_stat_activity
WHERE datname = current_database() AND state LIKE 'idle in transaction%')::int AS idle_in_transaction,
current_setting('max_connections')::int AS max_connections,
(SELECT round(100.0 * blks_hit / nullif(blks_hit + blks_read, 0), 2)
FROM pg_stat_database WHERE datname = current_database()) AS cache_hit_pct,
(SELECT age(datfrozenxid) FROM pg_database WHERE datname = current_database())::bigint AS xid_age,
(SELECT round(100.0 * age(datfrozenxid) / 2147483647, 2)
FROM pg_database WHERE datname = current_database()) AS xid_pct,
(SELECT extract(epoch FROM now() - min(xact_start))
FROM pg_stat_activity
WHERE datname = current_database() AND xact_start IS NOT NULL AND pid <> pg_backend_pid()) AS longest_xact_seconds,
(SELECT pid FROM pg_stat_activity
WHERE datname = current_database() AND xact_start IS NOT NULL AND pid <> pg_backend_pid()
ORDER BY xact_start LIMIT 1)::int AS longest_pid;`;
},

/** One row per metric: database size, connections by state, cache hit ratio, XID age, longest transaction. */
overview(): string {
return `SELECT metric, value, detail
Expand Down Expand Up @@ -92,7 +118,8 @@ LIMIT 25;`;
s.indexrelname AS index_name,
s.idx_scan AS scans,
pg_size_pretty(pg_relation_size(s.indexrelid)) AS index_size,
pg_get_indexdef(s.indexrelid) AS definition
pg_get_indexdef(s.indexrelid) AS definition,
pg_relation_size(s.indexrelid)::bigint AS index_bytes
FROM pg_stat_user_indexes s
JOIN pg_index i ON i.indexrelid = s.indexrelid
WHERE s.idx_scan = 0
Expand Down
Loading
Loading