{"record":{"id":"3ec7cb1466851436","repo":"prestodb/presto","slug":"materialized-view-stale","errorCode":"MATERIALIZED_VIEW_STALE","errorMessage":"Materialized view '%s' is stale","messagePattern":"Materialized view '(.+?)' is stale","errorType":"error_code","errorClass":"PrestoException","httpStatus":null,"severity":"error","filePath":"presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/materializedview/MaterializedViewRewrite.java","lineNumber":277,"sourceCode":"            Duration stalenessWindow)\n    {\n        if (status.isFullyMaterialized()) {\n            return false;\n        }\n\n        // If within staleness window, just return stale data - don't do stitching\n        if (isWithinStalenessWindow(status, stalenessWindow)) {\n            return false;\n        }\n\n        return staleReadBehavior == MaterializedViewStaleReadBehavior.USE_STITCHING;\n    }\n\n    private boolean shouldUseDataTableWhenStale(MaterializedViewStaleReadBehavior behavior, QualifiedObjectName viewName)\n    {\n        switch (behavior) {\n            case FAIL:\n                throw new PrestoException(\n                        MATERIALIZED_VIEW_STALE,\n                        String.format(\"Materialized view '%s' is stale\", viewName));\n            case USE_VIEW_QUERY:\n                return false;\n            case USE_STITCHING:\n                return true;\n            default:\n                throw new IllegalStateException(\"Unexpected stale read behavior: \" + behavior);\n        }\n    }\n\n    private boolean canUseDataTableWithSecurityChecks(\n            MaterializedViewScanNode node,\n            MetadataResolver metadataResolver,\n            Session session,\n            MaterializedViewDefinition definition,\n            Context context)\n    {","sourceCodeStart":259,"sourceCodeEnd":295,"githubUrl":"https://github.com/prestodb/presto/blob/55bb57d202de3b926896fa966c2c4a44c779634e/presto-main-base/src/main/java/com/facebook/presto/sql/planner/iterative/rule/materializedview/MaterializedViewRewrite.java#L259-L295","documentation":"When rewriting a query to use a materialized view, Presto checks base-table timestamps to decide whether the view is stale. If the view is stale and the view's staleness behavior is FAIL, planning throws MATERIALIZED_VIEW_STALE, forcing the user to refresh or change the policy.","triggerScenarios":"Querying a materialized view whose base tables have been modified after the last refresh, with the materialized-view staleness policy set to FAIL for that view.","commonSituations":"Base tables updated by streaming/ETL jobs without a subsequent view refresh; staleness policy configured as FAIL in the view definition; scheduling gap in the refresh job.","solutions":["Refresh the materialized view: CALL system.sync_materialized_view('catalog.schema.view') or run its refresh query","Change the refresh type / staleness policy (e.g. allow STALE or use stitching) in the view definition","Adjust the ETL schedule so the view refresh runs after base-table updates"],"exampleFix":"// before\nSELECT * FROM mv_daily_revenue; -- stale, FAIL policy\n// after\nCALL system.sync_materialized_view('hive.sales.mv_daily_revenue');\nSELECT * FROM mv_daily_revenue;","handlingStrategy":"try-catch","validationCode":"// compare base table modified time to view freshness before querying\nSELECT max(modification_time) FROM system.metadata.table_comments ...;","typeGuard":"null","tryCatchPattern":"try {\n    return query(\"SELECT * FROM mv\");\n} catch (PrestoException e) {\n    if (e.getErrorCode() == MATERIALIZED_VIEW_STALE.toErrorCode()) {\n        call(\"CALL system.sync_materialized_view('catalog.schema.mv')\");\n        return retryQuery(\"SELECT * FROM mv\");\n    }\n    throw e;\n}","preventionTips":["Schedule sync_materialized_view runs after every base-table ETL job","Prefer USE_STITCHING or STALE-tolerant policies for latency-sensitive reads","Monitor staleness lag with scheduled freshness checks"],"tags":["materialized-view","staleness","refresh"],"backgroundTag":"materialized-view-stale","analyzedSha":"55bb57d202de3b926896fa966c2c4a44c779634e","analyzedAt":"2026-09-04T12:50:26.162Z","contentChangedAt":"2026-09-04T12:50:26.162Z","schemaVersion":2},"datasetVersion":"2026-09-11T21:17:09.523Z"}