Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Add option force_initdb to PostgreSQL::Test::Cluster:init()
authorMichael Paquier <michael@paquier.xyz>
Wed, 21 Feb 2024 04:28:51 +0000 (13:28 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 21 Feb 2024 04:28:51 +0000 (13:28 +0900)
This option is useful to bypass the default behavior of init() which
would create the data folder of a new cluster by copying it from a
template previously initdb'd, if any.  Copying the data folder is much
cheaper than running initdb, but some tests may want to force that.  For
example, one scenario of pg_combinebackup updated in this commit needs a
different system ID for two nodes.

Previously, this could only be achieved by unsetting
$ENV{'INITDB_TEMPLATE'}, which could become a problem in complex node
setups by making tests less efficient.

Author: Amul Sul
Reviewed-by: Robert Haas, Michael Paquier
Discussion: https://postgr.es/m/Zc1tX9lLonLGu6oH@paquier.xyz

src/bin/pg_combinebackup/t/005_integrity.pl
src/test/perl/PostgreSQL/Test/Cluster.pm

index 3b445d0e30fa55d88c396688fa21ccc102bdbf05..5dc71ddcf85ce9096cf5dc06f006920f6671db9e 100644 (file)
@@ -18,18 +18,12 @@ $node1->init(has_archiving => 1, allows_streaming => 1);
 $node1->append_conf('postgresql.conf', 'summarize_wal = on');
 $node1->start;
 
-# Set up another new database instance. We don't want to use the cached
-# INITDB_TEMPLATE for this, because we want it to be a separate cluster
-# with a different system ID.
-my $node2;
-{
-   local $ENV{'INITDB_TEMPLATE'} = undef;
-
-   $node2 = PostgreSQL::Test::Cluster->new('node2');
-   $node2->init(has_archiving => 1, allows_streaming => 1);
-   $node2->append_conf('postgresql.conf', 'summarize_wal = on');
-   $node2->start;
-}
+# Set up another new database instance.  force_initdb is used because
+# we want it to be a separate cluster with a different system ID.
+my $node2 = PostgreSQL::Test::Cluster->new('node2');
+$node2->init(force_initdb => 1, has_archiving => 1, allows_streaming => 1);
+$node2->append_conf('postgresql.conf', 'summarize_wal = on');
+$node2->start;
 
 # Take a full backup from node1.
 my $backup1path = $node1->backup_dir . '/backup1';
index cfaf91ec63922c543fd6e5c81cda4904123a2c7d..44c1bb5afd07e27855fbcc08e4c2165c5786c2bb 100644 (file)
@@ -503,6 +503,9 @@ parameter allows_streaming => 'logical' or 'physical' (passing 1 will also
 suffice for physical replication) depending on type of replication that
 should be enabled. This is disabled by default.
 
+force_initdb => 1 will force the initialization of the cluster with a new
+initdb rather than copying the data folder from a template.
+
 The new node is set up in a fast but unsafe configuration where fsync is
 disabled.
 
@@ -518,6 +521,7 @@ sub init
    local %ENV = $self->_get_env();
 
    $params{allows_streaming} = 0 unless defined $params{allows_streaming};
+   $params{force_initdb} = 0 unless defined $params{force_initdb};
    $params{has_archiving} = 0 unless defined $params{has_archiving};
 
    my $initdb_extra_opts_env = $ENV{PG_TEST_INITDB_EXTRA_OPTS};
@@ -529,14 +533,17 @@ sub init
    mkdir $self->backup_dir;
    mkdir $self->archive_dir;
 
-   # If available and if there aren't any parameters, use a previously
-   # initdb'd cluster as a template by copying it. For a lot of tests, that's
-   # substantially cheaper. Do so only if there aren't parameters, it doesn't
-   # seem worth figuring out whether they affect compatibility.
+   # If available, if there aren't any parameters and if force_initdb is
+   # disabled, use a previously initdb'd cluster as a template by copying it.
+   # For a lot of tests, that's substantially cheaper. It does not seem
+   # worth figuring out whether extra parameters affect compatibility, so
+   # initdb is forced if any are defined.
    #
    # There's very similar code in pg_regress.c, but we can't easily
    # deduplicate it until we require perl at build time.
-   if (defined $params{extra} or !defined $ENV{INITDB_TEMPLATE})
+   if (   $params{force_initdb}
+       or defined $params{extra}
+       or !defined $ENV{INITDB_TEMPLATE})
    {
        note("initializing database system by running initdb");
        PostgreSQL::Test::Utils::system_or_bail('initdb', '-D', $pgdata, '-A',