Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * catalog.c
4 : * routines concerned with catalog naming conventions and other
5 : * bits of hard-wired knowledge
6 : *
7 : *
8 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
9 : * Portions Copyright (c) 1994, Regents of the University of California
10 : *
11 : *
12 : * IDENTIFICATION
13 : * src/backend/catalog/catalog.c
14 : *
15 : *-------------------------------------------------------------------------
16 : */
17 :
18 : #include "postgres.h"
19 :
20 : #include <fcntl.h>
21 : #include <unistd.h>
22 :
23 : #include "access/genam.h"
24 : #include "access/htup_details.h"
25 : #include "access/table.h"
26 : #include "access/transam.h"
27 : #include "catalog/catalog.h"
28 : #include "catalog/namespace.h"
29 : #include "catalog/pg_auth_members.h"
30 : #include "catalog/pg_authid.h"
31 : #include "catalog/pg_database.h"
32 : #include "catalog/pg_db_role_setting.h"
33 : #include "catalog/pg_largeobject.h"
34 : #include "catalog/pg_namespace.h"
35 : #include "catalog/pg_parameter_acl.h"
36 : #include "catalog/pg_replication_origin.h"
37 : #include "catalog/pg_seclabel.h"
38 : #include "catalog/pg_shdepend.h"
39 : #include "catalog/pg_shdescription.h"
40 : #include "catalog/pg_shseclabel.h"
41 : #include "catalog/pg_subscription.h"
42 : #include "catalog/pg_tablespace.h"
43 : #include "catalog/pg_type.h"
44 : #include "miscadmin.h"
45 : #include "utils/fmgroids.h"
46 : #include "utils/fmgrprotos.h"
47 : #include "utils/rel.h"
48 : #include "utils/snapmgr.h"
49 : #include "utils/syscache.h"
50 :
51 : /*
52 : * Parameters to determine when to emit a log message in
53 : * GetNewOidWithIndex()
54 : */
55 : #define GETNEWOID_LOG_THRESHOLD 1000000
56 : #define GETNEWOID_LOG_MAX_INTERVAL 128000000
57 :
58 : /*
59 : * IsSystemRelation
60 : * True iff the relation is either a system catalog or a toast table.
61 : * See IsCatalogRelation for the exact definition of a system catalog.
62 : *
63 : * We treat toast tables of user relations as "system relations" for
64 : * protection purposes, e.g. you can't change their schemas without
65 : * special permissions. Therefore, most uses of this function are
66 : * checking whether allow_system_table_mods restrictions apply.
67 : * For other purposes, consider whether you shouldn't be using
68 : * IsCatalogRelation instead.
69 : *
70 : * This function does not perform any catalog accesses.
71 : * Some callers rely on that!
72 : */
73 : bool
74 283862 : IsSystemRelation(Relation relation)
75 : {
76 283862 : return IsSystemClass(RelationGetRelid(relation), relation->rd_rel);
77 : }
78 :
79 : /*
80 : * IsSystemClass
81 : * Like the above, but takes a Form_pg_class as argument.
82 : * Used when we do not want to open the relation and have to
83 : * search pg_class directly.
84 : */
85 : bool
86 1211708 : IsSystemClass(Oid relid, Form_pg_class reltuple)
87 : {
88 : /* IsCatalogRelationOid is a bit faster, so test that first */
89 1211708 : return (IsCatalogRelationOid(relid) || IsToastClass(reltuple));
90 : }
91 :
92 : /*
93 : * IsCatalogRelation
94 : * True iff the relation is a system catalog.
95 : *
96 : * By a system catalog, we mean one that is created during the bootstrap
97 : * phase of initdb. That includes not just the catalogs per se, but
98 : * also their indexes, and TOAST tables and indexes if any.
99 : *
100 : * This function does not perform any catalog accesses.
101 : * Some callers rely on that!
102 : */
103 : bool
104 56296074 : IsCatalogRelation(Relation relation)
105 : {
106 56296074 : return IsCatalogRelationOid(RelationGetRelid(relation));
107 : }
108 :
109 : /*
110 : * IsCatalogRelationOid
111 : * True iff the relation identified by this OID is a system catalog.
112 : *
113 : * By a system catalog, we mean one that is created during the bootstrap
114 : * phase of initdb. That includes not just the catalogs per se, but
115 : * also their indexes, and TOAST tables and indexes if any.
116 : *
117 : * This function does not perform any catalog accesses.
118 : * Some callers rely on that!
119 : */
120 : bool
121 99212338 : IsCatalogRelationOid(Oid relid)
122 : {
123 : /*
124 : * We consider a relation to be a system catalog if it has a pinned OID.
125 : * This includes all the defined catalogs, their indexes, and their TOAST
126 : * tables and indexes.
127 : *
128 : * This rule excludes the relations in information_schema, which are not
129 : * integral to the system and can be treated the same as user relations.
130 : * (Since it's valid to drop and recreate information_schema, any rule
131 : * that did not act this way would be wrong.)
132 : *
133 : * This test is reliable since an OID wraparound will skip this range of
134 : * OIDs; see GetNewObjectId().
135 : */
136 99212338 : return (relid < (Oid) FirstUnpinnedObjectId);
137 : }
138 :
139 : /*
140 : * IsCatalogTextUniqueIndexOid
141 : * True iff the relation identified by this OID is a catalog UNIQUE index
142 : * having a column of type "text".
143 : *
144 : * The relcache must not use these indexes. Inserting into any UNIQUE
145 : * index compares index keys while holding BUFFER_LOCK_EXCLUSIVE.
146 : * bttextcmp() can search the COLLOID catcache. Depending on concurrent
147 : * invalidation traffic, catcache can reach relcache builds. A backend
148 : * would self-deadlock on LWLocks if the relcache build read the
149 : * exclusive-locked buffer.
150 : *
151 : * To avoid being itself the cause of self-deadlock, this doesn't read
152 : * catalogs. Instead, it uses a hard-coded list with a supporting
153 : * regression test.
154 : */
155 : bool
156 1240 : IsCatalogTextUniqueIndexOid(Oid relid)
157 : {
158 1240 : switch (relid)
159 : {
160 24 : case ParameterAclParnameIndexId:
161 : case ReplicationOriginNameIndex:
162 : case SecLabelObjectIndexId:
163 : case SharedSecLabelObjectIndexId:
164 24 : return true;
165 : }
166 1216 : return false;
167 : }
168 :
169 : /*
170 : * IsInplaceUpdateRelation
171 : * True iff core code performs inplace updates on the relation.
172 : *
173 : * This is used for assertions and for making the executor follow the
174 : * locking protocol described at README.tuplock section "Locking to write
175 : * inplace-updated tables". Extensions may inplace-update other heap
176 : * tables, but concurrent SQL UPDATE on the same table may overwrite
177 : * those modifications.
178 : *
179 : * The executor can assume these are not partitions or partitioned and
180 : * have no triggers.
181 : */
182 : bool
183 442308 : IsInplaceUpdateRelation(Relation relation)
184 : {
185 442308 : return IsInplaceUpdateOid(RelationGetRelid(relation));
186 : }
187 :
188 : /*
189 : * IsInplaceUpdateOid
190 : * Like the above, but takes an OID as argument.
191 : */
192 : bool
193 442308 : IsInplaceUpdateOid(Oid relid)
194 : {
195 442308 : return (relid == RelationRelationId ||
196 : relid == DatabaseRelationId);
197 : }
198 :
199 : /*
200 : * IsToastRelation
201 : * True iff relation is a TOAST support relation (or index).
202 : *
203 : * Does not perform any catalog accesses.
204 : */
205 : bool
206 5407792 : IsToastRelation(Relation relation)
207 : {
208 : /*
209 : * What we actually check is whether the relation belongs to a pg_toast
210 : * namespace. This should be equivalent because of restrictions that are
211 : * enforced elsewhere against creating user relations in, or moving
212 : * relations into/out of, a pg_toast namespace. Notice also that this
213 : * will not say "true" for toast tables belonging to other sessions' temp
214 : * tables; we expect that other mechanisms will prevent access to those.
215 : */
216 5407792 : return IsToastNamespace(RelationGetNamespace(relation));
217 : }
218 :
219 : /*
220 : * IsToastClass
221 : * Like the above, but takes a Form_pg_class as argument.
222 : * Used when we do not want to open the relation and have to
223 : * search pg_class directly.
224 : */
225 : bool
226 1137568 : IsToastClass(Form_pg_class reltuple)
227 : {
228 1137568 : Oid relnamespace = reltuple->relnamespace;
229 :
230 1137568 : return IsToastNamespace(relnamespace);
231 : }
232 :
233 : /*
234 : * IsCatalogNamespace
235 : * True iff namespace is pg_catalog.
236 : *
237 : * Does not perform any catalog accesses.
238 : *
239 : * NOTE: the reason this isn't a macro is to avoid having to include
240 : * catalog/pg_namespace.h in a lot of places.
241 : */
242 : bool
243 216068 : IsCatalogNamespace(Oid namespaceId)
244 : {
245 216068 : return namespaceId == PG_CATALOG_NAMESPACE;
246 : }
247 :
248 : /*
249 : * IsToastNamespace
250 : * True iff namespace is pg_toast or my temporary-toast-table namespace.
251 : *
252 : * Does not perform any catalog accesses.
253 : *
254 : * Note: this will return false for temporary-toast-table namespaces belonging
255 : * to other backends. Those are treated the same as other backends' regular
256 : * temp table namespaces, and access is prevented where appropriate.
257 : * If you need to check for those, you may be able to use isAnyTempNamespace,
258 : * but beware that that does involve a catalog access.
259 : */
260 : bool
261 6640582 : IsToastNamespace(Oid namespaceId)
262 : {
263 13185270 : return (namespaceId == PG_TOAST_NAMESPACE) ||
264 6544688 : isTempToastNamespace(namespaceId);
265 : }
266 :
267 :
268 : /*
269 : * IsReservedName
270 : * True iff name starts with the pg_ prefix.
271 : *
272 : * For some classes of objects, the prefix pg_ is reserved for
273 : * system objects only. As of 8.0, this was only true for
274 : * schema and tablespace names. With 9.6, this is also true
275 : * for roles.
276 : */
277 : bool
278 3458 : IsReservedName(const char *name)
279 : {
280 : /* ugly coding for speed */
281 3528 : return (name[0] == 'p' &&
282 3476 : name[1] == 'g' &&
283 18 : name[2] == '_');
284 : }
285 :
286 :
287 : /*
288 : * IsSharedRelation
289 : * Given the OID of a relation, determine whether it's supposed to be
290 : * shared across an entire database cluster.
291 : *
292 : * In older releases, this had to be hard-wired so that we could compute the
293 : * locktag for a relation and lock it before examining its catalog entry.
294 : * Since we now have MVCC catalog access, the race conditions that made that
295 : * a hard requirement are gone, so we could look at relaxing this restriction.
296 : * However, if we scanned the pg_class entry to find relisshared, and only
297 : * then locked the relation, pg_class could get updated in the meantime,
298 : * forcing us to scan the relation again, which would definitely be complex
299 : * and might have undesirable performance consequences. Fortunately, the set
300 : * of shared relations is fairly static, so a hand-maintained list of their
301 : * OIDs isn't completely impractical.
302 : */
303 : bool
304 40383516 : IsSharedRelation(Oid relationId)
305 : {
306 : /* These are the shared catalogs (look for BKI_SHARED_RELATION) */
307 40383516 : if (relationId == AuthIdRelationId ||
308 40158522 : relationId == AuthMemRelationId ||
309 39991382 : relationId == DatabaseRelationId ||
310 39946782 : relationId == DbRoleSettingRelationId ||
311 39923096 : relationId == ParameterAclRelationId ||
312 39892842 : relationId == ReplicationOriginRelationId ||
313 39443768 : relationId == SharedDependRelationId ||
314 39437972 : relationId == SharedDescriptionRelationId ||
315 39427834 : relationId == SharedSecLabelRelationId ||
316 39392590 : relationId == SubscriptionRelationId ||
317 : relationId == TableSpaceRelationId)
318 1043314 : return true;
319 : /* These are their indexes */
320 39340202 : if (relationId == AuthIdOidIndexId ||
321 39227970 : relationId == AuthIdRolnameIndexId ||
322 39211624 : relationId == AuthMemMemRoleIndexId ||
323 39202640 : relationId == AuthMemRoleMemIndexId ||
324 39198752 : relationId == AuthMemOidIndexId ||
325 39196090 : relationId == AuthMemGrantorIndexId ||
326 39156402 : relationId == DatabaseNameIndexId ||
327 39071510 : relationId == DatabaseOidIndexId ||
328 38933014 : relationId == DbRoleSettingDatidRolidIndexId ||
329 38927506 : relationId == ParameterAclOidIndexId ||
330 38922068 : relationId == ParameterAclParnameIndexId ||
331 38914190 : relationId == ReplicationOriginIdentIndex ||
332 38906576 : relationId == ReplicationOriginNameIndex ||
333 38671424 : relationId == SharedDependDependerIndexId ||
334 38658694 : relationId == SharedDependReferenceIndexId ||
335 38654506 : relationId == SharedDescriptionObjIndexId ||
336 38645978 : relationId == SharedSecLabelObjectIndexId ||
337 38638164 : relationId == SubscriptionNameIndexId ||
338 38629648 : relationId == SubscriptionObjectIndexId ||
339 38625844 : relationId == TablespaceNameIndexId ||
340 : relationId == TablespaceOidIndexId)
341 740322 : return true;
342 : /* These are their toast tables and toast indexes */
343 38599880 : if (relationId == PgDatabaseToastTable ||
344 38596562 : relationId == PgDatabaseToastIndex ||
345 38594972 : relationId == PgDbRoleSettingToastTable ||
346 38593664 : relationId == PgDbRoleSettingToastIndex ||
347 38592074 : relationId == PgParameterAclToastTable ||
348 38590766 : relationId == PgParameterAclToastIndex ||
349 38589176 : relationId == PgReplicationOriginToastTable ||
350 38587868 : relationId == PgReplicationOriginToastIndex ||
351 38586216 : relationId == PgShdescriptionToastTable ||
352 38584850 : relationId == PgShdescriptionToastIndex ||
353 38583260 : relationId == PgShseclabelToastTable ||
354 38581952 : relationId == PgShseclabelToastIndex ||
355 38580362 : relationId == PgSubscriptionToastTable ||
356 38579054 : relationId == PgSubscriptionToastIndex ||
357 38577414 : relationId == PgTablespaceToastTable ||
358 : relationId == PgTablespaceToastIndex)
359 23824 : return true;
360 38576056 : return false;
361 : }
362 :
363 : /*
364 : * IsPinnedObject
365 : * Given the class + OID identity of a database object, report whether
366 : * it is "pinned", that is not droppable because the system requires it.
367 : *
368 : * We used to represent this explicitly in pg_depend, but that proved to be
369 : * an undesirable amount of overhead, so now we rely on an OID range test.
370 : */
371 : bool
372 2584180 : IsPinnedObject(Oid classId, Oid objectId)
373 : {
374 : /*
375 : * Objects with OIDs above FirstUnpinnedObjectId are never pinned. Since
376 : * the OID generator skips this range when wrapping around, this check
377 : * guarantees that user-defined objects are never considered pinned.
378 : */
379 2584180 : if (objectId >= FirstUnpinnedObjectId)
380 756082 : return false;
381 :
382 : /*
383 : * Large objects are never pinned. We need this special case because
384 : * their OIDs can be user-assigned.
385 : */
386 1828098 : if (classId == LargeObjectRelationId)
387 54 : return false;
388 :
389 : /*
390 : * There are a few objects defined in the catalog .dat files that, as a
391 : * matter of policy, we prefer not to treat as pinned. We used to handle
392 : * that by excluding them from pg_depend, but it's just as easy to
393 : * hard-wire their OIDs here. (If the user does indeed drop and recreate
394 : * them, they'll have new but certainly-unpinned OIDs, so no problem.)
395 : *
396 : * Checking both classId and objectId is overkill, since OIDs below
397 : * FirstGenbkiObjectId should be globally unique, but do it anyway for
398 : * robustness.
399 : */
400 :
401 : /* the public namespace is not pinned */
402 1828044 : if (classId == NamespaceRelationId &&
403 : objectId == PG_PUBLIC_NAMESPACE)
404 58454 : return false;
405 :
406 : /*
407 : * Databases are never pinned. It might seem that it'd be prudent to pin
408 : * at least template0; but we do this intentionally so that template0 and
409 : * template1 can be rebuilt from each other, thus letting them serve as
410 : * mutual backups (as long as you've not modified template1, anyway).
411 : */
412 1769590 : if (classId == DatabaseRelationId)
413 0 : return false;
414 :
415 : /*
416 : * All other initdb-created objects are pinned. This is overkill (the
417 : * system doesn't really depend on having every last weird datatype, for
418 : * instance) but generating only the minimum required set of dependencies
419 : * seems hard, and enforcing an accurate list would be much more expensive
420 : * than the simple range test used here.
421 : */
422 1769590 : return true;
423 : }
424 :
425 :
426 : /*
427 : * GetNewOidWithIndex
428 : * Generate a new OID that is unique within the system relation.
429 : *
430 : * Since the OID is not immediately inserted into the table, there is a
431 : * race condition here; but a problem could occur only if someone else
432 : * managed to cycle through 2^32 OIDs and generate the same OID before we
433 : * finish inserting our row. This seems unlikely to be a problem. Note
434 : * that if we had to *commit* the row to end the race condition, the risk
435 : * would be rather higher; therefore we use SnapshotAny in the test, so that
436 : * we will see uncommitted rows. (We used to use SnapshotDirty, but that has
437 : * the disadvantage that it ignores recently-deleted rows, creating a risk
438 : * of transient conflicts for as long as our own MVCC snapshots think a
439 : * recently-deleted row is live. The risk is far higher when selecting TOAST
440 : * OIDs, because SnapshotToast considers dead rows as active indefinitely.)
441 : *
442 : * Note that we are effectively assuming that the table has a relatively small
443 : * number of entries (much less than 2^32) and there aren't very long runs of
444 : * consecutive existing OIDs. This is a mostly reasonable assumption for
445 : * system catalogs.
446 : *
447 : * Caller must have a suitable lock on the relation.
448 : */
449 : Oid
450 971482 : GetNewOidWithIndex(Relation relation, Oid indexId, AttrNumber oidcolumn)
451 : {
452 : Oid newOid;
453 : SysScanDesc scan;
454 : ScanKeyData key;
455 : bool collides;
456 971482 : uint64 retries = 0;
457 971482 : uint64 retries_before_log = GETNEWOID_LOG_THRESHOLD;
458 :
459 : /* Only system relations are supported */
460 : Assert(IsSystemRelation(relation));
461 :
462 : /* In bootstrap mode, we don't have any indexes to use */
463 971482 : if (IsBootstrapProcessingMode())
464 11270 : return GetNewObjectId();
465 :
466 : /*
467 : * We should never be asked to generate a new pg_type OID during
468 : * pg_upgrade; doing so would risk collisions with the OIDs it wants to
469 : * assign. Hitting this assert means there's some path where we failed to
470 : * ensure that a type OID is determined by commands in the dump script.
471 : */
472 : Assert(!IsBinaryUpgrade || RelationGetRelid(relation) != TypeRelationId);
473 :
474 : /* Generate new OIDs until we find one not in the table */
475 : do
476 : {
477 960212 : CHECK_FOR_INTERRUPTS();
478 :
479 960212 : newOid = GetNewObjectId();
480 :
481 960212 : ScanKeyInit(&key,
482 : oidcolumn,
483 : BTEqualStrategyNumber, F_OIDEQ,
484 : ObjectIdGetDatum(newOid));
485 :
486 : /* see notes above about using SnapshotAny */
487 960212 : scan = systable_beginscan(relation, indexId, true,
488 : SnapshotAny, 1, &key);
489 :
490 960212 : collides = HeapTupleIsValid(systable_getnext(scan));
491 :
492 960212 : systable_endscan(scan);
493 :
494 : /*
495 : * Log that we iterate more than GETNEWOID_LOG_THRESHOLD but have not
496 : * yet found OID unused in the relation. Then repeat logging with
497 : * exponentially increasing intervals until we iterate more than
498 : * GETNEWOID_LOG_MAX_INTERVAL. Finally repeat logging every
499 : * GETNEWOID_LOG_MAX_INTERVAL unless an unused OID is found. This
500 : * logic is necessary not to fill up the server log with the similar
501 : * messages.
502 : */
503 960212 : if (retries >= retries_before_log)
504 : {
505 0 : ereport(LOG,
506 : (errmsg("still searching for an unused OID in relation \"%s\"",
507 : RelationGetRelationName(relation)),
508 : errdetail_plural("OID candidates have been checked %" PRIu64 " time, but no unused OID has been found yet.",
509 : "OID candidates have been checked %" PRIu64 " times, but no unused OID has been found yet.",
510 : retries,
511 : retries)));
512 :
513 : /*
514 : * Double the number of retries to do before logging next until it
515 : * reaches GETNEWOID_LOG_MAX_INTERVAL.
516 : */
517 0 : if (retries_before_log * 2 <= GETNEWOID_LOG_MAX_INTERVAL)
518 0 : retries_before_log *= 2;
519 : else
520 0 : retries_before_log += GETNEWOID_LOG_MAX_INTERVAL;
521 : }
522 :
523 960212 : retries++;
524 960212 : } while (collides);
525 :
526 : /*
527 : * If at least one log message is emitted, also log the completion of OID
528 : * assignment.
529 : */
530 960212 : if (retries > GETNEWOID_LOG_THRESHOLD)
531 : {
532 0 : ereport(LOG,
533 : (errmsg_plural("new OID has been assigned in relation \"%s\" after %" PRIu64 " retry",
534 : "new OID has been assigned in relation \"%s\" after %" PRIu64 " retries",
535 : retries,
536 : RelationGetRelationName(relation), retries)));
537 : }
538 :
539 960212 : return newOid;
540 : }
541 :
542 : /*
543 : * GetNewRelFileNumber
544 : * Generate a new relfilenumber that is unique within the
545 : * database of the given tablespace.
546 : *
547 : * If the relfilenumber will also be used as the relation's OID, pass the
548 : * opened pg_class catalog, and this routine will guarantee that the result
549 : * is also an unused OID within pg_class. If the result is to be used only
550 : * as a relfilenumber for an existing relation, pass NULL for pg_class.
551 : *
552 : * As with GetNewOidWithIndex(), there is some theoretical risk of a race
553 : * condition, but it doesn't seem worth worrying about.
554 : *
555 : * Note: we don't support using this in bootstrap mode. All relations
556 : * created by bootstrap have preassigned OIDs, so there's no need.
557 : */
558 : RelFileNumber
559 123512 : GetNewRelFileNumber(Oid reltablespace, Relation pg_class, char relpersistence)
560 : {
561 : RelFileLocatorBackend rlocator;
562 : RelPathStr rpath;
563 : bool collides;
564 : ProcNumber procNumber;
565 :
566 : /*
567 : * If we ever get here during pg_upgrade, there's something wrong; all
568 : * relfilenumber assignments during a binary-upgrade run should be
569 : * determined by commands in the dump script.
570 : */
571 : Assert(!IsBinaryUpgrade);
572 :
573 123512 : switch (relpersistence)
574 : {
575 6712 : case RELPERSISTENCE_TEMP:
576 6712 : procNumber = ProcNumberForTempRelations();
577 6712 : break;
578 116800 : case RELPERSISTENCE_UNLOGGED:
579 : case RELPERSISTENCE_PERMANENT:
580 116800 : procNumber = INVALID_PROC_NUMBER;
581 116800 : break;
582 0 : default:
583 0 : elog(ERROR, "invalid relpersistence: %c", relpersistence);
584 : return InvalidRelFileNumber; /* placate compiler */
585 : }
586 :
587 : /* This logic should match RelationInitPhysicalAddr */
588 123512 : rlocator.locator.spcOid = reltablespace ? reltablespace : MyDatabaseTableSpace;
589 123512 : rlocator.locator.dbOid =
590 123512 : (rlocator.locator.spcOid == GLOBALTABLESPACE_OID) ?
591 123512 : InvalidOid : MyDatabaseId;
592 :
593 : /*
594 : * The relpath will vary based on the backend number, so we must
595 : * initialize that properly here to make sure that any collisions based on
596 : * filename are properly detected.
597 : */
598 123512 : rlocator.backend = procNumber;
599 :
600 : do
601 : {
602 123512 : CHECK_FOR_INTERRUPTS();
603 :
604 : /* Generate the OID */
605 123512 : if (pg_class)
606 108556 : rlocator.locator.relNumber = GetNewOidWithIndex(pg_class, ClassOidIndexId,
607 : Anum_pg_class_oid);
608 : else
609 14956 : rlocator.locator.relNumber = GetNewObjectId();
610 :
611 : /* Check for existing file of same name */
612 123512 : rpath = relpath(rlocator, MAIN_FORKNUM);
613 :
614 123512 : if (access(rpath.str, F_OK) == 0)
615 : {
616 : /* definite collision */
617 0 : collides = true;
618 : }
619 : else
620 : {
621 : /*
622 : * Here we have a little bit of a dilemma: if errno is something
623 : * other than ENOENT, should we declare a collision and loop? In
624 : * practice it seems best to go ahead regardless of the errno. If
625 : * there is a colliding file we will get an smgr failure when we
626 : * attempt to create the new relation file.
627 : */
628 123512 : collides = false;
629 : }
630 123512 : } while (collides);
631 :
632 123512 : return rlocator.locator.relNumber;
633 : }
634 :
635 : /*
636 : * SQL callable interface for GetNewOidWithIndex(). Outside of initdb's
637 : * direct insertions into catalog tables, and recovering from corruption, this
638 : * should rarely be needed.
639 : *
640 : * Function is intentionally not documented in the user facing docs.
641 : */
642 : Datum
643 0 : pg_nextoid(PG_FUNCTION_ARGS)
644 : {
645 0 : Oid reloid = PG_GETARG_OID(0);
646 0 : Name attname = PG_GETARG_NAME(1);
647 0 : Oid idxoid = PG_GETARG_OID(2);
648 : Relation rel;
649 : Relation idx;
650 : HeapTuple atttuple;
651 : Form_pg_attribute attform;
652 : AttrNumber attno;
653 : Oid newoid;
654 :
655 : /*
656 : * As this function is not intended to be used during normal running, and
657 : * only supports system catalogs (which require superuser permissions to
658 : * modify), just checking for superuser ought to not obstruct valid
659 : * usecases.
660 : */
661 0 : if (!superuser())
662 0 : ereport(ERROR,
663 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
664 : errmsg("must be superuser to call %s()",
665 : "pg_nextoid")));
666 :
667 0 : rel = table_open(reloid, RowExclusiveLock);
668 0 : idx = index_open(idxoid, RowExclusiveLock);
669 :
670 0 : if (!IsSystemRelation(rel))
671 0 : ereport(ERROR,
672 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
673 : errmsg("pg_nextoid() can only be used on system catalogs")));
674 :
675 0 : if (idx->rd_index->indrelid != RelationGetRelid(rel))
676 0 : ereport(ERROR,
677 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
678 : errmsg("index \"%s\" does not belong to table \"%s\"",
679 : RelationGetRelationName(idx),
680 : RelationGetRelationName(rel))));
681 :
682 0 : atttuple = SearchSysCacheAttName(reloid, NameStr(*attname));
683 0 : if (!HeapTupleIsValid(atttuple))
684 0 : ereport(ERROR,
685 : (errcode(ERRCODE_UNDEFINED_COLUMN),
686 : errmsg("column \"%s\" of relation \"%s\" does not exist",
687 : NameStr(*attname), RelationGetRelationName(rel))));
688 :
689 0 : attform = ((Form_pg_attribute) GETSTRUCT(atttuple));
690 0 : attno = attform->attnum;
691 :
692 0 : if (attform->atttypid != OIDOID)
693 0 : ereport(ERROR,
694 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
695 : errmsg("column \"%s\" is not of type oid",
696 : NameStr(*attname))));
697 :
698 0 : if (IndexRelationGetNumberOfKeyAttributes(idx) != 1 ||
699 0 : idx->rd_index->indkey.values[0] != attno)
700 0 : ereport(ERROR,
701 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
702 : errmsg("index \"%s\" is not the index for column \"%s\"",
703 : RelationGetRelationName(idx),
704 : NameStr(*attname))));
705 :
706 0 : newoid = GetNewOidWithIndex(rel, idxoid, attno);
707 :
708 0 : ReleaseSysCache(atttuple);
709 0 : table_close(rel, RowExclusiveLock);
710 0 : index_close(idx, RowExclusiveLock);
711 :
712 0 : PG_RETURN_OID(newoid);
713 : }
714 :
715 : /*
716 : * SQL callable interface for StopGeneratingPinnedObjectIds().
717 : *
718 : * This is only to be used by initdb, so it's intentionally not documented in
719 : * the user facing docs.
720 : */
721 : Datum
722 94 : pg_stop_making_pinned_objects(PG_FUNCTION_ARGS)
723 : {
724 : /*
725 : * Belt-and-suspenders check, since StopGeneratingPinnedObjectIds will
726 : * fail anyway in non-single-user mode.
727 : */
728 94 : if (!superuser())
729 0 : ereport(ERROR,
730 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
731 : errmsg("must be superuser to call %s()",
732 : "pg_stop_making_pinned_objects")));
733 :
734 94 : StopGeneratingPinnedObjectIds();
735 :
736 94 : PG_RETURN_VOID();
737 : }
|