Fix race condition in pg_mkdir_p()
pg_mkdir_p() is used by initdb and pg_basebackup to create a directory as well as its parent directories. The logic used to be like below: if (stat(path) < 0) { if (mkdir(path) < 0) retval = -1; } It first checks for the existence of the path, if path does not pre-exist then it creates the directory. However if two processes try to create path concurrently, then one possible execution order is as below: A: stat(path) returns -1, so decide to create it; B: stat(path) returns -1, so decide to create it; B: mkdir(path) returns 0, successfully created path; A: mkdir(path) returns -1, fail to create path as it already exist; This race condition could be fixed by swapping the order of stat() and mkdir(), this is also what the "mkdir -p" command does. Co-authored-by: NPaul Guo <pguo@pivotal.io> Co-authored-by: NNing Yu <nyu@pivotal.io>
Showing
想要评论请 注册 或 登录