PostgreSQL backup tool with Point-In-Time-Recovery and restore verification
TypeScript backupbackupsdatabasedatabase-backupdevopsdockergolangkubernetesmariadbmongodbmysqlpgpostgrespostgresqls3self-hostedsystem-administrationtoolsweb-ui
FEATURE (postgresql): Let a logical backup exclude only the rows of chosen tables while keeping their structure, and relabel the existing list as Exclude whole tables (issue #828)
Rostislav Dugin1605e46b349a
diff --git a/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc.go b/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc.goindex 2381601d..5411e24d 100644--- a/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc.go+++ b/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc.go@@ -359,6 +359,10 @@ func (uc *CreatePostgresqlBackupUsecase) buildPgDumpArgs(pg *pgtypes.PostgresqlLargs = append(args, "--exclude-table="+excludedTable)}+ for _, dataExcludedTable := range namelist.NormalizeUniqueNames(pg.ExcludeTableData) {+ args = append(args, "--exclude-table-data="+dataExcludedTable)+ }+compressionArgs := uc.getCompressionArgs(pg.Version)return append(args, compressionArgs...)}diff --git a/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc_test.go b/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc_test.goindex 0bab56ab..e950b83d 100644--- a/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc_test.go+++ b/backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc_test.go@@ -4,6 +4,7 @@ import ("context""io""log/slog"+ "strings""testing""github.com/google/uuid"@@ -44,6 +45,37 @@ func Test_BuildPgDumpArgs_TreatsDuplicateParameterAsLiteralDatabaseName(t *testirequire.Equal(t, "dbname='postgres dbname=databasus'", args[databaseArgumentIndex])}+func Test_BuildPgDumpArgs_WhenBothExclusionListsAreSet_AddsWholeTableThenDataFlagsNormalized(t *testing.T) {+ databaseName := "appdb"+ postgresDatabase := &pgtypes.PostgresqlLogicalDatabase{+ Version: tools.PostgresqlVersion17,+ Host: "localhost",+ Port: 5437,+ Username: "postgres",+ Database: &databaseName,+ ExcludeTables: []string{" audit_tmp", "audit_tmp\n"},+ ExcludeTableData: []string{"logs, history_*", "\nlogs", " "},+ }++ usecase := &CreatePostgresqlBackupUsecase{+ logger: slog.New(slog.NewTextHandler(io.Discard, nil)),+ }+ args := usecase.buildPgDumpArgs(postgresDatabase)++ var exclusionArgs []string+ for _, argument := range args {+ if strings.HasPrefix(argument, "--exclude-table") {+ exclusionArgs = append(exclusionArgs, argument)+ }+ }++ require.Equal(t, []string{+ "--exclude-table=audit_tmp",+ "--exclude-table-data=logs",+ "--exclude-table-data=history_*",+ }, exclusionArgs)+}+// Test_Execute_WhenStorageTimesOut_ReportsStorageFailure_RegressionForIssue816 pins issue #816:// a storage timeout that ended the backup context was reported as "backup cancelled".func Test_Execute_WhenStorageTimesOut_ReportsStorageFailure_RegressionForIssue816(t *testing.T) {diff --git a/backend/internal/features/databases/controller_test.go b/backend/internal/features/databases/controller_test.goindex 7a7b2ef9..03831736 100644--- a/backend/internal/features/databases/controller_test.go+++ b/backend/internal/features/databases/controller_test.go@@ -1367,6 +1367,7 @@ func Test_UpdateDatabase_WhenExcludeTablesArePastedMultiline_StoresTrimmedUniquedatabase.PostgresqlLogical.ExcludeTables = []string{"orders", "\npersonnel_real_time", " ", "orders"}database.PostgresqlLogical.IncludeSchemas = []string{"public", "\treporting"}+ database.PostgresqlLogical.ExcludeTableData = []string{"logs, history_*", "\nlogs", " "}var updatedDatabase Databasetest_utils.MakePostRequestAndUnmarshal(@@ -1399,6 +1400,74 @@ func Test_UpdateDatabase_WhenExcludeTablesArePastedMultiline_StoresTrimmedUnique[]string{"public", "reporting"},reloadedDatabase.PostgresqlLogical.IncludeSchemas,)+ assert.Equal(+ t,+ []string{"logs", "history_*"},+ reloadedDatabase.PostgresqlLogical.ExcludeTableData,+ )+}++func Test_ExcludeTableData_WhenDatabaseIsCreatedCopiedAndUpdatedWithoutIt_ListIsKeptCopiedAndCleared(+ t *testing.T,+) {+ router := createTestRouter()+ owner := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Test Workspace", owner, router)+ defer workspaces_testing.RemoveTestWorkspace(t.Context(), workspace, router)++ createRequest := newTestPostgresqlLogicalDatabaseRequest("Data-excluded Database", workspace.ID)+ createRequest.PostgresqlLogical.ExcludeTables = []string{"audit_tmp"}+ createRequest.PostgresqlLogical.ExcludeTableData = []string{"logs", "history_*"}++ var createdDatabase Database+ test_utils.MakePostRequestAndUnmarshal(+ t, router, "/api/v1/databases/create", "Bearer "+owner.Token,+ createRequest, http.StatusCreated, &createdDatabase,+ )+ defer RemoveTestDatabase(t.Context(), &createdDatabase)++ var readDatabase Database+ test_utils.MakeGetRequestAndUnmarshal(+ t, router, "/api/v1/databases/"+createdDatabase.ID.String(),+ "Bearer "+owner.Token, http.StatusOK, &readDatabase,+ )+ require.NotNil(t, readDatabase.PostgresqlLogical)+ assert.Equal(t, []string{"audit_tmp"}, readDatabase.PostgresqlLogical.ExcludeTables)+ assert.Equal(t, []string{"logs", "history_*"}, readDatabase.PostgresqlLogical.ExcludeTableData)++ var copiedDatabase Database+ test_utils.MakePostRequestAndUnmarshal(+ t, router, "/api/v1/databases/"+createdDatabase.ID.String()+"/copy",+ "Bearer "+owner.Token, nil, http.StatusCreated, &copiedDatabase,+ )+ defer RemoveTestDatabase(t.Context(), &copiedDatabase)++ var readCopiedDatabase Database+ test_utils.MakeGetRequestAndUnmarshal(+ t, router, "/api/v1/databases/"+copiedDatabase.ID.String(),+ "Bearer "+owner.Token, http.StatusOK, &readCopiedDatabase,+ )+ require.NotNil(t, readCopiedDatabase.PostgresqlLogical)+ assert.Equal(+ t, []string{"logs", "history_*"}, readCopiedDatabase.PostgresqlLogical.ExcludeTableData,+ )++ readDatabase.PostgresqlLogical.ExcludeTableData = nil++ var updatedDatabase Database+ test_utils.MakePostRequestAndUnmarshal(+ t, router, "/api/v1/databases/update", "Bearer "+owner.Token,+ readDatabase, http.StatusOK, &updatedDatabase,+ )++ var reloadedDatabase Database+ test_utils.MakeGetRequestAndUnmarshal(+ t, router, "/api/v1/databases/"+createdDatabase.ID.String(),+ "Bearer "+owner.Token, http.StatusOK, &reloadedDatabase,+ )+ require.NotNil(t, reloadedDatabase.PostgresqlLogical)+ assert.Empty(t, reloadedDatabase.PostgresqlLogical.ExcludeTableData)+ assert.Equal(t, []string{"audit_tmp"}, reloadedDatabase.PostgresqlLogical.ExcludeTables)}func createTestDatabaseViaAPI(@@ -1407,27 +1476,7 @@ func createTestDatabaseViaAPI(token string,router *gin.Engine,) *Database {- env := config.GetEnv()- port, err := strconv.Atoi(env.TestLogicalPostgres16Port)- if err != nil {- panic(fmt.Sprintf("Failed to parse TEST_LOGICAL_POSTGRES_16_PORT: %v", err))- }-- testDbName := "testdb"- request := Database{- Name: name,- WorkspaceID: &workspaceID,- Type: DatabaseTypePostgresLogical,- PostgresqlLogical: &postgresql_logical.PostgresqlLogicalDatabase{- Version: tools.PostgresqlVersion16,- Host: config.GetEnv().TestLocalhost,- Port: port,- Username: "testuser",- Password: "testpassword",- Database: &testDbName,- CpuCount: 1,- },- }+ request := newTestPostgresqlLogicalDatabaseRequest(name, workspaceID)w := workspaces_testing.MakeAPIRequest(router,@@ -1451,6 +1500,15 @@ func createTestDatabaseViaAPI(return &database}+func newTestPostgresqlLogicalDatabaseRequest(name string, workspaceID uuid.UUID) Database {+ return Database{+ Name: name,+ WorkspaceID: &workspaceID,+ Type: DatabaseTypePostgresLogical,+ PostgresqlLogical: getTestPostgresConfig(),+ }+}+func createTestRouter() *gin.Engine {gin.SetMode(gin.TestMode)router := gin.New()diff --git a/backend/internal/features/databases/databases/postgresql/logical/dumpscope.go b/backend/internal/features/databases/databases/postgresql/logical/dumpscope.goindex 92fcd3d1..cc03c1ae 100644--- a/backend/internal/features/databases/databases/postgresql/logical/dumpscope.go+++ b/backend/internal/features/databases/databases/postgresql/logical/dumpscope.go@@ -1,9 +1,13 @@package postgresql_logicalimport (+ "context"+ "fmt""path""strings"+ "github.com/jackc/pgx/v5"+"databasus-backend/internal/util/namelist")@@ -32,13 +36,30 @@ const dumpedRelationScope = ``type DumpFilter struct {- IncludeSchemas []string- ExcludeTablePatterns []string+ IncludeSchemas []string+ ExcludeTablePatterns []string+ ExcludeTableDataPatterns []string}type relationName struct {- SchemaName string- TableName string+ SchemaName string+ TableName string+ IsVisibleOnSearchPath bool+}++// pg_dump resolves table patterns right after RESET search_path, so whether an unqualified+// pattern reaches a table depends on the role's default search path, not on this session's.+func queryDumpedRelations(+ ctx context.Context,+ conn *pgx.Conn,+ query string,+ filter DumpFilter,+) (pgx.Rows, error) {+ if _, err := conn.Exec(ctx, "RESET search_path"); err != nil {+ return nil, fmt.Errorf("failed to reset search path: %w", err)+ }++ return conn.Query(ctx, query, filter.getIncludedSchemaNames())}// The dump arguments are built from normalized names, so a pasted multiline value must be@@ -53,6 +74,10 @@ func (f DumpFilter) getExcludeTablePatterns() []string {return namelist.NormalizeUniqueNames(f.ExcludeTablePatterns)}+func (f DumpFilter) getExcludeTableDataPatterns() []string {+ return namelist.NormalizeUniqueNames(f.ExcludeTableDataPatterns)+}+func isRelationExcluded(relation relationName, excludeTablePatterns []string) bool {for _, excludeTablePattern := range excludeTablePatterns {if parseDumpTablePattern(excludeTablePattern).isMatchingRelation(relation) {@@ -74,11 +99,15 @@ func (p dumpTablePattern) isMatchingRelation(relation relationName) bool {return false}+ if !p.IsSchemaQualified && !relation.IsVisibleOnSearchPath {+ return false+ }+return p.TableGlob.isMatchingName(relation.TableName)}-// Mirrors pg_dump's --exclude-table: an unqualified pattern hits the table name in-// every schema, and the split happens on the first dot outside double quotes.+// Mirrors pg_dump's table patterns: an unqualified pattern hits only tables visible on the+// default search path, as in psql, and the split happens on the first dot outside double quotes.func parseDumpTablePattern(excludeTablePattern string) dumpTablePattern {isInsideQuotes := falsediff --git a/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.go b/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.goindex bbecd7de..76056a9b 100644--- a/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.go+++ b/backend/internal/features/databases/databases/postgresql/logical/dumpscope_test.go@@ -27,14 +27,30 @@ func Test_IsRelationExcluded_WithVariousPatterns_MatchesPgDumpSemantics(t *testiisExcluded bool}{{- name: "unqualified name matches table in any schema",+ name: "unqualified name matches a table visible on the search path",+ relation: relationName{+ SchemaName: "public", TableName: "audit_logs", IsVisibleOnSearchPath: true,+ },+ excludeTablePatterns: []string{"audit_logs"},+ isExcluded: true,+ },+ {+ name: "unqualified name skips a same-named table off the search path",relation: relationName{SchemaName: "reporting", TableName: "audit_logs"},excludeTablePatterns: []string{"audit_logs"},+ isExcluded: false,+ },+ {+ name: "star schema part matches a table off the search path",+ relation: relationName{SchemaName: "reporting", TableName: "audit_logs"},+ excludeTablePatterns: []string{"*.audit_logs"},isExcluded: true,},{- name: "unqualified name does not match a different table",- relation: relationName{SchemaName: "public", TableName: "orders"},+ name: "unqualified name does not match a different table",+ relation: relationName{+ SchemaName: "public", TableName: "orders", IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{"audit_logs"},isExcluded: false,},@@ -51,8 +67,12 @@ func Test_IsRelationExcluded_WithVariousPatterns_MatchesPgDumpSemantics(t *testiisExcluded: false,},{- name: "star glob matches a name prefix",- relation: relationName{SchemaName: "public", TableName: "logs_2026_07"},+ name: "star glob matches a name prefix",+ relation: relationName{+ SchemaName: "public",+ TableName: "logs_2026_07",+ IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{"logs_*"},isExcluded: true,},@@ -70,31 +90,43 @@ func Test_IsRelationExcluded_WithVariousPatterns_MatchesPgDumpSemantics(t *testi},{name: "question mark glob matches a single character",- relation: relationName{SchemaName: "public", TableName: "shard_7"},+ relation: relationName{SchemaName: "public", TableName: "shard_7", IsVisibleOnSearchPath: true},excludeTablePatterns: []string{"shard_?"},isExcluded: true,},{- name: "question mark glob does not match two characters",- relation: relationName{SchemaName: "public", TableName: "shard_42"},+ name: "question mark glob does not match two characters",+ relation: relationName{+ SchemaName: "public",+ TableName: "shard_42",+ IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{"shard_?"},isExcluded: false,},{name: "character class glob matches a listed character",- relation: relationName{SchemaName: "public", TableName: "shard_b"},+ relation: relationName{SchemaName: "public", TableName: "shard_b", IsVisibleOnSearchPath: true},excludeTablePatterns: []string{"shard_[ab]"},isExcluded: true,},{- name: "quoted part is compared literally including the dot",- relation: relationName{SchemaName: "public", TableName: "weird.name"},+ name: "quoted part is compared literally including the dot",+ relation: relationName{+ SchemaName: "public",+ TableName: "weird.name",+ IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{`"weird.name"`},isExcluded: true,},{- name: "quoted part does not glob",- relation: relationName{SchemaName: "public", TableName: "logs_2026"},+ name: "quoted part does not glob",+ relation: relationName{+ SchemaName: "public",+ TableName: "logs_2026",+ IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{`"logs_*"`},isExcluded: false,},@@ -105,14 +137,18 @@ func Test_IsRelationExcluded_WithVariousPatterns_MatchesPgDumpSemantics(t *testiisExcluded: true,},{- name: "malformed glob falls back to literal comparison",- relation: relationName{SchemaName: "public", TableName: "shard_[ab"},+ name: "malformed glob falls back to literal comparison",+ relation: relationName{+ SchemaName: "public",+ TableName: "shard_[ab",+ IsVisibleOnSearchPath: true,+ },excludeTablePatterns: []string{"shard_[ab"},isExcluded: true,},{name: "malformed glob does not match a different name",- relation: relationName{SchemaName: "public", TableName: "shard_a"},+ relation: relationName{SchemaName: "public", TableName: "shard_a", IsVisibleOnSearchPath: true},excludeTablePatterns: []string{"shard_[ab"},isExcluded: false,},diff --git a/backend/internal/features/databases/databases/postgresql/logical/model.go b/backend/internal/features/databases/databases/postgresql/logical/model.goindex 1c4adfcc..d8088503 100644--- a/backend/internal/features/databases/databases/postgresql/logical/model.go+++ b/backend/internal/features/databases/databases/postgresql/logical/model.go@@ -48,12 +48,14 @@ type PostgresqlLogicalDatabase struct {LocalTunnelEndpoint *sshtunnel.Endpoint `json:"-" gorm:"-"`// backup settings- IncludeSchemas []string `json:"includeSchemas" gorm:"-"`- IncludeSchemasString string `json:"-" gorm:"column:include_schemas;type:text;not null;default:''"`- ExcludeTables []string `json:"excludeTables" gorm:"-"`- ExcludeTablesString string `json:"-" gorm:"column:exclude_tables;type:text;not null;default:''"`- CpuCount int `json:"cpuCount" gorm:"column:cpu_count;type:int;not null;default:1"`- IsSkipUserMappings bool `json:"isSkipUserMappings" gorm:"column:is_skip_user_mappings;type:bool;not null;default:false"`+ IncludeSchemas []string `json:"includeSchemas" gorm:"-"`+ IncludeSchemasString string `json:"-" gorm:"column:include_schemas;type:text;not null;default:''"`+ ExcludeTables []string `json:"excludeTables" gorm:"-"`+ ExcludeTablesString string `json:"-" gorm:"column:exclude_tables;type:text;not null;default:''"`+ ExcludeTableData []string `json:"excludeTableData" gorm:"-"`+ ExcludeTableDataString string `json:"-" gorm:"column:exclude_table_data;type:text;not null;default:''"`+ CpuCount int `json:"cpuCount" gorm:"column:cpu_count;type:int;not null;default:1"`+ IsSkipUserMappings bool `json:"isSkipUserMappings" gorm:"column:is_skip_user_mappings;type:bool;not null;default:false"`// restore settings (not saved to DB)IsExcludeExtensions bool `json:"isExcludeExtensions" gorm:"-"`@@ -68,6 +70,7 @@ func (p *PostgresqlLogicalDatabase) TableName() string {func (p *PostgresqlLogicalDatabase) BeforeSave(_ *gorm.DB) error {p.IncludeSchemasString = namelist.FormatUniqueNames(p.IncludeSchemas)p.ExcludeTablesString = namelist.FormatUniqueNames(p.ExcludeTables)+ p.ExcludeTableDataString = namelist.FormatUniqueNames(p.ExcludeTableData)return nil}@@ -75,6 +78,7 @@ func (p *PostgresqlLogicalDatabase) BeforeSave(_ *gorm.DB) error {func (p *PostgresqlLogicalDatabase) AfterFind(_ *gorm.DB) error {p.IncludeSchemas = namelist.ParseUniqueNames(p.IncludeSchemasString)p.ExcludeTables = namelist.ParseUniqueNames(p.ExcludeTablesString)+ p.ExcludeTableData = namelist.ParseUniqueNames(p.ExcludeTableDataString)return nil}@@ -151,8 +155,9 @@ func (p *PostgresqlLogicalDatabase) GetRawDbSizeMb(}()sizeBytes, err := getDumpedRelationsSizeBytes(ctx, conn, DumpFilter{- IncludeSchemas: p.IncludeSchemas,- ExcludeTablePatterns: p.ExcludeTables,+ IncludeSchemas: p.IncludeSchemas,+ ExcludeTablePatterns: p.ExcludeTables,+ ExcludeTableDataPatterns: p.ExcludeTableData,})if err != nil {return 0, err@@ -186,6 +191,7 @@ func (p *PostgresqlLogicalDatabase) Update(incoming *PostgresqlLogicalDatabase)p.SslRootCert = incoming.SslRootCertp.IncludeSchemas = incoming.IncludeSchemasp.ExcludeTables = incoming.ExcludeTables+ p.ExcludeTableData = incoming.ExcludeTableDatap.CpuCount = incoming.CpuCountp.IsSkipUserMappings = incoming.IsSkipUserMappingsp.SshTunnel.Update(&incoming.SshTunnel)@@ -210,6 +216,7 @@ func (p *PostgresqlLogicalDatabase) CopyForNewDatabase() *PostgresqlLogicalDatabcopiedDatabase.DatabaseID = nilcopiedDatabase.IncludeSchemas = slices.Clone(p.IncludeSchemas)copiedDatabase.ExcludeTables = slices.Clone(p.ExcludeTables)+ copiedDatabase.ExcludeTableData = slices.Clone(p.ExcludeTableData)copiedDatabase.LocalTunnelEndpoint = nilif p.Database != nil {@@ -940,8 +947,9 @@ func testSingleDatabaseConnection(postgresDb.Version = detectedVersionif err := checkDumpReadPrivileges(ctx, conn, DumpFilter{- IncludeSchemas: postgresDb.IncludeSchemas,- ExcludeTablePatterns: postgresDb.ExcludeTables,+ IncludeSchemas: postgresDb.IncludeSchemas,+ ExcludeTablePatterns: postgresDb.ExcludeTables,+ ExcludeTableDataPatterns: postgresDb.ExcludeTableData,}); err != nil {return err}diff --git a/backend/internal/features/databases/databases/postgresql/logical/model_test.go b/backend/internal/features/databases/databases/postgresql/logical/model_test.goindex a492b08c..3eea9b26 100644--- a/backend/internal/features/databases/databases/postgresql/logical/model_test.go+++ b/backend/internal/features/databases/databases/postgresql/logical/model_test.go@@ -226,6 +226,24 @@ func Test_PostgresqlModel_AcrossSupportedVersions(t *testing.T) {},)+ t.Run(+ "Test_TestConnection_WhenUnreadableTableDataIsExcluded_ReturnsErrorNamingTable",+ func(t *testing.T) {+ testConnectionUnreadableTableDataExcluded(t, endpoint, dbVersion.tag)+ },+ )++ t.Run(+ "Test_TestConnection_WhenUnqualifiedExclusionNamesTableOffSearchPath_ReturnsErrorNamingIt",+ func(t *testing.T) {+ testConnectionUnqualifiedExclusionDoesNotReachTableOffSearchPath(+ t,+ endpoint,+ dbVersion.tag,+ )+ },+ )+t.Run("Test_TestConnection_WhenUnreadableTableIsOutsideIncludedSchemas_Success", func(t *testing.T) {testConnectionUnreadableTableOutsideIncludedSchemas(t, endpoint, dbVersion.tag)})@@ -1720,7 +1738,11 @@ func Test_GetRawDbSizeMb_WhenExcludeTablesSet_SkipsExcludedTableSize(t *testing.assert.InDelta(t,- measureRelationsSizeMb(t, container, relationName{seededSchema.SchemaName, seededSchema.LightTableName}),+ measureRelationsSizeMb(+ t,+ container,+ relationName{SchemaName: seededSchema.SchemaName, TableName: seededSchema.LightTableName},+ ),sizeWithoutHeavyTableMb,0.5,"excluded table size must not be counted",@@ -1728,6 +1750,39 @@ func Test_GetRawDbSizeMb_WhenExcludeTablesSet_SkipsExcludedTableSize(t *testing.assert.Greater(t, sizeWithHeavyTableMb-sizeWithoutHeavyTableMb, heavyTableMinSizeMb)}+func Test_GetRawDbSizeMb_WhenExcludeTableDataSet_SkipsTableSize(t *testing.T) {+ container := connectToPostgresContainer(t, "postgres:16")+ defer container.DB.Close()++ seededSchema := seedSizeTestSchema(t, container)++ _, err := container.DB.Exec(fmt.Sprintf(+ `CREATE INDEX ON %s.%s (payload)`,+ seededSchema.SchemaName, seededSchema.HeavyTableName,+ ))+ assert.NoError(t, err)++ pgModel := createPostgresModel(container)+ pgModel.IncludeSchemas = []string{seededSchema.SchemaName}++ sizeWithHeavyTableDataMb := measureRawDbSizeMb(t, pgModel)++ pgModel.ExcludeTableData = []string{seededSchema.SchemaName + "." + seededSchema.HeavyTableName}+ sizeWithoutHeavyTableDataMb := measureRawDbSizeMb(t, pgModel)++ heavyTableTotalSizeMb := measureRelationsSizeMb(+ t, container,+ relationName{SchemaName: seededSchema.SchemaName, TableName: seededSchema.HeavyTableName},+ )++ assert.GreaterOrEqual(+ t,+ sizeWithHeavyTableDataMb-sizeWithoutHeavyTableDataMb,+ heavyTableTotalSizeMb-0.01,+ "the data-excluded table must leave the estimate together with its indexes",+ )+}+func Test_GetRawDbSizeMb_WhenExcludeTablesUsesGlobPattern_SkipsMatchingTables(t *testing.T) {container := connectToPostgresContainer(t, "postgres:16")defer container.DB.Close()@@ -1736,17 +1791,70 @@ func Test_GetRawDbSizeMb_WhenExcludeTablesUsesGlobPattern_SkipsMatchingTables(tpgModel := createPostgresModel(container)pgModel.IncludeSchemas = []string{seededSchema.SchemaName}- pgModel.ExcludeTables = []string{"heavy_*"}+ pgModel.ExcludeTables = []string{seededSchema.SchemaName + ".heavy_*"}assert.InDelta(t,- measureRelationsSizeMb(t, container, relationName{seededSchema.SchemaName, seededSchema.LightTableName}),+ measureRelationsSizeMb(+ t,+ container,+ relationName{SchemaName: seededSchema.SchemaName, TableName: seededSchema.LightTableName},+ ),measureRawDbSizeMb(t, pgModel),0.5,"glob-matched table size must not be counted",)}+func Test_GetRawDbSizeMb_WhenUnqualifiedTableIsExcludedByEitherList_CountsSameNamedTableInAnotherSchema(t *testing.T) {+ container := connectToPostgresContainer(t, "postgres:16")+ defer container.DB.Close()++ otherSchema := seedSizeTestSchema(t, container)+ sameNamedTableName := otherSchema.HeavyTableName++ _, err := container.DB.Exec(fmt.Sprintf(`+ CREATE TABLE public.%s (id SERIAL PRIMARY KEY, payload TEXT NOT NULL);+ INSERT INTO public.%s (payload) SELECT repeat('x', 1024) FROM generate_series(1, 20000);+ `, sameNamedTableName, sameNamedTableName))+ assert.NoError(t, err)++ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf("DROP TABLE IF EXISTS public.%s", sameNamedTableName))+ })++ publicTableSizeMb := measureRelationsSizeMb(+ t, container, relationName{SchemaName: "public", TableName: sameNamedTableName},+ )++ pgModel := createPostgresModel(container)+ pgModel.IncludeSchemas = []string{"public", otherSchema.SchemaName}++ sizeWithoutExclusionMb := measureRawDbSizeMb(t, pgModel)++ pgModel.ExcludeTables = []string{sameNamedTableName}+ sizeWithWholeTableExclusionMb := measureRawDbSizeMb(t, pgModel)++ pgModel.ExcludeTables = nil+ pgModel.ExcludeTableData = []string{sameNamedTableName}+ sizeWithDataExclusionMb := measureRawDbSizeMb(t, pgModel)++ assert.InDelta(+ t,+ publicTableSizeMb,+ sizeWithoutExclusionMb-sizeWithWholeTableExclusionMb,+ 0.5,+ "a whole-table exclusion must leave out only the table visible on the default search path",+ )+ assert.InDelta(+ t,+ publicTableSizeMb,+ sizeWithoutExclusionMb-sizeWithDataExclusionMb,+ 0.5,+ "a data exclusion must leave out only the table visible on the default search path",+ )+}+func Test_GetRawDbSizeMb_WhenIncludeSchemasSet_CountsOnlyIncludedSchema(t *testing.T) {container := connectToPostgresContainer(t, "postgres:16")defer container.DB.Close()@@ -1766,8 +1874,8 @@ func Test_GetRawDbSizeMb_WhenIncludeSchemasSet_CountsOnlyIncludedSchema(t *testit,measureRelationsSizeMb(t, container,- relationName{includedSchema.SchemaName, includedSchema.HeavyTableName},- relationName{includedSchema.SchemaName, includedSchema.LightTableName},+ relationName{SchemaName: includedSchema.SchemaName, TableName: includedSchema.HeavyTableName},+ relationName{SchemaName: includedSchema.SchemaName, TableName: includedSchema.LightTableName},),sizeOfIncludedSchemaMb,0.5,@@ -1794,7 +1902,7 @@ func Test_GetRawDbSizeMb_WhenIncludeSchemasAndExcludeTablesSet_AppliesBothFiltert,measureRelationsSizeMb(t, container,- relationName{includedSchema.SchemaName, includedSchema.LightTableName},+ relationName{SchemaName: includedSchema.SchemaName, TableName: includedSchema.LightTableName},),measureRawDbSizeMb(t, pgModel),0.5,@@ -1818,8 +1926,8 @@ func Test_GetRawDbSizeMb_WhenNoFiltersSet_CountsEveryUserTable(t *testing.T) {seededSchemasSizeMb := sizeOfFirstSchemaMb + measureRelationsSizeMb(t, container,- relationName{secondSchema.SchemaName, secondSchema.HeavyTableName},- relationName{secondSchema.SchemaName, secondSchema.LightTableName},+ relationName{SchemaName: secondSchema.SchemaName, TableName: secondSchema.HeavyTableName},+ relationName{SchemaName: secondSchema.SchemaName, TableName: secondSchema.LightTableName},)assert.GreaterOrEqual(t, sizeOfWholeDatabaseMb, seededSchemasSizeMb-0.5)@@ -1853,8 +1961,8 @@ func Test_GetRawDbSizeMb_WhenTableIsPartitioned_CountsEveryPartitionOnce(t *testt,measureRelationsSizeMb(t, container,- relationName{schemaName, "events_first"},- relationName{schemaName, "events_second"},+ relationName{SchemaName: schemaName, TableName: "events_first"},+ relationName{SchemaName: schemaName, TableName: "events_second"},),measureRawDbSizeMb(t, pgModel),0.5,diff --git a/backend/internal/features/databases/databases/postgresql/logical/privileges.go b/backend/internal/features/databases/databases/postgresql/logical/privileges.goindex 1ef5ddb2..ca9addbe 100644--- a/backend/internal/features/databases/databases/postgresql/logical/privileges.go+++ b/backend/internal/features/databases/databases/postgresql/logical/privileges.go@@ -17,6 +17,7 @@ import (const unreadableRelationsQuery = `SELECT n.nspname,c.relname,+ pg_catalog.pg_table_is_visible(c.oid),NOT COALESCE(has_schema_privilege(current_user, n.oid, 'USAGE'), false)` + dumpedRelationScope + `AND c.relkind IN ('r', 'p', 'S')@@ -80,7 +81,7 @@ func getUnreadableDumpedRelations(conn *pgx.Conn,filter DumpFilter,) ([]unreadableRelation, error) {- rows, err := conn.Query(ctx, unreadableRelationsQuery, filter.getIncludedSchemaNames())+ rows, err := queryDumpedRelations(ctx, conn, unreadableRelationsQuery, filter)if err != nil {return nil, fmt.Errorf("failed to check dump read privileges: %w", err)}@@ -97,6 +98,7 @@ func getUnreadableDumpedRelations(if err := rows.Scan(&relation.SchemaName,&relation.TableName,+ &relation.IsVisibleOnSearchPath,&relation.IsSchemaUsageMissing,); err != nil {return nil, fmt.Errorf("failed to scan unreadable relation: %w", err)@@ -151,8 +153,9 @@ func newInsufficientDumpPrivilegesError(messageParts = append(messageParts, fmt.Sprintf("Grant USAGE ON SCHEMA, SELECT ON ALL TABLES IN SCHEMA and SELECT ON ALL SEQUENCES IN SCHEMA "+- "to %q for every schema listed above, or narrow the backup with include schemas and exclude tables. "+- "Note that excluding a table does not exclude the sequences it owns.",+ "to %q for every schema listed above, or narrow the backup with include schemas and whole-table exclusion. "++ "Note that excluding a table does not exclude the sequences it owns, and excluding only "++ "a table's data still requires SELECT on that table.",roleName,))diff --git a/backend/internal/features/databases/databases/postgresql/logical/privileges_test.go b/backend/internal/features/databases/databases/postgresql/logical/privileges_test.goindex 5dfec162..e4942a67 100644--- a/backend/internal/features/databases/databases/postgresql/logical/privileges_test.go+++ b/backend/internal/features/databases/databases/postgresql/logical/privileges_test.go@@ -4,6 +4,7 @@ import ("fmt""log/slog""os"+ "strings""testing""github.com/google/uuid"@@ -183,6 +184,61 @@ func testConnectionUnreadableTableExcluded(assert.NoError(t, fixture.testConnection())}+// pg_dump still locks a table whose data it skips, so excluding the data must not lift the check.+func testConnectionUnreadableTableDataExcluded(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_data_excl")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE %s.kept (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`CREATE TABLE %s.logs (id INT)`, fixture.SchemaName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ fmt.Sprintf(`GRANT SELECT ON %s.kept TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ fixture.Model.ExcludeTableData = []string{fixture.SchemaName + ".logs"}++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), fixture.SchemaName+".logs")+ assert.Contains(t, err.Error(), "excluding only a table's data still requires SELECT")+}++// pg_dump matches an unqualified pattern only against tables visible on the default search path,+// so it skips public's table but still dumps and locks a same-named table in a schema off that path.+// The fixture schema sorts before public, so its table stays among the names the error lists.+func testConnectionUnqualifiedExclusionDoesNotReachTableOffSearchPath(+ t *testing.T,+ endpoint containers.Endpoint,+ version string,+) {+ fixture := createBackupPrivilegeFixture(t, endpoint, version, "perm_unqualified")+ sameNamedTableName := "logs_" + strings.TrimPrefix(fixture.SchemaName, "perm_unqualified_")++ fixture.executeStatements(t,+ fmt.Sprintf(`CREATE TABLE public.%s (id INT)`, sameNamedTableName),+ fmt.Sprintf(`CREATE TABLE %s.%s (id INT)`, fixture.SchemaName, sameNamedTableName),+ fmt.Sprintf(`GRANT USAGE ON SCHEMA %s TO "%s"`, fixture.SchemaName, fixture.RoleName),+ )++ t.Cleanup(func() {+ _, _ = fixture.Container.DB.Exec(fmt.Sprintf(`DROP TABLE IF EXISTS public.%s`, sameNamedTableName))+ })++ fixture.Model.IncludeSchemas = []string{fixture.SchemaName, "public"}+ fixture.Model.ExcludeTables = []string{sameNamedTableName}++ err := fixture.testConnection()++ require.Error(t, err)+ assert.Contains(t, err.Error(), fixture.SchemaName+"."+sameNamedTableName)+ assert.NotContains(t, err.Error(), "public."+sameNamedTableName)+}+// pg_dump receives the exclude list normalized, so an entry pasted with a leading newline still// excludes the table and must not be re-demanded by the privilege check.func testConnectionUnreadableTableExcludedWithUnnormalizedEntry(diff --git a/backend/internal/features/databases/databases/postgresql/logical/size.go b/backend/internal/features/databases/databases/postgresql/logical/size.goindex 6ddd22d7..df768bbb 100644--- a/backend/internal/features/databases/databases/postgresql/logical/size.go+++ b/backend/internal/features/databases/databases/postgresql/logical/size.go@@ -10,7 +10,10 @@ import (// Materialized views are left out on purpose: pg_dump emits their definition but// not their contents, so counting them would overstate what a restore recreates.const dumpedRelationsSizeQuery = `- SELECT n.nspname, c.relname, COALESCE(pg_total_relation_size(c.oid), 0)+ SELECT n.nspname,+ c.relname,+ pg_catalog.pg_table_is_visible(c.oid),+ COALESCE(pg_total_relation_size(c.oid), 0)` + dumpedRelationScope + `AND c.relkind IN ('r', 'p')`@@ -20,7 +23,7 @@ func getDumpedRelationsSizeBytes(conn *pgx.Conn,filter DumpFilter,) (int64, error) {- rows, err := conn.Query(ctx, dumpedRelationsSizeQuery, filter.getIncludedSchemaNames())+ rows, err := queryDumpedRelations(ctx, conn, dumpedRelationsSizeQuery, filter)if err != nil {return 0, fmt.Errorf("failed to query dumped relations size: %w", err)}@@ -28,6 +31,7 @@ func getDumpedRelationsSizeBytes(defer rows.Close()excludeTablePatterns := filter.getExcludeTablePatterns()+ excludeTableDataPatterns := filter.getExcludeTableDataPatterns()var totalSizeBytes int64@@ -35,11 +39,17 @@ func getDumpedRelationsSizeBytes(var relation relationNamevar relationSizeBytes int64- if err := rows.Scan(&relation.SchemaName, &relation.TableName, &relationSizeBytes); err != nil {+ if err := rows.Scan(+ &relation.SchemaName,+ &relation.TableName,+ &relation.IsVisibleOnSearchPath,+ &relationSizeBytes,+ ); err != nil {return 0, fmt.Errorf("failed to scan dumped relation size: %w", err)}- if isRelationExcluded(relation, excludeTablePatterns) {+ if isRelationExcluded(relation, excludeTablePatterns) ||+ isRelationExcluded(relation, excludeTableDataPatterns) {continue}diff --git a/backend/internal/features/databases/model_test.go b/backend/internal/features/databases/model_test.goindex 3a23e37e..93ecb098 100644--- a/backend/internal/features/databases/model_test.go+++ b/backend/internal/features/databases/model_test.go@@ -291,6 +291,7 @@ func filledSourceDatabase(databaseType DatabaseType) *Database {LocalTunnelEndpoint: &sshtunnel.Endpoint{Host: "127.0.0.1", Port: 41000},IncludeSchemas: []string{"public", "reporting"},ExcludeTables: []string{"audit_log"},+ ExcludeTableData: []string{"event_log"},CpuCount: 4,IsSkipUserMappings: true,}diff --git a/backend/internal/features/tests/logical/postgresql/backup_restore_test.go b/backend/internal/features/tests/logical/postgresql/backup_restore_test.goindex 8617c1c1..da5c0b9f 100644--- a/backend/internal/features/tests/logical/postgresql/backup_restore_test.go+++ b/backend/internal/features/tests/logical/postgresql/backup_restore_test.go@@ -171,6 +171,34 @@ func Test_PostgresqlBackupRestore_AcrossSupportedVersions(t *testing.T) {},)+ t.Run(+ "Test_BackupAndRestorePostgresql_WithExcludeTableData_TableRestoredEmptyWithIndexes",+ func(t *testing.T) {+ testBackupRestoreWithExcludeTableDataForVersion(t, endpoint, dbVersion.tag)+ },+ )++ t.Run(+ "Test_BackupAndRestorePostgresql_WithExclusionPatterns_MatchLikePgDump",+ func(t *testing.T) {+ testBackupRestoreWithExclusionPatternsForVersion(t, endpoint, dbVersion.tag)+ },+ )++ t.Run(+ "Test_BackupAndRestorePostgresql_WithExcludeTableDataOnPartitions_OnlyMatchedPartitionsEmptied",+ func(t *testing.T) {+ testBackupRestoreWithExcludeTableDataOnPartitionsForVersion(t, endpoint, dbVersion.tag)+ },+ )++ t.Run(+ "Test_BackupAndRestorePostgresql_WithSchemasAndExcludeTableData_DataExcludedOnlyInIncludedSchemas",+ func(t *testing.T) {+ testBackupRestoreWithSchemasAndExcludeTableDataForVersion(t, endpoint, dbVersion.tag)+ },+ )+t.Run("Test_BackupAndRestorePostgresql_WithReadOnlyUser_RestoreIsSuccessful", func(t *testing.T) {testBackupRestoreWithReadOnlyUserForVersion(t, endpoint, dbVersion.tag)})diff --git a/backend/internal/features/tests/logical/postgresql/backup_scope_test.go b/backend/internal/features/tests/logical/postgresql/backup_scope_test.gonew file mode 100644index 00000000..c9bf37d6--- /dev/null+++ b/backend/internal/features/tests/logical/postgresql/backup_scope_test.go@@ -0,0 +1,473 @@+package postgresql_logical++import (+ "context"+ "fmt"+ "net/http"+ "os"+ "path/filepath"+ "testing"+ "time"++ "github.com/google/uuid"+ "github.com/jmoiron/sqlx"+ "github.com/stretchr/testify/assert"+ "github.com/stretchr/testify/require"++ "databasus-backend/internal/config"+ backups_core_enums "databasus-backend/internal/features/backups/backups/core/enums"+ backups_core_logical "databasus-backend/internal/features/backups/backups/core/logical"+ restores_core "databasus-backend/internal/features/restores/core"+ "databasus-backend/internal/features/storages"+ logicaltesting "databasus-backend/internal/features/tests/logical/shared"+ users_enums "databasus-backend/internal/features/users/enums"+ users_testing "databasus-backend/internal/features/users/testing"+ workspaces_testing "databasus-backend/internal/features/workspaces/testing"+ test_utils "databasus-backend/internal/util/testing"+ "databasus-backend/internal/util/testing/containers"+)++type backupScopeSettings struct {+ IncludeSchemas []string+ ExcludeTables []string+ ExcludeTableData []string+}++type restoredBackupScope struct {+ Backup *backups_core_logical.LogicalBackup+ RestoredDB *sqlx.DB+}++func backupAndRestoreWithScope(+ t *testing.T,+ container *PostgresContainer,+ pgVersion string,+ settings backupScopeSettings,+) restoredBackupScope {+ t.Helper()++ router := logicaltesting.CreateTestRouter()+ user := users_testing.CreateTestUser(t.Context(), users_enums.UserRoleMember)+ workspace := workspaces_testing.CreateTestWorkspace(t.Context(), "Backup Scope Workspace", user, router)+ storage := storages.CreateTestStorage(workspace.ID)++ database := createDatabaseViaAPI(+ t, router, "Backup Scope Database", workspace.ID,+ container.Host, container.Port,+ container.Username, container.Password, container.Database,+ user.Token,+ )++ t.Cleanup(func() {+ test_utils.MakeDeleteRequest(+ t, router, "/api/v1/databases/"+database.ID.String(),+ "Bearer "+user.Token, http.StatusNoContent,+ )+ storages.RemoveTestStorage(context.Background(), storage.ID)+ workspaces_testing.RemoveTestWorkspace(context.Background(), workspace, router)+ })++ database.PostgresqlLogical.IncludeSchemas = settings.IncludeSchemas+ database.PostgresqlLogical.ExcludeTables = settings.ExcludeTables+ database.PostgresqlLogical.ExcludeTableData = settings.ExcludeTableData+ updateDatabaseViaAPI(t, router, database, user.Token)++ logicaltesting.EnableBackupsViaAPI(+ t, router, database.ID, storage.ID,+ backups_core_enums.BackupEncryptionNone, user.Token,+ )++ logicaltesting.CreateBackupViaAPI(t, router, database.ID, user.Token)++ backup := logicaltesting.WaitForBackupCompletion(t, router, database.ID, user.Token, 5*time.Minute)+ require.Equal(t, backups_core_logical.BackupStatusCompleted, backup.Status)++ t.Cleanup(func() {+ _ = os.Remove(filepath.Join(config.GetEnv().DataFolder, backup.ID.String()))+ })++ restoredDBName := fmt.Sprintf("restored_scope_%s_%s", pgVersion, uuid.New().String()[:8])+ _, err := container.DB.Exec(fmt.Sprintf("CREATE DATABASE %s;", restoredDBName))+ require.NoError(t, err)++ restoredDB, err := sqlx.Connect("postgres", fmt.Sprintf(+ "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",+ container.Host, container.Port, container.Username, container.Password, restoredDBName,+ ))+ require.NoError(t, err)++ t.Cleanup(func() {+ restoredDB.Close()+ _, _ = container.DB.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s;", restoredDBName))+ })++ createRestoreViaAPI(+ t, router, backup.ID,+ container.Host, container.Port,+ container.Username, container.Password, restoredDBName,+ user.Token,+ )++ restore := waitForRestoreCompletion(t, router, backup.ID, user.Token, 5*time.Minute)+ require.Equal(t, restores_core.RestoreStatusCompleted, restore.Status)++ return restoredBackupScope{Backup: backup, RestoredDB: restoredDB}+}++func seedBackupScopeStatements(t *testing.T, container *PostgresContainer, statements string) {+ t.Helper()++ _, err := container.DB.Exec(statements)+ require.NoError(t, err)+}++func isTableRestored(t *testing.T, db *sqlx.DB, relation relationRef) bool {+ t.Helper()++ var isTablePresent bool+ err := db.Get(&isTablePresent, `+ SELECT EXISTS (+ SELECT FROM pg_class c+ JOIN pg_namespace n ON n.oid = c.relnamespace+ WHERE n.nspname = $1 AND c.relname = $2 AND c.relkind IN ('r', 'p')+ )+ `, relation.SchemaName, relation.TableName)+ require.NoError(t, err)++ return isTablePresent+}++func isSchemaRestored(t *testing.T, db *sqlx.DB, schemaName string) bool {+ t.Helper()++ var isSchemaPresent bool+ err := db.Get(+ &isSchemaPresent,+ `SELECT EXISTS (SELECT FROM pg_namespace WHERE nspname = $1)`,+ schemaName,+ )+ require.NoError(t, err)++ return isSchemaPresent+}++func countTableRows(t *testing.T, db *sqlx.DB, relation relationRef) int {+ t.Helper()++ var rowCount int+ err := db.Get(+ &rowCount,+ fmt.Sprintf(`SELECT count(*) FROM %q.%q`, relation.SchemaName, relation.TableName),+ )+ require.NoError(t, err)++ return rowCount+}++func getTableIndexNames(t *testing.T, db *sqlx.DB, relation relationRef) []string {+ t.Helper()++ var indexNames []string+ err := db.Select(&indexNames, `+ SELECT indexname FROM pg_indexes+ WHERE schemaname = $1 AND tablename = $2+ ORDER BY indexname+ `, relation.SchemaName, relation.TableName)+ require.NoError(t, err)++ return indexNames+}++func getTableColumnDefinitions(t *testing.T, db *sqlx.DB, relation relationRef) []string {+ t.Helper()++ var columnDefinitions []string+ err := db.Select(&columnDefinitions, `+ SELECT column_name || ' ' || data_type || ' ' || is_nullable+ FROM information_schema.columns+ WHERE table_schema = $1 AND table_name = $2+ ORDER BY ordinal_position+ `, relation.SchemaName, relation.TableName)+ require.NoError(t, err)++ return columnDefinitions+}++func hasPrimaryKey(t *testing.T, db *sqlx.DB, relation relationRef) bool {+ t.Helper()++ var isPrimaryKeyPresent bool+ err := db.Get(&isPrimaryKeyPresent, `+ SELECT EXISTS (+ SELECT FROM pg_constraint+ WHERE contype = 'p'+ AND conrelid = format('%I.%I', $1::text, $2::text)::regclass+ )+ `, relation.SchemaName, relation.TableName)+ require.NoError(t, err)++ return isPrimaryKeyPresent+}++func testBackupRestoreWithExcludeTableDataForVersion(+ t *testing.T,+ endpoint containers.Endpoint,+ pgVersion string,+) {+ container, err := connectToPostgresEndpoint(t, endpoint)+ require.NoError(t, err)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ emptiedTable := relationRef{SchemaName: "public", TableName: "logs_" + suffix}+ neighbourTable := relationRef{SchemaName: "public", TableName: "orders_" + suffix}++ seedBackupScopeStatements(t, container, fmt.Sprintf(`+ CREATE TABLE public.%[1]s (id SERIAL PRIMARY KEY, payload TEXT NOT NULL);+ CREATE INDEX %[1]s_payload_idx ON public.%[1]s (payload);+ INSERT INTO public.%[1]s (payload) SELECT repeat('x', 1024) FROM generate_series(1, 20000);+ CREATE TABLE public.%[2]s (id SERIAL PRIMARY KEY, data TEXT NOT NULL);+ INSERT INTO public.%[2]s (data) VALUES ('first'), ('second');+ `, emptiedTable.TableName, neighbourTable.TableName))+ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf(+ "DROP TABLE IF EXISTS public.%s, public.%s",+ emptiedTable.TableName, neighbourTable.TableName,+ ))+ })++ sourceIndexNames := getTableIndexNames(t, container.DB, emptiedTable)+ require.Len(t, sourceIndexNames, 2)+ sourceColumnDefinitions := getTableColumnDefinitions(t, container.DB, emptiedTable)++ restored := backupAndRestoreWithScope(t, container, pgVersion, backupScopeSettings{+ ExcludeTableData: []string{"public." + emptiedTable.TableName},+ })++ assert.True(t, isTableRestored(t, restored.RestoredDB, emptiedTable))+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, emptiedTable))+ assert.Equal(t, sourceIndexNames, getTableIndexNames(t, restored.RestoredDB, emptiedTable))+ assert.Equal(+ t, sourceColumnDefinitions, getTableColumnDefinitions(t, restored.RestoredDB, emptiedTable),+ )+ assert.True(t, hasPrimaryKey(t, restored.RestoredDB, emptiedTable))+ assert.Equal(t, 2, countTableRows(t, restored.RestoredDB, neighbourTable))++ emptiedTableSizeMb := measureTableSizeMb(t, container.DB, emptiedTable)+ assert.Greater(t, emptiedTableSizeMb, skippedFromDumpTableMinSizeMb)+ assert.Greater(t, restored.Backup.BackupRawDbSizeMb, 0.0)+ assert.LessOrEqual(+ t,+ restored.Backup.BackupRawDbSizeMb+emptiedTableSizeMb,+ measureWholeDatabaseSizeMb(t, container.DB),+ "recorded raw db size must not count the table whose data is excluded",+ )+}++func testBackupRestoreWithExclusionPatternsForVersion(+ t *testing.T,+ endpoint containers.Endpoint,+ pgVersion string,+) {+ container, err := connectToPostgresEndpoint(t, endpoint)+ require.NoError(t, err)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ archiveSchema := "archive_" + suffix+ historyPrefix := "history_" + suffix+ logsTable := "logs_" + suffix+ auditTable := "audit_tmp_" + suffix+ overlapTable := "overlap_" + suffix+ oldOrdersTable := "old_orders_" + suffix++ seedBackupScopeStatements(t, container, fmt.Sprintf(`+ CREATE SCHEMA %[1]s;+ CREATE TABLE public.%[2]s_2024 (id INT);+ CREATE TABLE %[1]s.%[2]s_2025 (id INT);+ CREATE TABLE public.%[3]s (id INT);+ CREATE TABLE %[1]s.%[3]s (id INT);+ CREATE TABLE public.%[4]s (id INT);+ CREATE TABLE %[1]s.%[4]s (id INT);+ CREATE TABLE public.%[5]s (id INT);+ INSERT INTO public.%[2]s_2024 VALUES (1);+ INSERT INTO %[1]s.%[2]s_2025 VALUES (1);+ INSERT INTO public.%[3]s VALUES (1);+ INSERT INTO %[1]s.%[3]s VALUES (1), (2);+ INSERT INTO public.%[4]s VALUES (1);+ INSERT INTO %[1]s.%[4]s VALUES (1), (2);+ INSERT INTO public.%[5]s VALUES (1);+ CREATE TABLE public.%[6]s (id INT);+ CREATE TABLE %[1]s.%[6]s (id INT);+ INSERT INTO public.%[6]s VALUES (1);+ INSERT INTO %[1]s.%[6]s VALUES (1), (2);+ `, archiveSchema, historyPrefix, logsTable, auditTable, overlapTable, oldOrdersTable))+ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf(+ "DROP SCHEMA IF EXISTS %s CASCADE; "++ "DROP TABLE IF EXISTS public.%s_2024, public.%s, public.%s, public.%s, public.%s",+ archiveSchema, historyPrefix, logsTable, auditTable, overlapTable, oldOrdersTable,+ ))+ })++ restored := backupAndRestoreWithScope(t, container, pgVersion, backupScopeSettings{+ ExcludeTables: []string{+ auditTable,+ "public." + overlapTable,+ "public.old_orders_" + suffix[:4] + "*",+ },+ ExcludeTableData: []string{+ "*." + historyPrefix + "_*",+ logsTable,+ "public." + overlapTable,+ },+ })++ publicHistoryTable := relationRef{SchemaName: "public", TableName: historyPrefix + "_2024"}+ archiveHistoryTable := relationRef{SchemaName: archiveSchema, TableName: historyPrefix + "_2025"}+ assert.True(t, isTableRestored(t, restored.RestoredDB, publicHistoryTable))+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, publicHistoryTable))+ assert.True(t, isTableRestored(t, restored.RestoredDB, archiveHistoryTable))+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, archiveHistoryTable))++ publicLogsTable := relationRef{SchemaName: "public", TableName: logsTable}+ archiveLogsTable := relationRef{SchemaName: archiveSchema, TableName: logsTable}+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, publicLogsTable))+ assert.Equal(+ t, 2, countTableRows(t, restored.RestoredDB, archiveLogsTable),+ "an unqualified data exclusion must not reach a table off the default search path",+ )++ assert.False(t, isTableRestored(+ t, restored.RestoredDB, relationRef{SchemaName: "public", TableName: auditTable},+ ))+ assert.Equal(+ t, 2,+ countTableRows(t, restored.RestoredDB, relationRef{SchemaName: archiveSchema, TableName: auditTable}),+ "an unqualified whole-table exclusion must not reach a table off the default search path",+ )++ assert.False(t, isTableRestored(+ t, restored.RestoredDB, relationRef{SchemaName: "public", TableName: oldOrdersTable},+ ))+ assert.Equal(+ t, 2,+ countTableRows(t, restored.RestoredDB, relationRef{SchemaName: archiveSchema, TableName: oldOrdersTable}),+ "a schema-qualified whole-table pattern must not reach another schema",+ )++ assert.False(+ t,+ isTableRestored(t, restored.RestoredDB, relationRef{SchemaName: "public", TableName: overlapTable}),+ "a table in both lists must be excluded entirely",+ )+}++func testBackupRestoreWithExcludeTableDataOnPartitionsForVersion(+ t *testing.T,+ endpoint containers.Endpoint,+ pgVersion string,+) {+ container, err := connectToPostgresEndpoint(t, endpoint)+ require.NoError(t, err)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ parentNamedAlone := "events_a_" + suffix+ partitionsByPattern := "events_b_" + suffix++ partitionedTableStatements := func(parentName string) string {+ return fmt.Sprintf(`+ CREATE TABLE public.%[1]s (id INT NOT NULL) PARTITION BY RANGE (id);+ CREATE TABLE public.%[1]s_2025 PARTITION OF public.%[1]s FOR VALUES FROM (0) TO (100);+ CREATE TABLE public.%[1]s_2026 PARTITION OF public.%[1]s FOR VALUES FROM (100) TO (200);+ INSERT INTO public.%[1]s VALUES (1), (2), (101);+ `, parentName)+ }++ seedBackupScopeStatements(+ t, container,+ partitionedTableStatements(parentNamedAlone)+partitionedTableStatements(partitionsByPattern),+ )+ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf(+ "DROP TABLE IF EXISTS public.%s, public.%s", parentNamedAlone, partitionsByPattern,+ ))+ })++ restored := backupAndRestoreWithScope(t, container, pgVersion, backupScopeSettings{+ ExcludeTableData: []string{+ "public." + parentNamedAlone,+ "public." + partitionsByPattern + "_*",+ },+ })++ assert.Equal(+ t,+ 2,+ countTableRows(+ t,+ restored.RestoredDB,+ relationRef{SchemaName: "public", TableName: parentNamedAlone + "_2025"},+ ),+ "naming only the partitioned parent must keep its partitions' rows",+ )+ assert.Equal(+ t,+ 1,+ countTableRows(+ t,+ restored.RestoredDB,+ relationRef{SchemaName: "public", TableName: parentNamedAlone + "_2026"},+ ),+ )++ for _, tableName := range []string{+ partitionsByPattern,+ partitionsByPattern + "_2025",+ partitionsByPattern + "_2026",+ } {+ relation := relationRef{SchemaName: "public", TableName: tableName}++ assert.True(t, isTableRestored(t, restored.RestoredDB, relation), tableName)+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, relation), tableName)+ }+}++func testBackupRestoreWithSchemasAndExcludeTableDataForVersion(+ t *testing.T,+ endpoint containers.Endpoint,+ pgVersion string,+) {+ container, err := connectToPostgresEndpoint(t, endpoint)+ require.NoError(t, err)+ t.Cleanup(func() { container.DB.Close() })++ suffix := uuid.New().String()[:8]+ reportingSchema := "reporting_" + suffix+ logsTable := "logs_" + suffix++ seedBackupScopeStatements(t, container, fmt.Sprintf(`+ CREATE SCHEMA %[1]s;+ CREATE TABLE public.%[2]s (id INT);+ CREATE TABLE %[1]s.%[2]s (id INT);+ INSERT INTO public.%[2]s VALUES (1);+ INSERT INTO %[1]s.%[2]s VALUES (1);+ `, reportingSchema, logsTable))+ t.Cleanup(func() {+ _, _ = container.DB.Exec(fmt.Sprintf(+ "DROP SCHEMA IF EXISTS %s CASCADE; DROP TABLE IF EXISTS public.%s",+ reportingSchema, logsTable,+ ))+ })++ restored := backupAndRestoreWithScope(t, container, pgVersion, backupScopeSettings{+ IncludeSchemas: []string{"public"},+ ExcludeTableData: []string{logsTable},+ })++ publicLogsTable := relationRef{SchemaName: "public", TableName: logsTable}+ assert.True(t, isTableRestored(t, restored.RestoredDB, publicLogsTable))+ assert.Equal(t, 0, countTableRows(t, restored.RestoredDB, publicLogsTable))+ assert.False(t, isSchemaRestored(t, restored.RestoredDB, reportingSchema))+}diff --git a/backend/migrations/20261022072202_add_postgresql_exclude_table_data.sql b/backend/migrations/20261022072202_add_postgresql_exclude_table_data.sqlnew file mode 100644index 00000000..a93b8223--- /dev/null+++ b/backend/migrations/20261022072202_add_postgresql_exclude_table_data.sql@@ -0,0 +1,11 @@+-- +goose Up+-- +goose StatementBegin+ALTER TABLE postgresql_logical_databases+ ADD COLUMN exclude_table_data TEXT NOT NULL DEFAULT '';+-- +goose StatementEnd++-- +goose Down+-- +goose StatementBegin+ALTER TABLE postgresql_logical_databases+ DROP COLUMN exclude_table_data;+-- +goose StatementEnddiff --git a/frontend/src/entity/databases/model/postgresql/PostgresqlLogicalDatabase.ts b/frontend/src/entity/databases/model/postgresql/PostgresqlLogicalDatabase.tsindex d87dfe83..26b5c2e1 100644--- a/frontend/src/entity/databases/model/postgresql/PostgresqlLogicalDatabase.ts+++ b/frontend/src/entity/databases/model/postgresql/PostgresqlLogicalDatabase.ts@@ -25,6 +25,7 @@ export interface PostgresqlLogicalDatabase {// backup settingsincludeSchemas?: string[];excludeTables?: string[];+ excludeTableData?: string[];cpuCount: number;isSkipUserMappings?: boolean;diff --git a/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.test.tsx b/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.test.tsxnew file mode 100644index 00000000..67c8d742--- /dev/null+++ b/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.test.tsx@@ -0,0 +1,56 @@+import { renderToStaticMarkup } from 'react-dom/server';+import { describe, expect, it, vi } from 'vitest';++import { type Database, DatabaseType, MysqlVersion } from '../../../../entity/databases';+import { LocaleProvider } from '../../../../shared/i18n';+import { EditMySqlSpecificDataComponent } from './EditMySqlSpecificDataComponent';++// The entity's public API imports its API client, and the client's config+// module reads window at import time. The tests run in node, so they supply+// window. LocaleProvider then reads localStorage, which here reports no saved+// language.+vi.hoisted(() => {+ vi.stubGlobal('window', {});+ vi.stubGlobal('localStorage', { getItem: () => null });+ vi.stubGlobal('navigator', { language: 'en-US', languages: ['en-US'] });+});++const mysqlDatabaseWithExcludedTable: Database = {+ id: 'database-id',+ name: 'Shop',+ workspaceId: 'workspace-id',+ type: DatabaseType.MYSQL,+ notifiers: [],+ mysql: {+ id: 'mysql-id',+ version: MysqlVersion.MysqlVersion84,+ host: 'db.example.com',+ port: 3306,+ username: 'backup',+ password: '',+ database: 'shop',+ isHttps: false,+ excludeTables: ['audit_log'],+ },+};++describe('EditMySqlSpecificDataComponent', () => {+ it('keeps the table exclusion label that has no data-only counterpart', () => {+ const markup = renderToStaticMarkup(+ <LocaleProvider>+ <EditMySqlSpecificDataComponent+ database={mysqlDatabaseWithExcludedTable}+ onCancel={() => {}}+ isShowBackButton={false}+ onBack={() => {}}+ isSaveToApi={false}+ onSaved={() => {}}+ />+ </LocaleProvider>,+ );++ expect(markup).toContain('Exclude tables');+ expect(markup).toContain('audit_log');+ expect(markup).not.toContain('Exclude whole tables');+ });+});diff --git a/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.tsx b/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.tsxindex c42e055b..b2a75678 100644--- a/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.tsx+++ b/frontend/src/features/databases/ui/edit/EditMySqlSpecificDataComponent.tsx@@ -61,7 +61,7 @@ export const EditMySqlSpecificDataComponent = ({const { locale } = useLocale();const { message } = App.useApp();- const [editingDatabase, setEditingDatabase] = useState<Database>();+ const [editingDatabase, setEditingDatabase] = useState<Database>({ ...database });const [isSaving, setIsSaving] = useState(false);const [isConnectionTested, setIsConnectionTested] = useState(false);@@ -186,8 +186,6 @@ export const EditMySqlSpecificDataComponent = ({setEditingDatabase({ ...database });}, [database]);- if (!editingDatabase) return null;-const engineName = t(DATABASE_TYPE_LABEL_KEYS[DatabaseType.MYSQL]);const hasStoredSshSecrets = hasStoredSshTunnelSecretsForAuthType(diff --git a/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.test.tsx b/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.test.tsxnew file mode 100644index 00000000..dd23811e--- /dev/null+++ b/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.test.tsx@@ -0,0 +1,77 @@+import { renderToStaticMarkup } from 'react-dom/server';+import { describe, expect, it, vi } from 'vitest';++import {+ type Database,+ DatabaseType,+ type PostgresqlLogicalDatabase,+ PostgresSslMode,+ PostgresqlVersion,+} from '../../../../entity/databases';+import { LocaleProvider } from '../../../../shared/i18n';+import { EditPostgreSqlLogicalSpecificDataComponent } from './EditPostgreSqlLogicalSpecificDataComponent';++// The entity's public API imports its API client, and the client's config+// module reads window at import time. The tests run in node, so they supply+// window. LocaleProvider then reads localStorage, which here reports no saved+// language.+vi.hoisted(() => {+ vi.stubGlobal('window', {});+ vi.stubGlobal('localStorage', { getItem: () => null });+ vi.stubGlobal('navigator', { language: 'en-US', languages: ['en-US'] });+});++const createPostgresqlDatabase = (change: Partial<PostgresqlLogicalDatabase>): Database => ({+ id: 'database-id',+ name: 'Orders',+ workspaceId: 'workspace-id',+ type: DatabaseType.POSTGRES_LOGICAL,+ notifiers: [],+ postgresqlLogical: {+ id: 'postgresql-id',+ version: PostgresqlVersion.PostgresqlVersion16,+ host: 'db.example.com',+ port: 5432,+ username: 'backup',+ password: '',+ database: 'orders',+ sslMode: PostgresSslMode.Require,+ cpuCount: 1,+ ...change,+ },+});++const renderPostgresqlForm = (database: Database) =>+ renderToStaticMarkup(+ <LocaleProvider>+ <EditPostgreSqlLogicalSpecificDataComponent+ database={database}+ onCancel={() => {}}+ isShowBackButton={false}+ onBack={() => {}}+ isSaveToApi={false}+ onSaved={() => {}}+ />+ </LocaleProvider>,+ );++describe('EditPostgreSqlLogicalSpecificDataComponent', () => {+ it('opens the advanced settings with both exclusion fields when only table data is excluded', () => {+ const markup = renderPostgresqlForm(createPostgresqlDatabase({ excludeTableData: ['logs'] }));++ const wholeTablesLabelIndex = markup.indexOf('Exclude whole tables');+ const tableDataLabelIndex = markup.indexOf('Exclude table data');++ expect(wholeTablesLabelIndex).toBeGreaterThan(-1);+ expect(tableDataLabelIndex).toBeGreaterThan(wholeTablesLabelIndex);+ expect(markup.indexOf('logs', tableDataLabelIndex)).toBeGreaterThan(tableDataLabelIndex);+ });++ it('keeps the advanced settings collapsed when no advanced value is set', () => {+ const markup = renderPostgresqlForm(createPostgresqlDatabase({}));++ expect(markup).toContain('Advanced settings');+ expect(markup).not.toContain('Exclude whole tables');+ expect(markup).not.toContain('Exclude table data');+ });+});diff --git a/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.tsx b/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.tsxindex b4699fa3..e03c279b 100644--- a/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.tsx+++ b/frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.tsx@@ -92,7 +92,7 @@ export const EditPostgreSqlLogicalSpecificDataComponent = ({const { locale } = useLocale();const { message } = App.useApp();- const [editingDatabase, setEditingDatabase] = useState<Database>();+ const [editingDatabase, setEditingDatabase] = useState<Database>({ ...database });const [isSaving, setIsSaving] = useState(false);const [isConnectionTested, setIsConnectionTested] = useState(false);@@ -109,6 +109,7 @@ export const EditPostgreSqlLogicalSpecificDataComponent = ({!!database.postgresqlLogical?.isRestorePrivileges: !!database.postgresqlLogical?.includeSchemas?.length ||!!database.postgresqlLogical?.excludeTables?.length ||+ !!database.postgresqlLogical?.excludeTableData?.length ||!!database.postgresqlLogical?.isSkipUserMappings);const [isShowAdvanced, setShowAdvanced] = useState(hasAdvancedValues);@@ -289,8 +290,6 @@ export const EditPostgreSqlLogicalSpecificDataComponent = ({setEditingDatabase({ ...database });}, [database]);- if (!editingDatabase) return null;-const renderFooter = (footerContent?: React.ReactNode) => (<div className="mt-5 flex">{isShowCancelButton && (@@ -701,7 +700,7 @@ export const EditPostgreSqlLogicalSpecificDataComponent = ({{!isRestoreMode && (<div className="mb-1 flex w-full items-center">- <div className="min-w-[150px] pr-2">{t('databases.fields.excludeTables')}</div>+ <div className="min-w-[150px] pr-2">{t('databases.fields.excludeWholeTables')}</div><Selectmode="tags"value={editingDatabase.postgresqlLogical?.excludeTables || []}@@ -731,6 +730,38 @@ export const EditPostgreSqlLogicalSpecificDataComponent = ({</div>)}+ {!isRestoreMode && (+ <div className="mb-1 flex w-full items-center">+ <div className="min-w-[150px] pr-2">{t('databases.fields.excludeTableData')}</div>+ <Select+ mode="tags"+ value={editingDatabase.postgresqlLogical?.excludeTableData || []}+ onChange={(values) => {+ if (!editingDatabase.postgresqlLogical) return;++ setEditingDatabase({+ ...editingDatabase,+ postgresqlLogical: {+ ...editingDatabase.postgresqlLogical,+ excludeTableData: normalizeNameList(values),+ },+ });+ }}+ size="small"+ className="max-w-[200px] grow"+ placeholder={t('databases.edit.postgresql.excludeTableDataPlaceholder')}+ tokenSeparators={NAME_LIST_TOKEN_SEPARATORS}+ />++ <Tooltip+ className="cursor-pointer"+ title={t('databases.edit.postgresql.excludeTableDataTooltip')}+ >+ <InfoCircleOutlined className="ml-2" style={{ color: 'gray' }} />+ </Tooltip>+ </div>+ )}+{!isRestoreMode && (<div className="mb-1 flex w-full items-center"><div className="flex min-w-[150px] items-center pr-2">diff --git a/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.test.tsx b/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.test.tsxnew file mode 100644index 00000000..5f1b5366--- /dev/null+++ b/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.test.tsx@@ -0,0 +1,72 @@+import { renderToStaticMarkup } from 'react-dom/server';+import { describe, expect, it, vi } from 'vitest';++import {+ type Database,+ DatabaseType,+ type PostgresqlLogicalDatabase,+ PostgresSslMode,+ PostgresqlVersion,+} from '../../../../entity/databases';+import { LocaleProvider } from '../../../../shared/i18n';+import { ShowPostgreSqlLogicalSpecificDataComponent } from './ShowPostgreSqlLogicalSpecificDataComponent';++// The entity's public API imports its API client, and the client's config+// module reads window at import time. The tests run in node, so they supply+// window. LocaleProvider then reads localStorage, which here reports no saved+// language.+vi.hoisted(() => {+ vi.stubGlobal('window', {});+ vi.stubGlobal('localStorage', { getItem: () => null });+ vi.stubGlobal('navigator', { language: 'en-US', languages: ['en-US'] });+});++const createPostgresqlDatabase = (change: Partial<PostgresqlLogicalDatabase>): Database => ({+ id: 'database-id',+ name: 'Orders',+ workspaceId: 'workspace-id',+ type: DatabaseType.POSTGRES_LOGICAL,+ notifiers: [],+ postgresqlLogical: {+ id: 'postgresql-id',+ version: PostgresqlVersion.PostgresqlVersion16,+ host: 'db.example.com',+ port: 5432,+ username: 'backup',+ password: '',+ database: 'orders',+ sslMode: PostgresSslMode.Require,+ cpuCount: 1,+ ...change,+ },+});++const renderPostgresqlPage = (database: Database) =>+ renderToStaticMarkup(+ <LocaleProvider>+ <ShowPostgreSqlLogicalSpecificDataComponent database={database} />+ </LocaleProvider>,+ );++describe('ShowPostgreSqlLogicalSpecificDataComponent', () => {+ it('shows each exclusion list under its own label', () => {+ const markup = renderPostgresqlPage(+ createPostgresqlDatabase({ excludeTables: ['audit_tmp'], excludeTableData: ['logs'] }),+ );++ const wholeTablesLabelIndex = markup.indexOf('Exclude whole tables');+ const tableDataLabelIndex = markup.indexOf('Exclude table data');++ expect(wholeTablesLabelIndex).toBeGreaterThan(-1);+ expect(markup.indexOf('audit_tmp')).toBeGreaterThan(wholeTablesLabelIndex);+ expect(markup.indexOf('audit_tmp')).toBeLessThan(tableDataLabelIndex);+ expect(markup.indexOf('logs')).toBeGreaterThan(tableDataLabelIndex);+ });++ it('hides both exclusion rows when neither list is set', () => {+ const markup = renderPostgresqlPage(createPostgresqlDatabase({}));++ expect(markup).not.toContain('Exclude whole tables');+ expect(markup).not.toContain('Exclude table data');+ });+});diff --git a/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.tsx b/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.tsxindex 22578e54..e0baf0ad 100644--- a/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.tsx+++ b/frontend/src/features/databases/ui/show/ShowPostgreSqlLogicalSpecificDataComponent.tsx@@ -93,11 +93,18 @@ export const ShowPostgreSqlLogicalSpecificDataComponent = ({ database }: Props){!!database.postgresqlLogical?.excludeTables?.length && (<div className="mb-1 flex w-full items-center">- <div className="min-w-[150px] pr-2">{t('databases.fields.excludeTables')}</div>+ <div className="min-w-[150px] pr-2">{t('databases.fields.excludeWholeTables')}</div><div>{database.postgresqlLogical.excludeTables.join(', ')}</div></div>)}+ {!!database.postgresqlLogical?.excludeTableData?.length && (+ <div className="mb-1 flex w-full items-center">+ <div className="min-w-[150px] pr-2">{t('databases.fields.excludeTableData')}</div>+ <div>{database.postgresqlLogical.excludeTableData.join(', ')}</div>+ </div>+ )}+{!!database.postgresqlLogical?.isSkipUserMappings && (<div className="mb-1 flex w-full items-center"><div className="min-w-[150px] pr-2">{t('databases.fields.skipUserMappings')}</div>diff --git a/frontend/src/shared/i18n/locales/en.ts b/frontend/src/shared/i18n/locales/en.tsindex 674f4b02..cfda9865 100644--- a/frontend/src/shared/i18n/locales/en.ts+++ b/frontend/src/shared/i18n/locales/en.ts@@ -377,6 +377,8 @@ export const en = {serverCaCertificate: 'Server CA certificate',includeSchemas: 'Include schemas',excludeTables: 'Exclude tables',+ excludeWholeTables: 'Exclude whole tables',+ excludeTableData: 'Exclude table data',skipUserMappings: 'Skip user mappings',excludeExtensions: 'Exclude extensions',restoreOwnership: 'Restore ownership',@@ -424,7 +426,10 @@ export const en = {'Please <docsLink>read this document</docsLink> to study how to backup Supabase database',includeSchemasPlaceholder: 'All schemas (default)',excludeTablesTooltip:- "Tables to exclude from the backup. Use 'tablename' or 'schema.tablename'. Glob patterns are supported (e.g. 'logs_*'). You can paste a list separated by commas or new lines.",+ "Tables left out of the backup completely: after a restore they will be missing. Use 'tablename' or 'schema.tablename'. A name without a schema matches only tables on the default search path, usually 'public'; use '*.tablename' to match it in every schema. Glob patterns are supported (e.g. 'logs_*'). You can paste a list separated by commas or new lines.",+ excludeTableDataTooltip:+ "Tables backed up without their rows: after a restore they exist with their structure, indexes and constraints, but empty. Use 'tablename' or 'schema.tablename'. A name without a schema matches only tables on the default search path, usually 'public'; use '*.tablename' to match it in every schema. Glob patterns are supported (e.g. 'logs_*'). A table listed in both fields is excluded entirely. Partitions of a partitioned table need their own names or a pattern such as 'events_*'. If another backed-up table has a foreign key to rows of an emptied table, the restore fails to recreate that foreign key. You can paste a list separated by commas or new lines.",+ excludeTableDataPlaceholder: 'No table data excluded',skipUserMappingsTooltip:'Skip restoring user mappings (CREATE USER MAPPING statements). Enable this when the backup role cannot read the mapping credentials - otherwise they are dumped without options and break restore for FDWs like oracle_fdw.',excludeExtensionsTooltip:diff --git a/frontend/src/shared/i18n/locales/es.ts b/frontend/src/shared/i18n/locales/es.tsindex 752b7e4a..3cfc197a 100644--- a/frontend/src/shared/i18n/locales/es.ts+++ b/frontend/src/shared/i18n/locales/es.ts@@ -385,6 +385,8 @@ export const es: typeof en = {serverCaCertificate: 'Certificado CA del servidor',includeSchemas: 'Incluir esquemas',excludeTables: 'Excluir tablas',+ excludeWholeTables: 'Excluir tablas completas',+ excludeTableData: 'Excluir datos de tablas',skipUserMappings: 'Omitir user mappings',excludeExtensions: 'Excluir extensiones',restoreOwnership: 'Restaurar propietarios',@@ -432,7 +434,10 @@ export const es: typeof en = {'<docsLink>Lea este artículo</docsLink> para saber cómo hacer copias de seguridad de una base de datos de Supabase',includeSchemasPlaceholder: 'Todos los esquemas (predeterminado)',excludeTablesTooltip:- "Tablas que no se incluirán en la copia de seguridad. Use 'tablename' o 'schema.tablename'. Se admiten patrones glob (p. ej., 'logs_*'). Puede pegar una lista separada por comas o por saltos de línea.",+ "Tablas que quedan fuera de la copia de seguridad por completo: después de restaurar no existirán. Use 'tablename' o 'schema.tablename'. Un nombre sin esquema solo encuentra tablas del search_path predeterminado, normalmente 'public'; use '*.tablename' para buscar en todos los esquemas. Se admiten patrones glob (p. ej., 'logs_*'). Puede pegar una lista separada por comas o por saltos de línea.",+ excludeTableDataTooltip:+ "Tablas que se guardan en la copia de seguridad sin sus filas: después de restaurar existen con su estructura, índices y restricciones, pero vacías. Use 'tablename' o 'schema.tablename'. Un nombre sin esquema solo encuentra tablas del search_path predeterminado, normalmente 'public'; use '*.tablename' para buscar en todos los esquemas. Se admiten patrones glob (p. ej., 'logs_*'). Una tabla indicada en ambos campos se excluye por completo. Las particiones de una tabla particionada necesitan su propio nombre o un patrón como 'events_*'. Si otra tabla de la copia tiene una clave foránea hacia filas de una tabla vaciada, la restauración no podrá recrear esa clave foránea. Puede pegar una lista separada por comas o por saltos de línea.",+ excludeTableDataPlaceholder: 'No se excluyen datos de tablas',skipUserMappingsTooltip:'No restaura los user mappings (sentencias CREATE USER MAPPING). Active esta opción si el rol de la copia de seguridad no puede leer sus credenciales: si no, se vuelcan sin opciones y rompen la restauración con FDW como oracle_fdw.',excludeExtensionsTooltip:diff --git a/frontend/src/shared/i18n/locales/fr.ts b/frontend/src/shared/i18n/locales/fr.tsindex 5a7e40d9..8e28858c 100644--- a/frontend/src/shared/i18n/locales/fr.ts+++ b/frontend/src/shared/i18n/locales/fr.ts@@ -384,6 +384,8 @@ export const fr: typeof en = {serverCaCertificate: 'Certificat CA du serveur',includeSchemas: 'Schémas inclus',excludeTables: 'Tables exclues',+ excludeWholeTables: 'Tables exclues entièrement',+ excludeTableData: 'Données de tables exclues',skipUserMappings: "Ignorer les correspondances d'utilisateurs",excludeExtensions: 'Exclure les extensions',restoreOwnership: 'Restaurer les propriétaires',@@ -432,7 +434,10 @@ export const fr: typeof en = {'<docsLink>Lisez cette page</docsLink> pour savoir comment sauvegarder une base de données Supabase',includeSchemasPlaceholder: 'Tous les schémas (par défaut)',excludeTablesTooltip:- "Tables à exclure de la sauvegarde. Utilisez 'tablename' ou 'schema.tablename'. Les motifs glob sont acceptés (par ex. 'logs_*'). Vous pouvez coller une liste séparée par des virgules ou des retours à la ligne.",+ "Tables entièrement exclues de la sauvegarde : après une restauration, elles n'existeront pas. Utilisez 'tablename' ou 'schema.tablename'. Un nom sans schéma ne trouve que les tables du search_path par défaut, généralement 'public' ; utilisez '*.tablename' pour chercher dans tous les schémas. Les motifs glob sont acceptés (par ex. 'logs_*'). Vous pouvez coller une liste séparée par des virgules ou des retours à la ligne.",+ excludeTableDataTooltip:+ "Tables sauvegardées sans leurs lignes : après une restauration, elles existent avec leur structure, leurs index et leurs contraintes, mais vides. Utilisez 'tablename' ou 'schema.tablename'. Un nom sans schéma ne trouve que les tables du search_path par défaut, généralement 'public' ; utilisez '*.tablename' pour chercher dans tous les schémas. Les motifs glob sont acceptés (par ex. 'logs_*'). Une table indiquée dans les deux champs est entièrement exclue. Les partitions d'une table partitionnée doivent être indiquées une par une ou par un motif comme 'events_*'. Si une autre table sauvegardée a une clé étrangère vers des lignes d'une table vidée, la restauration ne pourra pas recréer cette clé étrangère. Vous pouvez coller une liste séparée par des virgules ou des retours à la ligne.",+ excludeTableDataPlaceholder: 'Aucune donnée de table exclue',skipUserMappingsTooltip:"Ne pas restaurer les correspondances d'utilisateurs (user mappings, instructions CREATE USER MAPPING). Activez cette option si le rôle de sauvegarde ne peut pas lire les identifiants de ces correspondances : sinon, elles sont exportées sans options et font échouer la restauration des FDW comme oracle_fdw.",excludeExtensionsTooltip:diff --git a/frontend/src/shared/i18n/locales/pt.ts b/frontend/src/shared/i18n/locales/pt.tsindex 008c2f3d..71cd84f7 100644--- a/frontend/src/shared/i18n/locales/pt.ts+++ b/frontend/src/shared/i18n/locales/pt.ts@@ -383,6 +383,8 @@ export const pt: typeof en = {serverCaCertificate: 'Certificado CA do servidor',includeSchemas: 'Incluir schemas',excludeTables: 'Ignorar tabelas',+ excludeWholeTables: 'Ignorar tabelas inteiras',+ excludeTableData: 'Ignorar dados de tabelas',skipUserMappings: 'Pular mapeamentos de usuário',excludeExtensions: 'Ignorar extensões',restoreOwnership: 'Restaurar proprietários',@@ -431,7 +433,10 @@ export const pt: typeof en = {'<docsLink>Leia este guia</docsLink> para saber como fazer backup de um banco de dados Supabase',includeSchemasPlaceholder: 'Todos os schemas (padrão)',excludeTablesTooltip:- "Tabelas que ficam fora do backup. Use 'tablename' ou 'schema.tablename'. Padrões glob são aceitos (por exemplo, 'logs_*'). Você pode colar uma lista separada por vírgulas ou quebras de linha.",+ "Tabelas que ficam totalmente fora do backup: depois da restauração, elas não vão existir. Use 'tablename' ou 'schema.tablename'. Um nome sem schema só encontra tabelas do search_path padrão, normalmente 'public'; use '*.tablename' para buscar em todos os schemas. Padrões glob são aceitos (por exemplo, 'logs_*'). Você pode colar uma lista separada por vírgulas ou quebras de linha.",+ excludeTableDataTooltip:+ "Tabelas que entram no backup sem as linhas: depois da restauração, elas existem com estrutura, índices e restrições, mas vazias. Use 'tablename' ou 'schema.tablename'. Um nome sem schema só encontra tabelas do search_path padrão, normalmente 'public'; use '*.tablename' para buscar em todos os schemas. Padrões glob são aceitos (por exemplo, 'logs_*'). Uma tabela informada nos dois campos fica totalmente fora do backup. As partições de uma tabela particionada precisam do próprio nome ou de um padrão como 'events_*'. Se outra tabela do backup tiver uma chave estrangeira para linhas de uma tabela esvaziada, a restauração não vai conseguir recriar essa chave estrangeira. Você pode colar uma lista separada por vírgulas ou quebras de linha.",+ excludeTableDataPlaceholder: 'Nenhum dado de tabela ignorado',skipUserMappingsTooltip:'Não restaura os mapeamentos de usuário (user mappings, instruções CREATE USER MAPPING). Ative se o papel usado no backup não consegue ler as credenciais dos mapeamentos: sem isso, eles vão para o dump sem opções e quebram a restauração de FDWs como o oracle_fdw.',excludeExtensionsTooltip:diff --git a/frontend/src/shared/i18n/locales/ru.ts b/frontend/src/shared/i18n/locales/ru.tsindex 00a89d69..25181add 100644--- a/frontend/src/shared/i18n/locales/ru.ts+++ b/frontend/src/shared/i18n/locales/ru.ts@@ -377,6 +377,8 @@ export const ru: typeof en = {serverCaCertificate: 'CA-сертификат сервера',includeSchemas: 'Включаемые схемы',excludeTables: 'Исключаемые таблицы',+ excludeWholeTables: 'Исключать таблицы целиком',+ excludeTableData: 'Исключать данные таблиц',skipUserMappings: 'Пропускать user mappings',excludeExtensions: 'Исключать расширения',restoreOwnership: 'Восстанавливать владельцев',@@ -424,7 +426,10 @@ export const ru: typeof en = {'<docsLink>Прочитайте эту статью</docsLink>, чтобы узнать, как делать бекап базы данных Supabase',includeSchemasPlaceholder: 'Все схемы (по умолчанию)',excludeTablesTooltip:- "Таблицы, которые не попадут в бекап. Укажите 'tablename' или 'schema.tablename'. Поддерживаются glob-шаблоны (например, 'logs_*'). Можно вставить список через запятую или с новой строки.",+ "Таблицы, которые совсем не попадут в бекап: после восстановления их не будет. Укажите 'tablename' или 'schema.tablename'. Имя без схемы находит только таблицы из search_path по умолчанию, обычно из 'public', а '*.tablename' ищет во всех схемах. Поддерживаются glob-шаблоны (например, 'logs_*'). Можно вставить список через запятую или с новой строки.",+ excludeTableDataTooltip:+ "Таблицы, которые попадут в бекап без строк: после восстановления они будут пустыми, но сохранят структуру, индексы и ограничения. Укажите 'tablename' или 'schema.tablename'. Имя без схемы находит только таблицы из search_path по умолчанию, обычно из 'public', а '*.tablename' ищет во всех схемах. Поддерживаются glob-шаблоны (например, 'logs_*'). Таблица, указанная в обоих полях, исключается целиком. Партиции секционированной таблицы нужно указать отдельно или шаблоном, например 'events_*'. Если другая таблица из бекапа ссылается внешним ключом на строки опустевшей таблицы, при восстановлении этот внешний ключ не создастся. Можно вставить список через запятую или с новой строки.",+ excludeTableDataPlaceholder: 'Данные всех таблиц сохраняются',skipUserMappingsTooltip:'Не восстанавливать user mappings (команды CREATE USER MAPPING). Включите, если роль для бекапа не может прочитать их учетные данные: иначе они попадут в дамп без параметров и сломают восстановление для FDW вроде oracle_fdw.',excludeExtensionsTooltip:diff --git a/frontend/src/shared/i18n/locales/zh.ts b/frontend/src/shared/i18n/locales/zh.tsindex 1402299e..b84fa525 100644--- a/frontend/src/shared/i18n/locales/zh.ts+++ b/frontend/src/shared/i18n/locales/zh.ts@@ -365,6 +365,8 @@ export const zh: typeof en = {serverCaCertificate: '服务器 CA 证书',includeSchemas: '包含的模式',excludeTables: '排除的表',+ excludeWholeTables: '整表排除',+ excludeTableData: '排除表数据',skipUserMappings: '跳过用户映射',excludeExtensions: '排除扩展',restoreOwnership: '恢复所有者',@@ -408,7 +410,10 @@ export const zh: typeof en = {supabaseHint: '请<docsLink>阅读这篇文档</docsLink>,了解如何备份 Supabase 数据库',includeSchemasPlaceholder: '所有模式(默认)',excludeTablesTooltip:- "不纳入备份的表。格式为 'tablename' 或 'schema.tablename',支持 glob 模式(例如 'logs_*')。可以粘贴以逗号或换行分隔的列表。",+ "完全不纳入备份的表:恢复后这些表将不存在。格式为 'tablename' 或 'schema.tablename'。不带模式名的名称只匹配默认 search_path 中的表(通常是 'public'),用 '*.tablename' 可以匹配所有模式。支持 glob 模式(例如 'logs_*')。可以粘贴以逗号或换行分隔的列表。",+ excludeTableDataTooltip:+ "备份时不含数据行的表:恢复后表仍然存在,保留结构、索引和约束,但没有数据。格式为 'tablename' 或 'schema.tablename'。不带模式名的名称只匹配默认 search_path 中的表(通常是 'public'),用 '*.tablename' 可以匹配所有模式。支持 glob 模式(例如 'logs_*')。同时填写在两个字段中的表会被整表排除。分区表的各个分区需要单独列出,或用 'events_*' 这样的模式匹配。如果备份中的其他表通过外键引用了被清空表中的行,恢复时将无法重建这个外键。可以粘贴以逗号或换行分隔的列表。",+ excludeTableDataPlaceholder: '未排除任何表数据',skipUserMappingsTooltip:'恢复时跳过用户映射(CREATE USER MAPPING 语句)。如果备份所用的角色无法读取映射中的凭据,请开启此项:否则转储出的映射会丢失选项(OPTIONS),导致 oracle_fdw 等 FDW 恢复失败。',excludeExtensionsTooltip:diff --git a/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/.openspec.yaml b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/.openspec.yamlnew file mode 100644index 00000000..5c881ba7--- /dev/null+++ b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/.openspec.yaml@@ -0,0 +1,2 @@+schema: spec-driven+created: 2026-10-18diff --git a/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/design.md b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/design.mdnew file mode 100644index 00000000..627add95--- /dev/null+++ b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/design.md@@ -0,0 +1,103 @@+## Context++See proposal.md - Why for the motivation. The requirements are in `specs/postgresql-logical-backup-scope/spec.md`.++The existing whole-table exclusion runs through four places, and the new list has to run through the same ones:++- **Stored settings.** `ExcludeTables` is a `[]string` kept as a comma-joined `exclude_tables` column, converted in `BeforeSave` and `AfterFind` (`backend/internal/features/databases/databases/postgresql/logical/model.go:53-54`, `model.go:68-79`). `Update` and `CopyForNewDatabase` carry it over (`model.go:187-188`, `model.go:211-212`). Normalization lives in `backend/internal/util/namelist`, which splits on commas, new lines and tabs, trims and deduplicates.+- **Dump arguments.** Each normalized entry becomes `--exclude-table=<entry>` (`backend/internal/features/backups/backups/usecases/logical/postgresql/create_backup_uc.go:366-368`). `pg_dump` runs from the client tools of the server's own major version, so every supported version, 12 to 18, accepts `--exclude-table-data`.+- **Privilege check.** When a database is created, updated or has its connection tested, `PostgresqlLogicalDatabase.TestConnection` runs `checkDumpReadPrivileges`, which receives a `DumpFilter` (`model.go:942-945`). `getUnreadableDumpedRelations` drops whole-excluded relations from the list of unreadable tables and sequences (`privileges.go:90-107`). The error text suggests "include schemas and exclude tables" (`privileges.go:153-156`).+- **Size estimate.** `GetRawDbSizeMb` passes the same `DumpFilter` (`model.go:153-156`), and `getDumpedRelationsSizeBytes` skips whole-excluded tables (`size.go:30-44`). `DumpFilter` and the pattern matcher are in `dumpscope.go:34-101`. The matcher does not fully mirror `pg_dump`. Its comment at `dumpscope.go:80` says an unqualified pattern hits the table name in every schema, and `isMatchingRelation` behaves that way (`dumpscope.go:71-77`). `pg_dump` resolves table patterns the way psql does: it runs `RESET search_path` and, for a pattern without a schema part, adds `pg_table_is_visible(c.oid)`, so `logs` matches only a `logs` table visible on the role's default search path. Today this already skews the existing list: an unreadable `archive.logs` passes the privilege check when `logs` is excluded, although `pg_dump` dumps and locks it, and its size is left out of the estimate.++In the frontend, the PostgreSQL form renders "Include schemas" and "Exclude tables" as tag inputs inside Advanced settings (`frontend/src/features/databases/ui/edit/EditPostgreSqlLogicalSpecificDataComponent.tsx:677-732`) and opens that section on its own when either is set (`EditPostgreSqlLogicalSpecificDataComponent.tsx:102-113`). The database page lists them in `ShowPostgreSqlLogicalSpecificDataComponent.tsx:87-99`. The label key `databases.fields.excludeTables` is shared with the MySQL and MariaDB forms and pages, while the PostgreSQL tooltip already has its own key, `databases.edit.postgresql.excludeTablesTooltip`.++Restore needs no change. An archive made with `--exclude-table-data` simply has no `TABLE DATA` entry for the table, and `pg_restore` creates the table empty.++## Goals / Non-Goals++**Goals:**++- One more list that flows through storage, dump arguments, the privilege check, the size estimate, copy and the UI by the same paths as the existing one.+- Labels and tooltips that answer "what will the restored database contain" for each list.++**Non-Goals:**++- No change to restore, to physical backups or to the other engines.+- No validation across the two lists.++## Decisions++### Two separate lists++The model gets `ExcludeTableData []string` with `json:"excludeTableData"`, stored in a new `exclude_table_data` column in the same comma-joined format, and converted, updated and copied next to `ExcludeTables`.++- *Rejected: one list where every entry carries a mode ("whole table" or "data only").* The mode is visible next to each entry, but `excludeTables` would change from a string list to a list of objects. That breaks the API and the stored column, and it needs a row-editor component that matches no other field in the form. Two modes do not justify it.+- *Rejected: one list with a single switch for the whole list.* It cannot express the common case of dropping `tmp_*` completely while keeping `logs` as an empty table.+- *Rejected: a JSON column or a child table for the new list.* The existing list, the included schemas and the MySQL, MariaDB and MongoDB lists all use the comma-joined text column through `namelist`. A different format for one list adds a second code path for the same kind of data.++### The API field keeps its name, only the label changes++`excludeTables` stays the API and Go name of the whole-table list. Only the PostgreSQL label becomes "Exclude whole tables".++- *Rejected: renaming the field to `excludeWholeTables`.* It changes no behavior and breaks every API client and the stored column for a naming gain the user never sees.++### Whole exclusion is skipped by the privilege check, data exclusion is not++`DumpFilter` gains `ExcludeTableDataPatterns`. The size estimate skips a relation matched by either list. The privilege check keeps skipping only whole-excluded relations. The reason is in `pg_dump` itself: it takes `LOCK TABLE ... IN ACCESS SHARE MODE` on every table whose definition it dumps, data or not, and a role without read access fails that lock. The missing-privileges error changes its advice to "narrow the backup with include schemas and whole-table exclusion" and adds that excluding only a table's data still requires SELECT on it.++- *Rejected: skipping data-excluded tables in the privilege check too.* The check would pass and the backup would then fail inside `pg_dump` on the lock, with a less clear message, which is the failure the check exists to prevent.+- *Rejected: checking for any privilege that permits an `ACCESS SHARE` lock (INSERT, UPDATE, DELETE or TRUNCATE also do) for data-excluded tables.* It would admit a few more roles at the cost of a second privilege query, and no one has asked for it.++The size query already counts `pg_total_relation_size`, which includes indexes, so skipping the relation removes its indexes from the estimate as the spec requires.++### Unqualified entries match the way `pg_dump` matches them++The backup keeps passing entries to `pg_dump` unchanged, so what gets excluded does not change for anyone. The Go matcher is fixed to agree with it, for both lists. The two queries built on `dumpedRelationScope` (the size query in `size.go` and `unreadableRelationsQuery` in `privileges.go`) also select `pg_catalog.pg_table_is_visible(c.oid)`, and they run after `RESET search_path` on the same connection, as `pg_dump` does. `relationName` carries that flag, and `dumpTablePattern.isMatchingRelation` requires it for a pattern without a schema part. The comment at `dumpscope.go:80` is corrected.++This also fixes the existing list. The privilege check now reports an unreadable same-named table in another schema, which `pg_dump` would have failed on anyway, and the size estimate now counts such a table, which the backup contains. A backup that used to complete keeps completing, because a table the check now reports would already have broken the lock in `pg_dump`.++- *Rejected: rewriting an unqualified entry to `*.<entry>` before passing it to `pg_dump`, so it really matches every schema as the old comment claimed.* It silently widens what existing databases exclude, so a table that is backed up today would disappear from the next backup. The current tooltip promises nothing about other schemas, and `*.name` already gives that behavior to anyone who wants it.+- *Rejected: leaving the matcher as it is and documenting the gap.* Then the new list would inherit the same disagreement between the size estimate, the privilege check and the actual dump.++### Plain `--exclude-table-data`, partitions matched by their own names++Each normalized entry of the new list becomes `--exclude-table-data=<entry>`, appended after the `--exclude-table` arguments. The same matcher in `dumpscope.go` serves both lists, so the size estimate agrees with what `pg_dump` leaves out.++- *Rejected: `--exclude-table-data-and-children` on PostgreSQL 16 and later.* Naming a partitioned table would then empty its partitions, but only on some server versions, and the size estimate would have to learn partition trees. The existing whole-table list has the same per-name behavior, so the two lists stay consistent, and the tooltip tells the user to match partitions with a pattern such as `events_*`.++### Overlap and foreign keys are handled by the tooltip++A table in both lists is excluded entirely, which is `pg_dump`'s own precedence. Saving such a database succeeds. A backed-up table with a foreign key into an emptied table makes `pg_restore` fail on that constraint. The restore runs without `--exit-on-error` (`backend/internal/features/restores/usecases/postgresql/preexistingpublicschema.go:11-13`), so it reports the ignored errors and fails.++- *Rejected: refusing to save when the lists share an entry.* Overlapping globs such as `log*` and `logs` cannot be detected reliably, so a check would catch only exact duplicates, and the outcome of an overlap is already safe and predictable.+- *Rejected: a save-time query that finds foreign keys from backed-up tables into emptied tables and warns.* It is the most helpful option, but it needs a new query, a warning channel the save endpoint does not have, and its own tests. The usual targets, log and history tables, are rarely referenced. It can be added if users hit the problem.++### Interface keys++The PostgreSQL form and page get their own label keys: `databases.fields.excludeWholeTables` ("Exclude whole tables") and `databases.fields.excludeTableData` ("Exclude table data"). The existing `databases.edit.postgresql.excludeTablesTooltip` gets the new wording, and `databases.edit.postgresql.excludeTableDataTooltip` and `databases.edit.postgresql.excludeTableDataPlaceholder` are added. `databases.fields.excludeTables`, `databases.edit.excludeTablesPlaceholder` and `databases.edit.excludeTablesTooltip` stay for MySQL and MariaDB, and the PostgreSQL form keeps using the shared placeholder "No tables excluded" for the whole-table list. All six dictionaries get the new keys; English is the source of the key set, as `frontend/AGENTS.md` requires.++- *Rejected: renaming the shared `databases.fields.excludeTables`.* MySQL and MariaDB would show "whole tables" with no second list to contrast it with.++### Form layout++The new row goes directly below the whole-table row and copies its markup: a 150px label column, a tag input with `NAME_LIST_TOKEN_SEPARATORS` and `normalizeNameList`, and an info tooltip. It is hidden in restore mode like the other backup settings. "Exclude whole tables" may wrap to two lines in the label column, which the form already tolerates.++- *Rejected: switching the PostgreSQL rows to the shared `AdvancedFieldRowComponent`.* That component stacks the label above the field on narrow screens, unlike the rows around it, so changing only these two rows would make the section inconsistent. Moving the whole form to it is a separate refactoring.++### Constraints from the module rules++- `backend/AGENTS.md`: the migration is generated with `goose create` and filled by hand. Behavior is tested through the HTTP API where it has one, with model-level tests for the size estimate and the privilege check, and the backup-to-restore test goes in `internal/features/tests/logical/postgresql/`. Test names follow `Test_WhatWeDo_WhichConditions_WhatWeExpect`.+- `frontend/AGENTS.md`: no user-facing literals, every new string goes into all six dictionaries, and English strings use a plain hyphen.+- The root `AGENTS.md`: no issue numbers in production code, and no backward-compatibility shim, which this change does not need.++## Risks / Trade-offs++- [A user empties a table that other backed-up tables reference, and the restore fails on the foreign key] → The tooltip warns about it, and the restore error names the constraint. A save-time check remains possible later.+- [A user names a partitioned table and expects its partitions to be emptied] → The tooltip says partitions need their own names or a pattern. The size estimate follows the same rule, so it does not hide the mistake by showing a smaller size.+- [A user wrote `logs` expecting it to cover every schema] → The tooltip says that a bare name reaches only the default search path and that `*.logs` covers every schema. The fixed size estimate and privilege check now show what the backup really contains instead of hiding the difference.+- [A user expects data exclusion to get past the privilege error] → The error says explicitly that data exclusion still needs SELECT and points to whole-table exclusion.+- [The longer label wraps in the 150px column] → Accepted. Shorter wording such as "Skip tables" would weaken the contrast with "Exclude table data".++## Migration Plan++One goose migration adds `exclude_table_data TEXT NOT NULL DEFAULT ''` to `postgresql_logical_databases`. Existing rows get an empty list, so their backups do not change. The down migration drops the column. A rollback to an older build ignores the column, and data-excluded tables are then backed up with their rows again.diff --git a/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/proposal.md b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/proposal.mdnew file mode 100644index 00000000..9a80a01f--- /dev/null+++ b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/proposal.md@@ -0,0 +1,47 @@+## Why++A PostgreSQL logical backup can only drop a table completely: the "Exclude tables" setting removes its structure and its rows, so the table is missing after a restore. People with large log, history or archive tables want the opposite trade. They do not need those rows in the backup, but the application expects the tables to exist, with their indexes and constraints, once the database is restored. `pg_dump` supports this with `--exclude-table-data`, and Databasus does not expose it.++Adding a second list next to "Exclude tables" only helps if the two are easy to tell apart. With the current label, "Exclude tables" and a new "Exclude table data" read almost the same. The existing field gets a label that says the whole table goes, and both fields get tooltips that say what the restored database will contain.++## What Changes++- A PostgreSQL logical database gets a second list in its backup settings, "Exclude table data". A table matched by it is backed up with its structure, indexes and constraints but without its rows, so a restore recreates it empty. Entries accept the same names and patterns as the existing list: `table`, `schema.table` and glob patterns such as `logs_*`.+- The existing setting keeps its behavior and its API field, `excludeTables`. In the PostgreSQL form and on the database page its label changes from "Exclude tables" to "Exclude whole tables", and its tooltip says that the table will be missing after a restore.+- The new field sits directly below "Exclude whole tables" in Advanced settings. Its tooltip says the table is restored empty, that a table in both lists is excluded entirely, that the partitions of a partitioned table need their own entries or a pattern, and that the restore fails to recreate a foreign key when other backed-up tables reference rows of an emptied table. The Advanced settings section opens on its own when the new list is not empty.+- The privilege check that runs when a database is saved or its connection is tested keeps requiring read access to a table whose data is excluded, because `pg_dump` still locks every table whose structure it dumps. Only whole-table exclusion lifts that requirement. The error that reports missing privileges says so, instead of suggesting "exclude tables" in general.+- The estimated database size leaves out tables whose data is excluded, as it already does for whole-excluded tables.+- The size estimate and the privilege check match table names the way `pg_dump` does, for both lists. A name without a schema part matches only tables visible on the role's default search path, usually `public`. Until now they treated it as matching every schema, so an unreadable same-named table in another schema passed the check and then failed inside `pg_dump`, and its size was left out of the estimate. What the backup contains does not change. Both tooltips explain the rule and point to `*.name` for every schema.+- Copying a database carries the new list over, as it does the existing one.+- The new label, placeholder and tooltip, the renamed label and the updated tooltip are added to all six interface dictionaries.++Nothing here is BREAKING. Existing databases get an empty "Exclude table data" list and back up exactly as before, and the API only gains an optional `excludeTableData` field.++## Capabilities++### New Capabilities++- `postgresql-logical-backup-scope`: which tables a PostgreSQL logical backup leaves out, either completely or by their rows only, what a restore of such a backup contains, which privileges the backup still requires and how the estimated size accounts for left-out tables.++### Modified Capabilities++None. `logical-backup-outcome` covers whether a logical backup is reported as succeeded or failed, which this change does not affect.++## Out of scope++- Data-only exclusion for MySQL, MariaDB and MongoDB. `mysqldump` and `mongodump` have no flag for it, so each would need its own design. Their "Exclude tables" and "Exclude collections" labels stay as they are, because there is no second field to tell them apart from.+- Physical PostgreSQL backups. They copy the whole cluster and have no table exclusion of either kind.+- A per-entry mode switch in a single list instead of two lists.+- Detecting at save time that a backed-up table has a foreign key into a table whose data is excluded. The tooltip warns about it instead.+- Rejecting a pattern that appears in both lists. `pg_dump` excludes such a table entirely, and the tooltip says so.+- Special handling of sequences owned by an emptied table. They follow `pg_dump`'s own behavior for `--exclude-table-data`.+- Regrouping Advanced settings into sub-sections such as "Backup scope".+- Restore-time selection of which tables to restore.++## Impact++- **Module rules this change answers to:** the root `AGENTS.md`, `backend/AGENTS.md` and `frontend/AGENTS.md`.+- **Database:** one migration adds `exclude_table_data TEXT NOT NULL DEFAULT ''` to `postgresql_logical_databases`.+- **Backend:** the PostgreSQL logical database model, its dump filter, the privilege check, the size estimate and their tests in `backend/internal/features/databases/databases/postgresql/logical/`; the `pg_dump` argument builder in `backend/internal/features/backups/backups/usecases/logical/postgresql/`; the database copy test in `backend/internal/features/databases/`; the backup and restore tests in `backend/internal/features/tests/logical/postgresql/`.+- **API:** create, update, read and copy of a PostgreSQL logical database carry an optional `excludeTableData` list.+- **Frontend:** the PostgreSQL logical database type, its edit form and read-only view, and the six dictionaries under `frontend/src/shared/i18n/locales/`.diff --git a/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/specs/postgresql-logical-backup-scope/spec.md b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/specs/postgresql-logical-backup-scope/spec.mdnew file mode 100644index 00000000..87ce3322--- /dev/null+++ b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/specs/postgresql-logical-backup-scope/spec.md@@ -0,0 +1,133 @@+## Purpose++Defines which tables a PostgreSQL logical backup leaves out, either completely or by their rows only, what a restore of such a backup contains, which privileges the backup still requires and how the estimated database size accounts for the tables left out.++## ADDED Requirements++### Requirement: A whole-excluded table is missing from the backup and the restore++A PostgreSQL logical database SHALL have a list of whole-excluded tables. A table matched by an entry of this list SHALL be left out of the backup together with its structure, indexes, constraints and rows, so a restore of that backup SHALL NOT create the table. An entry of the form `schema.table` SHALL match by schema and table name. An entry with no schema part SHALL match only tables that the connecting role sees by their bare name under its default search path, which is usually only `public`; a same-named table in another schema SHALL NOT be matched by it. Either part MAY be a glob pattern, such as `logs_*`, and a double-quoted part SHALL match that name literally. Tables not matched by any entry SHALL be backed up and restored with their rows.++#### Scenario: Whole-excluded table by name++- **WHEN** a database lists `audit_tmp` as a whole-excluded table, a backup completes and that backup is restored into an empty database+- **THEN** the restored database has no `public.audit_tmp` table, and every other table is restored with its rows++#### Scenario: Unqualified name leaves other schemas alone++- **WHEN** a database lists `audit_tmp` as a whole-excluded table, the role's default search path is `"$user", public`, and the tables `public.audit_tmp` and `archive.audit_tmp` exist+- **THEN** a restore of its backup has no `public.audit_tmp` table and has `archive.audit_tmp` with its rows++#### Scenario: Whole-excluded tables by pattern++- **WHEN** a database lists `public.old_*` as a whole-excluded table and has the tables `public.old_orders` and `reporting.old_orders`+- **THEN** a restore of its backup has no `public.old_orders` table and has `reporting.old_orders` with its rows++### Requirement: A table whose data is excluded is restored empty++A PostgreSQL logical database SHALL have a second list, of tables whose data is excluded. Its entries SHALL match tables by the same rules as the list of whole-excluded tables. A table matched by an entry of this list SHALL be backed up without its rows, while its structure, column defaults, indexes, constraints and triggers SHALL stay in the backup. A restore of that backup SHALL create the table with all of these and with no rows. When the database also limits the backup to certain schemas, the list SHALL apply only within those schemas. An entry SHALL match only the tables whose own names it matches: naming a partitioned table SHALL NOT exclude the rows of its partitions, which need entries or a pattern of their own. Tables matched by neither list SHALL be backed up and restored with their rows.++#### Scenario: Naming a partitioned table keeps its partitions' rows++- **WHEN** a database has the partitioned table `public.events` with the partitions `public.events_2025` and `public.events_2026`, both holding rows, and lists only `public.events` as a table whose data is excluded+- **THEN** a restore of its backup has `public.events_2025` and `public.events_2026` with their rows++#### Scenario: Partitioned table needs its partitions matched++- **WHEN** a database has the partitioned table `public.events` with the partitions `public.events_2025` and `public.events_2026`, and lists `public.events_*` as tables whose data is excluded+- **THEN** a restore of its backup has `public.events` and both partitions with no rows++#### Scenario: Data-excluded table restored empty++- **WHEN** a database lists `public.logs` as a table whose data is excluded, `public.logs` holds rows and has a primary key and a secondary index, a backup completes and that backup is restored into an empty database+- **THEN** the restored database has the `public.logs` table with the same columns, primary key and secondary index and with no rows, and every other table is restored with its rows++#### Scenario: Data-excluded tables by pattern++- **WHEN** a database lists `*.history_*` as tables whose data is excluded and has the tables `public.history_2024` and `archive.history_2025`+- **THEN** a restore of its backup has both tables with no rows++#### Scenario: Data exclusion together with included schemas++- **WHEN** a database limits the backup to the schema `public` and lists `logs` as a table whose data is excluded, and the tables `public.logs` and `reporting.logs` exist+- **THEN** a restore of its backup has `public.logs` with no rows and has no `reporting` schema++#### Scenario: Existing databases after the upgrade++- **WHEN** Databasus is upgraded and a PostgreSQL logical database created before the upgrade is backed up+- **THEN** the database has an empty list of tables whose data is excluded, and its backup contains the same tables and rows as before the upgrade++### Requirement: A table in both lists is excluded entirely++When a table is matched by an entry of the whole-excluded list and by an entry of the data-excluded list, the backup SHALL leave out the whole table, as if only the whole-excluded entry existed. Saving a database whose lists overlap SHALL succeed.++#### Scenario: Same table in both lists++- **WHEN** a database lists `public.logs` both as a whole-excluded table and as a table whose data is excluded, and its backup is restored+- **THEN** the save succeeds, and the restored database has no `public.logs` table++### Requirement: Excluding a table's data does not lift the read-privilege check++When a PostgreSQL logical database is created, updated or has its connection tested, Databasus SHALL refuse it if its connecting role cannot read every table and sequence the backup will contain. A table whose data is excluded SHALL count as a table the backup contains, because the backup still has to lock it, so the role SHALL still need read access to it. Only a whole-excluded table SHALL be skipped by this check, matched by the same rules the backup uses, so a same-named table in a schema an unqualified entry does not reach SHALL still be checked. The error returned when the check fails SHALL name the tables and schemas the role cannot read, SHALL suggest whole-table exclusion as a way to narrow the backup, and SHALL state that excluding only a table's data still requires read access to that table.++#### Scenario: Role cannot read a data-excluded table++- **WHEN** a database lists `public.logs` as a table whose data is excluded and its connecting role has no read access to `public.logs`+- **THEN** testing the connection fails with an error that names `public.logs` and says that excluding only its data still requires read access++#### Scenario: Role cannot read a same-named table in another schema++- **WHEN** a database lists `logs` as a whole-excluded table, the role's default search path is `"$user", public`, and the role cannot read `archive.logs`+- **THEN** testing the connection fails with an error that names `archive.logs`++#### Scenario: Role cannot read a whole-excluded table++- **WHEN** a database lists `public.logs` as a whole-excluded table, its connecting role has no read access to `public.logs` and can read every other table and sequence in the backup+- **THEN** testing the connection succeeds++### Requirement: The estimated database size leaves out excluded rows++The estimated size of a PostgreSQL logical database SHALL leave out the size of whole-excluded tables and the size of tables whose data is excluded, including the size of their indexes. Tables SHALL be matched by the same rules the backup uses, so a same-named table that an unqualified entry does not reach SHALL still be counted.++#### Scenario: Large data-excluded table++- **WHEN** a database lists a table whose data is excluded, and that table has rows and a secondary index+- **THEN** the estimated database size is smaller than the estimate without any exclusion by at least that table's total size, its indexes included++#### Scenario: Same-named table in another schema is still counted++- **WHEN** a database lists `logs` as a table whose data is excluded, the role's default search path is `"$user", public`, and the tables `public.logs` and `archive.logs` hold rows+- **THEN** the estimated database size leaves out `public.logs` and includes `archive.logs`++### Requirement: Both lists are saved, shown and copied++Both lists SHALL be accepted when a PostgreSQL logical database is created or updated, SHALL be returned when the database is read and SHALL be carried over when the database is copied. Each list SHALL accept entries pasted as one value separated by commas or new lines, SHALL drop surrounding whitespace and empty entries, and SHALL keep each entry once. Updating a database without the list of tables whose data is excluded SHALL clear that list, as it does for the whole-excluded list.++#### Scenario: Saving and reading the lists++- **WHEN** the user saves a database with `audit_tmp` as a whole-excluded table and with `logs, history_*` pasted as tables whose data is excluded+- **THEN** reading the database returns `audit_tmp` in the whole-excluded list and `logs` and `history_*` as two entries in the data-excluded list++#### Scenario: Copying a database++- **WHEN** the user copies a database whose data-excluded list holds `logs`+- **THEN** the copy has `logs` in its data-excluded list++### Requirement: The form tells the two exclusions apart++The backup settings of a PostgreSQL logical database SHALL show the two lists as separate fields, one directly below the other. The whole-excluded list SHALL be labeled "Exclude whole tables" and its tooltip SHALL say that the table will be missing after a restore. The data-excluded list SHALL be labeled "Exclude table data" and its tooltip SHALL say that the table is restored empty with its structure kept, that a table in both lists is excluded entirely, that the partitions of a partitioned table must be matched by their own names or a pattern, and that a restore fails to recreate a foreign key when another backed-up table references rows of an emptied table. Both tooltips SHALL describe the accepted name and pattern forms, including that a name without a schema matches only tables on the default search path, usually `public`, and that `*.name` matches it in every schema. The database page SHALL show each list under the same label when it is not empty. The section holding these fields SHALL open on its own when either list is not empty. The labels and tooltips SHALL be available in every interface language. The forms of other database engines SHALL keep their current labels.++#### Scenario: Editing a database with data-excluded tables++- **WHEN** the user opens the edit form of a PostgreSQL logical database whose data-excluded list holds `logs` and whose other advanced settings are empty+- **THEN** the advanced settings are open, and the form shows "Exclude whole tables" followed by "Exclude table data" with `logs` in it++#### Scenario: Database page++- **WHEN** the user opens the page of a PostgreSQL logical database with `audit_tmp` as a whole-excluded table and `logs` as a table whose data is excluded+- **THEN** the page shows `audit_tmp` under "Exclude whole tables" and `logs` under "Exclude table data"++#### Scenario: MySQL form keeps its label++- **WHEN** the user opens the edit form of a MySQL database+- **THEN** its table exclusion field is still labeled "Exclude tables"diff --git a/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/tasks.md b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/tasks.mdnew file mode 100644index 00000000..5bd834bb--- /dev/null+++ b/openspec/changes/archive/2026-10-18-add-postgresql-exclude-table-data/tasks.md@@ -0,0 +1,18 @@+## 1. Backend++- [x] 1.1 Create the migration stub with `goose -dir backend/migrations create add_postgresql_exclude_table_data sql` and fill it to add `exclude_table_data TEXT NOT NULL DEFAULT ''` to `postgresql_logical_databases` in Up and drop it in Down. Add `ExcludeTableData []string` (`json:"excludeTableData"`, `gorm:"-"`) and `ExcludeTableDataString` (column `exclude_table_data`) to `PostgresqlLogicalDatabase`, convert them in `BeforeSave` and `AfterFind`, assign the list in `Update` and clone it in `CopyForNewDatabase`. Extend `Test_UpdateDatabase_WhenExcludeTablesArePastedMultiline_StoresTrimmedUniqueNames` in `backend/internal/features/databases/controller_test.go` so the data-excluded list is pasted unnormalized and read back trimmed and unique, add a controller test that creates a database with a data-excluded list, reads it back with that list, copies it and finds the list on the copy, then updates the original without `excludeTableData` and reads it back with an empty list, and add `ExcludeTableData` to the filled PostgreSQL model in `backend/internal/features/databases/model_test.go` so the copy test covers it; verify with `just test-package ./internal/features/databases/...` in `backend/`+- [x] 1.2 Make the dump-scope matcher agree with `pg_dump` for unqualified entries: run `RESET search_path` before the size and privilege queries, select `pg_catalog.pg_table_is_visible(c.oid)` in both, carry it on `relationName`, require it in `isMatchingRelation` for a pattern without a schema part, and correct the comment at `dumpscope.go:80`. Add a size test in `postgresql/logical/model_test.go` where `logs` is excluded and a same-named table in a second schema is still counted, and a per-version case in `privileges_test.go` where `logs` is excluded and an unreadable same-named table in a second schema makes the connection test fail with its name; verify with `just test-package ./internal/features/databases/databases/postgresql/logical/...` in `backend/`+- [x] 1.3 Add `ExcludeTableDataPatterns` to `DumpFilter` with a normalizing getter beside `getExcludeTablePatterns`, pass `ExcludeTableData` into both `DumpFilter` literals in `model.go`, and make `getDumpedRelationsSizeBytes` skip a relation matched by either list. Add `Test_GetRawDbSizeMb_WhenExcludeTableDataSet_SkipsTableSize` beside the existing exclude-tables size tests in `postgresql/logical/model_test.go`, asserting that the estimate drops by at least the table's total relation size, indexes included; verify with `just test-package ./internal/features/databases/databases/postgresql/logical/...` in `backend/`+- [x] 1.4 Keep the privilege check skipping only whole-excluded relations, and change the missing-privileges message in `privileges.go` to suggest include schemas and whole-table exclusion and to state that excluding only a table's data still requires SELECT on it. Add a per-version case in `privileges_test.go` beside `testConnectionUnreadableTableExcluded` where the unreadable table is listed in the data-excluded list and the connection test fails with an error naming that table and mentioning the data-exclusion note; verify with `just test-package ./internal/features/databases/databases/postgresql/logical/...` in `backend/`+- [x] 1.5 Append `--exclude-table-data=<entry>` for every normalized entry of `ExcludeTableData` after the `--exclude-table` arguments in `buildPgDumpArgs`. Add a unit test in `create_backup_uc_test.go` that both lists produce their flags in that order with unnormalized input trimmed and deduplicated; verify with `just test-package ./internal/features/backups/backups/usecases/logical/postgresql/...` in `backend/`+- [x] 1.6 Add backup-to-restore subtests in `backend/internal/features/tests/logical/postgresql/backup_restore_test.go`, run for every version like `Test_BackupAndRestorePostgresql_WithExcludeTables_ExcludedTablesNotRestored`: a table listed in the data-excluded list, with rows, a primary key and a secondary index, is restored with both indexes and zero rows while a neighbour keeps its rows, and the backup's raw size leaves the emptied table out; the pattern `*.history_*` empties matching tables in two schemas, while the unqualified `logs` empties `public.logs` and leaves a same-named table in a second schema with its rows; the unqualified whole-excluded `audit_tmp` removes `public.audit_tmp` and keeps a same-named table in a second schema; a partitioned table named alone in the data-excluded list is restored with its partitions' rows, and the same partitions matched by a pattern are restored empty; with the backup limited to the `public` schema, a data-excluded `logs` leaves `public.logs` empty and the restore has no `reporting` schema; a table listed in both lists is not restored at all; verify with `just test-package ./internal/features/tests/logical/postgresql/...` in `backend/`++## 2. Frontend++- [x] 2.1 Add `excludeTableData?: string[]` to `PostgresqlLogicalDatabase.ts`. Add `databases.fields.excludeWholeTables`, `databases.fields.excludeTableData`, `databases.edit.postgresql.excludeTableDataTooltip` and `databases.edit.postgresql.excludeTableDataPlaceholder` to all six dictionaries, and rewrite `databases.edit.postgresql.excludeTablesTooltip` in all six, with the tooltip content the spec requires and each language written per the translation rules in `website/AGENTS.md`; verify with `pnpm test` (the dictionary placeholder test) and `pnpm typecheck` in `frontend/`+- [x] 2.2 In `EditPostgreSqlLogicalSpecificDataComponent.tsx`, switch the whole-table row to the `excludeWholeTables` label, add the "Exclude table data" row directly below it with the same markup, normalization and restore-mode hiding, and include `excludeTableData` in the condition that opens Advanced settings. Add `EditPostgreSqlLogicalSpecificDataComponent.test.tsx` rendering with `renderToStaticMarkup`, following `EditTelegramNotifierComponent.test.tsx`: a database whose only advanced value is a data-excluded table renders both labels in order with the entry, and a database with no advanced values renders neither. Add `EditMySqlSpecificDataComponent.test.tsx` rendering a MySQL database with an excluded table the same way and checking the label is still "Exclude tables"; verify with `pnpm test` in `frontend/`+- [x] 2.3 In `ShowPostgreSqlLogicalSpecificDataComponent.tsx`, show the whole-table list under `excludeWholeTables` and the data-excluded list under `excludeTableData` when each is not empty. Add `ShowPostgreSqlLogicalSpecificDataComponent.test.tsx` checking that each list renders under its label when set and is absent otherwise; verify with `pnpm test` in `frontend/`++## 3. Final checks++- [x] 3.1 Run `just format` and `just lint` at the repository root and `pnpm build` in `frontend/`, and fix every finding in the touched files; verify all three exit cleanlydiff --git a/openspec/specs/postgresql-logical-backup-scope/spec.md b/openspec/specs/postgresql-logical-backup-scope/spec.mdnew file mode 100644index 00000000..e651427f--- /dev/null+++ b/openspec/specs/postgresql-logical-backup-scope/spec.md@@ -0,0 +1,134 @@+# postgresql-logical-backup-scope Specification++## Purpose+Defines which tables a PostgreSQL logical backup leaves out, either completely or by their rows only, what a restore of such a backup contains, which privileges the backup still requires and how the estimated database size accounts for the tables left out.++## Requirements++### Requirement: A whole-excluded table is missing from the backup and the restore++A PostgreSQL logical database SHALL have a list of whole-excluded tables. A table matched by an entry of this list SHALL be left out of the backup together with its structure, indexes, constraints and rows, so a restore of that backup SHALL NOT create the table. An entry of the form `schema.table` SHALL match by schema and table name. An entry with no schema part SHALL match only tables that the connecting role sees by their bare name under its default search path, which is usually only `public`; a same-named table in another schema SHALL NOT be matched by it. Either part MAY be a glob pattern, such as `logs_*`, and a double-quoted part SHALL match that name literally. Tables not matched by any entry SHALL be backed up and restored with their rows.++#### Scenario: Whole-excluded table by name++- **WHEN** a database lists `audit_tmp` as a whole-excluded table, a backup completes and that backup is restored into an empty database+- **THEN** the restored database has no `public.audit_tmp` table, and every other table is restored with its rows++#### Scenario: Unqualified name leaves other schemas alone++- **WHEN** a database lists `audit_tmp` as a whole-excluded table, the role's default search path is `"$user", public`, and the tables `public.audit_tmp` and `archive.audit_tmp` exist+- **THEN** a restore of its backup has no `public.audit_tmp` table and has `archive.audit_tmp` with its rows++#### Scenario: Whole-excluded tables by pattern++- **WHEN** a database lists `public.old_*` as a whole-excluded table and has the tables `public.old_orders` and `reporting.old_orders`+- **THEN** a restore of its backup has no `public.old_orders` table and has `reporting.old_orders` with its rows++### Requirement: A table whose data is excluded is restored empty++A PostgreSQL logical database SHALL have a second list, of tables whose data is excluded. Its entries SHALL match tables by the same rules as the list of whole-excluded tables. A table matched by an entry of this list SHALL be backed up without its rows, while its structure, column defaults, indexes, constraints and triggers SHALL stay in the backup. A restore of that backup SHALL create the table with all of these and with no rows. When the database also limits the backup to certain schemas, the list SHALL apply only within those schemas. An entry SHALL match only the tables whose own names it matches: naming a partitioned table SHALL NOT exclude the rows of its partitions, which need entries or a pattern of their own. Tables matched by neither list SHALL be backed up and restored with their rows.++#### Scenario: Naming a partitioned table keeps its partitions' rows++- **WHEN** a database has the partitioned table `public.events` with the partitions `public.events_2025` and `public.events_2026`, both holding rows, and lists only `public.events` as a table whose data is excluded+- **THEN** a restore of its backup has `public.events_2025` and `public.events_2026` with their rows++#### Scenario: Partitioned table needs its partitions matched++- **WHEN** a database has the partitioned table `public.events` with the partitions `public.events_2025` and `public.events_2026`, and lists `public.events_*` as tables whose data is excluded+- **THEN** a restore of its backup has `public.events` and both partitions with no rows++#### Scenario: Data-excluded table restored empty++- **WHEN** a database lists `public.logs` as a table whose data is excluded, `public.logs` holds rows and has a primary key and a secondary index, a backup completes and that backup is restored into an empty database+- **THEN** the restored database has the `public.logs` table with the same columns, primary key and secondary index and with no rows, and every other table is restored with its rows++#### Scenario: Data-excluded tables by pattern++- **WHEN** a database lists `*.history_*` as tables whose data is excluded and has the tables `public.history_2024` and `archive.history_2025`+- **THEN** a restore of its backup has both tables with no rows++#### Scenario: Data exclusion together with included schemas++- **WHEN** a database limits the backup to the schema `public` and lists `logs` as a table whose data is excluded, and the tables `public.logs` and `reporting.logs` exist+- **THEN** a restore of its backup has `public.logs` with no rows and has no `reporting` schema++#### Scenario: Existing databases after the upgrade++- **WHEN** Databasus is upgraded and a PostgreSQL logical database created before the upgrade is backed up+- **THEN** the database has an empty list of tables whose data is excluded, and its backup contains the same tables and rows as before the upgrade++### Requirement: A table in both lists is excluded entirely++When a table is matched by an entry of the whole-excluded list and by an entry of the data-excluded list, the backup SHALL leave out the whole table, as if only the whole-excluded entry existed. Saving a database whose lists overlap SHALL succeed.++#### Scenario: Same table in both lists++- **WHEN** a database lists `public.logs` both as a whole-excluded table and as a table whose data is excluded, and its backup is restored+- **THEN** the save succeeds, and the restored database has no `public.logs` table++### Requirement: Excluding a table's data does not lift the read-privilege check++When a PostgreSQL logical database is created, updated or has its connection tested, Databasus SHALL refuse it if its connecting role cannot read every table and sequence the backup will contain. A table whose data is excluded SHALL count as a table the backup contains, because the backup still has to lock it, so the role SHALL still need read access to it. Only a whole-excluded table SHALL be skipped by this check, matched by the same rules the backup uses, so a same-named table in a schema an unqualified entry does not reach SHALL still be checked. The error returned when the check fails SHALL name the tables and schemas the role cannot read, SHALL suggest whole-table exclusion as a way to narrow the backup, and SHALL state that excluding only a table's data still requires read access to that table.++#### Scenario: Role cannot read a data-excluded table++- **WHEN** a database lists `public.logs` as a table whose data is excluded and its connecting role has no read access to `public.logs`+- **THEN** testing the connection fails with an error that names `public.logs` and says that excluding only its data still requires read access++#### Scenario: Role cannot read a same-named table in another schema++- **WHEN** a database lists `logs` as a whole-excluded table, the role's default search path is `"$user", public`, and the role cannot read `archive.logs`+- **THEN** testing the connection fails with an error that names `archive.logs`++#### Scenario: Role cannot read a whole-excluded table++- **WHEN** a database lists `public.logs` as a whole-excluded table, its connecting role has no read access to `public.logs` and can read every other table and sequence in the backup+- **THEN** testing the connection succeeds++### Requirement: The estimated database size leaves out excluded rows++The estimated size of a PostgreSQL logical database SHALL leave out the size of whole-excluded tables and the size of tables whose data is excluded, including the size of their indexes. Tables SHALL be matched by the same rules the backup uses, so a same-named table that an unqualified entry does not reach SHALL still be counted.++#### Scenario: Large data-excluded table++- **WHEN** a database lists a table whose data is excluded, and that table has rows and a secondary index+- **THEN** the estimated database size is smaller than the estimate without any exclusion by at least that table's total size, its indexes included++#### Scenario: Same-named table in another schema is still counted++- **WHEN** a database lists `logs` as a table whose data is excluded, the role's default search path is `"$user", public`, and the tables `public.logs` and `archive.logs` hold rows+- **THEN** the estimated database size leaves out `public.logs` and includes `archive.logs`++### Requirement: Both lists are saved, shown and copied++Both lists SHALL be accepted when a PostgreSQL logical database is created or updated, SHALL be returned when the database is read and SHALL be carried over when the database is copied. Each list SHALL accept entries pasted as one value separated by commas or new lines, SHALL drop surrounding whitespace and empty entries, and SHALL keep each entry once. Updating a database without the list of tables whose data is excluded SHALL clear that list, as it does for the whole-excluded list.++#### Scenario: Saving and reading the lists++- **WHEN** the user saves a database with `audit_tmp` as a whole-excluded table and with `logs, history_*` pasted as tables whose data is excluded+- **THEN** reading the database returns `audit_tmp` in the whole-excluded list and `logs` and `history_*` as two entries in the data-excluded list++#### Scenario: Copying a database++- **WHEN** the user copies a database whose data-excluded list holds `logs`+- **THEN** the copy has `logs` in its data-excluded list++### Requirement: The form tells the two exclusions apart++The backup settings of a PostgreSQL logical database SHALL show the two lists as separate fields, one directly below the other. The whole-excluded list SHALL be labeled "Exclude whole tables" and its tooltip SHALL say that the table will be missing after a restore. The data-excluded list SHALL be labeled "Exclude table data" and its tooltip SHALL say that the table is restored empty with its structure kept, that a table in both lists is excluded entirely, that the partitions of a partitioned table must be matched by their own names or a pattern, and that a restore fails to recreate a foreign key when another backed-up table references rows of an emptied table. Both tooltips SHALL describe the accepted name and pattern forms, including that a name without a schema matches only tables on the default search path, usually `public`, and that `*.name` matches it in every schema. The database page SHALL show each list under the same label when it is not empty. The section holding these fields SHALL open on its own when either list is not empty. The labels and tooltips SHALL be available in every interface language. The forms of other database engines SHALL keep their current labels.++#### Scenario: Editing a database with data-excluded tables++- **WHEN** the user opens the edit form of a PostgreSQL logical database whose data-excluded list holds `logs` and whose other advanced settings are empty+- **THEN** the advanced settings are open, and the form shows "Exclude whole tables" followed by "Exclude table data" with `logs` in it++#### Scenario: Database page++- **WHEN** the user opens the page of a PostgreSQL logical database with `audit_tmp` as a whole-excluded table and `logs` as a table whose data is excluded+- **THEN** the page shows `audit_tmp` under "Exclude whole tables" and `logs` under "Exclude table data"++#### Scenario: MySQL form keeps its label++- **WHEN** the user opens the edit form of a MySQL database+- **THEN** its table exclusion field is still labeled "Exclude tables"