postgresql/src/test/modules
Tomas Vondra fc7dc728d1 Fix handling of empty ranges and NULLs in BRIN
BRIN indexes did not properly distinguish between summaries for empty
(no rows) and all-NULL ranges, treating them as essentially the same
thing. Summaries were initialized with allnulls=true, and opclasses
simply reset allnulls to false when processing the first non-NULL value.
This however produces incorrect results if the range starts with a NULL
value (or a sequence of NULL values), in which case we forget the range
contains NULL values when adding the first non-NULL value.

This happens because the allnulls flag is used for two separate
purposes - to mark empty ranges (not representing any rows yet) and
ranges containing only NULL values.

Opclasses don't know which of these cases it is, and so don't know
whether to set hasnulls=true. Setting the flag in both cases would make
it correct, but it would also make BRIN indexes useless for queries with
IS NULL clauses. All ranges start empty (and thus allnulls=true), so all
ranges would end up with either allnulls=true or hasnulls=true.

The severity of the issue is somewhat reduced by the fact that it only
happens when adding values to an existing summary with allnulls=true.
This can happen e.g. for small tables (because a summary for the first
range exists for all BRIN indexes), or for tables with large fraction of
NULL values in the indexed columns.

Bulk summarization (e.g. during CREATE INDEX or automatic summarization)
that processes all values at once is not affected by this issue. In this
case the flags were updated in a slightly different way, not forgetting
the NULL values.

To identify empty ranges we use a new flag, stored in an unused bit in
the BRIN tuple header so the on-disk format remains the same. A matching
flag is added to BrinMemTuple, into a 3B gap after bt_placeholder.
That means there's no risk of ABI breakage, although we don't actually
pass the BrinMemTuple to any public API.

We could also skip storing index tuples for empty summaries, but then
we'd have to always process such ranges - even if there are no rows in
large parts of the table (e.g. after a bulk DELETE), it would still
require reading the pages etc. So we store them, but ignore them when
building the bitmap.

Backpatch to 11. The issue exists since BRIN indexes were introduced in
9.5, but older releases are already EOL.

Backpatch-through: 11
Reviewed-by: Justin Pryzby, Matthias van de Meent, Alvaro Herrera
Discussion: https://postgr.es/m/402430e4-7d9d-6cf1-09ef-464d80afff3b@enterprisedb.com
2023-05-19 00:21:05 +02:00
..
brin Fix handling of empty ranges and NULLs in BRIN 2023-05-19 00:21:05 +02:00
commit_ts Rework activation of commit timestamps during recovery 2018-09-26 10:29:20 +09:00
dummy_seclabel doc: Fix naming of SELinux 2020-01-10 09:37:21 +09:00
snapshot_too_old Reduce test runtime of src/test/modules/snapshot_too_old. 2022-08-03 11:14:55 -04:00
test_bloomfilter Post-feature-freeze pgindent run. 2018-04-26 14:47:16 -04:00
test_ddl_deparse Fix DDL deparse of CREATE OPERATOR CLASS 2022-05-20 18:52:55 +02:00
test_extensions Stabilize output of new regression test. 2022-08-08 12:16:01 -04:00
test_parser Update copyright for 2018 2018-01-02 23:30:12 -05:00
test_pg_dump Fix pg_dump for GRANT OPTION among initial privileges. 2021-01-16 12:21:39 -08:00
test_predtest Improve predtest.c's handling of cases with NULL-constant inputs. 2018-03-21 18:30:46 -04:00
test_rbtree Rename rbtree.c functions to use "rbt" prefix not "rb" prefix. 2018-11-06 13:25:24 -05:00
test_rls_hooks Fix test_rls_hooks to assign expression collations properly. 2018-12-11 11:48:00 -05:00
test_shm_mq Avoid using ambiguous word "non-negative" in error messages. 2021-07-28 01:24:51 +09:00
worker_spi Fix worker_spi for new parameter to initialize connection 2018-04-05 19:14:50 +02:00
Makefile Fix partial-build problems introduced by having more generated headers. 2018-04-09 16:42:10 -04:00
README Add README in src/test and src/test/modules 2016-02-25 21:08:32 -03:00

Test extensions and libraries
=============================

src/test/modules contains PostgreSQL extensions that are primarily or entirely
intended for testing PostgreSQL and/or to serve as example code. The extensions
here aren't intended to be installed in a production server and aren't suitable
for "real work".

Most extensions have their own pg_regress tests or isolationtester specs. Some
are also used by tests elsewhere in the tree.

If you're adding new hooks or other functionality exposed as C-level API this
is where to add the tests for it.