Skip to content

Commit a9819ca

Browse files
committed
The attached patch cleans up the implementation of the TRUNCATE command;
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
1 parent d8e70cd commit a9819ca

File tree

5 files changed

+40
-36
lines changed

5 files changed

+40
-36
lines changed

src/backend/catalog/heap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.187 2002/03/19 02:18:14 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.188 2002/03/19 02:58:19 momjian Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1057,7 +1057,7 @@ RelationTruncateIndexes(Oid heapId)
10571057
*/
10581058

10591059
void
1060-
heap_truncate(char *relname)
1060+
heap_truncate(const char *relname)
10611061
{
10621062
Relation rel;
10631063
Oid rid;

src/backend/commands/creatinh.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.86 2002/03/19 02:18:15 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/creatinh.c,v 1.87 2002/03/19 02:58:19 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
1515

1616
#include "postgres.h"
1717

1818
#include "access/heapam.h"
19+
#include "catalog/catalog.h"
1920
#include "catalog/catname.h"
2021
#include "catalog/indexing.h"
2122
#include "catalog/heap.h"
@@ -221,7 +222,7 @@ DefineRelation(CreateStmt *stmt, char relkind)
221222
* themselves will be destroyed, too.
222223
*/
223224
void
224-
RemoveRelation(char *name)
225+
RemoveRelation(const char *name)
225226
{
226227
AssertArg(name);
227228
heap_drop_with_catalog(name, allowSystemTableMods);
@@ -238,10 +239,34 @@ RemoveRelation(char *name)
238239
* Rows are removed, indices are truncated and reconstructed.
239240
*/
240241
void
241-
TruncateRelation(char *name)
242+
TruncateRelation(const char *relname)
242243
{
243-
AssertArg(name);
244-
heap_truncate(name);
244+
Relation rel;
245+
246+
AssertArg(relname);
247+
248+
if (!allowSystemTableMods && IsSystemRelationName(relname))
249+
elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
250+
relname);
251+
252+
if (!pg_ownercheck(GetUserId(), relname, RELNAME))
253+
elog(ERROR, "you do not own relation \"%s\"", relname);
254+
255+
/* Grab exclusive lock in preparation for truncate */
256+
rel = heap_openr(relname, AccessExclusiveLock);
257+
258+
if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
259+
elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
260+
relname);
261+
262+
if (rel->rd_rel->relkind == RELKIND_VIEW)
263+
elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a view",
264+
relname);
265+
266+
/* Keep the lock until transaction commit */
267+
heap_close(rel, NoLock);
268+
269+
heap_truncate(relname);
245270
}
246271

247272

src/backend/tcop/utility.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.133 2002/03/19 02:18:20 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.134 2002/03/19 02:58:19 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -216,9 +216,7 @@ ProcessUtility(Node *parsetree,
216216
break;
217217

218218
/*
219-
* ******************************** relation and attribute
220-
* manipulation ********************************
221-
*
219+
* relation and attribute manipulation
222220
*/
223221
case T_CreateStmt:
224222
DefineRelation((CreateStmt *) parsetree, RELKIND_RELATION);
@@ -301,26 +299,7 @@ ProcessUtility(Node *parsetree,
301299

302300
case T_TruncateStmt:
303301
{
304-
Relation rel;
305-
306-
relname = ((TruncateStmt *) parsetree)->relName;
307-
if (!allowSystemTableMods && IsSystemRelationName(relname))
308-
elog(ERROR, "TRUNCATE cannot be used on system tables. '%s' is a system table",
309-
relname);
310-
311-
/* Grab exclusive lock in preparation for truncate... */
312-
rel = heap_openr(relname, AccessExclusiveLock);
313-
if (rel->rd_rel->relkind == RELKIND_SEQUENCE)
314-
elog(ERROR, "TRUNCATE cannot be used on sequences. '%s' is a sequence",
315-
relname);
316-
if (rel->rd_rel->relkind == RELKIND_VIEW)
317-
elog(ERROR, "TRUNCATE cannot be used on views. '%s' is a view",
318-
relname);
319-
heap_close(rel, NoLock);
320-
321-
if (!pg_ownercheck(GetUserId(), relname, RELNAME))
322-
elog(ERROR, "you do not own class \"%s\"", relname);
323-
TruncateRelation(relname);
302+
TruncateRelation(((TruncateStmt *) parsetree)->relName);
324303
}
325304
break;
326305

src/include/catalog/heap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: heap.h,v 1.44 2002/03/19 02:18:22 momjian Exp $
10+
* $Id: heap.h,v 1.45 2002/03/19 02:58:19 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -41,7 +41,7 @@ extern Oid heap_create_with_catalog(char *relname, TupleDesc tupdesc,
4141
extern void heap_drop_with_catalog(const char *relname,
4242
bool allow_system_table_mods);
4343

44-
extern void heap_truncate(char *relname);
44+
extern void heap_truncate(const char *relname);
4545

4646
extern void AddRelationRawConstraints(Relation rel,
4747
List *rawColDefaults,

src/include/commands/creatinh.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: creatinh.h,v 1.17 2001/11/05 17:46:33 momjian Exp $
10+
* $Id: creatinh.h,v 1.18 2002/03/19 02:58:20 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -17,7 +17,7 @@
1717
#include "nodes/parsenodes.h"
1818

1919
extern void DefineRelation(CreateStmt *stmt, char relkind);
20-
extern void RemoveRelation(char *name);
21-
extern void TruncateRelation(char *name);
20+
extern void RemoveRelation(const char *name);
21+
extern void TruncateRelation(const char *name);
2222

2323
#endif /* CREATINH_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy