Martin,

According to our conversation on IRC, here's the patch against latest
BZR version of postgresql-common to allow passing pg_ctl options from
command line and configuration file to pg_ctlcluster.

The syntax is:
pg_ctlcluster version cluster -- pg_ctl_option

This is useful for run-time options like '-m smart" when doing
maintenance.


This patch also introduces a new configuration file called
/etc/postgresql/version/cluster/pg_ctl.conf in which you can pass
default arguments that will be called at boot time from initscripts.

The syntax is:
pg_ctl_options = 'OPTIONS'

This is quite useful when you want to activate options on the long term
run, like '-c'.


I updated the documentation accordingly [1], wrote tests for the new
features [2] and modified the other tools for coherence [3].

My patch also ships a test to ensure that the user postgres in the UNIX
group ssl-sert because tests will silently fail otherwise and fixes a
small issue in t/TestLib.pm regarding open files.

Regards,

[1] architecture.html, debian/changelog, pg_ctlcluster(8)
[2] t/085_pg_ctl.conf.t
[3] pg_createcluster, pg_dropcluster, pg_upgradecluster
-- 
 ,''`.
: :' :      Cyril Bouthors
`. `'         Debian.org
  `-
=== modified file 'PgCommon.pm'
--- PgCommon.pm	2009-02-15 12:54:33 +0000
+++ PgCommon.pm	2009-03-17 11:42:32 +0000
@@ -11,7 +11,7 @@
 our @ISA = ('Exporter');
 our @EXPORT = qw/error user_cluster_map get_cluster_port set_cluster_port
     get_cluster_socketdir set_cluster_socketdir cluster_port_running
-    get_cluster_start_conf set_cluster_start_conf
+    get_cluster_start_conf set_cluster_start_conf set_cluster_pg_ctl_conf
     get_program_path cluster_info get_versions get_newest_version version_exists
     get_version_clusters next_free_port cluster_exists install_file
     change_ugid config_bool get_db_encoding get_cluster_locales
@@ -408,6 +408,29 @@
     close F;
 }
 
+# Change pg_ctl.conf setting.
+# Arguments: <version> <cluster>
+# <value> = options passed to pg_ctl(1)
+sub set_cluster_pg_ctl_conf {
+    my ($v, $c, $val) = @_;
+
+    my $perms = 0644;
+
+    # pg_ctl.conf setting
+    my $pg_ctl_conf = "$confroot/$v/$c/pg_ctl.conf";
+    my $text = "# Automatic pg_ctl configuration
+# This configuration file contains cluster specific options to be passed to
+# pg_ctl(1).
+
+pg_ctl_options = '$val'
+";
+
+    open F, '>' . $pg_ctl_conf or error "Could not open $pg_ctl_conf for writing: $!";
+    chmod $perms, $pg_ctl_conf;
+    print F $text;
+    close F;
+}
+
 # Return a hash with information about a specific cluster.
 # Arguments: <version> <cluster name>
 # Returns: information hash (keys: pgdata, port, running, logfile [unless it

=== modified file 'architecture.html'
--- architecture.html	2009-02-15 12:55:03 +0000
+++ architecture.html	2009-03-13 10:42:15 +0000
@@ -93,6 +93,7 @@
         <code>manual</code> (do not start/stop in init script, but manual
         control with <code>pg_ctlcluster</code> is possible), <i>disabled</i>
         (<code>pg_ctlcluster</code> is not allowed).</li>
+      <li>optionally <code>pg_ctl.conf</code>: options to be passed to pg_ctl.
       <li>optionally a symbolic link <code>log</code> which points to
       the postmaster log file. Defaults to
       <code>/var/log/postgresql/postgresql-</code><i>version</i><code>-</code><i>cluster</i><code>.conf</code>.  

=== modified file 'debian/changelog'
--- debian/changelog	2009-02-28 12:45:39 +0000
+++ debian/changelog	2009-03-13 11:25:52 +0000
@@ -2,6 +2,8 @@
 
   * t/030_errors.t: Fix "no space left on device" test for non-English
     locales.
+  * Added support for pg_ctl.conf by Cyril Bouthors <cy...@bouthors.org>
+    (Closes: #492843)
 
  -- Martin Pitt <mp...@debian.org>  Sat, 28 Feb 2009 13:45:13 +0100
 

=== modified file 'pg_createcluster'
--- pg_createcluster	2009-02-15 12:55:03 +0000
+++ pg_createcluster	2009-03-13 15:24:29 +0000
@@ -270,11 +270,15 @@
 # create default "start" file
 set_cluster_start_conf $version, $cluster, $startconf;
 
+# create default pg_ctl.conf file
+set_cluster_pg_ctl_conf $version, $cluster, '';
+
 # move conffiles, setup permissions
 move_conffile "$datadir/postgresql.conf", $confdir, $owneruid, $ownergid, '644';
 move_conffile "$datadir/pg_hba.conf", $confdir, $owneruid, $ownergid, '640', 'hba_file';
 move_conffile "$datadir/pg_ident.conf", $confdir, $owneruid, $ownergid, '640', 'ident_file';
 chown $owneruid, $ownergid, $datadir, $confdir, "$confdir/start.conf" or die "chown: $!";
+chown $owneruid, $ownergid, $datadir, $confdir, "$confdir/pg_ctl.conf" or die "chown: $!";
 
 PgCommon::set_conf_value $version, $cluster, 'postgresql.conf', 'data_directory', $datadir;
 
@@ -422,9 +426,9 @@
 I<name>, it creates the necessary configuration files in
 C</etc/postgresql/>I<version>C</>I<name>C</>; in particular these are
 C<postgresql.conf>, C<pg_ident.conf>, C<pg_hba.conf>, a postgresql-common
-specific configuration file C<start.conf> (see B<STARTUP CONTROL> below), and a
-symbolic link C<log> which points to the log file (by default,
-C</var/log/postgresql/postgresql->I<version>C<->I<name>C<.log>).
+specific configuration file C<start.conf> (see B<STARTUP CONTROL> below),
+C<pg_ctl.conf> and a symbolic link C<log> which points to the log file (by
+default, C</var/log/postgresql/postgresql->I<version>C<->I<name>C<.log>).
 
 C<postgresql.conf> is automatically adapted to use the next available port, i.
 e. the first port (starting from 5432) which is not yet used by an already
@@ -569,6 +573,9 @@
 lower level tools to control the postmaster process; this option is only meant
 to prevent accidents during maintenance, not more.
 
+The C<pg_ctl.conf> file in the cluster configuration directory controls the
+options passed to pg_ctl of that cluster.
+
 =back
 
 =head1 SEE ALSO

=== modified file 'pg_ctlcluster'
--- pg_ctlcluster	2009-02-15 13:53:59 +0000
+++ pg_ctlcluster	2009-03-17 11:36:44 +0000
@@ -5,6 +5,7 @@
 # stops on 'stop'.
 #
 # (C) 2005 Martin Pitt <mp...@debian.org>
+# (C) 2009 Cyril Bouthors <cy...@bouthors.org>
 
 use lib '/usr/share/postgresql-common';
 use Getopt::Long;
@@ -210,6 +211,13 @@
         }
         $logsize = (stat $info{'logfile'})[7];
     }
+
+    push @options, $pg_ctl_opts_from_cli if $pg_ctl_opts_from_cli ne '';
+
+    my %pg_ctl_opts_from_file = read_cluster_conf_file $version, $cluster, 'pg_ctl.conf';
+    push @options, $pg_ctl_opts_from_file{'pg_ctl_options'}
+	if defined $pg_ctl_opts_from_file{'pg_ctl_options'} and $pg_ctl_opts_from_file{'pg_ctl_options'} ne '';
+
     push @options, ('-s', '-o', $postmaster_opts);
 
     if (fork) {
@@ -311,12 +319,18 @@
 exit 1 unless GetOptions ('o|options=s' => \...@postmaster_auxoptions,
     'f|force' => \$force);
 
-if ($#ARGV != 2) {
+if ($#ARGV < 2) {
     print "Usage: $0 <version> <cluster> <action>\n";
     exit 1;
 }
 
-($version, $cluster, $action) = @ARGV;
+$version = shift @ARGV;
+$cluster = shift @ARGV;
+$action = shift @ARGV;
+
+$pg_ctl_opts_from_cli = (join ' ', @ARGV);
+($pg_ctl_opts_from_cli) = $pg_ctl_opts_from_cli =~ /(.*)/; # untaint
+
 ($version) = $version =~ /^(\d+\.\d+)$/; # untaint
 ($cluster) = $cluster =~ /^([^'"\s]+)$/; # untaint
 error 'specified cluster does not exist' unless $version && $cluster && cluster_exists $version, $cluster;
@@ -372,7 +386,7 @@
 
 =head1 SYNOPSIS
 
-B<pg_ctlcluster> [I<options>] I<cluster-version> I<cluster-name> I<action>
+B<pg_ctlcluster> [I<options>] I<cluster-version> I<cluster-name> I<action> B<--> [I<pg-ctl-options>]
 
 where I<action> = B<start>|B<stop>|B<restart>|B<reload>
 
@@ -434,6 +448,22 @@
 possible to specify B<-o> multiple times. See L<postmaster(1)> for a
 description of valid options.
 
+=item I<pg-ctl-options>
+
+Pass given I<pg-ctl-options> as command line option to pg_ctl. See L<pg_ctl(1)>
+for a description of valid options.
+
+=back
+
+=head1 FILES
+
+=over 4
+
+=item C</etc/postgresql/>I<cluster-version>C</>I<cluster-name>C</pg_ctl.options>
+
+This configuration file contains cluster specific options to be passed to
+L<pg_ctl(1)>.
+
 =back
 
 =head1 SEE ALSO

=== modified file 'pg_dropcluster'
--- pg_dropcluster	2009-02-15 12:54:33 +0000
+++ pg_dropcluster	2009-03-13 10:35:44 +0000
@@ -66,7 +66,7 @@
 }
 unlink $c.'/pg_hba.conf', $c.'/pg_ident.conf', $c.'/postgresql.conf',
     $c.'/start.conf', $c.'/log', $c.'/autovacuum_log', $c.'/pgdata',
-    $c.'/environment';
+    $c.'/environment', $c.'/pg_ctl.conf';
 unlink $info{'logfile'} if defined ($info{'logfile'});
 if ($info{'socketdir'} !~ /^(\/tmp|\/var\/run\/postgresql)\/?$/) {
     rmdir $info{'socketdir'};

=== modified file 'pg_upgradecluster'
--- pg_upgradecluster	2009-02-15 14:04:23 +0000
+++ pg_upgradecluster	2009-03-17 11:57:37 +0000
@@ -450,6 +450,11 @@
     install_file $info{'configdir'}.'/start.conf', $newinfo{'configdir'},
 	$newinfo{'owneruid'}, $newinfo{'ownergid'}, "644";
 }
+if ( -e $info{'configdir'}.'/pg_ctl.conf') {
+    print "Copying old pg_ctl.conf...\n";
+    install_file $info{'configdir'}.'/pg_ctl.conf', $newinfo{'configdir'},
+	$newinfo{'owneruid'}, $newinfo{'ownergid'}, "644";
+}
 
 adapt_conffiles;
 

=== modified file 't/001_packages.t'
--- t/001_packages.t	2009-02-15 12:55:03 +0000
+++ t/001_packages.t	2009-03-13 14:59:06 +0000
@@ -6,7 +6,7 @@
 use TestLib;
 use POSIX qw/setlocale LC_ALL/;
 
-use Test::More tests => 4 + ($#MAJORS+1)*4;
+use Test::More tests => 5 + ($#MAJORS+1)*4;
 
 foreach my $v (@MAJORS) {
     ok ((deb_installed "postgresql-$v"), "postgresql-$v installed");
@@ -23,4 +23,6 @@
 ok (setlocale (LC_ALL, "ru_RU"), 'locale ru_RU exists');
 ok (setlocale (LC_ALL, "ru_RU.UTF-8"), 'locale ru_RU.UTF-8 exists');
 
+ok ((getgrnam("ssl-cert"))[3] =~ /postgres/, 'user postgres in the UNIX group ssl-sert');
+
 # vim: filetype=perl

=== added file 't/085_pg_ctl.conf.t'
--- t/085_pg_ctl.conf.t	1970-01-01 00:00:00 +0000
+++ t/085_pg_ctl.conf.t	2009-03-17 11:54:55 +0000
@@ -0,0 +1,76 @@
+# Check pg_ctl.conf handling.
+
+use strict; 
+
+use lib 't';
+use TestLib;
+
+use lib '/usr/share/postgresql-common';
+use PgCommon;
+
+use Test::More tests => 35;
+
+# Do test with oldest version
+my $v = $MAJORS[0];
+
+is ((system "pg_createcluster $v main >/dev/null"), 0, "pg_createcluster $v main");
+
+ok (-f "/etc/postgresql/$v/main/pg_ctl.conf", "/etc/postgresql/$v/main/pg_ctl.conf exists");
+
+# Default behaviour, core size=0
+is_program_out 'postgres', "pg_ctlcluster $v main start", 0, '', "starting cluster";
+
+is_program_out 'postgres', "xargs -i awk '/core/ {print \$5}' /proc/{}/limits < /var/run/postgresql/$v-main.pid", 0, "0\n", "soft core size is 0";
+
+# -c in pg_ctl.conf, core size=unlimited
+ok (set_cluster_pg_ctl_conf($v, 'main', '-c'), "set pg_ctl default option to -c");
+
+is_program_out 'postgres', "pg_ctlcluster $v main restart", 0, '', "restarting cluster";
+
+is_program_out 'postgres', "xargs -i awk '/core/ {print \$5}' /proc/{}/limits < /var/run/postgresql/$v-main.pid", 0, "unlimited\n", "soft core size is unlimited";
+
+# Back to default behaviour, core size=0
+
+ok (set_cluster_pg_ctl_conf($v, 'main', ''), "restored pg_ctl default option");
+
+is_program_out 'postgres', "pg_ctlcluster $v main restart", 0, '', "restarting cluster";
+
+is_program_out 'postgres', "xargs -i awk '/core/ {print \$5}' /proc/{}/limits < /var/run/postgresql/$v-main.pid", 0, "0\n", "soft core size is 0";
+
+# pg_ctl -c, core size=unlimited
+
+is_program_out 'postgres', "pg_ctlcluster $v main restart -- -c", 0, '', "restarting cluster with -c on the command line";
+
+is_program_out 'postgres', "xargs -i awk '/core/ {print \$5}' /proc/{}/limits < /var/run/postgresql/$v-main.pid", 0, "unlimited\n", "soft core size is unlimited";
+
+# upgrade cluster
+if ($#MAJORS == 0) {
+    pass 'only one major version installed, skipping upgrade test';
+    pass '...';
+} else {
+    like_program_out 0, "pg_upgradecluster $v main", 0, qr/Success/;
+}
+
+# check pg_ctl.conf of old and upgraded cluster
+ok (-f "/etc/postgresql/$v/main/pg_ctl.conf", "/etc/postgresql/$v/main/pg_ctl.conf exists");
+ok (-f "/etc/postgresql/$MAJORS[-1]/main/pg_ctl.conf", "/etc/postgresql/$v/main/pg_ctl.conf exists");
+
+# clean up
+if ($#MAJORS == 0) {
+    pass '...';
+} else {
+    is ((system "pg_dropcluster $v main"), 0, 
+        'dropping old cluster');
+}
+
+is ((system "pg_dropcluster $MAJORS[-1] main --stop"), 0, 
+    'dropping upgraded cluster');
+
+is_program_out 'postgres', 'pg_lsclusters -h', 0, '', 'no clusters any more';
+
+# Check if pg_ctl.conf was removed
+ok (! -f "/etc/postgresql/$v/main/pg_ctl.conf", "/etc/postgresql/$v/main/pg_ctl.conf removed");
+
+check_clean;
+
+# vim: filetype=perl

=== modified file 't/TestLib.pm'
--- t/TestLib.pm	2009-02-15 12:54:33 +0000
+++ t/TestLib.pm	2009-03-13 16:08:52 +0000
@@ -30,6 +30,7 @@
     while (<DPKG>) {
 	return 1 if /^Version:/;
     }
+    close DPKG;
 
     return 0;
 }

Reply via email to