mirror of
https://github.com/postgres/postgres.git
synced 2025-06-22 02:52:08 +03:00
Fix tablespace inheritance for partitioned rels
Commitca4103025d
left a few loose ends. The most important one (broken pg_dump output) is already fixed by virtue of commit3b23552ad8
, but some things remained: * When ALTER TABLE rewrites tables, the indexes must remain in the tablespace they were originally in. This didn't work because index recreation during ALTER TABLE runs manufactured SQL (yuck), which runs afoul of default_tablespace in competition with the parent relation tablespace. To fix, reset default_tablespace to the empty string temporarily, and add the TABLESPACE clause as appropriate. * Setting a partitioned rel's tablespace to the database default is confusing; if it worked, it would direct the partitions to that tablespace regardless of default_tablespace. But in reality it does not work, and making it work is a larger project. Therefore, throw an error when this condition is detected, to alert the unwary. Add some docs and tests, too. Author: Álvaro Herrera Discussion: https://postgr.es/m/CAKJS1f_1c260nOt_vBJ067AZ3JXptXVRohDVMLEBmudX1YEx-A@mail.gmail.com
This commit is contained in:
@ -467,8 +467,21 @@ DefineIndex(Oid relationId,
|
||||
LOCKTAG heaplocktag;
|
||||
LOCKMODE lockmode;
|
||||
Snapshot snapshot;
|
||||
int save_nestlevel = -1;
|
||||
int i;
|
||||
|
||||
/*
|
||||
* Some callers need us to run with an empty default_tablespace; this is a
|
||||
* necessary hack to be able to reproduce catalog state accurately when
|
||||
* recreating indexes after table-rewriting ALTER TABLE.
|
||||
*/
|
||||
if (stmt->reset_default_tblspc)
|
||||
{
|
||||
save_nestlevel = NewGUCNestLevel();
|
||||
(void) set_config_option("default_tablespace", "",
|
||||
PGC_USERSET, PGC_S_SESSION,
|
||||
GUC_ACTION_SAVE, true, 0, false);
|
||||
}
|
||||
|
||||
/*
|
||||
* Start progress report. If we're building a partition, this was already
|
||||
@ -622,10 +635,15 @@ DefineIndex(Oid relationId,
|
||||
if (stmt->tableSpace)
|
||||
{
|
||||
tablespaceId = get_tablespace_oid(stmt->tableSpace, false);
|
||||
if (partitioned && tablespaceId == MyDatabaseTableSpace)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("cannot specify default tablespace for partitioned relation")));
|
||||
}
|
||||
else
|
||||
{
|
||||
tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence);
|
||||
tablespaceId = GetDefaultTablespace(rel->rd_rel->relpersistence,
|
||||
partitioned);
|
||||
/* note InvalidOid is OK in this case */
|
||||
}
|
||||
|
||||
@ -980,6 +998,13 @@ DefineIndex(Oid relationId,
|
||||
|
||||
ObjectAddressSet(address, RelationRelationId, indexRelationId);
|
||||
|
||||
/*
|
||||
* Revert to original default_tablespace. Must do this before any return
|
||||
* from this function, but after index_create, so this is a good time.
|
||||
*/
|
||||
if (save_nestlevel >= 0)
|
||||
AtEOXact_GUC(true, save_nestlevel);
|
||||
|
||||
if (!OidIsValid(indexRelationId))
|
||||
{
|
||||
table_close(rel, NoLock);
|
||||
|
Reference in New Issue
Block a user