11
11
12
12
13
13
class PostgresCsvLog :
14
- def __init__ (self , postgres_server_config : PostgresServerConfig , csv_log_table_name = "csv_log" ):
14
+ def __init__ (self , postgres_server_config : PostgresServerConfig ):
15
15
self ._postgres_server_config = postgres_server_config
16
16
self ._postgres_parameter = PostgresParameter (postgres_server_config )
17
- self .csv_log_table_name = csv_log_table_name
18
- self .start_csv_log = None # The time when the csv log started to be output(epoch time)
19
- self .end_csv_log = None # The time when the csv log output was finished(epoch time)
17
+ self ._csv_log_table_name = "csv_log"
18
+ self .start_csv_log_unix_time = None # The time when the csv log started to be output(epoch time)
19
+ self .end_csv_log_unix_time = None # The time when the csv log output was finished(epoch time)
20
20
self ._csv_log_file_path = None
21
21
self .csv_log_local_file_path = None # File path when the csv log file is copied to localhost
22
22
@@ -26,7 +26,10 @@ def __init__(self, postgres_server_config: PostgresServerConfig, csv_log_table_n
26
26
The current setting is off; to turn it on, you need to restart PostgreSQL." )
27
27
# get current settings
28
28
self ._current_log_destination = self ._get_log_destination ()
29
- self ._log_min_duration_statement = self ._get_log_min_duration_statement ()
29
+ self ._current_log_min_duration_statement = self ._get_log_min_duration_statement ()
30
+
31
+ def __del__ (self ):
32
+ self .disable ()
30
33
31
34
def enable (self ):
32
35
# set log_destination = '[current_setting],csvlog'
@@ -36,33 +39,41 @@ def enable(self):
36
39
else :
37
40
log_destination_value = self ._current_log_destination + ',csvlog'
38
41
self ._postgres_parameter .set_parameter (param_name = "log_destination" , param_value = log_destination_value )
42
+ logger .debug ("Start outputting PostgreSQL log message to CSV file.\n "
43
+ "log_destination = '{}'" .format (log_destination_value ))
39
44
# set log_min_duration_statement = 0
40
45
self ._postgres_parameter .set_parameter (param_name = "log_min_duration_statement" , param_value = 0 , pg_reload = True )
41
- self .start_csv_log = time .time ()
46
+ logger .debug ("Changed setting to output all executed SQL to PostgreSQL log file.\n "
47
+ "log_min_duration_statement = '0'" )
48
+ self .start_csv_log_unix_time = time .time ()
42
49
self ._csv_log_file_path = self ._get_csv_log_file_path ()
43
50
44
51
def disable (self ):
45
52
# set log_destination = '[current_setting]'
46
53
self ._postgres_parameter .set_parameter (param_name = "log_destination" , param_value = self ._current_log_destination )
54
+ logger .debug ("PostgreSQL log message output to CSV file is disabled.\n "
55
+ "log_destination = '{}'" .format (self ._current_log_min_duration_statement ))
47
56
# set log_min_duration_statement = current_setting
48
57
self ._postgres_parameter .set_parameter (param_name = "log_min_duration_statement" ,
49
- param_value = self ._log_min_duration_statement , pg_reload = True )
50
- self .end_csv_log = time .time ()
51
-
52
- def load_csv_to_local_database (self ):
53
- self ._copy_csv_logfile_to_local () # copy logfile to /tmp directory(localhost)
54
- self ._create_csv_log_table ()
55
- self ._truncate_csv_log_table () # truncate csv log table
56
- with get_pg_connection (dsn = self ._postgres_server_config .dsn ) as conn :
58
+ param_value = self ._current_log_min_duration_statement , pg_reload = True )
59
+ logger .debug ("The value of the log_min_duration_statement parameter has been restored to its original value.\n "
60
+ "log_min_duration_statement = '{}'" .format (self ._current_log_min_duration_statement ))
61
+ self .end_csv_log_unix_time = time .time ()
62
+
63
+ def load_csv_to_database (self , copy_dir = "/tmp" , dsn = None ):
64
+ self ._copy_csv_logfile_to_local (copy_dir ) # copy logfile to directory(localhost)
65
+ self ._create_csv_log_table (dsn )
66
+ self ._truncate_csv_log_table (dsn ) # truncate csv log table
67
+ with get_pg_connection (dsn = dsn ) as conn :
57
68
conn .set_session (autocommit = True )
58
69
with conn .cursor () as cur :
59
70
with open (self .csv_log_local_file_path ) as f :
60
71
# cur.copy_from(f, self.csv_log_table_name, sep=',')
61
- cur .copy_expert ("copy {} from stdin (format csv)" .format (self .csv_log_table_name ), f )
72
+ cur .copy_expert ("copy {} from stdin (format csv)" .format (self ._csv_log_table_name ), f )
62
73
63
- def _copy_csv_logfile_to_local (self ):
74
+ def _copy_csv_logfile_to_local (self , copy_dir = "/tmp" ):
64
75
file_name = os .path .basename (self ._csv_log_file_path )
65
- self .csv_log_local_file_path = os .path .join ("/tmp" , file_name )
76
+ self .csv_log_local_file_path = os .path .join (copy_dir , file_name )
66
77
ssh = SSHCommandExecutor (user = self ._postgres_server_config .os_user ,
67
78
password = self ._postgres_server_config .ssh_password ,
68
79
hostname = self ._postgres_server_config .host ,
@@ -94,7 +105,7 @@ def _get_csv_log_file_path(self):
94
105
csv_file_path = os .path .join (self ._postgres_server_config .pgdata , csv_file_path )
95
106
return csv_file_path
96
107
97
- def _create_csv_log_table (self ):
108
+ def _create_csv_log_table (self , dsn ):
98
109
create_table_sql = "CREATE TABLE IF NOT EXISTS {} (" \
99
110
"log_time timestamp(3) with time zone," \
100
111
"user_name text," \
@@ -119,15 +130,15 @@ def _create_csv_log_table(self):
119
130
"query_pos integer," \
120
131
"location text," \
121
132
"application_name text," \
122
- "PRIMARY KEY (session_id, session_line_num));" .format (self .csv_log_table_name )
123
- with get_pg_connection (dsn = self . _postgres_server_config . dsn ) as conn : # FIXME
133
+ "PRIMARY KEY (session_id, session_line_num));" .format (self ._csv_log_table_name )
134
+ with get_pg_connection (dsn = dsn ) as conn :
124
135
conn .set_session (autocommit = True )
125
136
with conn .cursor () as cur :
126
137
cur .execute (create_table_sql )
127
138
128
- def _truncate_csv_log_table (self ):
129
- truncate_table_sql = "TRUNCATE {}" .format (self .csv_log_table_name )
130
- with get_pg_connection (dsn = self . _postgres_server_config . dsn ) as conn : # FIXME
139
+ def _truncate_csv_log_table (self , dsn ):
140
+ truncate_table_sql = "TRUNCATE {}" .format (self ._csv_log_table_name )
141
+ with get_pg_connection (dsn = dsn ) as conn :
131
142
conn .set_session (autocommit = True )
132
143
with conn .cursor () as cur :
133
144
cur .execute (truncate_table_sql )
@@ -136,12 +147,15 @@ def _truncate_csv_log_table(self):
136
147
if __name__ == "__main__" :
137
148
from pgopttune .config .postgres_server_config import PostgresServerConfig
138
149
150
+ logging .basicConfig (level = logging .DEBUG )
139
151
conf_path = './conf/postgres_opttune.conf'
140
152
postgres_server_config_test = PostgresServerConfig (conf_path ) # PostgreSQL Server config
141
153
csv_log = PostgresCsvLog (postgres_server_config_test )
142
154
csv_log .enable ()
143
- print (csv_log ._csv_log_file_path )
144
- print ("sleep..." )
145
- time .sleep (300 )
155
+ # logging.debug (csv_log._csv_log_file_path)
156
+ logging . debug ("sleep..." )
157
+ time .sleep (60 )
146
158
csv_log .disable ()
147
- csv_log .load_csv_to_local_database ()
159
+ csv_log .load_csv_to_database ()
160
+ logging .debug (csv_log .start_csv_log_unix_time )
161
+ logging .debug (csv_log .end_csv_log_unix_time )
0 commit comments