By default, views created in a database cannot reference objects in a different database. To enable cross-database references for views, set the
sql.cross_db_views.enabled to true.security_invoker checks privileges using the querying user’s privileges instead.
Why use views?
There are various reasons to use views, including:Hide query complexity
When you have a complex query that, for example, joins several tables, or performs complex calculations, you can store the query as a view and then select from the view as you would from a standard table.Example
Let’s say you’re using our , which contains two tables,episodes and quotes.
episodes.id column and the quotes.episode column. To count the number of famous quotes per season, you could run the following join:
SELECTing from the view:
Limit access to underlying data
When you do not want to grant a user access to all the data in one or more standard tables, you can create a view that contains only the columns and/or rows that the user should have access to and then grant the user permissions on the view. This pattern uses the default view behavior: CockroachDB checks access to the underlying tables using the view owner’s privileges.Example
Let’s say you have abank database containing an accounts table:
bob, to be able to see the types of accounts each user has without seeing the balance in each account, so you create a view to expose just the type and email columns:
bob does not have privileges on the underlying accounts table:
bob privileges on the user_accounts view:
bob will get a permissions error when trying to access the underlying accounts table but will be allowed to query the user_accounts view:
Use invoker privileges for underlying data
If you want a view to enforce privileges on the underlying tables for the querying user, create or alter the view withsecurity_invoker. This also makes the view apply policies as the querying user instead of the view owner.
How views work
Creating views
To create a view, use the statement:Any is valid as operand to
CREATE VIEW, not just .Listing views
Once created, views are listed alongside regular tables in the database:views table in the :
Query a view
To query a view, target it with a , for example using a , just as you would with a stored table:SELECTing a view executes the view’s stored SELECT statement, which returns the relevant data from the underlying table(s). To inspect the SELECT statement executed by the view, use the statement:
SELECT statement executed by a view by querying the views table in the :
View dependencies
A view depends on the objects targeted by its underlying query. Attempting to referenced in a view’s stored query therefore results in an error:num column of the episodes table, you can rename it to number:
title column of the episodes table, you can drop it. Note that to drop a column with data in it, you must first set sql_safe_updates = false.
CASCADE keyword to drop all dependent objects as well:
CASCADE drops all dependent objects without listing them, which can lead to inadvertent and difficult-to-recover losses. To avoid potential harm, we recommend dropping objects individually in most cases.Renaming views
To rename a view, use the statement:Replace a view
To replace a view, use :Remove a view
To remove a view, use the statement:Materialized views
CockroachDB supports materialized views. A materialized view is a view that stores the results of its underlying query. When you from a materialized view, the stored query data that is returned might be out-of-date. This contrasts with a standard (i.e., “dematerialized”) view, which runs its underlying query every time it is used, returning the latest results. In order to get the latest results from a materialized view, you must , and then select from it. Because materialized views store query results, they offer better performance than standard views, at the expense of the additional storage required to store query results and the guarantee that the results are up-to-date.Usage
Materialized views and standard views share similar syntax for , , , and . To create a materialized view, use a statement. For example, suppose that you have the loaded to a CockroachDB cluster, and populated with some workload values:balance values of the bank table:
SELECT statement against the table:
overdrawn_accounts returns stored results, which are outdated:
Add an index to a materialized view
To speed up queries on materialized views, you can add an to the view.-
Create a materialized view of the rides table where the revenue is less than $20.00:
-
To see the plan for a select on this view, run:
Notice that there are no statistics collected on the view.
-
Create an index on the
citycolumn: -
To see the change in the plan after adding the index, run:
Temporary views
CockroachDB supports session-scoped temporary views. Unlike persistent views, temporary views can only be accessed from the session in which they were created, and they are dropped at the end of the session. You can create temporary views on both persistent tables and .This feature is in and subject to change. To share feedback and/or issues, contact Support.
Temporary tables must be enabled in order to use temporary views. By default, temporary tables are disabled in CockroachDB. To enable temporary tables, set the
experimental_enable_temp_tables to on.Details
- Temporary views are automatically dropped at the end of the session.
- A temporary view can only be accessed from the session in which it was created.
- Temporary views persist across transactions in the same session.
- Temporary views cannot be converted to persistent views.
- Temporary views can be created on both persistent tables and .
- When you create a view on a temporary table, the view automatically becomes temporary.
Like , temporary views are not in the
public schema. Instead, when you create the first temporary table, view, or sequence for a session, CockroachDB generates a single temporary schema (pg_temp_<id) for all of the temporary objects in the current session for a database.Usage
To create a temporary view, add to a statement. For example:Known limitations
- Casting subqueries to (including ENUMs) in views is not supported.
- Statements within views do not currently respect hint injections. The workaround is to modify the inline hints directly in the body by replacing the view.
- The optimizer may not select the most optimal query plan when querying materialized views because CockroachDB does not on materialized views..
-
CockroachDB cannot refresh materialized views inside . Trying to refresh a materialized view inside an explicit transaction will result in an error.
-
Start with the sample
bankdata set: - Create the materialized view described in .
-
Start a new multi-statement transaction with :
-
Inside the open transaction, attempt to . This will result in an error.
-
Start with the sample

