Skip to content

Commit dc19aaa

Browse files
committed
Give a more user-friendly error message in case where a table is created
in a schema whose default tablespace has been dropped.
1 parent 2a63c16 commit dc19aaa

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/backend/commands/tablecmds.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.134 2004/10/12 21:54:36 petere Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.135 2004/10/16 21:16:36 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -168,6 +168,7 @@ static void StoreCatalogInheritance(Oid relationId, List *supers);
168168
static int findAttrByName(const char *attributeName, List *schema);
169169
static void setRelhassubclassInRelation(Oid relationId, bool relhassubclass);
170170
static bool needs_toast_table(Relation rel);
171+
static void check_tablespace_exists(Oid tablespaceId, Oid namespaceId);
171172
static int transformColumnNameList(Oid relId, List *colList,
172173
int16 *attnums, Oid *atttypids);
173174
static int transformFkeyGetPrimaryKey(Relation pkrel, Oid *indexOid,
@@ -337,6 +338,9 @@ DefineRelation(CreateStmt *stmt, char relkind)
337338
{
338339
tablespaceId = get_namespace_tablespace(namespaceId);
339340
/* note no permission check on tablespace in this case */
341+
/* check to see that schema's tablespace still exists */
342+
if (OidIsValid(tablespaceId))
343+
check_tablespace_exists(tablespaceId, namespaceId);
340344
}
341345

342346
/*
@@ -5865,6 +5869,42 @@ needs_toast_table(Relation rel)
58655869
return (tuple_length > TOAST_TUPLE_THRESHOLD);
58665870
}
58675871

5872+
/*
5873+
* Verify that a schema's tablespace still exists
5874+
*
5875+
* We need this because DROP TABLESPACE cannot check whether the target
5876+
* tablespace is the default tablespace for any schemas. (It could check
5877+
* in the current database, but that doesn't seem very helpful.) Subsequent
5878+
* attempts to create tables in that tablespace will fail. This code just
5879+
* exists to ensure that we give a helpful error message.
5880+
*/
5881+
static void
5882+
check_tablespace_exists(Oid tablespaceId, Oid namespaceId)
5883+
{
5884+
Relation pg_tablespace;
5885+
ScanKeyData entry[1];
5886+
HeapScanDesc scan;
5887+
HeapTuple tuple;
5888+
5889+
/* There's no syscache for pg_tablespace, so must look the hard way */
5890+
pg_tablespace = heap_openr(TableSpaceRelationName, AccessShareLock);
5891+
ScanKeyInit(&entry[0],
5892+
ObjectIdAttributeNumber,
5893+
BTEqualStrategyNumber, F_OIDEQ,
5894+
ObjectIdGetDatum(tablespaceId));
5895+
scan = heap_beginscan(pg_tablespace, SnapshotNow, 1, entry);
5896+
tuple = heap_getnext(scan, ForwardScanDirection);
5897+
if (!HeapTupleIsValid(tuple))
5898+
ereport(ERROR,
5899+
(errcode(ERRCODE_UNDEFINED_OBJECT),
5900+
errmsg("tablespace with OID %u does not exist",
5901+
tablespaceId),
5902+
errdetail("The default tablespace for schema \"%s\" has been dropped.",
5903+
get_namespace_name(namespaceId))));
5904+
heap_endscan(scan);
5905+
heap_close(pg_tablespace, AccessShareLock);
5906+
}
5907+
58685908

58695909
/*
58705910
* This code supports

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy