Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5b01a6f

Browse files
committed
Prevent drop of tablespaces used by partitioned relations
When a tablespace is used in a partitioned relation (per commitsca41030 in pg12 for tables and33e6c34 in pg11 for indexes),it is possible to drop the tablespace, potentially causing variousproblems. One such was reported in bug #16577, where a rewriting ALTERTABLE causes a server crash.Protect against this by using pg_shdepend to keep track of tablespaceswhen used for relations that don't keep physical files; we now abort atablespace if we see that the tablespace is referenced from anypartitioned relations.Backpatch this to 11, where this problem has been latent all along. Wedon't try to create pg_shdepend entries for existing partitionedindexes/tables, but any ones that are modified going forward will beprotected.Note slight behavior change: when trying to drop a tablespace thatcontains both regular tables as well as partitioned ones, you'dpreviously get ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE and now you'llget ERRCODE_DEPENDENT_OBJECTS_STILL_EXIST. Arguably, the latter is morecorrect.It is possible to add protecting pg_shdepend entries for existingtables/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 defaulttablespace. Because these partitioned objects do not have storage, nofile needs to be actually moved, so it shouldn't take more time thanwhat'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 <> 0Reported-by: Alexander Lakhin <exclusion@gmail.com>Discussion:https://postgr.es/m/16577-881633a9f9894fd5@postgresql.orgAuthor: Álvaro Herrera <alvherre@alvh.no-ip.org>Reviewed-by: Michael Paquier <michael@paquier.xyz>
1 parent8523a09 commit5b01a6f

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
@@ -6825,10 +6825,21 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
68256825
</para>
68266826
</listitem>
68276827
</varlistentry>
6828+
6829+
<varlistentry>
6830+
<term><symbol>SHARED_DEPENDENCY_TABLESPACE</symbol> (<literal>t</literal>)</term>
6831+
<listitem>
6832+
<para>
6833+
The referenced object (which must be a tablespace) is mentioned as
6834+
the tablespace for a relation that doesn't have storage.
6835+
</para>
6836+
</listitem>
6837+
</varlistentry>
68286838
</variablelist>
68296839

68306840
Other dependency flavors might be needed in future. Note in particular
6831-
that the current definition only supports roles as referenced objects.
6841+
that the current definition only supports roles and tablespaces as referenced
6842+
objects.
68326843
</para>
68336844

68346845
</sect1>

‎src/backend/catalog/heap.c

Lines changed: 9 additions & 0 deletions
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
returnrel;
444453
}
445454

‎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"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(OidclassId,OidobjectId,Oidtablespace)
362+
{
363+
ObjectAddressmyself,
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(OidclassId,OidobjectId,OidnewTablespaceId)
383+
{
384+
RelationsdepRel;
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.
@@ -1083,13 +1139,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
10831139
objectId)));
10841140
break;
10851141

1086-
/*
1087-
* Currently, this routine need not support any other shared
1088-
* object types besides roles. If we wanted to record explicit
1089-
* dependencies on databases or tablespaces, we'd need code along
1090-
* these lines:
1091-
*/
1092-
#ifdefNOT_USED
10931142
caseTableSpaceRelationId:
10941143
{
10951144
/* For lack of a syscache on pg_tablespace, do this: */
@@ -1103,7 +1152,6 @@ shdepLockAndCheckObject(Oid classId, Oid objectId)
11031152
pfree(tablespace);
11041153
break;
11051154
}
1106-
#endif
11071155

11081156
caseDatabaseRelationId:
11091157
{
@@ -1163,6 +1211,8 @@ storeObjectDescription(StringInfo descs,
11631211
appendStringInfo(descs,_("privileges for %s"),objdesc);
11641212
elseif (deptype==SHARED_DEPENDENCY_POLICY)
11651213
appendStringInfo(descs,_("target of %s"),objdesc);
1214+
elseif (deptype==SHARED_DEPENDENCY_TABLESPACE)
1215+
appendStringInfo(descs,_("tablespace for %s"),objdesc);
11661216
else
11671217
elog(ERROR,"unrecognized dependency type: %d",
11681218
(int)deptype);

‎src/backend/commands/tablecmds.c

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

13319+
/* Record dependency on tablespace */
13320+
changeDependencyOnTablespace(RelationRelationId,
13321+
reloid, rd_rel->reltablespace);
13322+
1331913323
InvokeObjectPostAlterHook(RelationRelationId, reloid, 0);
1332013324

1332113325
heap_freetuple(tuple);

‎src/backend/commands/tablespace.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,8 @@ DropTableSpace(DropTableSpaceStmt *stmt)
420420
Form_pg_tablespacespcform;
421421
ScanKeyDataentry[1];
422422
Oidtablespaceoid;
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

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

@@ -241,6 +248,12 @@ extern void recordDependencyOnOwner(Oid classId, Oid objectId, Oid owner);
241248
externvoidchangeDependencyOnOwner(OidclassId,OidobjectId,
242249
OidnewOwnerId);
243250

251+
externvoidrecordDependencyOnTablespace(OidclassId,OidobjectId,
252+
Oidtablespace);
253+
254+
externvoidchangeDependencyOnTablespace(OidclassId,OidobjectId,
255+
OidnewTablespaceId);
256+
244257
externvoidupdateAclDependencies(OidclassId,OidobjectId,int32objectSubId,
245258
OidownerId,
246259
intnoldmembers,Oid*oldmembers,

‎src/test/regress/input/tablespace.source

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

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

[8]ページ先頭

©2009-2025 Movatter.jp