<div dir="ltr">Hi,<br><br>We have just found a bug involving the recently added BRIN support for PostGIS datatype.<br><br>When new entries are added in an already indexed field, the stored bounding box is updated<br>if necessary, but the change is not then logged into the WALs: the resulting in memory updated<br>index continue to work properly and there are no effects or reported errors, but it is not crash safe,<br>and results to be corrupted both on a master server and on its relative standbys. The index<br>is neither restorable also through PITR from a physical backup.<br><br>Below it is reported a simple example showing the bug effects:<br><br>=# CREATE TABLE bench(id integer, geom geometry);<br>CREATE TABLE<br>=# CREATE INDEX ON bench USING brin(geom);<br>CREATE INDEX<br>=# INSERT INTO bench SELECT i, ST_MakeBox2D(ST_MakePoint(i, i), ST_MakePoint(i+0.1, i+0.1))<br>-# FROM generate_series(1, 3) AS i;<br>INSERT 2 2<br>=# SET enable_seqscan TO on;<br>SET<br>=# SET enable_bitmapscan TO off;<br>SET<br>=# SELECT count(*) FROM bench WHERE ST_Contains('BOX(2 2, 3000 3000)'::box2d, geom);<br><br>  count<br> --------<br>     2<br>(1 row)<br><br>=# SET enable_seqscan TO off;<br>SET<br>=# SET enable_bitmapscan TO on;<br>SET<br>=# SELECT count(*) FROM bench WHERE ST_Contains('BOX(2 2, 3000 3000)'::box2d, geom);<br><br>  count<br> --------<br>     0<br>(1 row)<br><br>=# SELECT value FROM brin_page_items(get_raw_page('<wbr>bench_geom_idx', 2), 'bench_geom_idx');<br><br>                              <wbr>       value<br> -----------------------------<wbr>------------------------------<wbr>---------<span class="gmail-im"><br> {BOX2DF(1 1, 1.10000002384 1.10000002384) .. f .. f}<br></span>(1 row)<br><br>So the in memory index has the bounding box correctly setup with regards to the initial <br>data, but records inserted after cannot be queried using the index.<br><br>A patch containing the fix has already been submitted to the PostGIS dev team, that should then <br>been released with the next updates of PostGIS. <br>In the meantime, since the bug should not affect just-initiated indexes, the workaround is to recreate <br>a new index everytime new entries are added in the indexed fields (consider that BRINs can be <br>quickly rebuilt).<br><br>Regards,<br>Giuseppe, Julien & Ronan.<br><br>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span></span>Giuseppe Broccolo - 2ndQuadrant Italy
<br>PostgreSQL & PostGIS Training, Services and Support
<br><a href="mailto:giuseppe.broccolo@2ndQuadrant.it" target="_blank">giuseppe.broccolo@2ndQuadrant.it</a> | <a href="http://www.2ndQuadrant.it" target="_blank">www.2ndQuadrant.it</a>
</div></div></div></div>
</div>