JuliusBrussee/caveman · error

postgres: inspect tenant foreign keys

Error message

postgres: inspect tenant foreign keys: %w

What it means

After enumerating tenant tables, ValidateTenantSchema queries catalog metadata for foreign keys (inspectTenantForeignKeys) to verify cross-tenant isolation. This error wraps a failure executing that FK introspection query inside the same transaction. It is a query-execution failure, not an isolation verdict.

Solutions

  1. Grant the app role read access to information_schema constraint views (table_constraints, key_column_usage, referential_constraints, constraint_column_usage)
  2. Inspect the wrapped error: increase context timeout if it was a deadline, or fix connectivity if a network error
  3. Confirm Postgres version compatibility for the catalog queries used
  4. Re-run after transient failure
Defensive patterns

Strategy: try-catch

Validate before calling

var n int
if err := db.QueryRow("SELECT count(*) FROM information_schema.table_constraints").Scan(&n); err != nil { /* role lacks catalog access */ }

Try / catch

if err := NewRuntimePool(ctx, cfg); err != nil {
    if strings.Contains(err.Error(), "inspect tenant foreign keys") {
        // check role privileges on constraint catalog views before retrying
    }
    return err
}

Prevention

When it happens

Trigger: NewRuntimePool triggers ValidateTenantSchema; the catalog query inside inspectTenantForeignKeys returns an error — context canceled, insufficient privileges on constraint catalog views, or a connection/driver error.

Common situations: Restricted DB role without access to information_schema.table_constraints / referential_constraints; slow startup context deadline hit between the tables and foreign-keys queries; network interruption mid-inspection.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/0515560f81d168ac. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/postgresconfig/postgresconfig.go:242

// FORCE RLS, and every foreign-key edge between tenant tables must carry the
// shared organization scope (plus project scope when both tables have it).
func ValidateTenantSchema(ctx context.Context, pool *pgxpool.Pool) error {
	tx, err := pool.BeginTx(ctx, pgx.TxOptions{
		IsoLevel:   pgx.RepeatableRead,
		AccessMode: pgx.ReadOnly,
	})
	if err != nil {
		return fmt.Errorf("postgres: begin tenant schema inspection: %w", err)
	}
	defer tx.Rollback(ctx)

	tables, err := inspectTenantTables(ctx, tx)
	if err != nil {
		return fmt.Errorf("postgres: inspect tenant tables: %w", err)
	}
	foreignKeys, err := inspectTenantForeignKeys(ctx, tx)
	if err != nil {
		return fmt.Errorf("postgres: inspect tenant foreign keys: %w", err)
	}
	if violations := tenantSchemaViolations(tables, foreignKeys); len(violations) > 0 {
		return fmt.Errorf("postgres: tenant schema isolation incomplete: %s", strings.Join(violations, "; "))
	}
	if err := validateResolverSchema(ctx, tx); err != nil {
		return err
	}
	if err := tx.Commit(ctx); err != nil {
		return fmt.Errorf("postgres: finish tenant schema inspection: %w", err)
	}
	return nil
}

func inspectTenantTables(ctx context.Context, queryer catalogQuerier) ([]tenantTableSchema, error) {
	rows, err := queryer.Query(ctx, `
		SELECT c.relname,
		       NOT organization.attnotnull,
		       c.relrowsecurity,

View on GitHub (pinned to 3ee70a1026)