> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# REFRESH

Stored query results in [materialized view](https://docs.cockroachlabs.com/docs/v24.2/views#materialized-views) are not automatically updated to reflect the latest state of the table(s) they query. The `REFRESH` [statement](https://docs.cockroachlabs.com/docs/v24.2/sql-statements) updates the stored query results of a materialized view.

> [!NOTE]
>
> CockroachDB does not support materialized views that are refreshed on [transaction commit](https://docs.cockroachlabs.com/docs/v24.2/commit-transaction).

## Required privileges

The user must be the [owner](https://docs.cockroachlabs.com/docs/v24.2/alter-view) of the materialized view or have [admin](https://docs.cockroachlabs.com/docs/v24.2/security-reference/authorization#admin-role) privileges.

## Synopsis

![refresh\_materialized\_views syntax diagram](https://mintcdn.com/cockroachlabs/O8LdfYQ-dz7oGPVh/images/sql-diagrams/v24.2/refresh_materialized_views.svg?fit=max\&auto=format\&n=O8LdfYQ-dz7oGPVh\&q=85\&s=51c2c11f5162236638d8f1bf19df6c71)

## Parameters

| Parameter | Description |
| - | - |
| `opt_concurrently` | `CONCURRENTLY` (Default behavior) This keyword has no effect. It is present for PostgreSQL compatibility. All materialized views are refreshed concurrently with other jobs. |
| `view_name` | The name of the materialized view to refresh. |
| `opt_clear_data` | `WITH DATA` (Default behavior) Refresh the stored query results. `WITH NO DATA` Drop the query results of the materialized view from storage. |

## Example

The following example uses the [sample `bank` database](https://docs.cockroachlabs.com/docs/v24.2/cockroach-workload#bank-workload), populated with some workload values.

Suppose that you create a materialized view on the `bank` table:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> CREATE MATERIALIZED VIEW overdrawn_accounts
  AS SELECT id, balance
  FROM bank
  WHERE balance < 0;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM overdrawn_accounts;
```

```
  id  | balance
------+----------
    1 |  -17643
    3 |   -5928
   13 |   -3700
...
(402 rows)
```

Now suppose that you update the `balance` values of the `bank` table:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> UPDATE bank SET balance = 0 WHERE balance < 0;
```

```
UPDATE 402
```

The changes can be seen in the table with a simple `SELECT` statement against the table:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT id, balance
FROM bank
WHERE balance < 0;
```

```
  id | balance
-----+----------
(0 rows)
```

Recall that materialized views do not automatically update their stored results. Selecting from `overdrawn_accounts` returns stored results, which are outdated:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM overdrawn_accounts;
```

```
  id  | balance
------+----------
    1 |  -17643
    3 |   -5928
   13 |   -3700
...
(402 rows)
```

To update the materialized view's results, use a [`REFRESH`](https://docs.cockroachlabs.com/docs/v24.2/refresh) statement:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> REFRESH MATERIALIZED VIEW overdrawn_accounts;
```

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
> SELECT * FROM overdrawn_accounts;
```

```
  id | balance
-----+----------
(0 rows)
```

## See also

* [Materialized views](https://docs.cockroachlabs.com/docs/v24.2/views#materialized-views)
* [`CREATE VIEW`](https://docs.cockroachlabs.com/docs/v24.2/create-view)
* [`SHOW TABLES`](https://docs.cockroachlabs.com/docs/v24.2/show-tables)
* [`ALTER VIEW`](https://docs.cockroachlabs.com/docs/v24.2/alter-view)
* [`DROP VIEW`](https://docs.cockroachlabs.com/docs/v24.2/drop-view)
