Skip to content

Commit c442b32

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 <exclusion@gmail.com> Discussion: https://postgr.es/m/16577-881633a9f9894fd5@postgresql.org Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Michael Paquier <michael@paquier.xyz>
1 parent d8bb22a commit c442b32

File tree

8 files changed

+117
-10
lines changed

8 files changed

+117
-10
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6166,10 +6166,21 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
61666166
</para>
61676167
</listitem>
61686168
</varlistentry>
6169+
6170+
<varlistentry>
6171+
<term><symbol>SHARED_DEPENDENCY_TABLESPACE</symbol> (<literal>t</literal>)</term>
6172+
<listitem>
6173+
<para>
6174+
The referenced object (which must be a tablespace) is mentioned as
6175+
the tablespace for a relation that doesn't have storage.
6176+
</para>
6177+
</listitem>
6178+
</varlistentry>
61696179
</variablelist>
61706180

61716181
Other dependency flavors might be needed in future. Note in particular
6172-
that the current definition only supports roles as referenced objects.
6182+
that the current definition only supports roles and tablespaces as referenced
6183+
objects.
61736184
</para>
61746185

61756186
</sect1>

src/backend/catalog/heap.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ heap_create(const char *relname,
376376
RelationCreateStorage(rel->rd_node, relpersistence);
377377
}
378378

379+
/*
380+
* If a tablespace is specified, removal of that tablespace is normally
381+
* protected by the existence of a physical file; but for relations with
382+
* no files, add a pg_shdepend entry to account for that.
383+
*/
384+
if (!create_storage && reltablespace != InvalidOid)
385+
recordDependencyOnTablespace(RelationRelationId, relid,
386+
reltablespace);
387+
379388
return rel;
380389
}
381390

src/backend/catalog/pg_shdepend.c

Lines changed: 59 additions & 9 deletions
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 "storage/lmgr.h"
6465
#include "miscadmin.h"
@@ -181,11 +182,14 @@ recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
181182
*
182183
* There must be no more than one existing entry for the given dependent
183184
* object and dependency type! So in practice this can only be used for
184-
* updating SHARED_DEPENDENCY_OWNER entries, which should have that property.
185+
* updating SHARED_DEPENDENCY_OWNER and SHARED_DEPENDENCY_TABLESPACE
186+
* entries, which should have that property.
185187
*
186188
* If there is no previous entry, we assume it was referencing a PINned
187189
* object, so we create a new entry. If the new referenced object is
188190
* PINned, we don't create an entry (and drop the old one, if any).
191+
* (For tablespaces, we don't record dependencies in certain cases, so
192+
* there are other possible reasons for entries to be missing.)
189193
*
190194
* sdepRel must be the pg_shdepend relation, already opened and suitably
191195
* locked.
@@ -339,6 +343,58 @@ changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
339343
heap_close(sdepRel, RowExclusiveLock);
340344
}
341345

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

1002-
/*
1003-
* Currently, this routine need not support any other shared
1004-
* object types besides roles. If we wanted to record explicit
1005-
* dependencies on databases or tablespaces, we'd need code along
1006-
* these lines:
1007-
*/
1008-
#ifdef NOT_USED
10091058
case TableSpaceRelationId:
10101059
{
10111060
/* For lack of a syscache on pg_tablespace, do this: */
@@ -1019,7 +1068,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
10191068
pfree(tablespace);
10201069
break;
10211070
}
1022-
#endif
10231071

