? src/backend/commands/.tablecmds.c.swp ? src/test/regress/expected/truncate.out ? src/test/regress/sql/truncate.sql Index: src/backend/commands/tablecmds.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/commands/tablecmds.c,v retrieving revision 1.30 diff -c -r1.30 tablecmds.c *** src/backend/commands/tablecmds.c 2002/08/19 15:08:46 1.30 --- src/backend/commands/tablecmds.c 2002/08/20 15:28:20 *************** *** 332,337 **** --- 332,341 ---- { Relation rel; Oid relid; + ScanKeyData key; + Relation fkeyRel; + SysScanDesc fkeyScan; + HeapTuple tuple; /* Grab exclusive lock in preparation for truncate */ rel = heap_openrv(relation, AccessExclusiveLock); *************** *** 355,360 **** --- 359,394 ---- if (!pg_class_ownercheck(relid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, RelationGetRelationName(rel)); + + /* + * Don't allow truncate on tables which are referenced + * by foreign keys + */ + fkeyRel = heap_openr(ConstraintRelationName, AccessShareLock); + + ScanKeyEntryInitialize(&key, 0, + Anum_pg_constraint_confrelid, + F_OIDEQ, + ObjectIdGetDatum(relid)); + + fkeyScan = systable_beginscan(fkeyRel, 0, false, + SnapshotNow, 1, &key); + + /* + * First foriegn key found with us as the reference + * should throw an error. + */ + while (HeapTupleIsValid(tuple = systable_getnext(fkeyScan))) + { + Form_pg_constraint con = (Form_pg_constraint) GETSTRUCT(tuple); + + if (con->contype == 'f') + elog(ERROR, "TRUNCATE cannot be used as other tables reference this one via foreign key constraint %s", + NameStr(con->conname)); + } + + systable_endscan(fkeyScan); + heap_close(fkeyRel, AccessShareLock); /* Keep the lock until transaction commit */ heap_close(rel, NoLock); Index: src/test/regress/parallel_schedule =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/test/regress/parallel_schedule,v retrieving revision 1.15 diff -c -r1.15 parallel_schedule *** src/test/regress/parallel_schedule 2002/08/11 02:06:32 1.15 --- src/test/regress/parallel_schedule 2002/08/20 15:28:23 *************** *** 74,77 **** # The sixth group of parallel test # ---------- # "plpgsql" cannot run concurrently with "rules" ! test: limit plpgsql temp domain rangefuncs copy2 conversion without_oid --- 74,77 ---- # The sixth group of parallel test # ---------- # "plpgsql" cannot run concurrently with "rules" ! test: limit plpgsql temp domain rangefuncs copy2 conversion without_oid truncate Index: src/test/regress/serial_schedule =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/test/regress/serial_schedule,v retrieving revision 1.15 diff -c -r1.15 serial_schedule *** src/test/regress/serial_schedule 2002/08/11 02:06:32 1.15 --- src/test/regress/serial_schedule 2002/08/20 15:28:23 *************** *** 88,90 **** --- 88,91 ---- test: rangefuncs test: without_oid test: conversion + test: truncate