From 723adb4bb90265c029326f28ab6ecec8514da5aa Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Sat, 22 Feb 2020 14:39:37 +0100 Subject: [PATCH] Warn about broken hypothetical BRIN indexes in some minor versions. Hypothetical BRIN indexes are broken in some minor versions of pg10, pg11 and pg12. Detect those versions and warn users with a useful error message recommending to update their minor versions of postgres. Also add regression tests to make sure that support BRIN hypothetical indexes doesn't get broken again. --- Makefile | 3 +++ expected/hypo_brin.out | 21 +++++++++++++++++++++ hypopg_index.c | 27 +++++++++++++++++++++++++++ test/sql/hypo_brin.sql | 18 ++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 expected/hypo_brin.out create mode 100644 test/sql/hypo_brin.sql diff --git a/Makefile b/Makefile index 95ac58e..3c80f5b 100644 --- a/Makefile +++ b/Makefile @@ -37,6 +37,9 @@ DATA = $(wildcard *--*.sql) PGXS := $(shell $(PG_CONFIG) --pgxs) include $(PGXS) +ifneq ($(MAJORVERSION),$(filter $(MAJORVERSION), 9.2 9.3 9.4)) + REGRESS += hypo_brin +endif ifeq ($(MAJORVERSION),$(filter $(MAJORVERSION),9.2 9.3 9.4 9.5 9.6)) REGRESS += hypo_no_table else diff --git a/expected/hypo_brin.out b/expected/hypo_brin.out new file mode 100644 index 0000000..a93465d --- /dev/null +++ b/expected/hypo_brin.out @@ -0,0 +1,21 @@ +-- Hypothetical BRIN index tests +CREATE TABLE hypo_brin (id integer); +INSERT INTO hypo_brin SELECT generate_series(1, 10000); +ANALYZE hypo_brin; +SELECT COUNT(*) AS nb +FROM public.hypopg_create_index('CREATE INDEX ON hypo_brin USING brin (id);'); + nb +---- + 1 +(1 row) + +-- Should use hypothetical index +SET enable_seqscan = 0; +SELECT COUNT(*) FROM do_explain('SELECT * FROM hypo_brin WHERE id = 1') e +WHERE e ~ 'Bitmap Index Scan on <\d+>brin_hypo_brin.*'; + count +------- + 1 +(1 row) + +DROP TABLE hypo_brin; diff --git a/hypopg_index.c b/hypopg_index.c index 8f26e06..595610f 100644 --- a/hypopg_index.c +++ b/hypopg_index.c @@ -339,6 +339,33 @@ hypo_index_store_parsetree(IndexStmt *node, const char *queryString) ListCell *lc; int attn; + /* + * Support for hypothetical BRIN indexes is broken in some minor versions + * of pg10, pg11 and pg12. For simplicity, check PG_VERSION_NUM rather + * than the real instance version, which should should be right most of the + * time. When it's not, the only effect is to have a less user-friendly + * error message. + */ +#if ((PG_VERSION_NUM >= 100000 && PG_VERSION_NUM < 100012) || \ + (PG_VERSION_NUM >= 110000 && PG_VERSION_NUM < 110007) || \ + (PG_VERSION_NUM >= 120000 && PG_VERSION_NUM < 120002)) + if (get_am_oid(node->accessMethod, true) == BRIN_AM_OID) + { + elog(ERROR, "hypopg: BRIN hypothetical indexes are only supported" + " with PostgreSQL " +#if PG_VERSION_NUM >= 120000 + "12.2" +#else +#if PG_VERSION_NUM >= 110000 + "11.7" +#else + "10.12" +#endif /* pg 11 */ +#endif /* pg 12 */ + " and later."); + } +#endif + #if PG_VERSION_NUM < 100000 relid = RangeVarGetRelid(node->relation, AccessShareLock, false); #else diff --git a/test/sql/hypo_brin.sql b/test/sql/hypo_brin.sql new file mode 100644 index 0000000..86d87fb --- /dev/null +++ b/test/sql/hypo_brin.sql @@ -0,0 +1,18 @@ +-- Hypothetical BRIN index tests + +CREATE TABLE hypo_brin (id integer); + +INSERT INTO hypo_brin SELECT generate_series(1, 10000); + +ANALYZE hypo_brin; + +SELECT COUNT(*) AS nb +FROM public.hypopg_create_index('CREATE INDEX ON hypo_brin USING brin (id);'); + + +-- Should use hypothetical index +SET enable_seqscan = 0; +SELECT COUNT(*) FROM do_explain('SELECT * FROM hypo_brin WHERE id = 1') e +WHERE e ~ 'Bitmap Index Scan on <\d+>brin_hypo_brin.*'; + +DROP TABLE hypo_brin;