10241072
case DatabaseRelationId:
10251073
{
@@ -1079,6 +1127,8 @@ storeObjectDescription(StringInfo descs,
10791127
appendStringInfo(descs, _("privileges for %s"), objdesc);
10801128
else if (deptype == SHARED_DEPENDENCY_POLICY)
10811129
appendStringInfo(descs, _("target of %s"), objdesc);
1130+
else if (deptype == SHARED_DEPENDENCY_TABLESPACE)
1131+
appendStringInfo(descs, _("tablespace for %s"), objdesc);
10821132
else
10831133
elog(ERROR, "unrecognized dependency type: %d",
10841134
(int) deptype);

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11845,6 +11845,10 @@ ATExecPartedIdxSetTableSpace(Relation rel, Oid newTableSpace)
1184511845
rd_rel->reltablespace = (newTableSpace == MyDatabaseTableSpace) ? InvalidOid : newTableSpace;
1184611846
CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
1184711847

11848+
/* Record dependency on tablespace */
11849+
changeDependencyOnTablespace(RelationRelationId,
11850+
indexOid, rd_rel->reltablespace);
11851+
1184811852
InvokeObjectPostAlterHook(RelationRelationId, indexOid, 0);
1184911853

1185011854
heap_freetuple(tuple);

src/backend/commands/tablespace.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ DropTableSpace(DropTableSpaceStmt *stmt)
408408
HeapTuple tuple;
409409
ScanKeyData entry[1];
410410
Oid tablespaceoid;
411+
char *detail;
412+
char *detail_log;
411413

412414
/*
413415
* Find the target tuple
@@ -455,6 +457,16 @@ DropTableSpace(DropTableSpaceStmt *stmt)
455457
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_TABLESPACE,
456458
tablespacename);
457459

460+
/* Check for pg_shdepend entries depending on this tablespace */
461+
if (checkSharedDependencies(TableSpaceRelationId, tablespaceoid,
462+
&detail, &detail_log))
463+
ereport(ERROR,
464+
(errcode(ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST),
465+
errmsg("tablespace \"%s\" cannot be dropped because some objects depend on it",
466+
tablespacename),
467+
errdetail_internal("%s", detail),
468+
errdetail_log("%s", detail_log)));
469+
458470
/* DROP hook for the tablespace being removed */
459471
InvokeObjectDropHook(TableSpaceRelationId, tablespaceoid, 0);
460472

src/include/catalog/dependency.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ typedef enum DependencyType
122122
* a role mentioned in a policy object. The referenced object must be a
123123
* pg_authid entry.
124124
*
125+
* (e) a SHARED_DEPENDENCY_TABLESPACE entry means that the referenced
126+
* object is a tablespace mentioned in a relation without storage. The
127+
* referenced object must be a pg_tablespace entry. (Relations that have
128+
* storage don't need this: they are protected by the existence of a physical
129+
* file in the tablespace.)
130+
*
125131
* SHARED_DEPENDENCY_INVALID is a value used as a parameter in internal
126132
* routines, and is not valid in the catalog itself.
127133
*/
@@ -131,6 +137,7 @@ typedef enum SharedDependencyType
131137
SHARED_DEPENDENCY_OWNER = 'o',
132138
SHARED_DEPENDENCY_ACL = 'a',
133139
SHARED_DEPENDENCY_POLICY = 'r',
140+
SHARED_DEPENDENCY_TABLESPACE = 't',
134141
SHARED_DEPENDENCY_INVALID = 0
135142
} SharedDependencyType;
136143

@@ -280,6 +287,12 @@ extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
280287
extern void changeDependencyOnOwner(Oid classId, Oid objectId,
281288
Oid newOwnerId);
282289

290+
extern void recordDependencyOnTablespace(Oid classId, Oid objectId,
291+
Oid tablespace);
292+
293+
extern void changeDependencyOnTablespace(Oid classId, Oid objectId,
294+
Oid newTablespaceId);
295+
283296
extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
284297
Oid ownerId,
285298
int noldmembers, Oid *oldmembers,

src/test/regress/input/tablespace.source

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ CREATE TABLESPACE regress_badspace LOCATION '/no/such/location';
115115
-- No such tablespace
116116
CREATE TABLE bar (i int) TABLESPACE regress_nosuchspace;
117117

118+
-- Fail, in use for some partitioned object
119+
DROP TABLESPACE regress_tblspace;
120+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
118121
-- Fail, not empty
119122
DROP TABLESPACE regress_tblspace;
120123

src/test/regress/output/tablespace.source

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ ERROR: directory "/no/such/location" does not exist
234234
-- No such tablespace
235235
CREATE TABLE bar (i int) TABLESPACE regress_nosuchspace;
236236
ERROR: tablespace "regress_nosuchspace" does not exist
237+
-- Fail, in use for some partitioned object
238+
DROP TABLESPACE regress_tblspace;
239+
ERROR: tablespace "regress_tblspace" cannot be dropped because some objects depend on it
240+
DETAIL: tablespace for index testschema.part_a_idx
241+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
237242
-- Fail, not empty
238243
DROP TABLESPACE regress_tblspace;
239244
ERROR: tablespace "regress_tblspace" is not empty

0 commit comments

Comments
 (0)