Make COPY TO keep locks until the transaction end.
authorAmit Kapila <[email protected]>
Fri, 15 May 2020 02:40:00 +0000 (08:10 +0530)
committerAmit Kapila <[email protected]>
Fri, 15 May 2020 02:40:00 +0000 (08:10 +0530)
COPY TO released the ACCESS SHARE lock immediately when it was done rather
than holding on to it until the end of the transaction.

This breaks the case where a REPEATABLE READ transaction could see an
empty table if it repeats a COPY statement and somebody truncated the
table in the meantime.

Before 4dded12faad the lock was also released after COPY FROM, but the
commit failed to notice the irregularity in COPY TO.

This is old behavior but doesn't seem important enough to backpatch.

Author: Laurenz Albe, based on suggestion by Robert Haas and Tom Lane
Reviewed-by: Amit Kapila
Discussion: https://postgr.es/m/7bcfc39d4176faf85ab317d0c26786953646a411[email protected]

src/backend/commands/copy.c

index ac07f75bc36b3c34943323ffe18afbd1ae68bb60..6d53dc463c185caa802509dac93b1e47e5c12a7f 100644 (file)
@@ -1081,13 +1081,8 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
                EndCopyTo(cstate);
        }
 
-       /*
-        * Close the relation. If reading, we can release the AccessShareLock we
-        * got; if writing, we should hold the lock until end of transaction to
-        * ensure that updates will be committed before lock is released.
-        */
        if (rel != NULL)
-               table_close(rel, (is_from ? NoLock : AccessShareLock));
+               table_close(rel, NoLock);
 }
 
 /*