Skip to content

Commit ebfe2db

Browse files
committed
Prevent drop of tablespaces used by partitioned relations
When a tablespace is used in a partitioned relation (per commits ca41030 in pg12 for tables and 33e6c34 in pg11 for indexes), it is possible to drop the tablespace, potentially causing various problems. One such was reported in bug #16577, where a rewriting ALTER TABLE causes a server crash. Protect against this by using pg_shdepend to keep track of tablespaces when used for relations that don't keep physical files; we now abort a tablespace if we see that the tablespace is referenced from any partitioned relations. Backpatch this to 11, where this problem has been latent all along. We don't try to create pg_shdepend entries for existing partitioned indexes/tables, but any ones that are modified going forward will be protected. Note slight behavior change: when trying to drop a tablespace that contains both regular tables as well as partitioned ones, you'd previously get ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE and now you'll get ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST. Arguably, the latter is more correct. It is possible to add protecting pg_shdepend entries for existing tables/indexes, by doing ALTER TABLE ONLY some_partitioned_table SET TABLESPACE pg_default; ALTER TABLE ONLY some_partitioned_table SET TABLESPACE original_tablespace; for each partitioned table/index that is not in the database default tablespace. Because these partitioned objects do not have storage, no file needs to be actually moved, so it shouldn't take more time than what's required to acquire locks. This query can be used to search for such relations: SELECT ... FROM pg_class WHERE relkind IN ('p', 'I') AND reltablespace <> 0 Reported-by: Alexander Lakhin <[email protected]> Discussion: https://postgr.es/m/[email protected] Author: Álvaro Herrera <[email protected]> Reviewed-by: Michael Paquier <[email protected]>
1 parent 424d7a9 commit ebfe2db

File tree

8 files changed

+117
-10
lines changed

8 files changed

+117
-10
lines changed

doc/src/sgml/catalogs.sgml

+12-1
Original file line numberDiff line numberDiff line change
@@ -6849,10 +6849,21 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
68496849
</para>
68506850
</listitem>
68516851
</varlistentry>
6852+
6853+
<varlistentry>
6854+
<term><symbol>SHARED_DEPENDENCY_TABLESPACE</symbol> (<literal>t</literal>)</term>
6855+
<listitem>
6856+
<para>
6857+
The referenced object (which must be a tablespace) is mentioned as
6858+
the tablespace for a relation that doesn't have storage.
6859+
</para>
6860+
</listitem>
6861+
</varlistentry>
68526862
</variablelist>
68536863

68546864
Other dependency flavors might be needed in future. Note in particular
6855-
that the current definition only supports roles as referenced objects.
6865+
that the current definition only supports roles and tablespaces as referenced
6866+
objects.
68566867
</para>
68576868

68586869
</sect1>

src/backend/catalog/heap.c

+9
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,15 @@ heap_create(const char *relname,
440440
}
441441
}
442442

443+
/*
444+
* If a tablespace is specified, removal of that tablespace is normally
445+
* protected by the existence of a physical file; but for relations with
446+
* no files, add a pg_shdepend entry to account for that.
447+
*/
448+
if (!create_storage && reltablespace != InvalidOid)
449+
recordDependencyOnTablespace(RelationRelationId, relid,
450+
reltablespace);
451+
443452
return rel;
444453
}
445454

src/backend/catalog/pg_shdepend.c

+59-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "commands/schemacmds.h"
6060
#include "commands/subscriptioncmds.h"
6161
#include "commands/tablecmds.h"
62+
#include "commands/tablespace.h"
6263
#include "commands/typecmds.h"
6364
#include "miscadmin.h"
6465
#include "storage/lmgr.h"
@@ -186,11 +187,14 @@ recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
186187
*
187188
* There must be no more than one existing entry for the given dependent
188189
* object and dependency type! So in practice this can only be used for
189-
* updating SHARED_DEPENDENCY_OWNER entries, which should have that property.
190+
* updating SHARED_DEPENDENCY_OWNER and SHARED_DEPENDENCY_TABLESPACE
191+
* entries, which should have that property.
190192
*
191193
* If there is no previous entry, we assume it was referencing a PINned
192194
* object, so we create a new entry. If the new referenced object is
193195
* PINned, we don't create an entry (and drop the old one, if any).
196+
* (For tablespaces, we don't record dependencies in certain cases, so
197+
* there are other possible reasons for entries to be missing.)
194198
*
195199
* sdepRel must be the pg_shdepend relation, already opened and suitably
196200
* locked.
@@ -344,6 +348,58 @@ changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
344348
table_close(sdepRel, RowExclusiveLock);
345349
}
346350

