? 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.29 diff -c -r1.29 tablecmds.c *** src/backend/commands/tablecmds.c 2002/08/15 16:36:02 1.29 --- src/backend/commands/tablecmds.c 2002/08/20 03:40:58 *************** *** 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, RowExclusiveLock); + + ScanKeyEntryInitialize(&key, 0, + Anum_pg_constraint_confrelid, + F_OIDEQ, + ObjectIdGetDatum(relid)); + + fkeyScan = systable_beginscan(fkeyRel, ConstraintFRelidIndex, true, + 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, RowExclusiveLock); /* Keep the lock until transaction commit */ heap_close(rel, NoLock); Index: src/include/catalog/indexing.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/catalog/indexing.h,v retrieving revision 1.74 diff -c -r1.74 indexing.h *** src/include/catalog/indexing.h 2002/08/05 03:29:17 1.74 --- src/include/catalog/indexing.h 2002/08/20 03:41:16 *************** *** 39,44 **** --- 39,45 ---- #define ClassOidIndex "pg_class_oid_index" #define ConstraintNameNspIndex "pg_constraint_conname_nsp_index" #define ConstraintOidIndex "pg_constraint_oid_index" + #define ConstraintFRelidIndex "pg_constraint_confrelid_index" #define ConstraintRelidIndex "pg_constraint_conrelid_index" #define ConversionDefaultIndex "pg_conversion_default_index" #define ConversionNameNspIndex "pg_conversion_name_nsp_index" *************** *** 129,134 **** --- 130,137 ---- DECLARE_INDEX(pg_constraint_conname_nsp_index on pg_constraint using btree(conname name_ops, connamespace oid_ops)); /* This following index is not used for a cache and is not unique */ DECLARE_INDEX(pg_constraint_conrelid_index on pg_constraint using btree(conrelid oid_ops)); + /* This following index is not used for a cache and is not unique */ + DECLARE_INDEX(pg_constraint_confrelid_index on pg_constraint using btree(confrelid oid_ops)); DECLARE_UNIQUE_INDEX(pg_constraint_oid_index on pg_constraint using btree(oid oid_ops)); DECLARE_UNIQUE_INDEX(pg_conversion_default_index on pg_conversion using btree(connamespace oid_ops, conforencoding int4_ops, contoencoding int4_ops, oid oid_ops)); DECLARE_UNIQUE_INDEX(pg_conversion_name_nsp_index on pg_conversion using btree(conname name_ops, connamespace oid_ops)); 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 03:41:35 *************** *** 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 03:41:35 *************** *** 88,90 **** --- 88,91 ---- test: rangefuncs test: without_oid test: conversion + test: truncate