Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9999f5a

Browse files
committed
Checking to decide whether relations are system relations now depends
on the namespace not the name; pg_ is not a reserved prefix for table names anymore. From Fernando Nasser.
1 parent 79b60cb commit 9999f5a

File tree

22 files changed

+197
-125
lines changed

22 files changed

+197
-125
lines changed

src/backend/catalog/aclchk.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.64 2002/04/11 19:59:56 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/aclchk.c,v 1.65 2002/04/12 20:38:17 tgl Exp $
1212
*
1313
* NOTES
1414
* See acl.h.
@@ -726,7 +726,6 @@ pg_class_aclcheck(Oid table_oid, Oid userid, AclMode mode)
726726
int32 result;
727727
bool usesuper,
728728
usecatupd;
729-
char *relname;
730729
HeapTuple tuple;
731730
Datum aclDatum;
732731
bool isNull;
@@ -761,9 +760,9 @@ pg_class_aclcheck(Oid table_oid, Oid userid, AclMode mode)
761760
* pg_shadow.usecatupd is set. (This is to let superusers protect
762761
* themselves from themselves.)
763762
*/
764-
relname = NameStr(((Form_pg_class) GETSTRUCT(tuple))->relname);
765763
if ((mode & (ACL_INSERT | ACL_UPDATE | ACL_DELETE)) &&
766-
!allowSystemTableMods && IsSystemRelationName(relname) &&
764+
!allowSystemTableMods &&
765+
IsSystemClass((Form_pg_class) GETSTRUCT(tuple)) &&
767766
!usecatupd)
768767
{
769768
#ifdef ACLDEBUG

src/backend/catalog/catalog.c

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/*-------------------------------------------------------------------------
22
*
33
* catalog.c
4+
* routines concerned with catalog naming conventions
45
*
56
*
67
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
78
* Portions Copyright (c) 1994, Regents of the University of California
89
*
910
*
1011
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/catalog.c,v 1.44 2001/11/16 23:30:35 tgl Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/catalog/catalog.c,v 1.45 2002/04/12 20:38:18 tgl Exp $
1213
*
1314
*-------------------------------------------------------------------------
1415
*/
@@ -18,9 +19,9 @@
1819
#include "access/transam.h"
1920
#include "catalog/catalog.h"
2021
#include "catalog/catname.h"
21-
#include "catalog/pg_type.h"
22+
#include "catalog/pg_namespace.h"
2223
#include "miscadmin.h"
23-
#include "utils/lsyscache.h"
24+
2425

2526
/*
2627
* relpath - construct path to a relation's file
@@ -74,54 +75,121 @@ GetDatabasePath(Oid tblNode)
7475

7576

7677
/*
77-
* IsSystemRelationName
78-
* True iff name is the name of a system catalog relation.
78+
* IsSystemRelation
79+
* True iff the relation is a system catalog relation.
7980
*
80-
* NB: TOAST relations are considered system relations by this test.
81+
* NB: TOAST relations are considered system relations by this test
82+
* for compatibility with the old IsSystemRelationName function.
8183
* This is appropriate in many places but not all. Where it's not,
82-
* also check IsToastRelationName.
84+
* also check IsToastRelation.
8385
*
84-
* We now make a new requirement where system catalog relns must begin
85-
* with pg_ while user relns are forbidden to do so. Make the test
86-
* trivial and instantaneous.
86+
* We now just test if the relation is in the system catalog namespace;
87+
* so it's no longer necessary to forbid user relations from having
88+
* names starting with pg_. Now only schema names have the pg_
89+
* distinction/requirement.
90+
*/
91+
bool
92+
IsSystemRelation(Relation relation)
93+
{
94+
return IsSystemNamespace(RelationGetNamespace(relation)) ||
95+
IsToastNamespace(RelationGetNamespace(relation));
96+
}
97+
98+
/*
99+
* IsSystemClass
100+
* Like the above, but takes a Form_pg_class as argument.
101+
* Used when we do not want to open the relation and have to
102+
* search pg_class directly.
103+
*/
104+
bool
105+
IsSystemClass(Form_pg_class reltuple)
106+
{
107+
Oid relnamespace = reltuple->relnamespace;
108+
109+
return IsSystemNamespace(relnamespace) ||
110+
IsToastNamespace(relnamespace);
111+
}
112+
113+
/*
114+
* IsToastRelation
115+
* True iff relation is a TOAST support relation (or index).
116+
*/
117+
bool
118+
IsToastRelation(Relation relation)
119+
{
120+
return IsToastNamespace(RelationGetNamespace(relation));
121+
}
122+
123+
/*
124+
* IsToastClass
125+
* Like the above, but takes a Form_pg_class as argument.
126+
* Used when we do not want to open the relation and have to
127+
* search pg_class directly.
128+
*/
129+
bool
130+
IsToastClass(Form_pg_class reltuple)
131+
{
132+
Oid relnamespace = reltuple->relnamespace;
133+
134+
return IsToastNamespace(relnamespace);
135+
}
136+
137+
/*
138+
* IsSystemNamespace
139+
* True iff namespace is pg_catalog.
87140
*
88-
* XXX this is way bogus. -- pma
141+
* NOTE: the reason this isn't a macro is to avoid having to include
142+
* catalog/pg_namespace.h in a lot of places.
89143
*/
90144
bool
91-
IsSystemRelationName(const char *relname)
145+
IsSystemNamespace(Oid namespaceId)
92146
{
93-
/* ugly coding for speed */
94-
return (relname[0] == 'p' &&
95-
relname[1] == 'g' &&
96-
relname[2] == '_');
147+
return namespaceId == PG_CATALOG_NAMESPACE;
148+
}
149+
150+
/*
151+
* IsToastNamespace
152+
* True iff namespace is pg_toast.
153+
*
154+
* NOTE: the reason this isn't a macro is to avoid having to include
155+
* catalog/pg_namespace.h in a lot of places.
156+
*/
157+
bool
158+
IsToastNamespace(Oid namespaceId)
159+
{
160+
return namespaceId == PG_TOAST_NAMESPACE;
97161
}
98162

163+
99164
/*
100-
* IsToastRelationName
101-
* True iff name is the name of a TOAST support relation (or index).
165+
* IsReservedName
166+
* True iff name starts with the pg_ prefix.
167+
*
168+
* For some classes of objects, the prefix pg_ is reserved
169+
* for system objects only.
102170
*/
103171
bool
104-
IsToastRelationName(const char *relname)
172+
IsReservedName(const char *name)
105173
{
106-
return strncmp(relname, "pg_toast_", 9) == 0;
174+
/* ugly coding for speed */
175+
return (name[0] == 'p' &&
176+
name[1] == 'g' &&
177+
name[2] == '_');
107178
}
108179

109180
/*
110181
* IsSharedSystemRelationName
111182
* True iff name is the name of a shared system catalog relation.
183+
*
184+
* Note: This function assumes that this is a system relation
185+
* in the first place. If that is not known, check the namespace
186+
* (with IsSystemNamespace) before calling this function.
112187
*/
113188
bool
114189
IsSharedSystemRelationName(const char *relname)
115190
{
116191
int i;
117192

118-
/*
119-
* Quick out: if it's not a system relation, it can't be a shared
120-
* system relation.
121-
*/
122-
if (!IsSystemRelationName(relname))
123-
return FALSE;
124-
125193
i = 0;
126194
while (SharedSystemRelationNames[i] != NULL)
127195
{
@@ -132,6 +200,7 @@ IsSharedSystemRelationName(const char *relname)
132200
return FALSE;
133201
}
134202

203+
135204
/*
136205
* newoid - returns a unique identifier across all catalogs.
137206
*

src/backend/catalog/heap.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.195 2002/04/11 19:59:56 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.196 2002/04/12 20:38:18 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -38,7 +38,6 @@
3838
#include "catalog/indexing.h"
3939
#include "catalog/pg_attrdef.h"
4040
#include "catalog/pg_inherits.h"
41-
#include "catalog/pg_namespace.h"
4241
#include "catalog/pg_relcheck.h"
4342
#include "catalog/pg_statistic.h"
4443
#include "catalog/pg_type.h"
@@ -224,10 +223,10 @@ heap_create(const char *relname,
224223
* sanity checks
225224
*/
226225
if (!allow_system_table_mods &&
227-
IsSystemRelationName(relname) &&
226+
(IsSystemNamespace(relnamespace) || IsToastNamespace(relnamespace)) &&
228227
IsNormalProcessingMode())
229-
elog(ERROR, "invalid relation name \"%s\"; "
230-
"the 'pg_' name prefix is reserved for system catalogs",
228+
elog(ERROR, "invalid relation \"%s\"; "
229+
"system catalog modifications are currently disallowed",
231230
relname);
232231

233232
/*
@@ -237,7 +236,7 @@ heap_create(const char *relname,
237236
* have to take special care for those rels that should be nailed
238237
* in cache and/or are shared across databases.
239238
*/
240-
if (relnamespace == PG_CATALOG_NAMESPACE)
239+
if (IsSystemNamespace(relnamespace))
241240
{
242241
if (strcmp(TypeRelationName, relname) == 0)
243242
{
@@ -1193,7 +1192,7 @@ heap_drop_with_catalog(Oid rid,
11931192
* prevent deletion of system relations
11941193
*/
11951194
if (!allow_system_table_mods &&
1196-
IsSystemRelationName(RelationGetRelationName(rel)))
1195+
IsSystemRelation(rel))
11971196
elog(ERROR, "System relation \"%s\" may not be dropped",
11981197
RelationGetRelationName(rel));
11991198

src/backend/catalog/index.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.175 2002/03/31 06:26:29 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.176 2002/04/12 20:38:19 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -33,7 +33,6 @@
3333
#include "catalog/index.h"
3434
#include "catalog/indexing.h"
3535
#include "catalog/pg_index.h"
36-
#include "catalog/pg_namespace.h"
3736
#include "catalog/pg_opclass.h"
3837
#include "catalog/pg_proc.h"
3938
#include "catalog/pg_type.h"
@@ -320,7 +319,7 @@ ConstructIndexReldesc(Relation indexRelation, Oid amoid)
320319
indexRelation->rd_rel->relowner = GetUserId();
321320
indexRelation->rd_rel->relam = amoid;
322321
indexRelation->rd_rel->relisshared =
323-
(RelationGetNamespace(indexRelation) == PG_CATALOG_NAMESPACE) &&
322+
IsSystemNamespace(RelationGetNamespace(indexRelation)) &&
324323
IsSharedSystemRelationName(RelationGetRelationName(indexRelation));
325324
indexRelation->rd_rel->relkind = RELKIND_INDEX;
326325
indexRelation->rd_rel->relhasoids = false;
@@ -582,7 +581,7 @@ index_create(Oid heapRelationId,
582581
elog(ERROR, "must index at least one column");
583582

584583
if (!allow_system_table_mods &&
585-
IsSystemRelationName(RelationGetRelationName(heapRelation)) &&
584+
IsSystemRelation(heapRelation) &&
586585
IsNormalProcessingMode())
587586
elog(ERROR, "User-defined indexes on system catalogs are not supported");
588587

@@ -1221,7 +1220,7 @@ setNewRelfilenode(Relation relation)
12211220
Buffer buffer;
12221221
RelationData workrel;
12231222

1224-
Assert(!IsSystemRelationName(NameStr(relation->rd_rel->relname)) || relation->rd_rel->relkind == RELKIND_INDEX);
1223+
Assert(!IsSystemRelation(relation) || relation->rd_rel->relkind == RELKIND_INDEX);
12251224

12261225
pg_class = heap_openr(RelationRelationName, RowExclusiveLock);
12271226
/* Fetch and lock the classTuple associated with this relation */
@@ -1912,7 +1911,7 @@ reindex_relation(Oid relid, bool force)
19121911
* ignore the indexes of the target system relation while processing
19131912
* reindex.
19141913
*/
1915-
if (!IsIgnoringSystemIndexes() && IsSystemRelationName(NameStr(rel->rd_rel->relname)))
1914+
if (!IsIgnoringSystemIndexes() && IsSystemRelation(rel))
19161915
deactivate_needed = true;
19171916
#ifndef ENABLE_REINDEX_NAILED_RELATIONS
19181917

src/backend/catalog/namespace.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.7 2002/04/09 20:35:47 tgl Exp $
16+
* $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.8 2002/04/12 20:38:19 tgl Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
2020
#include "postgres.h"
2121

2222
#include "access/heapam.h"
2323
#include "access/xact.h"
24+
#include "catalog/catalog.h"
2425
#include "catalog/catname.h"
2526
#include "catalog/heap.h"
2627
#include "catalog/namespace.h"
@@ -391,7 +392,7 @@ FuncnameGetCandidates(List *names, int nargs)
391392
{
392393
/* Consider only procs that are in the search path */
393394
if (pathContainsSystemNamespace ||
394-
procform->pronamespace != PG_CATALOG_NAMESPACE)
395+
!IsSystemNamespace(procform->pronamespace))
395396
{
396397
List *nsp;
397398

src/backend/commands/analyze.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.30 2002/04/02 01:03:05 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/analyze.c,v 1.31 2002/04/12 20:38:20 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -18,9 +18,9 @@
1818

1919
#include "access/heapam.h"
2020
#include "access/tuptoaster.h"
21+
#include "catalog/catalog.h"
2122
#include "catalog/catname.h"
2223
#include "catalog/indexing.h"
23-
#include "catalog/pg_namespace.h"
2424
#include "catalog/pg_operator.h"
2525
#include "catalog/pg_statistic.h"
2626
#include "catalog/pg_type.h"
@@ -218,7 +218,7 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
218218
/*
219219
* We can ANALYZE any table except pg_statistic. See update_attstats
220220
*/
221-
if (RelationGetNamespace(onerel) == PG_CATALOG_NAMESPACE &&
221+
if (IsSystemNamespace(RelationGetNamespace(onerel)) &&
222222
strcmp(RelationGetRelationName(onerel), StatisticRelationName) == 0)
223223
{
224224
relation_close(onerel, AccessShareLock);

0 commit comments

Comments
 (0)