351+
/*
352+
* recordDependencyOnTablespace
353+
*
354+
* A convenient wrapper of recordSharedDependencyOn -- register the specified
355+
* tablespace as default for the given object.
356+
*
357+
* Note: it's the caller's responsibility to ensure that there isn't a
358+
* tablespace entry for the object already.
359+
*/
360+
void
361+
recordDependencyOnTablespace(Oid classId, Oid objectId, Oid tablespace)
362+
{
363+
ObjectAddress myself,
364+
referenced;
365+
366+
ObjectAddressSet(myself, classId, objectId);
367+
ObjectAddressSet(referenced, TableSpaceRelationId, tablespace);
368+
369+
recordSharedDependencyOn(&myself, &referenced,
370+
SHARED_DEPENDENCY_TABLESPACE);
371+
}
372+
373+
/*
374+
* changeDependencyOnTablespace
375+
*
376+
* Update the shared dependencies to account for the new tablespace.
377+
*
378+
* Note: we don't need an objsubid argument because only whole objects
379+
* have tablespaces.
380+
*/
381+
void
382+
changeDependencyOnTablespace(Oid classId, Oid objectId, Oid newTablespaceId)
383+
{
384+
Relation sdepRel;
385+
386+
sdepRel = table_open(SharedDependRelationId, RowExclusiveLock);
387+
388+
if (newTablespaceId != DEFAULTTABLESPACE_OID &&
389+
newTablespaceId != InvalidOid)
390+
shdepChangeDep(sdepRel,
391+
classId, objectId, 0,
392+
TableSpaceRelationId, newTablespaceId,
393+
SHARED_DEPENDENCY_TABLESPACE);
394+
else
395+
shdepDropDependency(sdepRel,
396+
classId, objectId, 0, true,
397+
InvalidOid, InvalidOid,
398+
SHARED_DEPENDENCY_INVALID);
399+
400+
table_close(sdepRel, RowExclusiveLock);
401+
}
402+
347403
/*
348404
* getOidListDiff
349405
* Helper for updateAclDependencies.
@@ -1121,13 +1177,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
11211177
objectId)));
11221178
break;
11231179

1124-
/*
1125-
* Currently, this routine need not support any other shared
1126-
* object types besides roles. If we wanted to record explicit
1127-
* dependencies on databases or tablespaces, we'd need code along
1128-
* these lines:
1129-
*/
1130-
#ifdef NOT_USED
11311180
case TableSpaceRelationId:
11321181
{
11331182
/* For lack of a syscache on pg_tablespace, do this: */
@@ -1141,7 +1190,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
11411190
pfree(tablespace);
11421191
break;
11431192
}
1144-
#endif
11451193

