documentation/query/sql/compile-view.md
Manually triggers recompilation of a view to validate its definition against the current database state.
This command is optional. Views are automatically compiled when queried, so
you don't need to run COMPILE VIEW for normal operation. Use it when you want
to validate a view without executing it, or to check if schema changes have
broken a view.
For more information on views, see the Views documentation.
COMPILE VIEW view_name
| Parameter | Description |
|---|---|
view_name | Name of the view to compile |
COMPILE VIEW my_view
-- Check view status
SELECT view_name, view_status, invalidation_reason
FROM views()
WHERE view_name = 'my_view';
-- If invalid, fix the underlying issue, then compile
COMPILE VIEW my_view;
-- Verify it's now valid
SELECT view_status FROM views() WHERE view_name = 'my_view';
When multiple views are broken due to schema changes:
-- Find all invalid views
SELECT view_name, invalidation_reason
FROM views()
WHERE view_status = 'invalid';
-- Compile each view after fixing underlying issues
COMPILE VIEW view1;
COMPILE VIEW view2;
COMPILE VIEW view3;
validinvalid and the reason is
stored in invalidation_reasonViews are automatically compiled in these situations:
SELECT from a view, it is compiled if neededBecause of automatic compilation, COMPILE VIEW is rarely needed for normal
operation.
Use COMPILE VIEW when you want to:
| Error | Cause |
|---|---|
view does not exist [view=name] | View with specified name doesn't exist |
table does not exist [table=name] | View references a table that doesn't exist |
Invalid column | View references a column that doesn't exist |
Access denied [COMPILE VIEW on view_name] | User lacks permission (Enterprise) |
Compiling a view requires the COMPILE VIEW permission on that view:
-- Grant COMPILE VIEW permission
GRANT COMPILE VIEW ON my_view TO username;
-- Grant on multiple views
GRANT COMPILE VIEW ON view1, view2 TO username;
Note: COMPILE VIEW does not require SELECT permission on the underlying
tables. The compilation validates the view definition using system privileges,
not user privileges.