postgresql/src/backend/catalog
Andres Freund 3dbb317d32 Fix potential assertion failure when reindexing a pg_class index.
When reindexing individual indexes on pg_class it was possible to
either trigger an assertion failure:
TRAP: FailedAssertion("!(!ReindexIsProcessingIndex(((index)->rd_id)))

That's because reindex_index() called SetReindexProcessing() - which
enables an asserts ensuring no index insertions happen into the index
- before calling RelationSetNewRelfilenode(). That not correct for
indexes on pg_class, because RelationSetNewRelfilenode() updates the
relevant pg_class row, which needs to update the indexes.

The are two reasons this wasn't noticed earlier. Firstly the bug
doesn't trigger when reindexing all of pg_class, as reindex_relation
has code "hiding" all yet-to-be-reindexed indexes. Secondly, the bug
only triggers when the the update to pg_class doesn't turn out to be a
HOT update - otherwise there's no index insertion to trigger the
bug. Most of the time there's enough space, making this bug hard to
trigger.

To fix, move RelationSetNewRelfilenode() to before the
SetReindexProcessing() (and, together with some other code, to outside
of the PG_TRY()).

To make sure the error checking intended by SetReindexProcessing() is
more robust, modify CatalogIndexInsert() to check
ReindexIsProcessingIndex() even when the update is a HOT update.

Also add a few regression tests for REINDEXing of system catalogs.

The last two improvements would have prevented some of the issues
fixed in 5c1560606d from being introduced in the first place.

Reported-By: Michael Paquier
Diagnosed-By: Tom Lane and Andres Freund
Author: Andres Freund
Reviewed-By: Tom Lane
Discussion: https://postgr.es/m/20190418011430.GA19133@paquier.xyz
Backpatch: 9.4-, the bug is present in all branches
2019-04-29 19:42:08 -07:00
..
.gitignore
aclchk.c
catalog.c
Catalog.pm
dependency.c REINDEX CONCURRENTLY 2019-03-29 08:26:33 +01:00
genbki.pl Sync commentary in transam.h and bki.sgml. 2019-03-14 00:23:40 -04:00
heap.c Fix several recently introduced issues around handling new relation forks. 2019-04-29 19:28:05 -07:00
index.c Fix potential assertion failure when reindexing a pg_class index. 2019-04-29 19:42:08 -07:00
indexing.c Fix potential assertion failure when reindexing a pg_class index. 2019-04-29 19:42:08 -07:00
information_schema.sql Generated columns 2019-03-30 08:15:57 +01:00
Makefile
namespace.c
objectaccess.c
objectaddress.c
partition.c Add index_get_partition convenience function 2019-03-20 18:18:50 -03:00
pg_aggregate.c Implement OR REPLACE option for CREATE AGGREGATE. 2019-03-19 01:16:50 +00:00
pg_collation.c Collations with nondeterministic comparison 2019-03-22 12:12:43 +01:00
pg_constraint.c
pg_conversion.c
pg_db_role_setting.c
pg_depend.c Fix REINDEX CONCURRENTLY of partitions 2019-04-12 08:36:05 +02:00
pg_enum.c
pg_inherits.c
pg_largeobject.c
pg_namespace.c
pg_operator.c
pg_proc.c Implement OR REPLACE option for CREATE AGGREGATE. 2019-03-19 01:16:50 +00:00
pg_publication.c
pg_range.c
pg_shdepend.c Sort dependent objects before reporting them in DROP ROLE. 2019-03-24 18:17:53 -04:00
pg_subscription.c
pg_type.c
sql_feature_packages.txt
sql_features.txt Improve documentation about our XML functionality. 2019-04-01 16:20:22 -04:00
storage.c Fix several recently introduced issues around handling new relation forks. 2019-04-29 19:28:05 -07:00
system_views.sql Return NULL for checksum failures if checksums are not enabled 2019-04-17 13:51:48 +02:00
toasting.c Ignore attempts to add TOAST table to shared or catalog tables 2019-03-19 11:15:50 +01:00