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

# REFRESH

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

Stored query results in <InternalLink path="views#materialized-views">materialized view</InternalLink> are not automatically updated to reflect the latest state of the table(s) they query. The `REFRESH` <InternalLink path="sql-statements">statement</InternalLink> updates the stored query results of a materialized view.

<Note>
  CockroachDB does not support materialized views that are refreshed on <InternalLink path="commit-transaction">transaction commit</InternalLink>.
</Note>

## Required privileges

The user must be the <InternalLink path="alter-view">owner</InternalLink> of the materialized view or have <InternalLink path="security-reference/authorization#admin-role">admin</InternalLink> privileges.

## Synopsis

<img src="https://mintcdn.com/cockroachlabs/uBcLAizjWFXF4pfd/images/sql-diagrams/v25.1/refresh_materialized_views.svg?fit=max&auto=format&n=uBcLAizjWFXF4pfd&q=85&s=b5ba35bf2af44bf6efe124f2001a31e4" alt="refresh_materialized_views syntax diagram" style={{maxWidth: "100%", overflowX: "auto"}} width="613" height="103" data-path="images/sql-diagrams/v25.1/refresh_materialized_views.svg" />

## 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. <br />`WITH NO DATA` Drop the query results of the materialized view from storage.                          |

## Example

The following example uses the <InternalLink path="cockroach-workload#bank-workload">sample `bank` database</InternalLink>, 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 <InternalLink path="refresh">`REFRESH`</InternalLink> 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

* <InternalLink path="views#materialized-views">Materialized views</InternalLink>
* <InternalLink path="create-view">`CREATE VIEW`</InternalLink>
* <InternalLink path="show-tables">`SHOW TABLES`</InternalLink>
* <InternalLink path="alter-view">`ALTER VIEW`</InternalLink>
* <InternalLink path="drop-view">`DROP VIEW`</InternalLink>
