Skip to content

Commit 4dded12

Browse files
committed
COPY to a relation should keep write lock till transaction commit.
Thanks to Hiroshi for spotting the problem.
1 parent 37432f5 commit 4dded12

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/backend/commands/copy.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.91 1999/11/22 17:56:00 momjian Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.92 1999/11/27 21:52:53 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -257,14 +257,16 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
257257
Relation rel;
258258
extern char *UserName; /* defined in global.c */
259259
const AclMode required_access = from ? ACL_WR : ACL_RD;
260-
LOCKMODE required_lock = from ? AccessExclusiveLock : AccessShareLock;
261-
/* Note: AccessExclusive is probably overkill for copying to a relation,
262-
* but that's what the existing code grabs on the rel's indices. If
263-
* this is relaxed then I think the index locks need relaxed also.
264-
*/
265260
int result;
266261

267-
rel = heap_openr(relname, required_lock);
262+
/*
263+
* Open and lock the relation, using the appropriate lock type.
264+
*
265+
* Note: AccessExclusive is probably overkill for copying to a relation,
266+
* but that's what the code grabs on the rel's indices. If this lock is
267+
* relaxed then I think the index locks need relaxed also.
268+
*/
269+
rel = heap_openr(relname, (from ? AccessExclusiveLock : AccessShareLock));
268270

269271
result = pg_aclcheck(relname, UserName, required_access);
270272
if (result != ACLCHECK_OK)
@@ -349,7 +351,12 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
349351
}
350352
}
351353

352-
heap_close(rel, required_lock);
354+
/*
355+
* Close the relation. If reading, we can release the AccessShareLock
356+
* we got; if writing, we should hold the lock until end of transaction
357+
* to ensure that updates will be committed before lock is released.
358+
*/
359+
heap_close(rel, (from ? NoLock : AccessShareLock));
353360
}
354361

355362

0 commit comments

Comments
 (0)