t8y2/dbx · error

materialized view %q.%q returned an empty source definition

Error message

materialized view %q.%q returned an empty source definition

What it means

The driver fetched the source of a materialized view via pg_get_viewdef and got back sql.ErrNoRows (an empty definition). Since a materialized view must have a definition, the driver converts this into a descriptive error naming schema and view.

Source

Thrown at agents/drivers/kingbase-go/kingbase_metadata.go:2187

	}

	source, err := querySource(function)
	if err == nil {
		return source, nil
	}
	if function == "sys_get_viewdef" {
		if isUndefinedFunction(err, function) {
			s.usePgViewDefinition = true
		} else if !errors.Is(err, sql.ErrNoRows) {
			return "", err
		}
		source, err = querySource("pg_get_viewdef")
		if err == nil {
			return source, nil
		}
	}
	if errors.Is(err, sql.ErrNoRows) {
		return "", fmt.Errorf("materialized view %q.%q returned an empty source definition", schema, name)
	}
	return "", err
}

func (s *server) getTableDDL(schema, table string) (string, error) {
	effective, err := s.effectiveSchema(schema)
	if err != nil {
		return "", err
	}
	columns, err := s.getColumns(effective, table)
	if err != nil {
		return "", err
	}
	tableComment, _ := s.getTableComment(effective, table)
	ddl := s.renderTableDDL(effective, table, columns, tableComment)
	ddl, err = s.appendTableIndexDDL(effective, table, ddl)
	if err != nil {
		return "", err

View on GitHub (pinned to c0390bff16)

Solutions

  1. Verify the schema and materialized view name (check search_path / case sensitivity)
  2. Confirm the view exists: SELECT 1 FROM pg_matviews WHERE schemaname=... AND matviewname=...
  3. Check the current user has privileges on the view (or query as superuser)
  4. Run the underlying definition query manually to see why it returns no rows

Example fix

// before
src, err := server.GetMaterializedViewDDL(ctx, "pubic", "mv_sales") // typo schema
// after
src, err := server.GetMaterializedViewDDL(ctx, "public", "mv_sales")
Defensive patterns

Strategy: validation

Validate before calling

// Go: confirm the matview exists and is visible before fetching DDL
var exists bool
db.QueryRow(`SELECT EXISTS (SELECT 1 FROM pg_matviews WHERE schemaname=$1 AND matviewname=$2)`, schema, name).Scan(&exists)
if !exists {
    return fmt.Errorf("materialized view %s.%s not found", schema, name)
}

Try / catch

src, err := server.GetMaterializedViewDDL(ctx, schema, name)
if err != nil {
    if strings.Contains(err.Error(), "empty source definition") {
        return fmt.Errorf("check name/privileges for matview %s.%s: %w", schema, name, err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling getMaterializedViewDDL/getMaterializedViewSource for a matview whose pg_get_viewdef query returns no row — typically because the view does not actually exist in the effective schema, or the definition is empty/unreadable by the current user.

Common situations: Typo in schema or view name combined with a non-strict search_path; insufficient privileges to see the view definition; querying a view that was dropped concurrently; cross-database references the def-query cannot resolve.

Related errors


AI-assisted analysis of t8y2/dbx@c0390bff16 (2026-09-05). Data as JSON: /api/errors/b8d5aa6bc29c9e4d. Report an issue: GitHub.