Skip to content

Commit 12a4ab3

Browse files
authored
feat: disable CoW on linux systems (#12)
1 parent 26e615a commit 12a4ab3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

start-ephemeral-postgres.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ trap popd EXIT
88

99
source ./ephemeral-postgres-config.sh
1010

11+
has_no_cow() {
12+
local file="$1"
13+
[[ "$(lsattr "$file" 2>/dev/null | awk '{print $1}')" == *C* ]]
14+
}
15+
16+
disable_cow() {
17+
local dir="$1"
18+
19+
if has_no_cow "$dir"; then
20+
echo "Skipping $dir: already has No_COW attribute"
21+
return
22+
fi
23+
24+
# Set the No_COW attribute on the directory
25+
chattr +C "$dir"
26+
if [[ $? -ne 0 ]]; then
27+
echo "Failed to set +C attribute on $dir"
28+
exit 1
29+
fi
30+
31+
# Recreate files and directories to ensure +C applies to them
32+
for file in "$dir"/*; do
33+
if [[ -f "$file" ]]; then
34+
if has_no_cow "$file"; then
35+
echo "Skipping $file: already has No_COW attribute"
36+
else
37+
echo "Disable CoW on $file"
38+
mv "$file" "$file.tmp"
39+
cp --preserve=all "$file.tmp" "$file"
40+
rm "$file.tmp"
41+
fi
42+
elif [[ -d "$file" ]]; then
43+
disable_cow "$file" # Recursively disable CoW in subdirectories
44+
fi
45+
done
46+
}
47+
1148
if [[ "${EPHEMERAL_POSTGRES_AUTO_UPDATE}" -eq 1 ]]; then
1249
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
1350
if [ -z "$(git status --porcelain)" ]; then
@@ -47,6 +84,13 @@ if [ -n "$EPHEMERAL_POSTGRES_DATA_DIR" ]; then
4784

4885
EPHEMERAL_POSTGRES_DOCKER_RUN_ARGS+=" -v $EPHEMERAL_POSTGRES_DATA_DIR:/var/lib/postgresql/data"
4986
echo "Using data directory $EPHEMERAL_POSTGRES_DATA_DIR"
87+
88+
# CoW (eg: with btrs) has a bad time with the frequent small writes from postgres
89+
if [[ "$OSTYPE" =~ ^linux ]]; then
90+
echo "Disabling CoW (copy on write) in data directory $EPHEMERAL_POSTGRES_DATA_DIR"
91+
disable_cow "$EPHEMERAL_POSTGRES_DATA_DIR"
92+
fi
93+
5094
else
5195
if [[ "$OSTYPE" =~ ^linux ]]; then
5296
echo "Using ram disk"

0 commit comments

Comments
 (0)