9
9
use Test::More;
10
10
11
11
# 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 .
14
14
sub query_log
15
15
{
16
16
my ($node , $sql , $params ) = @_ ;
17
17
$params ||= {};
18
18
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 ;
27
21
28
22
my $log = $node -> logfile();
29
23
my $offset = -s $log ;
30
24
31
25
$node -> safe_psql(" postgres" , $sql );
32
26
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 );
45
28
}
46
29
47
30
my $node = PostgreSQL::Test::Cluster-> new(' main' );
48
31
$node -> init;
49
32
$node -> append_conf(' postgresql.conf' ,
50
- " shared_preload_libraries = 'auto_explain'" );
33
+ " session_preload_libraries = 'auto_explain'" );
51
34
$node -> append_conf(' postgresql.conf' , " auto_explain.log_min_duration = 0" );
52
35
$node -> append_conf(' postgresql.conf' , " auto_explain.log_analyze = on" );
53
36
$node -> start;
@@ -126,12 +109,12 @@ sub query_log
126
109
# JSON format.
127
110
$log_contents = query_log(
128
111
$node ,
129
- " SELECT * FROM pg_proc ;" ,
112
+ " SELECT * FROM pg_class ;" ,
130
113
{ " auto_explain.log_format" => " json" });
131
114
132
115
like(
133
116
$log_contents ,
134
- qr / "Query Text": "SELECT \* FROM pg_proc ;"/ ,
117
+ qr / "Query Text": "SELECT \* FROM pg_class ;"/ ,
135
118
" query text logged, json mode" );
136
119
137
120
unlike(
@@ -141,7 +124,7 @@ sub query_log
141
124
142
125
like(
143
126
$log_contents ,
144
- qr / "Node Type": "Seq Scan"[^}]*"Relation Name": "pg_proc "/ s ,
127
+ qr / "Node Type": "Seq Scan"[^}]*"Relation Name": "pg_class "/ s ,
145
128
" sequential scan logged, json mode" );
146
129
147
130
# Prepared query in JSON format.
@@ -160,4 +143,43 @@ sub query_log
160
143
qr / "Node Type": "Index Scan"[^}]*"Index Name": "pg_class_relname_nsp_index"/ s ,
161
144
" index scan logged, json mode" );
162
145
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
8000
* 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
+
163
185
done_testing();
0 commit comments