Class: EY::Snaplock::Database::Postgresql

Inherits:
Object
  • Object
show all
Defined in:
lib/ey_snaplock/database/postgresql9.rb

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Postgresql

Creates an object called @postgresql for usage with IO.popen.



6
7
8
# File 'lib/ey_snaplock/database/postgresql9.rb', line 6

def initialize(uri)
  @postgresql = postgresql_command(uri)
end

Instance Method Details

#acquire_lockObject

wiki.postgresql.org/wiki/Hot_Standby We don’t need to acquire a read lock for the snapshot. However we do need to create an backup of the base database and force a checkpoint to ensure the data is written so when the slave comes up it has enough data to start.



24
25
26
27
28
29
30
# File 'lib/ey_snaplock/database/postgresql9.rb', line 24

def acquire_lock
  pipe = IO.popen(@postgresql, 'w')
  @read_lock_pid = pipe.pid
  pipe.puts("select pg_start_backup('backup',true);")
  sleep 1
  pipe.close
end

#lock_filenameObject



10
11
12
# File 'lib/ey_snaplock/database/postgresql9.rb', line 10

def lock_filename
  ENV["POSTGRES_LOCK_FILENAME"] || "/var/run/ey_snaplock_postgresql.pid"
end

#postgresql_command(uri) ⇒ Object

Construct to help prepare the command.



42
43
44
45
46
# File 'lib/ey_snaplock/database/postgresql9.rb', line 42

def postgresql_command(uri)
  command = "psql"
  command << " -U" << (uri.user || 'postgres')
  command
end

#release_lockObject

The backup should have been started to finish this and actually force the checkpoint we need to stop the backup. This completes the only action that PostgreSQL should require for a hot_standby.



33
34
35
36
37
38
39
# File 'lib/ey_snaplock/database/postgresql9.rb', line 33

def release_lock
  pipe2 = IO.popen(@postgresql, 'w')
  @read_lock_pid2 = pipe2.pid
  pipe2.puts("select pg_stop_backup();")
  sleep 1
  pipe2.close
end

#with_lock(timeout) ⇒ Object

Default function being called (e.g. database.with_lock)



15
16
17
18
19
20
# File 'lib/ey_snaplock/database/postgresql9.rb', line 15

def with_lock(timeout) #timeout is ignored
  acquire_lock
  yield
ensure
  release_lock
end