diff --git a/internal/platform/postgres/catalog_routing.go b/internal/platform/postgres/catalog_routing.go index 8232b2a..e1ce0a9 100644 --- a/internal/platform/postgres/catalog_routing.go +++ b/internal/platform/postgres/catalog_routing.go @@ -99,7 +99,9 @@ FOR UPDATE OF node`, request.ConnectionID, nodeIDs) } } - rows, err := tx.Query(ctx, `WITH RECURSIVE selected AS ( + rows, err := tx.Query(ctx, `WITH RECURSIVE filter_ctx AS MATERIALIZED ( + SELECT * FROM catalog_filter_context($1) +), selected AS ( SELECT node.snapshot_id,node.node_id,node.node_type FROM source_catalog_heads head JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id @@ -116,8 +118,11 @@ SELECT DISTINCT node.node_id,node.name,node.relative_path,node.size_bytes,node.m node.source_locator_hash,(node.node_id=ANY($2::text[])) AS directly_selected FROM descendants JOIN source_catalog_nodes node ON node.snapshot_id=descendants.snapshot_id AND node.node_id=descendants.node_id +CROSS JOIN filter_ctx ctx WHERE node.node_type='file' - AND NOT filename_is_filtered($1,node.relative_path,node.name) + AND (node.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,node.relative_path,node.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR node.ext = ANY(ctx.src_allow)) AND NOT node.ext = ANY(ctx.src_excl))) ORDER BY node.node_id`, request.ConnectionID, nodeIDs) if err != nil { return governance.CatalogRouteResult{}, fmt.Errorf("select catalog route files: %w", err) diff --git a/internal/platform/postgres/migrations/000037_fast_filename_filter.down.sql b/internal/platform/postgres/migrations/000037_fast_filename_filter.down.sql new file mode 100644 index 0000000..dbd697f --- /dev/null +++ b/internal/platform/postgres/migrations/000037_fast_filename_filter.down.sql @@ -0,0 +1,34 @@ +-- Restore the 000036 (WITH-based) evaluator. +CREATE OR REPLACE FUNCTION filename_is_filtered( + p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT +) RETURNS BOOLEAN AS $$ + WITH nearest_allow AS ( + SELECT rule.excluded_extensions AS extensions + FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'allow' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + ORDER BY LENGTH(rule.directory_path) DESC + LIMIT 1 + ) + SELECT CASE + WHEN filename_extension(p_name) = '' THEN FALSE + WHEN EXISTS ( + SELECT 1 + FROM filename_filter_rules rule + CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'exclude' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + AND extension = filename_extension(p_name) + ) THEN TRUE + WHEN EXISTS (SELECT 1 FROM nearest_allow) + THEN NOT EXISTS (SELECT 1 FROM nearest_allow + WHERE filename_extension(p_name) = ANY(extensions)) + ELSE FALSE + END +$$ LANGUAGE SQL STABLE PARALLEL SAFE; + +DROP INDEX IF EXISTS idx_source_catalog_nodes_snapshot_parent; diff --git a/internal/platform/postgres/migrations/000037_fast_filename_filter.up.sql b/internal/platform/postgres/migrations/000037_fast_filename_filter.up.sql new file mode 100644 index 0000000..2619e07 --- /dev/null +++ b/internal/platform/postgres/migrations/000037_fast_filename_filter.up.sql @@ -0,0 +1,42 @@ +-- Faster evaluator: no WITH blocks (scalar subqueries inline better and keep a +-- single prepared plan per call). Semantics identical to 000036: +-- extensionless names pass; exclude rules block; the nearest allow rule, when +-- one matches, wins over everything not in it. +CREATE OR REPLACE FUNCTION filename_is_filtered( + p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT +) RETURNS BOOLEAN AS $$ + SELECT CASE + WHEN filename_extension(p_name) = '' THEN FALSE + WHEN EXISTS ( + SELECT 1 + FROM filename_filter_rules rule + CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'exclude' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + AND extension = filename_extension(p_name) + ) THEN TRUE + WHEN EXISTS ( + SELECT 1 FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'allow' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + ) THEN NOT (filename_extension(p_name) = ANY(COALESCE(( + SELECT rule.excluded_extensions + FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'allow' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + ORDER BY LENGTH(rule.directory_path) DESC + LIMIT 1 + ), '{}'::TEXT[]))) + ELSE FALSE + END +$$ LANGUAGE SQL STABLE PARALLEL SAFE; + +-- The subtree walk needs (snapshot, parent); make sure it is indexed. +CREATE INDEX IF NOT EXISTS idx_source_catalog_nodes_snapshot_parent + ON source_catalog_nodes(snapshot_id, parent_node_id); diff --git a/internal/platform/postgres/migrations/000038_catalog_node_ext.down.sql b/internal/platform/postgres/migrations/000038_catalog_node_ext.down.sql new file mode 100644 index 0000000..8821d5c --- /dev/null +++ b/internal/platform/postgres/migrations/000038_catalog_node_ext.down.sql @@ -0,0 +1 @@ +ALTER TABLE source_catalog_nodes DROP COLUMN IF EXISTS ext; diff --git a/internal/platform/postgres/migrations/000038_catalog_node_ext.up.sql b/internal/platform/postgres/migrations/000038_catalog_node_ext.up.sql new file mode 100644 index 0000000..e8af68f --- /dev/null +++ b/internal/platform/postgres/migrations/000038_catalog_node_ext.up.sql @@ -0,0 +1,4 @@ +-- Catalog reads must not run the rule-table probe + regex per row. +-- Precompute the parsed extension on every node row (generated, immutable). +ALTER TABLE source_catalog_nodes + ADD COLUMN IF NOT EXISTS ext TEXT GENERATED ALWAYS AS (filename_extension(name)) STORED; diff --git a/internal/platform/postgres/migrations/000039_catalog_filter_context.down.sql b/internal/platform/postgres/migrations/000039_catalog_filter_context.down.sql new file mode 100644 index 0000000..023d51b --- /dev/null +++ b/internal/platform/postgres/migrations/000039_catalog_filter_context.down.sql @@ -0,0 +1,35 @@ +DROP FUNCTION IF EXISTS catalog_filter_context(VARCHAR); +CREATE OR REPLACE FUNCTION filename_is_filtered( + p_connection_id VARCHAR(36), p_relative_path TEXT, p_name TEXT +) RETURNS BOOLEAN AS $$ + SELECT CASE + WHEN filename_extension(p_name) = '' THEN FALSE + WHEN EXISTS ( + SELECT 1 + FROM filename_filter_rules rule + CROSS JOIN LATERAL UNNEST(rule.excluded_extensions) extension + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'exclude' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + AND extension = filename_extension(p_name) + ) THEN TRUE + WHEN EXISTS ( + SELECT 1 FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'allow' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + ) THEN NOT (filename_extension(p_name) = ANY(COALESCE(( + SELECT rule.excluded_extensions + FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id + AND rule.mode = 'allow' + AND (rule.directory_path = '' + OR p_relative_path LIKE rule.directory_path || '/%') + ORDER BY LENGTH(rule.directory_path) DESC + LIMIT 1 + ), '{}'::TEXT[]))) + ELSE FALSE + END +$$ LANGUAGE SQL STABLE PARALLEL SAFE; diff --git a/internal/platform/postgres/migrations/000039_catalog_filter_context.up.sql b/internal/platform/postgres/migrations/000039_catalog_filter_context.up.sql new file mode 100644 index 0000000..19f59a5 --- /dev/null +++ b/internal/platform/postgres/migrations/000039_catalog_filter_context.up.sql @@ -0,0 +1,18 @@ +-- Hot catalog reads resolve a connection's filter context ONCE per query and +-- evaluate n.ext against arrays; the exact row-by-row filename_is_filtered is +-- only used when directory-scoped rules exist. +CREATE OR REPLACE FUNCTION catalog_filter_context(p_connection_id VARCHAR(36)) +RETURNS TABLE(has_dir BOOLEAN, src_excl TEXT[], src_allow TEXT[]) AS $$ + SELECT coalesce(bool_or(rule.directory_path <> ''), FALSE), + coalesce((SELECT array_agg(extension) FROM ( + SELECT unnest(excluded_extensions) AS extension + FROM filename_filter_rules r2 + WHERE r2.source_connection_id = p_connection_id + AND r2.mode = 'exclude' AND r2.directory_path = '' + ) e), '{}'::text[]), + (SELECT r3.excluded_extensions FROM filename_filter_rules r3 + WHERE r3.source_connection_id = p_connection_id + AND r3.mode = 'allow' AND r3.directory_path = '' LIMIT 1) + FROM filename_filter_rules rule + WHERE rule.source_connection_id = p_connection_id +$$ LANGUAGE SQL STABLE PARALLEL SAFE; diff --git a/internal/platform/postgres/migrations_test.go b/internal/platform/postgres/migrations_test.go index 5932fcb..141d168 100644 --- a/internal/platform/postgres/migrations_test.go +++ b/internal/platform/postgres/migrations_test.go @@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) { if err != nil { t.Fatalf("loadMigrations() error = %v", err) } - if len(migrations) != 36 { - t.Fatalf("migration count = %d, want 36", len(migrations)) + if len(migrations) != 39 { + t.Fatalf("migration count = %d, want 39", len(migrations)) } if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") { t.Fatalf("migration 1 = %#v", migrations[0]) diff --git a/internal/platform/postgres/source_catalog.go b/internal/platform/postgres/source_catalog.go index 049bd63..c9a945e 100644 --- a/internal/platform/postgres/source_catalog.go +++ b/internal/platform/postgres/source_catalog.go @@ -17,8 +17,13 @@ func (s *Store) ListCatalogSources(ctx context.Context) ([]governance.CatalogSou SELECT connection.id, connection.source_type, snapshot.source_label, root.node_id, snapshot.folder_count - 1, (SELECT COUNT(*)::integer FROM source_catalog_nodes file + LEFT JOIN LATERAL catalog_filter_context(connection.id) ctx ON TRUE WHERE file.snapshot_id=snapshot.id AND file.node_type='file' - AND NOT filename_is_filtered(connection.id,file.relative_path,file.name)), + AND (file.ext='' + OR (coalesce(ctx.has_dir,FALSE) AND NOT filename_is_filtered(connection.id,file.relative_path,file.name)) + OR (NOT coalesce(ctx.has_dir,FALSE) + AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow)) + AND NOT file.ext = ANY(coalesce(ctx.src_excl,'{}'::text[]))))), head.activated_at, 'ready', COALESCE(refresh.duration_ms,0) FROM source_catalog_heads head @@ -66,18 +71,28 @@ func (s *Store) ListCatalogNodes(ctx context.Context, filter governance.CatalogN page := governance.CatalogNodePage{Items: []governance.CatalogNode{}, Limit: filter.Limit, Offset: filter.Offset} var parentID *string err := s.db.QueryRow(ctx, ` +WITH filter_ctx AS MATERIALIZED (SELECT * FROM catalog_filter_context($1)) SELECT node.node_id,node.parent_node_id,node.node_type,node.name, (SELECT COUNT(*)::integer FROM source_catalog_nodes child + LEFT JOIN filter_ctx ctx ON TRUE WHERE child.snapshot_id=node.snapshot_id AND child.parent_node_id=node.node_id - AND (child.node_type='folder' OR NOT filename_is_filtered($1,child.relative_path,child.name))), + AND (child.node_type='folder' OR child.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,child.relative_path,child.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR child.ext = ANY(ctx.src_allow)) AND NOT child.ext = ANY(ctx.src_excl)))), (SELECT COUNT(*)::integer FROM source_catalog_nodes file + LEFT JOIN filter_ctx ctx ON TRUE WHERE file.snapshot_id=node.snapshot_id AND file.node_type='file' AND (node.relative_path='' OR file.relative_path LIKE node.relative_path||'/%') - AND NOT filename_is_filtered($1,file.relative_path,file.name)), + AND (file.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,file.relative_path,file.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow)) AND NOT file.ext = ANY(ctx.src_excl)))), (SELECT COALESCE(SUM(COALESCE(file.size_bytes,0)),0)::bigint FROM source_catalog_nodes file + LEFT JOIN filter_ctx ctx ON TRUE WHERE file.snapshot_id=node.snapshot_id AND file.node_type='file' AND (node.relative_path='' OR file.relative_path LIKE node.relative_path||'/%') - AND NOT filename_is_filtered($1,file.relative_path,file.name)), + AND (file.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,file.relative_path,file.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR file.ext = ANY(ctx.src_allow)) AND NOT file.ext = ANY(ctx.src_excl)))), node.size_bytes,node.modified_at FROM source_catalog_heads head JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id @@ -125,20 +140,29 @@ SELECT node_id,name FROM trail ORDER BY depth DESC`, filter.ConnectionID, filter breadcrumbs.Close() if err := s.db.QueryRow(ctx, ` +WITH filter_ctx AS MATERIALIZED (SELECT * FROM catalog_filter_context($1)) SELECT COUNT(*)::integer FROM source_catalog_heads head JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id +LEFT JOIN filter_ctx ctx ON TRUE WHERE head.source_connection_id=$1 AND node.parent_node_id=$2 - AND (node.node_type='folder' OR NOT filename_is_filtered($1,node.relative_path,node.name))`, filter.ConnectionID, filter.ParentNodeID).Scan(&page.Total); err != nil { + AND (node.node_type='folder' OR node.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,node.relative_path,node.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR node.ext = ANY(ctx.src_allow)) AND NOT node.ext = ANY(ctx.src_excl)))`, filter.ConnectionID, filter.ParentNodeID).Scan(&page.Total); err != nil { return governance.CatalogNodePage{}, fmt.Errorf("count catalog nodes: %w", err) } rows, err := s.db.Query(ctx, ` -WITH RECURSIVE current_head AS ( +WITH RECURSIVE filter_ctx AS MATERIALIZED ( + SELECT * FROM catalog_filter_context($1) +), current_head AS ( SELECT snapshot_id FROM source_catalog_heads WHERE source_connection_id=$1 ), selected AS MATERIALIZED ( SELECT node.* FROM current_head head JOIN source_catalog_nodes node ON node.snapshot_id=head.snapshot_id + LEFT JOIN filter_ctx ctx ON TRUE WHERE node.parent_node_id=$2 - AND (node.node_type='folder' OR NOT filename_is_filtered($1,node.relative_path,node.name)) + AND (node.node_type='folder' OR node.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,node.relative_path,node.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR node.ext = ANY(ctx.src_allow)) AND NOT node.ext = ANY(ctx.src_excl))) ORDER BY CASE node.node_type WHEN 'folder' THEN 0 ELSE 1 END,node.sort_order,lower(node.name),node.node_id LIMIT $3 OFFSET $4 ), descendants(ancestor_id,snapshot_id,node_id) AS ( @@ -154,8 +178,11 @@ WITH RECURSIVE current_head AS ( current_version.content_state AS version_content_state,content.content_state AS object_content_state, content.local_spool_ref,leaf.size_bytes FROM descendants + CROSS JOIN filter_ctx ctx JOIN source_catalog_nodes leaf ON leaf.snapshot_id=descendants.snapshot_id AND leaf.node_id=descendants.node_id AND leaf.node_type='file' - AND NOT filename_is_filtered($1,leaf.relative_path,leaf.name) + AND (leaf.ext='' + OR (ctx.has_dir AND NOT filename_is_filtered($1,leaf.relative_path,leaf.name)) + OR (NOT ctx.has_dir AND (ctx.src_allow IS NULL OR leaf.ext = ANY(ctx.src_allow)) AND NOT leaf.ext = ANY(ctx.src_excl))) LEFT JOIN source_items item ON item.id=leaf.source_item_id LEFT JOIN source_item_versions current_version ON current_version.source_item_id=item.id AND current_version.revision=item.revision LEFT JOIN content_objects content ON content.id=current_version.content_object_id @@ -191,8 +218,11 @@ WITH RECURSIVE current_head AS ( SELECT selected.node_id,selected.parent_node_id,selected.node_type,selected.name, CASE WHEN selected.node_type='file' THEN 0 ELSE ( SELECT COUNT(*)::integer FROM source_catalog_nodes child + LEFT JOIN filter_ctx ctx2 ON TRUE WHERE child.snapshot_id=selected.snapshot_id AND child.parent_node_id=selected.node_id - AND (child.node_type='folder' OR NOT filename_is_filtered($1,child.relative_path,child.name)) + AND (child.node_type='folder' OR child.ext='' + OR (ctx2.has_dir AND NOT filename_is_filtered($1,child.relative_path,child.name)) + OR (NOT ctx2.has_dir AND (ctx2.src_allow IS NULL OR child.ext = ANY(ctx2.src_allow)) AND NOT child.ext = ANY(ctx2.src_excl))) ) END, COALESCE(stats.eligible_file_count,0),COALESCE(stats.eligible_bytes,0), selected.size_bytes,selected.modified_at,selected.content_sha256,COALESCE(stats.visibility,'unknown'),