@@ -291,9 +291,7 @@ struct DropRelationCallbackState
291
291
#define child_dependency_type (child_is_partition ) \
292
292
((child_is_partition) ? DEPENDENCY_AUTO : DEPENDENCY_NORMAL)
293
293
294
- static void truncate_check_rel (Oid relid , Form_pg_class reltuple );
295
- static void truncate_check_perms (Oid relid , Form_pg_class reltuple );
296
- static void truncate_check_activity (Relation rel );
294
+ static void truncate_check_rel (Relation rel );
297
295
static List * MergeAttributes (List * schema , List * supers , char relpersistence ,
298
296
bool is_partition , List * * supOids , List * * supconstr ,
299
297
int * supOidCount );
@@ -1244,11 +1242,7 @@ ExecuteTruncate(TruncateStmt *stmt)
1244
1242
heap_close (rel , lockmode );
1245
1243
continue ;
1246
1244
}
1247
-
1248
- truncate_check_rel (myrelid , rel -> rd_rel );
1249
- truncate_check_perms (myrelid , rel -> rd_rel );
1250
- truncate_check_activity (rel );
1251
-
1245
+ truncate_check_rel (rel );
1252
1246
rels = lappend (rels , rel );
1253
1247
relids = lappend_oid (relids , myrelid );
1254
1248
@@ -1284,15 +1278,7 @@ ExecuteTruncate(TruncateStmt *stmt)
1284
1278
continue ;
1285
1279
}
1286
1280
1287
- /*
1288
- * Inherited TRUNCATE commands perform access
1289
- * permission checks on the parent table only.
1290
- * So we skip checking the children's permissions
1291
- * and don't call truncate_check_perms() here.
1292
- */
1293
- truncate_check_rel (RelationGetRelid (rel ), rel -> rd_rel );
1294
- truncate_check_activity (rel );
1295
-
1281
+ truncate_check_rel (rel );
1296
1282
rels = lappend (rels , rel );
1297
1283
relids = lappend_oid (relids , childrelid );
1298
1284
}
@@ -1331,9 +1317,7 @@ ExecuteTruncate(TruncateStmt *stmt)
1331
1317
ereport (NOTICE ,
1332
1318
(errmsg ("truncate cascades to table \"%s\"" ,
1333
1319
RelationGetRelationName (rel ))));
1334
- truncate_check_rel (relid , rel -> rd_rel );
1335
- truncate_check_perms (relid , rel -> rd_rel );
1336
- truncate_check_activity (rel );
1320
+ truncate_check_rel (rel );
1337
1321
rels = lappend (rels , rel );
1338
1322
relids = lappend_oid (relids , relid );
1339
1323
}
@@ -1546,50 +1530,35 @@ ExecuteTruncate(TruncateStmt *stmt)
1546
1530
* Check that a given rel is safe to truncate. Subroutine for ExecuteTruncate
1547
1531
*/
1548
1532
static void
1549
- truncate_check_rel (Oid relid , Form_pg_class reltuple )
1533
+ truncate_check_rel (Relation rel )
1550
1534
{
1551
- char * relname = NameStr ( reltuple -> relname ) ;
1535
+ AclResult aclresult ;
1552
1536
1553
1537
/*
1554
1538
* Only allow truncate on regular tables and partitioned tables (although,
1555
1539
* the latter are only being included here for the following checks; no
1556
1540
* physical truncation will occur in their case.)
1557
1541
*/
1558
- if (reltuple -> relkind != RELKIND_RELATION &&
1559
- reltuple -> relkind != RELKIND_PARTITIONED_TABLE )
1542
+ if (rel -> rd_rel -> relkind != RELKIND_RELATION &&
1543
+ rel -> rd_rel -> relkind != RELKIND_PARTITIONED_TABLE )
1560
1544
ereport (ERROR ,
1561
1545
(errcode (ERRCODE_WRONG_OBJECT_TYPE ),
1562
- errmsg ("\"%s\" is not a table" , relname )));
1546
+ errmsg ("\"%s\" is not a table" ,
1547
+ RelationGetRelationName (rel ))));
1548
+
1549
+ /* Permissions checks */
1550
+ aclresult = pg_class_aclcheck (RelationGetRelid (rel ), GetUserId (),
1551
+ ACL_TRUNCATE );
1552
+ if (aclresult != ACLCHECK_OK )
1553
+ aclcheck_error (aclresult , ACL_KIND_CLASS ,
1554
+ RelationGetRelationName (rel ));
1563
1555
1564
- if (!allowSystemTableMods && IsSystemClass ( relid , reltuple ))
1556
+ if (!allowSystemTableMods && IsSystemRelation ( rel ))
1565
1557
ereport (ERROR ,
1566
1558
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
1567
1559
errmsg ("permission denied: \"%s\" is a system catalog" ,
1568
- relname )));
1569
- }
1570
-
1571
- /*
1572
- * Check that current user has the permission to truncate given relation.
1573
- */
1574
- static void
1575
- truncate_check_perms (Oid relid , Form_pg_class reltuple )
1576
- {
1577
- char * relname = NameStr (reltuple -> relname );
1578
- AclResult aclresult ;
1579
-
1580
- /* Permissions checks */
1581
- aclresult = pg_class_aclcheck (relid , GetUserId (), ACL_TRUNCATE );
1582
- if (aclresult != ACLCHECK_OK )
1583
- aclcheck_error (aclresult , ACL_KIND_CLASS , relname );
1584
- }
1560
+ RelationGetRelationName (rel ))));
1585
1561
1586
- /*
1587
- * Set of extra sanity checks to check if a given relation is safe to
1588
- * truncate.
1589
- */
1590
- static void
1591
- truncate_check_activity (Relation rel )
1592
- {
1593
1562
/*
1594
1563
* Don't allow truncate on temp tables of other backends ... their local
1595
1564
* buffer manager is not going to cope.
0 commit comments