use Test::More;
# Runs the specified query and returns the emitted server log.
-# If any parameters are specified, these are set in postgresql.conf,
-# and reset after the query is run.
+# params is an optional hash mapping GUC names to values;
+# any such settings are transmitted to the backend via PGOPTIONS.
sub query_log
{
my ($node, $sql, $params) = @_;
$params ||= {};
- if (keys %$params)
- {
- for my $key (keys %$params)
- {
- $node->append_conf('postgresql.conf', "$key = $params->{$key}\n");
- }
- $node->reload;
- }
+ local $ENV{PGOPTIONS} = join " ",
+ map { "-c $_=$params->{$_}" } keys %$params;
my $log = $node->logfile();
my $offset = -s $log;
$node->safe_psql("postgres", $sql);
- my $log_contents = slurp_file($log, $offset);
-
- if (keys %$params)
- {
- for my $key (keys %$params)
- {
- $node->adjust_conf('postgresql.conf', $key, undef);
- }
- $node->reload;
- }
-
- return $log_contents;
+ return slurp_file($log, $offset);
}
my $node = PostgreSQL::Test::Cluster->new('main');
$node->init;
$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_analyze = on");
$node->start;
# JSON format.
$log_contents = query_log(
$node,
- "SELECT * FROM pg_proc;",
+ "SELECT * FROM pg_class;",
{ "auto_explain.log_format" => "json" });
like(
$log_contents,
- qr/"Query Text": "SELECT \* FROM pg_proc;"/,
+ qr/"Query Text": "SELECT \* FROM pg_class;"/,
"query text logged, json mode");
unlike(
like(
$log_contents,
- qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_proc"/s,
+ qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_class"/s,
"sequential scan logged, json mode");
# Prepared query in JSON format.
qr/"Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/s,
"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();