11461194
case DatabaseRelationId:
11471195
{
@@ -1201,6 +1249,8 @@ storeObjectDescription(StringInfo descs,
12011249
appendStringInfo(descs, _("privileges for %s"), objdesc);
12021250
else if (deptype == SHARED_DEPENDENCY_POLICY)
12031251
appendStringInfo(descs, _("target of %s"), objdesc);
1252+
else if (deptype == SHARED_DEPENDENCY_TABLESPACE)
1253+
appendStringInfo(descs, _("tablespace for %s"), objdesc);
12041254
else
12051255
elog(ERROR, "unrecognized dependency type: %d",
12061256
(int) deptype);

src/backend/commands/tablecmds.c

+4
Original file line numberDiff line numberDiff line change
@@ -13340,6 +13340,10 @@ ATExecSetTableSpaceNoStorage(Relation rel, Oid newTableSpace)
1334013340
rd_rel->reltablespace = (newTableSpace == MyDatabaseTableSpace) ? InvalidOid : newTableSpace;
1334113341
CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
1334213342

13343+
/* Record dependency on tablespace */
13344+
changeDependencyOnTablespace(RelationRelationId,
13345+
reloid, rd_rel->reltablespace);
13346+
1334313347
InvokeObjectPostAlterHook(RelationRelationId, reloid, 0);
1334413348

1334513349
heap_freetuple(tuple);

src/backend/commands/tablespace.c

+12
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ DropTableSpace(DropTableSpaceStmt *stmt)
420420
Form_pg_tablespace spcform;
421421
ScanKeyData entry[1];
422422
Oid tablespaceoid;
423+
char *detail;
424+
char *detail_log;
423425

424426
/*
425427
* Find the target tuple
@@ -468,6 +470,16 @@ DropTableSpace(DropTableSpaceStmt *stmt)
468470
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_TABLESPACE,
469471
tablespacename);
470472

473+
/* Check for pg_shdepend entries depending on this tablespace */
474+
if (checkSharedDependencies(TableSpaceRelationId, tablespaceoid,
475+
&detail, &detail_log))
476+
ereport(ERROR,
477+
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
478+
errmsg("tablespace \"%s\" cannot be dropped because some objects depend on it",
479+
tablespacename),
480+
errdetail_internal("%s", detail),
481+
errdetail_log("%s", detail_log)));
482+
471483
/* DROP hook for the tablespace being removed */
472484
InvokeObjectDropHook(TableSpaceRelationId, tablespaceoid, 0);
473485

src/include/catalog/dependency.h

+13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ typedef enum DependencyType
6767
* a role mentioned in a policy object. The referenced object must be a
6868
* pg_authid entry.
6969
*
70+
* (e) a SHARED_DEPENDENCY_TABLESPACE entry means that the referenced
71+
* object is a tablespace mentioned in a relation without storage. The
72+
* referenced object must be a pg_tablespace entry. (Relations that have
73+
* storage don't need this: they are protected by the existence of a physical
74+
* file in the tablespace.)
75+
*
7076
* SHARED_DEPENDENCY_INVALID is a value used as a parameter in internal
7177
* routines, and is not valid in the catalog itself.
7278
*/
@@ -76,6 +82,7 @@ typedef enum SharedDependencyType
7682
SHARED_DEPENDENCY_OWNER = 'o',
7783
SHARED_DEPENDENCY_ACL = 'a',
7884
SHARED_DEPENDENCY_POLICY = 'r',
85+
SHARED_DEPENDENCY_TABLESPACE = 't',
7986
SHARED_DEPENDENCY_INVALID = 0
8087
} SharedDependencyType;
8188

@@ -253,6 +260,12 @@ extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
253260
extern void changeDependencyOnOwner(Oid classId, Oid objectId,
254261
Oid newOwnerId);
255262

263+
extern void recordDependencyOnTablespace(Oid classId, Oid objectId,
264+
Oid tablespace);
265+
266+
extern void changeDependencyOnTablespace(Oid classId, Oid objectId,
267+
Oid newTablespaceId);
268+
256269
extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
257270
Oid ownerId,
258271
int noldmembers, Oid *oldmembers,

src/test/regress/input/tablespace.source

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ CREATE TABLESPACE regress_badspace LOCATION '/no/such/location';
249249
-- No such tablespace
250250
CREATE TABLE bar (i int) TABLESPACE regress_nosuchspace;
251251

252+
-- Fail, in use for some partitioned object
253+
DROP TABLESPACE regress_tblspace;
254+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
252255
-- Fail, not empty
253256
DROP TABLESPACE regress_tblspace;
254257

src/test/regress/output/tablespace.source

+5
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,11 @@ ERROR: directory "/no/such/location" does not exist
712712
-- No such tablespace
713713
CREATE TABLE bar (i int) TABLESPACE regress_nosuchspace;
714714
ERROR: tablespace "regress_nosuchspace" does not exist
715+
-- Fail, in use for some partitioned object
716+
DROP TABLESPACE regress_tblspace;
717+
ERROR: tablespace "regress_tblspace" cannot be dropped because some objects depend on it
718+
DETAIL: tablespace for index testschema.part_a_idx
719+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
715720
-- Fail, not empty
716721
DROP TABLESPACE regress_tblspace;
717722
ERROR: tablespace "regress_tblspace" is not empty

0 commit comments

Comments
 (0)