HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

Postgresql, write archive file over mount, secure against simultaneous access?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlfilesecuremountwriteagainstsimultaneousoveraccessarchive

Problem

I am using Postgres 9.2.1 and am saving my archivable WAL over a NFS share.

I just use the basic command, given as an example in the postgresql.conf

test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f

Does the cp command somehow secure that my backup server, which reads the WAL archives, doesn't read half copied WAL files? Do I have to manually define a cp command that lets the file end with .tmp and then call rename afterwards to give it its proper name?

Has someone got a example of an save archive_command?

Solution

Neither NFS nor cp will protect you from partial write. The usual way of mitigating problems with write atomicity is to use a temporary file and a mv after copying (or writing) is complete. You might use something like:

test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/tmp/%f && \
    mv /mnt/server/archivedir/tmp/%f /mnt/server/archivedir/%f

Code Snippets

test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/tmp/%f && \
    mv /mnt/server/archivedir/tmp/%f /mnt/server/archivedir/%f

Context

StackExchange Database Administrators Q#30748, answer score: 5

Revisions (0)

No revisions yet.