Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create primary key on materialized view in Postgres

How can I create a primary key on a materialized view in Postgres?

ALTER MATERIALIZED VIEW my_mat_view ADD PRIMARY KEY (id)

returns error:

Error in query: ERROR: "my_mat_view" is not a table 
like image 640
111 Avatar asked Jan 11 '19 22:01

111


People also ask

Can materialized view have primary key?

WITH PRIMARY KEY Clause Primary key materialized views allow materialized view master tables to be reorganized without affecting the eligibility of the materialized view for fast refresh. The master table must contain an enabled primary key constraint.

How do I create a primary key in PostgreSQL?

In PostgreSQL, a primary key is created using either a CREATE TABLE statement or an ALTER TABLE statement. You use the ALTER TABLE statement in PostgreSQL to add or drop a primary key.

Can we create index on materialized view in Postgres?

Creating a Materialized View When you create a materialized view in PostgreSQL, it uses a regular database table underneath. You can create database indexes on the materialized view directly and improve performance of queries that access the materialized view.


1 Answers

Materialized views cannot have primary keys. You can use a unique index instead.

create unique index on my_mat_view (id)
like image 137
klin Avatar answered Nov 10 '22 03:11

klin