Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita9819ca

Browse files
committed
The attached patch cleans up the implementation of the TRUNCATE command;
in the current code, the authentication logic (check user, check therelation we're operating on, etc) is done in tcop/utility.c, whereas theactual TRUNCATE command in done in TruncateRelation() incommands/createinh.c (which is really just a wrapper overheap_truncate() in catalog/heap.c). This patch moves the authenticationlogic into TruncateRelation(), as well as making some minor codecleanups.Neil Conway
1 parentd8e70cd commita9819ca

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(constchar*relname)
10611061
{
10621062
Relationrel;
10631063
Oidrid;

‎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(constchar*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(constchar*relname)
242243
{
243-
AssertArg(name);
244-
heap_truncate(name);
244+
Relationrel;
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
caseT_CreateStmt:
224222
DefineRelation((CreateStmt*)parsetree,RELKIND_RELATION);
@@ -301,26 +299,7 @@ ProcessUtility(Node *parsetree,
301299

302300
caseT_TruncateStmt:
303301
{
304-
Relationrel;
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
externvoidheap_drop_with_catalog(constchar*relname,
4242
boolallow_system_table_mods);
4343

44-
externvoidheap_truncate(char*relname);
44+
externvoidheap_truncate(constchar*relname);
4545

4646
externvoidAddRelationRawConstraints(Relationrel,
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
externvoidDefineRelation(CreateStmt*stmt,charrelkind);
20-
externvoidRemoveRelation(char*name);
21-
externvoidTruncateRelation(char*name);
20+
externvoidRemoveRelation(constchar*name);
21+
externvoidTruncateRelation(constchar*name);
2222

2323
#endif/* CREATINH_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp