@@ -1565,6 +1565,93 @@ sub psql
15651565
15661566=pod
15671567
1568+ =item $node->background_psql($dbname, \$stdin, \$stdout, $timer, %params) => harness
1569+
1570+ Invoke B<psql > on B<$dbname > and return an IPC::Run harness object, which the
1571+ caller may use to send input to B<psql > . The process's stdin is sourced from
1572+ the $stdin scalar reference, and its stdout and stderr go to the $stdout
1573+ scalar reference. This allows the caller to act on other parts of the system
1574+ while idling this backend.
1575+
1576+ The specified timer object is attached to the harness, as well. It's caller's
1577+ responsibility to select the timeout length, and to restart the timer after
1578+ each command if the timeout is per-command.
1579+
1580+ psql is invoked in tuples-only unaligned mode with reading of B<.psqlrc >
1581+ disabled. That may be overridden by passing extra psql parameters.
1582+
1583+ Dies on failure to invoke psql, or if psql fails to connect. Errors occurring
1584+ later are the caller's problem. psql runs with on_error_stop by default so
1585+ that it will stop running sql and return 3 if passed SQL results in an error.
1586+
1587+ Be sure to "finish" the harness when done with it.
1588+
1589+ =over
1590+
1591+ =item on_error_stop => 1
1592+
1593+ By default, the B<psql > method invokes the B<psql > program with ON_ERROR_STOP=1
1594+ set, so SQL execution is stopped at the first error and exit code 3 is
1595+ returned. Set B<on_error_stop > to 0 to ignore errors instead.
1596+
1597+ =item replication => B<value >
1598+
1599+ If set, add B<replication=value > to the conninfo string.
1600+ Passing the literal value C<database > results in a logical replication
1601+ connection.
1602+
1603+ =item extra_params => ['--single-transaction']
1604+
1605+ If given, it must be an array reference containing additional parameters to B<psql > .
1606+
1607+ =back
1608+
1609+ =cut
1610+
1611+ sub background_psql
1612+ {
1613+ my ($self , $dbname , $stdin , $stdout , $timer , %params ) = @_ ;
1614+
1615+ my $replication = $params {replication };
1616+
1617+ my @psql_params = (
1618+ ' psql' ,
1619+ ' -XAtq' ,
1620+ ' -d' ,
1621+ $self -> connstr($dbname )
1622+ . (defined $replication ? " replication=$replication " : " " ),
1623+ ' -f' ,
1624+ ' -' );
1625+
1626+ $params {on_error_stop } = 1 unless defined $params {on_error_stop };
1627+
1628+ push @psql_params , ' -v' , ' ON_ERROR_STOP=1' if $params {on_error_stop };
1629+ push @psql_params , @{ $params {extra_params } }
1630+ if defined $params {extra_params };
1631+
1632+ # Ensure there is no data waiting to be sent:
1633+ $$stdin = " " if ref ($stdin );
1634+ # IPC::Run would otherwise append to existing contents:
1635+ $$stdout = " " if ref ($stdout );
1636+
1637+ my $harness = IPC::Run::start \@psql_params ,
1638+ ' <' , $stdin , ' >' , $stdout , $timer ;
1639+
1640+ # Request some output, and pump until we see it. This means that psql
1641+ # connection failures are caught here, relieving callers of the need to
1642+ # handle those. (Right now, we have no particularly good handling for
1643+ # errors anyway, but that might be added later.)
1644+ my $banner = " background_psql: ready" ;
1645+ $$stdin = " \\ echo $banner \n " ;
1646+ pump $harness until $$stdout =~ / $banner / || $timer -> is_expired;
1647+
1648+ die " psql startup timed out" if $timer -> is_expired;
1649+
1650+ return $harness ;
1651+ }
1652+
1653+ =pod
1654+
15681655=item $node->interactive_psql($dbname, \$stdin, \$stdout, $timer, %params) => harness
15691656
15701657Invoke B<psql > on B<$dbname > and return an IPC::Run harness object,
0 commit comments