postgresql/src/backend/utils
Andres Freund 5c1560606d Fix several recently introduced issues around handling new relation forks.
Most of these stem from d25f519107 "tableam: relation creation, VACUUM
FULL/CLUSTER, SET TABLESPACE.".

1) To pass data to the relation_set_new_filenode()
   RelationSetNewRelfilenode() was made to update RelationData.rd_rel
   directly. That's not OK however, as it makes the relcache entries
   temporarily inconsistent. Which among other scenarios is a problem
   if a REINDEX targets an index on pg_class - the
   CatalogTupleUpdate() in RelationSetNewRelfilenode().  Presumably
   that was introduced because other places in the code do so - while
   those aren't "good practice" they don't appear to be actively
   buggy (e.g. because system tables may not be targeted).

   I (Andres) should have caught this while reviewing and signficantly
   evolving the code in that commit, mea culpa.

   Fix that by instead passing in the new RelFileNode as separate
   argument to relation_set_new_filenode() and rely on the relcache to
   update the catalog entry. Also revert that the
   RelationMapUpdateMap() call was changed to immediate, and undo some
   other more unnecessary changes.

2) Document that the relation_set_new_filenode cannot rely on the
   whole relcache entry to be valid. It might be worthwhile to
   refactor the code to never have to rely on that, but given the way
   heap_create() is currently coded, that'd be a large change.

3) ATExecSetTableSpace() shouldn't do FlushRelationBuffers() itself. A
   table AM might not use shared buffers at all. Move to
   index_copy_data() and heapam_relation_copy_data().

4) heapam_relation_set_new_filenode() previously sometimes accessed
   rel->rd_rel->relpersistence rather than the `persistence`
   argument. Code movement mistake.

5) Previously heapam_relation_set_new_filenode() re-opened the smgr
   relation to create the init for, if necesary. Instead have
   RelationCreateStorage() return the SMgrRelation and use it to
   create the init fork.

6) Add a note about the danger of modifying the relcache directly to
   ATExecSetTableSpace() - it's currently not a bug because there's a
   check ERRORing for catalog tables.

Regression tests and assertion improvements that together trigger the
bug described in 1) will be added in a later commit, as there is a
related bug on all branches.

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
2019-04-29 19:28:05 -07:00
..
adt Clean up minor warnings from buildfarm. 2019-04-28 12:45:55 -04:00
cache Fix several recently introduced issues around handling new relation forks. 2019-04-29 19:28:05 -07:00
error Update copyright for 2019 2019-01-02 12:44:25 -05:00
fmgr Change function call information to be variable length. 2019-01-26 14:17:52 -08:00
hash Fix collection of typos and grammar mistakes in docs and comments 2019-04-19 16:57:40 +09:00
init Consistently test for in-use shared memory. 2019-04-12 22:36:38 -07:00
mb More unconstify use 2019-02-13 11:50:16 +01:00
misc postgresql.conf.sample: add proper defaults for include actions 2019-04-17 18:12:10 -04:00
mmgr Fix problems with auto-held portals. 2019-04-19 11:20:37 -04:00
resowner Enable parallel query with SERIALIZABLE isolation. 2019-03-15 17:47:04 +13:00
sort Make heap TID a tiebreaker nbtree index column. 2019-03-20 10:04:01 -07:00
time Mark correctly initial slot snapshots with MVCC type when built 2019-02-20 12:31:07 +09:00
.gitignore Rearrange makefile rules for running Gen_fmgrtab.pl. 2018-05-03 17:54:18 -04:00
errcodes.txt Partial implementation of SQL/JSON path language 2019-03-16 12:16:48 +03:00
Gen_dummy_probes.pl Update copyright for 2019 2019-01-02 12:44:25 -05:00
Gen_dummy_probes.sed Update copyright for 2019 2019-01-02 12:44:25 -05:00
Gen_fmgrtab.pl Use Getopt::Long for catalog scripts 2019-02-12 12:22:08 -03:00
generate-errcodes.pl Update copyright for 2019 2019-01-02 12:44:25 -05:00
Makefile Use Getopt::Long for catalog scripts 2019-02-12 12:22:08 -03:00
probes.d Update copyright for 2019 2019-01-02 12:44:25 -05:00