-
Notifications
You must be signed in to change notification settings - Fork 4
/
gucs.go
111 lines (86 loc) · 2.73 KB
/
gucs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"fmt"
"flag"
"github.com/MakeNowJust/heredoc"
"os"
)
var (
//Session Parameters
sessiongucs string = ""
sessiongucsfilename stringFlag
gathergucsfilename stringFlag
gather_gucs_query = heredoc.Doc(`
select
E' {\n'
||' "parameter" : "'||name||E'\",\n'
||' "value" : "'||current_setting(name)||E'"\n }'
from pg_settings
where
context = 'user'
and name not in ('application_name','search_path')
ORDER BY 1;
`)
//upper : we could grab also "superuser" parameters but..
//context in ('superuser','user') is only shown
//FIXME : search_patch causes problems because of the $user thing
// we should do some special treatments somewhere to avoid
// this but since this info is of poor interest in the
// pgSimload usage, we let it aside for now
gather_gucs_file_header = heredoc.Doc(`
{
"sessionparameters": [
`)
gather_gucs_file_footer = heredoc.Doc(`
]
}
`)
)
// Function to gather modifiable per-session GUCS, included contexts
// 'user' and 'superuser', because we don't know in advance what will
// be the user defined in the config.json file... Will be up to the
// user to try to modify things in the session_parameter.json file
// that this function will write into (gathergucsfilename.value)
func gatherGucs () {
pgManager, err := NewPGManager(configfilename.value)
if err != nil {
exit1("Failed to create PGManager:\n", err)
}
// Initial connection
conn, err := pgManager.PGConnect()
if err != nil {
//we won't try to reconnect here, since GUCS generation file
//is an unitary operation...
exit1("Failed to connect to PostgreSQL:\n", err)
}
defer conn.Close(context.Background())
flag.Parse()
var file string = gather_gucs_file_header
rows, _ := conn.Query(context.Background(), gather_gucs_query)
defer rows.Close()
for rows.Next() {
var parameter_json string
err := rows.Scan(¶meter_json)
if err != nil {
exit1("Error retrieving GUCs from PostgreSQL server:\n",err)
}
if err := rows.Err(); err != nil {
exit1("Error:\n",err)
}
file = file + parameter_json + ",\n"
i = i+1
}
file = file[:len(file)-2]
file = file + "\n" + gather_gucs_file_footer
rows.Close()
err = os.WriteFile(gathergucsfilename.value, []byte(file), 0644)
if err != nil {
exit1("Error while trying to write file "+gathergucsfilename.value,err)
}
fmt.Print(string(colorGreen))
fmt.Println("Session parameters template file created !")
fmt.Print(string(colorReset))
fmt.Println("You can now edit "+gathergucsfilename.value+" to suit your needs")
fmt.Println("to be used afterwards with -session_parameters in SQL-loop mode")
}