From fe41f03db274c0988bc6c2c1def8a83fbed01213 Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Mon, 28 Sep 2020 17:44:56 +0530 Subject: [PATCH v1] Skip ExecCheckRTPerms in CTAS with no data In CTAS with no data, we actually do not insert the tuples into the created table, so we can skip checking for the insert permissions. Anyways, the insert permissions will be checked when the tuples are inserted into the table. --- src/backend/commands/createas.c | 38 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/backend/commands/createas.c b/src/backend/commands/createas.c index d53ec952d0..b36422280f 100644 --- a/src/backend/commands/createas.c +++ b/src/backend/commands/createas.c @@ -436,7 +436,6 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) List *attrList; ObjectAddress intoRelationAddr; Relation intoRelationDesc; - RangeTblEntry *rte; ListCell *lc; int attnum; @@ -507,23 +506,28 @@ intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo) intoRelationDesc = table_open(intoRelationAddr.objectId, AccessExclusiveLock); /* - * Check INSERT permission on the constructed table. - * - * XXX: It would arguably make sense to skip this check if into->skipData - * is true. + * Check INSERT permission on the constructed table. Skip this check if + * into->skipData is true, as we do not actually insert the tuples, we + * just create the table. The insert permissions will be checked later, + * while inserting tuples into the table. */ - rte = makeNode(RangeTblEntry); - rte->rtekind = RTE_RELATION; - rte->relid = intoRelationAddr.objectId; - rte->relkind = relkind; - rte->rellockmode = RowExclusiveLock; - rte->requiredPerms = ACL_INSERT; - - for (attnum = 1; attnum <= intoRelationDesc->rd_att->natts; attnum++) - rte->insertedCols = bms_add_member(rte->insertedCols, - attnum - FirstLowInvalidHeapAttributeNumber); - - ExecCheckRTPerms(list_make1(rte), true); + if (!into->skipData) + { + RangeTblEntry *rte; + + rte = makeNode(RangeTblEntry); + rte->rtekind = RTE_RELATION; + rte->relid = intoRelationAddr.objectId; + rte->relkind = relkind; + rte->rellockmode = RowExclusiveLock; + rte->requiredPerms = ACL_INSERT; + + for (attnum = 1; attnum <= intoRelationDesc->rd_att->natts; attnum++) + rte->insertedCols = bms_add_member(rte->insertedCols, + attnum - FirstLowInvalidHeapAttributeNumber); + + ExecCheckRTPerms(list_make1(rte), true); + } /* * Make sure the constructed table does not have RLS enabled. -- 2.25.1