Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c9b7025

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 259b212 commit c9b7025

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
@@ -6175,10 +6175,21 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
61756175
</para>
61766176
</listitem>
61776177
</varlistentry>
6178+
6179+
<varlistentry>
6180+
<term><symbol>SHARED_DEPENDENCY_TABLESPACE</symbol> (<literal>t</literal>)</term>
6181+
<listitem>
6182+
<para>
6183+
The referenced object (which must be a tablespace) is mentioned as
6184+
the tablespace for a relation that doesn't have storage.
6185+
</para>
6186+
</listitem>
6187+
</varlistentry>
61786188
</variablelist>
61796189

61806190
Other dependency flavors might be needed in future. Note in particular
6181-
that the current definition only supports roles as referenced objects.
6191+
that the current definition only supports roles and tablespaces as referenced
6192+
objects.
61826193
</para>
61836194

61846195
</sect1>

src/backend/catalog/heap.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,15 @@ heap_create(const char *relname,
442442
}
443443
}
444444

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

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"
@@ -187,11 +188,14 @@ recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner)
187188
*
188189
* There must be no more than one existing entry for the given dependent
189190
* object and dependency type! So in practice this can only be used for
190-
* updating SHARED_DEPENDENCY_OWNER entries, which should have that property.
191+
* updating SHARED_DEPENDENCY_OWNER and SHARED_DEPENDENCY_TABLESPACE
192+
* entries, which should have that property.
191193
*
192194
* If there is no previous entry, we assume it was referencing a PINned
193195
* object, so we create a new entry. If the new referenced object is
194196
* PINned, we don't create an entry (and drop the old one, if any).
197+
* (For tablespaces, we don't record dependencies in certain cases, so
198+
* there are other possible reasons for entries to be missing.)
195199
*
196200
* sdepRel must be the pg_shdepend relation, already opened and suitably
197201
* locked.
@@ -345,6 +349,58 @@ changeDependencyOnOwner(Oid classId, Oid objectId, Oid newOwnerId)
345349
table_close(sdepRel, RowExclusiveLock);
346350
}
347351

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

1087-
/*
1088-
* Currently, this routine need not support any other shared
1089-
* object types besides roles. If we wanted to record explicit
1090-
* dependencies on databases or tablespaces, we'd need code along
1091-
* these lines:
1092-
*/
1093-
#ifdef NOT_USED
10941143
case TableSpaceRelationId:
10951144
{
10961145
/* For lack of a syscache on pg_tablespace, do this: */
@@ -1104,7 +1153,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
11041153
pfree(tablespace);
11051154
break;
11061155
}
1107-
#endif
11081156

11091157
case DatabaseRelationId:
11101158
{
@@ -1164,6 +1212,8 @@ storeObjectDescription(StringInfo descs,
11641212
appendStringInfo(descs, _("privileges for %s"), objdesc);
11651213
else if (deptype == SHARED_DEPENDENCY_POLICY)
11661214
appendStringInfo(descs, _("target of %s"), objdesc);
1215+
else if (deptype == SHARED_DEPENDENCY_TABLESPACE)
1216+
appendStringInfo(descs, _("tablespace for %s"), objdesc);
11671217
else
11681218
elog(ERROR, "unrecognized dependency type: %d",
11691219
(int) deptype);

src/backend/commands/tablecmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12839,6 +12839,10 @@ ATExecSetTableSpaceNoStorage(Relation rel, Oid newTableSpace)
1283912839
rd_rel->reltablespace = (newTableSpace == MyDatabaseTableSpace) ? InvalidOid : newTableSpace;
1284012840
CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
1284112841

12842+
/* Record dependency on tablespace */
12843+
changeDependencyOnTablespace(RelationRelationId,
12844+
reloid, rd_rel->reltablespace);
12845+
1284212846
InvokeObjectPostAlterHook(RelationRelationId, reloid, 0);
1284312847

1284412848
heap_freetuple(tuple);

src/backend/commands/tablespace.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ DropTableSpace(DropTableSpaceStmt *stmt)
421421
Form_pg_tablespace spcform;
422422
ScanKeyData entry[1];
423423
Oid tablespaceoid;
424+
char *detail;
425+
char *detail_log;
424426

425427
/*
426428
* Find the target tuple
@@ -469,6 +471,16 @@ DropTableSpace(DropTableSpaceStmt *stmt)
469471
aclcheck_error(ACLCHECK_NO_PRIV, OBJECT_TABLESPACE,
470472
tablespacename);
471473

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

src/include/catalog/dependency.h

Lines changed: 13 additions & 0 deletions
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

@@ -237,6 +244,12 @@ extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
237244
extern void changeDependencyOnOwner(Oid classId, Oid objectId,
238245
Oid newOwnerId);
239246

247+
extern void recordDependencyOnTablespace(Oid classId, Oid objectId,
248+
Oid tablespace);
249+
250+
extern void changeDependencyOnTablespace(Oid classId, Oid objectId,
251+
Oid newTablespaceId);
252+
240253
extern void updateAclDependencies(Oid classId, Oid objectId, int32 objectSubId,
241254
Oid ownerId,
242255
int noldmembers, Oid *oldmembers,

src/test/regress/input/tablespace.source

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

243+
-- Fail, in use for some partitioned object
244+
DROP TABLESPACE regress_tblspace;
245+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
243246
-- Fail, not empty
244247
DROP TABLESPACE regress_tblspace;
245248

src/test/regress/output/tablespace.source

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,11 @@ ERROR: directory "/no/such/location" does not exist
623623
-- No such tablespace
624624
CREATE TABLE bar (i int) TABLESPACE regress_nosuchspace;
625625
ERROR: tablespace "regress_nosuchspace" does not exist
626+
-- Fail, in use for some partitioned object
627+
DROP TABLESPACE regress_tblspace;
628+
ERROR: tablespace "regress_tblspace" cannot be dropped because some objects depend on it
629+
DETAIL: tablespace for index testschema.part_a_idx
630+
ALTER INDEX testschema.part_a_idx SET TABLESPACE pg_default;
626631
-- Fail, not empty
627632
DROP TABLESPACE regress_tblspace;
628633
ERROR: tablespace "regress_tblspace" is not empty

0 commit comments

Comments
 (0)