Class: EY::Snaplock::Database::Postgresql
- Inherits:
-
Object
- Object
- EY::Snaplock::Database::Postgresql
- Defined in:
- lib/ey_snaplock/database/postgresql9.rb
Instance Method Summary collapse
-
#aquire_lock ⇒ Object
wiki.postgresql.org/wiki/Hot_Standby We don’t need to acquire a read lock for the snapshot.
-
#initialize(uri) ⇒ Postgresql
constructor
Creates an object called @postgresql for usage with IO.popen.
-
#postgresql_command(uri) ⇒ Object
Construct to help prepare the command.
-
#release_lock ⇒ Object
The backup should have been started to finish this and actually force the checkpoint we need to stop the backup.
-
#with_lock(timeout) ⇒ Object
Default function being called (e.g. database.with_lock).
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
#aquire_lock ⇒ Object
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.
20 21 22 23 24 25 26 |
# File 'lib/ey_snaplock/database/postgresql9.rb', line 20 def aquire_lock pipe = IO.popen(@postgresql, 'w') @read_lock_pid = pipe.pid pipe.puts("select pg_start_backup('backup',true);") sleep 1 pipe.close end |
#postgresql_command(uri) ⇒ Object
Construct to help prepare the command.
38 39 40 41 42 |
# File 'lib/ey_snaplock/database/postgresql9.rb', line 38 def postgresql_command(uri) command = "psql" command << " -U" << (uri.user || 'postgres') command end |
#release_lock ⇒ Object
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.
29 30 31 32 33 34 35 |
# File 'lib/ey_snaplock/database/postgresql9.rb', line 29 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)
11 12 13 14 15 16 |
# File 'lib/ey_snaplock/database/postgresql9.rb', line 11 def with_lock(timeout) #timeout is ignored aquire_lock yield ensure release_lock end |