8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.81 2003/09/15 00:26:31 petere Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.82 2003/09/19 21:04:20 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
@@ -365,15 +365,9 @@ void
365
365
TruncateRelation (const RangeVar * relation )
366
366
{
367
367
Relation rel ;
368
- Oid relid ;
369
- ScanKeyData key ;
370
- Relation fkeyRel ;
371
- SysScanDesc fkeyScan ;
372
- HeapTuple tuple ;
373
368
374
369
/* Grab exclusive lock in preparation for truncate */
375
370
rel = heap_openrv (relation , AccessExclusiveLock );
376
- relid = RelationGetRelid (rel );
377
371
378
372
/* Only allow truncate on regular tables */
379
373
if (rel -> rd_rel -> relkind != RELKIND_RELATION )
@@ -383,7 +377,7 @@ TruncateRelation(const RangeVar *relation)
383
377
RelationGetRelationName (rel ))));
384
378
385
379
/* Permissions checks */
386
- if (!pg_class_ownercheck (relid , GetUserId ()))
380
+ if (!pg_class_ownercheck (RelationGetRelid ( rel ) , GetUserId ()))
387
381
aclcheck_error (ACLCHECK_NOT_OWNER , ACL_KIND_CLASS ,
388
382
RelationGetRelationName (rel ));
389
383
@@ -405,35 +399,7 @@ TruncateRelation(const RangeVar *relation)
405
399
/*
406
400
* Don't allow truncate on tables which are referenced by foreign keys
407
401
*/
408
- fkeyRel = heap_openr (ConstraintRelationName , AccessShareLock );
409
-
410
- ScanKeyEntryInitialize (& key , 0 ,
411
- Anum_pg_constraint_confrelid ,
412
- F_OIDEQ ,
413
- ObjectIdGetDatum (relid ));
414
-
415
- fkeyScan = systable_beginscan (fkeyRel , 0 , false,
416
- SnapshotNow , 1 , & key );
417
-
418
- /*
419
- * First foreign key found with us as the reference should throw an
420
- * error.
421
- */
422
- while (HeapTupleIsValid (tuple = systable_getnext (fkeyScan )))
423
- {
424
- Form_pg_constraint con = (Form_pg_constraint ) GETSTRUCT (tuple );
425
-
426
- if (con -> contype == 'f' && con -> conrelid != relid )
427
- ereport (ERROR ,
428
- (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
429
- errmsg ("cannot truncate a table referenced in a foreign key constraint" ),
430
- errdetail ("Table \"%s\" references this one via foreign key constraint \"%s\"." ,
431
- get_rel_name (con -> conrelid ),
432
- NameStr (con -> conname ))));
433
- }
434
-
435
- systable_endscan (fkeyScan );
436
- heap_close (fkeyRel , AccessShareLock );
402
+ heap_truncate_check_FKs (rel );
437
403
438
404
/*
439
405
* Do the real work using the same technique as cluster, but without
@@ -3137,11 +3103,28 @@ AlterTableAddForeignKeyConstraint(Relation rel, FkConstraint *fkconstraint)
3137
3103
aclcheck_error (aclresult , ACL_KIND_CLASS ,
3138
3104
RelationGetRelationName (rel ));
3139
3105
3140
- if (isTempNamespace (RelationGetNamespace (pkrel )) &&
3141
- !isTempNamespace (RelationGetNamespace (rel )))
3142
- ereport (ERROR ,
3143
- (errcode (ERRCODE_INVALID_TABLE_DEFINITION ),
3144
- errmsg ("cannot reference temporary table from permanent table constraint" )));
3106
+ /*
3107
+ * Disallow reference from permanent table to temp table or vice versa.
3108
+ * (The ban on perm->temp is for fairly obvious reasons. The ban on
3109
+ * temp->perm is because other backends might need to run the RI triggers
3110
+ * on the perm table, but they can't reliably see tuples the owning
3111
+ * backend has created in the temp table, because non-shared buffers
3112
+ * are used for temp tables.)
3113
+ */
3114
+ if (isTempNamespace (RelationGetNamespace (pkrel )))
3115
+ {
3116
+ if (!isTempNamespace (RelationGetNamespace (rel )))
3117
+ ereport (ERROR ,
3118
+ (errcode (ERRCODE_INVALID_TABLE_DEFINITION ),
3119
+ errmsg ("cannot reference temporary table from permanent table constraint" )));
3120
+ }
3121
+ else
3122
+ {
3123
+ if (isTempNamespace (RelationGetNamespace (rel )))
3124
+ ereport (ERROR ,
3125
+ (errcode (ERRCODE_INVALID_TABLE_DEFINITION ),
3126
+ errmsg ("cannot reference permanent table from temporary table constraint" )));
3127
+ }
3145
3128
3146
3129
/*
3147
3130
* Look up the referencing attributes to make sure they exist, and
0 commit comments