Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7c34555

Browse files
committed
Add test for session_preload_libraries and parameter permissions checks.
We weren't exercising the session_preload_libraries option in any meaningful way. auto_explain is a good testbed for doing so, since it's one of the primary use-cases for session_preload_libraries. Hence, adjust its TAP test to load the library via session_preload_libraries not shared_preload_libraries. While at it, feed test-specific settings to the backend via PGOPTIONS rather than tediously rewriting postgresql.conf. Also, since auto_explain has some PGC_SUSET parameters, we can use it to provide a test case for the permissions-checking bug just fixed by commit b35617d. Back-patch to v15 so that we have coverage for the permissions issue in that branch too. To do that, I back-patched the refactoring recently done by commit 550bc0a. Dagfinn Ilmari Mannsåker and Tom Lane Discussion: https://postgr.es/m/CABwTF4VEpwTHhRQ+q5MiC5ucngN-whN-PdcKeufX7eLSoAfbZA@mail.gmail.com
1 parent a45388d commit 7c34555

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

contrib/auto_explain/t/001_auto_explain.pl

+48-26
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,28 @@
99
use Test::More;
1010

1111
# Runs the specified query and returns the emitted server log.
12-
# If any parameters are specified, these are set in postgresql.conf,
13-
# and reset after the query is run.
12+
# params is an optional hash mapping GUC names to values;
13+
# any such settings are transmitted to the backend via PGOPTIONS.
1414
sub query_log
1515
{
1616
my ($node, $sql, $params) = @_;
1717
$params ||= {};
1818

19-
if (keys %$params)
20-
{
21-
for my $key (keys %$params)
22-
{
23-
$node->append_conf('postgresql.conf', "$key = $params->{$key}\n");
24-
}
25-
$node->reload;
26-
}
19+
local $ENV{PGOPTIONS} = join " ",
20+
map { "-c $_=$params->{$_}" } keys %$params;
2721

2822
my $log = $node->logfile();
2923
my $offset = -s $log;
3024

3125
$node->safe_psql("postgres", $sql);
3226

33-
my $log_contents = slurp_file($log, $offset);
34-
35-
if (keys %$params)
36-
{
37-
for my $key (keys %$params)
38-
{
39-
$node->adjust_conf('postgresql.conf', $key, undef);
40-
}
41-
$node->reload;
42-
}
43-
44-
return $log_contents;
27+
return slurp_file($log, $offset);
4528
}
4629

4730
my $node = PostgreSQL::Test::Cluster->new('main');
4831
$node->init;
4932
$node->append_conf('postgresql.conf',
50-
"shared_preload_libraries = 'auto_explain'");
33+
"session_preload_libraries = 'auto_explain'");
5134
$node->append_conf('postgresql.conf', "auto_explain.log_min_duration = 0");
5235
$node->append_conf('postgresql.conf', "auto_explain.log_analyze = on");
5336
$node->start;
@@ -126,12 +109,12 @@ sub query_log
126109
# JSON format.
127110
$log_contents = query_log(
128111
$node,
129-
"SELECT * FROM pg_proc;",
112+
"SELECT * FROM pg_class;",
130113
{ "auto_explain.log_format" => "json" });
131114

132115
like(
133116
$log_contents,
134-
qr/"Query Text": "SELECT \* FROM pg_proc;"/,
117+
qr/"Query Text": "SELECT \* FROM pg_class;"/,
135118
"query text logged, json mode");
136119

137120
unlike(
@@ -141,7 +124,7 @@ sub query_log
141124

142125
like(
143126
$log_contents,
144-
qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_proc"/s,
127+
qr/"Node Type": "Seq Scan"[^}]*"Relation Name": "pg_class"/s,
145128
"sequential scan logged, json mode");
146129

147130
# Prepared query in JSON format.
@@ -160,4 +143,43 @@ sub query_log
160143
qr/"Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/s,
161144
"index scan logged, json mode");
162145

146+
# Check that PGC_SUSET parameters can be set by non-superuser if granted,
147+
# otherwise not
148+
149+
$node->safe_psql(
150+
"postgres", q{
151+
CREATE USER regress_user1;
152+
GRANT SET ON PARAMETER auto_explain.log_format TO regress_user1;
153+
});
154+
155+
$ENV{PGUSER} = "regress_user1";
156+
157+
$log_contents = query_log(
158+
$node,
159+
"SELECT * FROM pg_database;",
160+
{ "auto_explain.log_format" => "json" });
161+
162+
like(
163+
$log_contents,
164+
qr/"Query Text": "SELECT \* FROM pg_database;"/,
165+
"query text logged, json mode selected by non-superuser");
166+
167+
$log_contents = query_log(
168+
$node,
169+
"SELECT * FROM pg_database;",
170+
{ "auto_explain.log_level" => "log" });
171+
172+
like(
173+
$log_contents,
174+
qr/WARNING: permission denied to set parameter "auto_explain\.log_level"/,
175+
"permission failure logged");
176+
177+
$ENV{PGUSER} = undef;
178+
179+
$node->safe_psql(
180+
"postgres", q{
181+
REVOKE SET ON PARAMETER auto_explain.log_format FROM regress_user1;
182+
DROP USER regress_user1;
183+
});
184+
163185
done_testing();

0 commit comments

Comments
 (0)