Skip to content

Instantly share code, notes, and snippets.

@mbokman
Last active October 27, 2015 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbokman/58b826258d05f1c7bec0 to your computer and use it in GitHub Desktop.
Save mbokman/58b826258d05f1c7bec0 to your computer and use it in GitHub Desktop.
Serializing postgresql.conf multiple times during a Chef run using the 'postgresql' cookbook.

Cookbook [postgresql] (https://supermarket.chef.io/cookbooks/postgresql) contains a recipe called server_conf.rb whose content is:

change_notify = node['postgresql']['server']['config_change_notify']

template "#{node['postgresql']['dir']}/postgresql.conf" do
  source "postgresql.conf.erb"
  owner "postgres"
  group "postgres"
  mode 0600
  notifies change_notify, 'service[postgresql]', :immediately
end

template "#{node['postgresql']['dir']}/pg_hba.conf" do
  source "pg_hba.conf.erb"
  owner "postgres"
  group "postgres"
  mode 00600
  notifies change_notify, 'service[postgresql]', :immediately
end

This is included (via include_recipe) in recipe server_debian.rb which is in turn is included in recipe server.rb which is part of my run list.

My recipe needs to do a large import into Postgresql and does the following (simplified) after Postgresql has been installed and tuned via the cookbook:

template "#{node['postgresql']['dir']}/postgresql.conf" do
  cookbook 'postgresql'
  source "postgresql.conf.erb"
  notifies node['postgresql']['server']['config_change_notify'], 'service[postgresql]', :immediately
  action :nothing
end

ruby_block 'update_postgresql_config_before_import' do
  block do
    node.default['postgresql']['config']['synchronous_commit'] = "off"
    node.default['postgresql']['config']['checkpoint_segments'] = 100
    node.default['postgresql']['config']['checkpoint_timeout'] = "10min"
    node.default['postgresql']['config']['checkpoint_completion_target'] = "0.9"
    node.default['postgresql']['config']['fsync'] = "off"
    node.default['postgresql']['config']['full_page_writes'] = "off"

    notifies :create, resources("template[#{node['postgresql']['dir']}/postgresql.conf]"), :immediately
  end
  action :nothing
end

ruby_block 'update_postgresql_config_after_import' do
  block do
    node.default['postgresql']['config']['fsync'] = "on"
    node.default['postgresql']['config']['full_page_writes'] = "on"

    notifies :create, resources("template[#{node['postgresql']['dir']}/postgresql.conf]"), :immediately
  end
  action :nothing
end

bash "drop DB" do
  user 'root'
  code <<-EOH
    sudo -u postgres psql postgres -c "DROP DATABASE IF EXISTS example"
  EOH
  notifies :run, 'ruby_block[update_postgresql_config_before_import]', :immediately
end

bash "import DB" do
  user 'root'
  code <<-EOH
    sudo -u postgres ./import-a-lot-of-data.sh
  EOH
  notifies :run, 'ruby_block[update_postgresql_config_after_import]', :immediately
end

So the intention of the above is to activate specific settings in postgresql.conf just before and after a big import takes place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment