Avoid unnecessary table open/close in TRUNCATE command.
authorFujii Masao <fujii@postgresql.org>
Sun, 11 Apr 2021 15:05:58 +0000 (00:05 +0900)
committerFujii Masao <fujii@postgresql.org>
Sun, 11 Apr 2021 15:05:58 +0000 (00:05 +0900)
ExecuteTruncate() filters out the duplicate tables specified
in the TRUNCATE command, for example in the case where "TRUNCATE foo, foo"
is executed. Such duplicate tables obviously don't need to be opened
and closed because they are skipped. But previously it always opened
the tables before checking whether they were duplicated ones or not,
and then closed them if they were. That is, the duplicated tables were
opened and closed unnecessarily.

This commit changes ExecuteTruncate() so that it opens the table
after it confirms that table is not duplicated one, which leads to
avoid unnecessary table open/close.

Do not back-patch because such unnecessary table open/close is not
a bug though it exists in older versions.

Author: Bharath Rupireddy
Reviewed-by: Amul Sul, Fujii Masao
Discussion: https://postgr.es/m/CALj2ACUdBO_sXJTa08OZ0YT0qk7F_gAmRa9hT4dxRcgPS4nsZA@mail.gmail.com

src/backend/commands/tablecmds.c

index f780918a9516419e964fa3f71cc99333be864e82..096a6f289155feb8f8061ffac35b97a7c657b06f 100644 (file)
@@ -1639,15 +1639,12 @@ ExecuteTruncate(TruncateStmt *stmt)
                                           0, RangeVarCallbackForTruncate,
                                           NULL);
 
-       /* open the relation, we already hold a lock on it */
-       rel = table_open(myrelid, NoLock);
-
        /* don't throw error for "TRUNCATE foo, foo" */
        if (list_member_oid(relids, myrelid))
-       {
-           table_close(rel, lockmode);
            continue;
-       }
+
+       /* open the relation, we already hold a lock on it */
+       rel = table_open(myrelid, NoLock);
 
        /*
         * RangeVarGetRelidExtended() has done most checks with its callback,