@@ -47,7 +47,7 @@ allowing to start, stop, backup and initialize it with various options.
4747The set of nodes managed by a given test is also managed by this module.
4848
4949In addition to node management, PostgresNode instances have some wrappers
50- around Test::More functions to run commands with anenvronment set up to
50+ around Test::More functions to run commands with anenvironment set up to
5151point to the instance.
5252
5353The IPC::Run module is required.
@@ -66,7 +66,6 @@ use File::Basename;
6666use File::Spec;
6767use File::Temp ();
6868use IPC::Run;
69- use PostgresNode;
7069use RecursiveCopy;
7170use Test::More;
7271use TestLib ();
@@ -347,6 +346,9 @@ On Windows, we use SSPI authentication to ensure the same (by pg_regress
347346pg_hba.conf is configured to allow replication connections. Pass the keyword
348347parameter hba_permit_replication => 0 to disable this.
349348
349+ postgresql.conf can be set up for replication by passing the keyword
350+ parameter allows_streaming => 1. This is disabled by default.
351+
350352The new node is set up in a fast but unsafe configuration where fsync is
351353disabled.
352354
@@ -360,7 +362,8 @@ sub init
360362my $host =$self -> host;
361363
362364$params {hba_permit_replication } = 1
363- if (!defined ($params {hba_permit_replication }));
365+ unless defined $params {hba_permit_replication };
366+ $params {allows_streaming } = 0unless defined $params {allows_streaming };
364367
365368mkdir $self -> backup_dir;
366369mkdir $self -> archive_dir;
@@ -373,6 +376,19 @@ sub init
373376print $conf " fsync = off\n " ;
374377print $conf " log_statement = all\n " ;
375378print $conf " port =$port \n " ;
379+
380+ if ($params {allows_streaming })
381+ {
382+ print $conf " wal_level = hot_standby\n " ;
383+ print $conf " max_wal_senders = 5\n " ;
384+ print $conf " wal_keep_segments = 20\n " ;
385+ print $conf " max_wal_size = 128MB\n " ;
386+ print $conf " shared_buffers = 1MB\n " ;
387+ print $conf " wal_log_hints = on\n " ;
388+ print $conf " hot_standby = on\n " ;
389+ print $conf " max_connections = 10\n " ;
390+ }
391+
376392if ($TestLib::windows_os )
377393{
378394print $conf " listen_addresses = '$host '\n " ;
@@ -384,7 +400,7 @@ sub init
384400}
385401close $conf ;
386402
387- $self -> set_replication_confif ( $params {hba_permit_replication }) ;
403+ $self -> set_replication_confif $params {hba_permit_replication };
388404}
389405
390406=pod
@@ -446,19 +462,23 @@ Does not start the node after init.
446462
447463A recovery.conf is not created.
448464
465+ Streaming replication can be enabled on this node by passing the keyword
466+ parameter has_streaming => 1. This is disabled by default.
467+
449468The backup is copied, leaving the original unmodified. pg_hba.conf is
450469unconditionally set to enable replication connections.
451470
452471=cut
453472
454473sub init_from_backup
455474{
456- my ($self ,$root_node ,$backup_name ) =@_ ;
475+ my ($self ,$root_node ,$backup_name , %params ) =@_ ;
457476my $backup_path =$root_node -> backup_dir .' /' .$backup_name ;
458477my $port =$self -> port;
459478my $node_name =$self -> name;
460479my $root_name =$root_node -> name;
461480
481+ $params {has_streaming } = 0unless defined $params {has_streaming };
462482print
463483" # Initializing node\" $node_name \" from backup\" $backup_name \" of node\" $root_name \"\n " ;
464484die " Backup\" $backup_name \" does not exist at$backup_path "
@@ -479,6 +499,7 @@ sub init_from_backup
479499port =$port
480500) );
481501$self -> set_replication_conf;
502+ $self -> enable_streaming($root_node )if $params {has_streaming };
482503}
483504
484505=pod
@@ -525,7 +546,7 @@ sub stop
525546my $port =$self -> port;
526547my $pgdata =$self -> data_dir;
527548my $name =$self -> name;
528- $mode =' fast' if (! defined ( $mode )) ;
549+ $mode =' fast' unless defined $mode ;
529550print " ### Stopping node\" $name \" using mode$mode \n " ;
530551TestLib::system_log(' pg_ctl' ,' -D' ,$pgdata ,' -m' ,$mode ,' stop' );
531552$self -> {_pid } =undef ;
@@ -536,7 +557,7 @@ sub stop
536557
537558=item $node->restart()
538559
539- wrapper for pg_ctl -w restart
560+ Wrapper for pg_ctl -w restart
540561
541562=cut
542563
@@ -553,6 +574,39 @@ sub restart
553574$self -> _update_pid;
554575}
555576
577+ =pod
578+
579+ =item $node->promote()
580+
581+ Wrapper for pg_ctl promote
582+
583+ =cut
584+
585+ sub promote
586+ {
587+ my ($self ) =@_ ;
588+ my $port =$self -> port;
589+ my $pgdata =$self -> data_dir;
590+ my $logfile =$self -> logfile;
591+ my $name =$self -> name;
592+ print " ### Promoting node\" $name \"\n " ;
593+ TestLib::system_log(' pg_ctl' ,' -D' ,$pgdata ,' -l' ,$logfile ,
594+ ' promote' );
595+ }
596+
597+ # Internal routine to enable streaming replication on a standby node.
598+ sub enable_streaming
599+ {
600+ my ($self ,$root_node ) =@_ ;
601+ my $root_connstr =$root_node -> connstr;
602+ my $name =$self -> name;
603+
604+ print " ### Enabling streaming replication for node\" $name \"\n " ;
605+ $self -> append_conf(' recovery.conf' ,qq(
606+ primary_conninfo='$root_connstr application_name=$name '
607+ standby_mode=on
608+ ) );
609+ }
556610
557611# Internal method
558612sub _update_pid
@@ -632,7 +686,7 @@ sub DESTROY
632686{
633687my $self =shift ;
634688my $name =$self -> name;
635- return if not defined $self -> {_pid };
689+ return unless defined $self -> {_pid };
636690print " ### Signalling QUIT to$self ->{_pid} for node\" $name \"\n " ;
637691TestLib::system_log(' pg_ctl' ,' kill' ,' QUIT' ,$self -> {_pid });
638692}
@@ -789,6 +843,7 @@ Run a command on the node, then verify that $expected_sql appears in the
789843server log file.
790844
791845Reads the whole log file so be careful when working with large log outputs.
846+ The log file is truncated prior to running the command, however.
792847
793848=cut
794849