From 70291a3c66eca599fd9f59f7f6051432b2020f4b Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 7 Nov 2024 12:11:27 +0900 Subject: [PATCH] Improve handling of empty query results in BackgroundPsql::query() A newline is not added at the end of an empty query result, causing the banner of the hardcoded \echo to not be discarded. This would reflect on scripts that expect an empty result by showing the "QUERY_SEPARATOR" in the output returned back to the caller, which was confusing. This commit changes BackgroundPsql::query() so as empty results are able to work correctly, making the first newline before the banner optional, bringing more flexibility. Note that this change affects 037_invalid_database.pl, where three queries generated an empty result, with the script relying on the data from the hardcoded banner to exist in the expected output. These queries are changed to use query_safe(), leading to a simpler script. The author has also proposed a test in a different patch where empty results would exist when using BackgroundPsql. Author: Jacob Champion Reviewed-by: Andrew Dunstan, Michael Paquier Discussion: https://postgr.es/m/CAOYmi+=60deN20WDyCoHCiecgivJxr=98s7s7-C8SkXwrCfHXg@mail.gmail.com --- src/test/perl/PostgreSQL/Test/BackgroundPsql.pm | 6 ++++-- src/test/recovery/t/037_invalid_database.pl | 14 +++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm b/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm index c7a3d3e920d..2216918b253 100644 --- a/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm +++ b/src/test/perl/PostgreSQL/Test/BackgroundPsql.pm @@ -236,8 +236,10 @@ sub query die "psql query timed out" if $self->{timeout}->is_expired; $output = $self->{stdout}; - # remove banner again, our caller doesn't care - $output =~ s/\n$banner\n$//s; + # Remove banner again, our caller doesn't care. The first newline is + # optional, as there would not be one if consuming an empty query + # result. + $output =~ s/\n?$banner\n$//s; # clear out output for the next query $self->{stdout} = ''; diff --git a/src/test/recovery/t/037_invalid_database.pl b/src/test/recovery/t/037_invalid_database.pl index 6d1c7117964..3735086aee4 100644 --- a/src/test/recovery/t/037_invalid_database.pl +++ b/src/test/recovery/t/037_invalid_database.pl @@ -96,13 +96,12 @@ my $bgpsql = $node->background_psql('postgres', on_error_stop => 0); my $pid = $bgpsql->query('SELECT pg_backend_pid()'); # create the database, prevent drop database via lock held by a 2PC transaction -ok( $bgpsql->query_safe( - qq( +$bgpsql->query_safe( + qq( CREATE DATABASE regression_invalid_interrupt; BEGIN; LOCK pg_tablespace; - PREPARE TRANSACTION 'lock_tblspc';)), - "blocked DROP DATABASE completion"); + PREPARE TRANSACTION 'lock_tblspc';)); # Try to drop. This will wait due to the still held lock. $bgpsql->query_until(qr//, "DROP DATABASE regression_invalid_interrupt;\n"); @@ -135,11 +134,8 @@ is($node->psql('regression_invalid_interrupt', ''), # To properly drop the database, we need to release the lock previously preventing # doing so. -ok($bgpsql->query_safe(qq(ROLLBACK PREPARED 'lock_tblspc')), - "unblock DROP DATABASE"); - -ok($bgpsql->query(qq(DROP DATABASE regression_invalid_interrupt)), - "DROP DATABASE invalid_interrupt"); +$bgpsql->query_safe(qq(ROLLBACK PREPARED 'lock_tblspc')); +$bgpsql->query_safe(qq(DROP DATABASE regression_invalid_interrupt)); $bgpsql->quit();