HypoPG / hypopg

Hypothetical Indexes for PostgreSQL
https://hypopg.readthedocs.io
Other
1.39k stars 59 forks source link

Support for materialized views #11

Closed ankane closed 6 years ago

ankane commented 6 years ago

Thanks again for this great project 👍

It looks like hypothetical indexes don't work with materialized views. Here's code to repro:

CREATE EXTENSION IF NOT EXISTS hypopg;
SELECT hypopg_reset();

-- create posts
DROP TABLE IF EXISTS posts CASCADE;
CREATE TABLE posts (
  id int,
  blog_id int,
  user_id int
);
INSERT INTO posts (SELECT n AS id, n % 1000 AS blog_id, n % 10 AS user_id FROM generate_series(1, 100000) n);

-- create materialized view
CREATE MATERIALIZED VIEW posts_materialized AS SELECT * FROM posts;

-- add hypothetical index and explain
-- i'd expect the hypo index to be used, but it's not
SELECT * FROM hypopg_create_index('CREATE INDEX ON posts_materialized (id)');
EXPLAIN SELECT * FROM posts_materialized WHERE id = 1;

-- add real index and explain
-- the real index is used here, as expected
CREATE INDEX ON posts_materialized (id);
EXPLAIN SELECT * FROM posts_materialized WHERE id = 1;
rjuju commented 6 years ago

Hi,

Thank you very much!

Indeed, I totally overlooked that materialized views can have indexes, thank you very much for the repro. I just pushed 89642076686ac3db135d2748e146a7b34fba5600 which should fix this, can you confirm if this is also ok for you?

ankane commented 6 years ago

Wow, thanks for the incredibly fast response and update 🕐 🔥 🔥 🔥 Works great for me!