@ -9,45 +9,28 @@ use PostgreSQL::Test::Utils;
use Test::More ;
use Test::More ;
# Runs the specified query and returns the emitted server log.
# Runs the specified query and returns the emitted server log.
# If any parameters are specified, these are set in postgresql.conf,
# params is an optional hash mapping GUC names to values;
# and reset after the query is run .
# any such settings are transmitted to the backend via PGOPTIONS .
sub query_log
sub query_log
{
{
my ( $ node , $ sql , $ params ) = @ _ ;
my ( $ node , $ sql , $ params ) = @ _ ;
$ params || = { } ;
$ params || = { } ;
if ( keys %$ params )
local $ ENV { PGOPTIONS } = join " " ,
{
map { "-c $_=$params->{$_}" } keys %$ params ;
for my $ key ( keys %$ params )
{
$ node - > append_conf ( 'postgresql.conf' , "$key = $params->{$key}\n" ) ;
}
$ node - > reload ;
}
my $ log = $ node - > logfile ( ) ;
my $ log = $ node - > logfile ( ) ;
my $ offset = - s $ log ;
my $ offset = - s $ log ;
$ node - > safe_psql ( "postgres" , $ sql ) ;
$ node - > safe_psql ( "postgres" , $ sql ) ;
my $ log_contents = slurp_file ( $ log , $ offset ) ;
return slurp_file ( $ log , $ offset ) ;
if ( keys %$ params )
{
for my $ key ( keys %$ params )
{
$ node - > adjust_conf ( 'postgresql.conf' , $ key , undef ) ;
}
$ node - > reload ;
}
return $ log_contents ;
}
}
my $ node = PostgreSQL::Test::Cluster - > new ( 'main' ) ;
my $ node = PostgreSQL::Test::Cluster - > new ( 'main' ) ;
$ node - > init ;
$ node - > init ;
$ node - > append_conf ( 'postgresql.conf' ,
$ node - > append_conf ( 'postgresql.conf' ,
"shared _preload_libraries = 'auto_explain'" ) ;
"session_preload_libraries = 'auto_explain'" ) ;
$ node - > append_conf ( 'postgresql.conf' , "auto_explain.log_min_duration = 0" ) ;
$ node - > append_conf ( 'postgresql.conf' , "auto_explain.log_min_duration = 0" ) ;
$ node - > append_conf ( 'postgresql.conf' , "auto_explain.log_analyze = on" ) ;
$ node - > append_conf ( 'postgresql.conf' , "auto_explain.log_analyze = on" ) ;
$ node - > start ;
$ node - > start ;
@ -126,12 +109,12 @@ unlike(
# JSON format.
# JSON format.
$ log_contents = query_log (
$ log_contents = query_log (
$ node ,
$ node ,
"SELECT * FROM pg_pro c;" ,
"SELECT * FROM pg_class ;" ,
{ "auto_explain.log_format" = > "json" } ) ;
{ "auto_explain.log_format" = > "json" } ) ;
like (
like (
$ log_contents ,
$ log_contents ,
qr/"Query Text": "SELECT \* FROM pg_pro c;"/ ,
qr/"Query Text": "SELECT \* FROM pg_class ;"/ ,
"query text logged, json mode" ) ;
"query text logged, json mode" ) ;
unlike (
unlike (
@ -141,7 +124,7 @@ unlike(
like (
like (
$ log_contents ,
$ log_contents ,
qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_pro c"/ s ,
qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_class "/ s ,
"sequential scan logged, json mode" ) ;
"sequential scan logged, json mode" ) ;
# Prepared query in JSON format.
# Prepared query in JSON format.
@ -160,4 +143,43 @@ like(
qr/"Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/ s ,
qr/"Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/ s ,
"index scan logged, json mode" ) ;
"index scan logged, json mode" ) ;
# Check that PGC_SUSET parameters can be set by non-superuser if granted,
# otherwise not
$ node - > safe_psql (
"postgres" , q{
CREATE USER regress_user1 ;
GRANT SET ON PARAMETER auto_explain . log_format TO regress_user1 ;
} ) ;
$ ENV { PGUSER } = "regress_user1" ;
$ log_contents = query_log (
$ node ,
"SELECT * FROM pg_database;" ,
{ "auto_explain.log_format" = > "json" } ) ;
like (
$ log_contents ,
qr/"Query Text": "SELECT \* FROM pg_database;"/ ,
"query text logged, json mode selected by non-superuser" ) ;
$ log_contents = query_log (
$ node ,
"SELECT * FROM pg_database;" ,
{ "auto_explain.log_level" = > "log" } ) ;
like (
$ log_contents ,
qr/WARNING: permission denied to set parameter "auto_explain\.log_level"/ ,
"permission failure logged" ) ;
$ ENV { PGUSER } = undef ;
$ node - > safe_psql (
"postgres" , q{
REVOKE SET ON PARAMETER auto_explain . log_format FROM regress_user1 ;
DROP USER regress_user1 ;
} ) ;
done_testing ( ) ;
done_testing ( ) ;