]> granicus.if.org Git - postgresql/commitdiff
The attached patch cleans up the implementation of the TRUNCATE command;
authorBruce Momjian <bruce@momjian.us>
Tue, 19 Mar 2002 02:58:20 +0000 (02:58 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 19 Mar 2002 02:58:20 +0000 (02:58 +0000)
in the current code, the authentication logic (check user, check the
relation we're operating on, etc) is done in tcop/utility.c, whereas the
actual TRUNCATE command in done in TruncateRelation() in
commands/createinh.c (which is really just a wrapper over
heap_truncate() in catalog/heap.c). This patch moves the authentication
logic into TruncateRelation(), as well as making some minor code
cleanups.

Neil Conway

src/backend/catalog/heap.c
src/backend/commands/creatinh.c
src/backend/tcop/utility.c
src/include/catalog/heap.h
src/include/commands/creatinh.h

index 9ec241e3ea3aae93db4321f093219375423dc175..b2359e4725c7f3b6ab5635bb753a6f0e0f3bafd1 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.187 2002/03/19 02:18:14 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.188 2002/03/19 02:58:19 momjian Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -1057,7 +1057,7 @@ RelationTruncateIndexes(Oid heapId)
  */
 
 void
-heap_truncate(char *relname)
+heap_truncate(const char *relname)
 {
        Relation        rel;
        Oid                     rid;
index ae2a90c93113ad9db6b2f9d908d24c11d65a4799..d6af805715c43bd2ec53415096e0160cbc1cedf9 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.86 2002/03/19 02:18:15 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.87 2002/03/19 02:58:19 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -16,6 +16,7 @@
 #include "postgres.h"
 
 #include "access/heapam.h"
+#include "catalog/catalog.h"
 #include "catalog/catname.h"
 #include "catalog/indexing.h"
 #include "catalog/heap.h"
@@ -221,7 +222,7 @@ DefineRelation(CreateStmt *stmt, char relkind)
  * themselves will be destroyed, too.
  */
 void
-RemoveRelation(char *name)
+RemoveRelation(const char *name)
 {
        AssertArg(name);
        heap_drop_with_catalog(name, allowSystemTableMods);
@@ -238,10 +239,34 @@ RemoveRelation(char *name)
  *                               Rows are removed, indices are truncated and reconstructed.
  */
 void
-TruncateRelation(char *name)
+TruncateRelation(const char *relname)
 {
-       AssertArg(name);
-       heap_truncate(name);
+       Relation        rel;
+
+       AssertArg(relname);
+
+       if (!allowSystemTableMods && IsSystemRelationName(relname))
+               elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
+                        relname);
+
+       if (!pg_ownercheck(GetUserId(), relname, RELNAME))
+               elog(ERROR, "you do not own relation \"%s\"", relname);
+
+       /* Grab exclusive lock in preparation for truncate */
+       rel = heap_openr(relname, AccessExclusiveLock);
+
+       if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
+               elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
+                        relname);
+
+       if (rel->rd_rel->relkind == RELKIND_VIEW)
+               elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a view",
+                        relname);
+
+       /* Keep the lock until transaction commit */
+       heap_close(rel, NoLock);
+
+       heap_truncate(relname);
 }
 
 
index a6400eabc3572e62646134963d35acb5c10f5ba2..0c1ead0331c78d100a10cd95a8a9287f2f4e1580 100644 (file)
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.133 2002/03/19 02:18:20 momjian Exp $
+ *       $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.134 2002/03/19 02:58:19 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -216,9 +216,7 @@ ProcessUtility(Node *parsetree,
                        break;
 
                        /*
-                        * ******************************** relation and attribute
-                        * manipulation ********************************
-                        *
+                        * relation and attribute manipulation
                         */
                case T_CreateStmt:
                        DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
@@ -301,26 +299,7 @@ ProcessUtility(Node *parsetree,
 
                case T_TruncateStmt:
                        {
-                               Relation        rel;
-
-                               relname = ((TruncateStmt *) parsetree)->relName;
-                               if (!allowSystemTableMods && IsSystemRelationName(relname))
-                                       elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
-                                                relname);
-
-                               /* Grab exclusive lock in preparation for truncate... */
-                               rel = heap_openr(relname, AccessExclusiveLock);
-                               if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
-                                       elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
-                                                relname);
-                               if (rel->rd_rel->relkind == RELKIND_VIEW)
-                                       elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a view",
-                                                relname);
-                               heap_close(rel, NoLock);
-
-                               if (!pg_ownercheck(GetUserId(), relname, RELNAME))
-                                       elog(ERROR, "you do not own class \"%s\"", relname);
-                               TruncateRelation(relname);
+                               TruncateRelation(((TruncateStmt *) parsetree)->relName);
                        }
                        break;
 
index 368293148bb344f1057ed8878ffe050de113b1b8..4298f53bdc4a743db1658cd591273f3c10f65e6e 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: heap.h,v 1.44 2002/03/19 02:18:22 momjian Exp $
+ * $Id: heap.h,v 1.45 2002/03/19 02:58:19 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -41,7 +41,7 @@ extern Oid heap_create_with_catalog(char *relname, TupleDesc tupdesc,
 extern void heap_drop_with_catalog(const char *relname,
                                           bool allow_system_table_mods);
 
-extern void heap_truncate(char *relname);
+extern void heap_truncate(const char *relname);
 
 extern void AddRelationRawConstraints(Relation rel,
                                                  List *rawColDefaults,
index e676fc25939b5c696c200db4f60c4fc43b5c9df6..4cc9801e85e83f60aa3f4a79595f3ffe6fa88dbc 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $Id: creatinh.h,v 1.17 2001/11/05 17:46:33 momjian Exp $
+ * $Id: creatinh.h,v 1.18 2002/03/19 02:58:20 momjian Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,7 +17,7 @@
 #include "nodes/parsenodes.h"
 
 extern void DefineRelation(CreateStmt *stmt, char relkind);
-extern void RemoveRelation(char *name);
-extern void TruncateRelation(char *name);
+extern void RemoveRelation(const char *name);
+extern void TruncateRelation(const char *name);
 
 #endif   /* CREATINH_H */