PostgreSQL 常用维护操作(三)

前端之家收集整理的这篇文章主要介绍了PostgreSQL 常用维护操作(三)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Postgresql配置文件修改方法

1. 配置文件所在路径:

  1. [postgres@kevin data]$ pwd
  2. /opt/postgresql/data
  3. [postgres@kevin data]$ ll
  4. 总用量 1332
  5. drwx------ 8 postgres dba 4096 7 13 10:50 base
  6. drwx------ 2 postgres dba 4096 7 19 03:42 global
  7. -rw------- 1 postgres dba 1181357 7 17 16:17 gmon.out
  8. drwx------ 159 postgres dba 4096 7 19 03:44 gprof
  9. drwx------ 2 postgres dba 4096 7 9 09:42 pg_clog
  10. -rw------- 1 postgres dba 3755 7 9 14:54 pg_hba.conf
  11. -rw------- 1 postgres dba 1631 7 9 09:42 pg_ident.conf
  12. drwx------ 4 postgres dba 4096 7 9 09:42 pg_multixact
  13. drwx------ 2 postgres dba 4096 7 19 05:57 pg_stat_tmp
  14. drwx------ 2 postgres dba 4096 7 9 09:42 pg_subtrans
  15. drwx------ 2 postgres dba 4096 7 13 10:54 pg_tblspc
  16. drwx------ 2 postgres dba 4096 7 9 09:42 pg_twophase
  17. -rw------- 1 postgres dba 4 7 9 09:42 PG_VERSION
  18. drwx------ 3 postgres dba 4096 7 9 09:42 pg_xlog
  19. -rw------- 1 postgres dba 16837 7 9 14:52 postgresql.conf
  20. -rw------- 1 postgres dba 16815 7 9 14:51 postgresql.conf_duanyx_bak
  21. -rw------- 1 postgres dba 29 7 19 03:42 postmaster.opts
  22. -rw------- 1 postgres dba 46 7 19 03:42 postmaster.pid
  23. [postgres@kevin data]$


2. sql方式修改,可以在运行时设置,当然只有于部分选项,使用方法如下:

  1. kevin_test=# SET work_mem = '16MB';
  2. SET
  3. kevin_test=#
  4.  
  5. kevin_test=# SELECT * FROM pg_settings WHERE source = 'session';
  6. -[ RECORD 1 ]-------------------------------------------------------------------
  7. --------------------------------------------------
  8. name | work_mem
  9. setting | 16384
  10. unit | kB
  11. category | Resource Usage / Memory
  12. short_desc | Sets the maximum memory to be used for query workspaces.
  13. extra_desc | This much memory can be used by each internal sort operation and ha
  14. sh table before switching to temporary disk files.
  15. context | user
  16. vartype | integer
  17. source | session
  18. min_val | 64
  19. max_val | 2097151
  20. enumvals |
  21. boot_val | 1024
  22. reset_val | 1024
  23. sourcefile |
  24. sourceline |
  25.  
  26. kevin_test=#


恢复默认设置的方法

  1. kevin_test=# RESET work_mem;
  2. RESET
  3. kevin_test=# SELECT * FROM pg_settings WHERE name = 'work_mem';
  4. -[ RECORD 1 ]---------------------------------------------------------------------------------------------------------------------
  5. name | work_mem
  6. setting | 1024
  7. unit | kB
  8. category | Resource Usage / Memory
  9. short_desc | Sets the maximum memory to be used for query workspaces.
  10. extra_desc | This much memory can be used by each internal sort operation and hash table before switching to temporary disk files.
  11. context | user
  12. vartype | integer
  13. source | default
  14. min_val | 64
  15. max_val | 2097151
  16. enumvals |
  17. boot_val | 1024
  18. reset_val | 1024
  19. sourcefile |
  20. sourceline |
  21.  
  22. kevin_test=#


3.查看配置项的值,当然你可以看接查看postgre.conf文件内容,不过最好使用sql方法查看和修改,更为快速,当然你要先知道要查看的选项的名称

  1. kevin_test=# SHOW work_mem;
  2. work_mem
  3. ----------
  4. 1MB
  5. (1 row)
  6.  
  7. kevin_test=#


补充一下,如果确实不知道选项的名了,可以先查一下,这些均存储在pg_settings表中,呵呵,Postgresql的一个特点就在这,所有数据均由数据库自己维护。

  1. kevin_test=# SELECT name,short_desc FROM pg_settings;
  2. name | short_desc
  3.  
  4. ---------------------------------+----------------------------------------------------------------------------------------------------------------------------
  5. ---
  6. add_missing_from | Automatically adds missing table references to FROM clauses.
  7. allow_system_table_mods | Allows modifications of the structure of system tables.
  8. archive_command | Sets the shell command that will be called to archive a WAL file.
  9. archive_mode | Allows archiving of WAL files using archive_command.
  10. archive_timeout | Forces a switch to the next xlog file if a new file has not been started within N seconds.
  11. array_nulls | Enable input of NULL elements in arrays.
  12. authentication_timeout | Sets the maximum allowed time to complete client authentication.
  13. autovacuum | Starts the autovacuum subprocess.
  14. autovacuum_analyze_scale_factor | Number of tuple inserts,updates or deletes prior to analyze as a fraction of reltuples.
  15. autovacuum_analyze_threshold | Minimum number of tuple inserts,updates or deletes prior to analyze.
  16. autovacuum_freeze_max_age | Age at which to autovacuum a table to prevent transaction ID wraparound.
  17. autovacuum_max_workers | Sets the maximum number of simultaneously running autovacuum worker processes.
  18. autovacuum_naptime | Time to sleep between autovacuum runs.
  19. autovacuum_vacuum_cost_delay | Vacuum cost delay in milliseconds,for autovacuum.
  20. autovacuum_vacuum_cost_limit | Vacuum cost amount available before napping,for autovacuum.
  21. autovacuum_vacuum_scale_factor | Number of tuple updates or deletes prior to vacuum as a fraction of reltuples.
  22. autovacuum_vacuum_threshold | Minimum number of tuple updates or deletes prior to vacuum.
  23. backslash_quote | Sets whether "\'" is allowed in string literals.
  24. bgwriter_delay | Background writer sleep time between rounds.
  25. bgwriter_lru_maxpages | Background writer maximum number of LRU pages to flush per round.
  26. bgwriter_lru_multiplier | Multiple of the average buffer usage to free per round.
  27. block_size | Shows the size of a disk block.
  28. bonjour_name | Sets the Bonjour broadcast service name.
  29. check_function_bodies | Check function bodies during CREATE FUNCTION.
  30. checkpoint_completion_target | Time spent flushing dirty buffers during checkpoint,as fraction of checkpoint interval.
  31. checkpoint_segments | Sets the maximum distance in log segments between automatic WAL checkpoints.
  32. checkpoint_timeout | Sets the maximum time between automatic WAL checkpoints.
  33. checkpoint_warning | Enables warnings if checkpoint segments are filled more frequently than this.
  34. client_encoding | Sets the client's character set encoding.
  35. client_min_messages | Sets the message levels that are sent to the client.
  36. commit_delay | Sets the delay in microseconds between transaction commit and flushing WAL to disk.
  37. commit_siblings | Sets the minimum concurrent open transactions before performing commit_delay.
  38. config_file | Sets the server's main configuration file.
  39. constraint_exclusion | Enables the planner to use constraints to optimize queries.
  40. cpu_index_tuple_cost | Sets the planner's estimate of the cost of processing each index entry during an index scan.
  41. cpu_operator_cost | Sets the planner's estimate of the cost of processing each operator or function call.
  42. cpu_tuple_cost | Sets the planner's estimate of the cost of processing each tuple (row).
  43. cursor_tuple_fraction | Sets the planner's estimate of the fraction of a cursor's rows that will be retrieved.
  44. custom_variable_classes | Sets the list of known custom variable classes.
  45. data_directory | Sets the server's data directory.
  46. DateStyle | Sets the display format for date and time values.
  47. db_user_namespace | Enables per-database user names.
  48. deadlock_timeout | Sets the time to wait on a lock before checking for deadlock.
  49. debug_assertions | Turns on varIoUs assertion checks.
  50. debug_pretty_print | Indents parse and plan tree displays.
  51. debug_print_parse | Logs each query's parse tree.
  52. debug_print_plan | Logs each query's execution plan.
  53. debug_print_rewritten | Logs each query's rewritten parse tree.
  54. default_statistics_target | Sets the default statistics target.
  55. default_tablespace | Sets the default tablespace to create tables and indexes in.
  56. default_text_search_config | Sets default text search configuration.
  57. default_transaction_isolation | Sets the transaction isolation level of each new transaction.
  58. default_transaction_read_only | Sets the default read-only status of new transactions.
  59. default_with_oids | Create new tables with OIDs by default.
  60. dynamic_library_path | Sets the path for dynamically loadable modules.
  61. effective_cache_size | Sets the planner's assumption about the size of the disk cache.
  62. effective_io_concurrency | Number of simultaneous requests that can be handled efficiently by the disk subsystem.
  63. enable_bitmapscan | Enables the planner's use of bitmap-scan plans.
  64. enable_hashagg | Enables the planner's use of hashed aggregation plans.
  65. enable_hashjoin | Enables the planner's use of hash join plans.
  66. enable_indexscan | Enables the planner's use of index-scan plans.
  67. enable_mergejoin | Enables the planner's use of merge join plans.
  68. enable_nestloop | Enables the planner's use of nested-loop join plans.
  69. enable_seqscan | Enables the planner's use of sequential-scan plans.
  70. enable_sort | Enables the planner's use of explicit sort steps.
  71. enable_tidscan | Enables the planner's use of TID scan plans.
  72. escape_string_warning | Warn about backslash escapes in ordinary string literals.
  73. external_pid_file | Writes the postmaster PID to the specified file.
  74. extra_float_digits | Sets the number of digits displayed for floating-point values.
  75. from_collapse_limit | Sets the FROM-list size beyond which subqueries are not collapsed.
  76. fsync | Forces synchronization of updates to disk.
  77. full_page_writes | Writes full pages to WAL when first modified after a checkpoint.
  78. geqo | Enables genetic query optimization.
  79. geqo_effort | GEQO: effort is used to set the default for other GEQO parameters.
  80. geqo_generations | GEQO: number of iterations of the algorithm.
  81. geqo_pool_size | GEQO: number of individuals in the population.
  82. geqo_selection_bias | GEQO: selective pressure within the population.
  83. geqo_threshold | Sets the threshold of FROM items beyond which GEQO is used.
  84. gin_fuzzy_search_limit | Sets the maximum allowed result for exact search by GIN.
  85. hba_file | Sets the server's "hba" configuration file.
  86. ident_file | Sets the server's "ident" configuration file.
  87. ignore_system_indexes | Disables reading from system indexes.
  88. integer_datetimes | Datetimes are integer based.
  89. IntervalStyle | Sets the display format for interval values.
  90. join_collapse_limit | Sets the FROM-list size beyond which JOIN constructs are not flattened.
  91. krb_caseins_users | Sets whether Kerberos and GSSAPI user names should be treated as case-insensitive.
  92. krb_server_keyfile | Sets the location of the Kerberos server key file.
  93. krb_srvname | Sets the name of the Kerberos service.
  94. lc_collate | Shows the collation order locale.
  95. lc_ctype | Shows the character classification and case conversion locale.
  96. lc_messages | Sets the language in which messages are displayed.
  97. lc_monetary | Sets the locale for formatting monetary amounts.
  98. lc_numeric | Sets the locale for formatting numbers.
  99. lc_time | Sets the locale for formatting date and time values.
  100. listen_addresses | Sets the host name or IP address(es) to listen to.
  101. local_preload_libraries | Lists shared libraries to preload into each backend.
  102. log_autovacuum_min_duration | Sets the minimum execution time above which autovacuum actions will be logged.
  103. log_checkpoints | Logs each checkpoint.
  104. log_connections | Logs each successful connection.
  105. log_destination | Sets the destination for server log output.
  106. log_directory | Sets the destination directory for log files.
  107. log_disconnections | Logs end of a session,including duration.
  108. log_duration | Logs the duration of each completed sql statement.
  109. log_error_verbosity | Sets the verbosity of logged messages.
  110. log_executor_stats | Writes executor performance statistics to the server log.
  111. log_filename | Sets the file name pattern for log files.
  112. log_hostname | Logs the host name in the connection logs.
  113. log_line_prefix | Controls information prefixed to each log line.
  114. log_lock_waits | Logs long lock waits.
  115. log_min_duration_statement | Sets the minimum execution time above which statements will be logged.
  116. log_min_error_statement | Causes all statements generating error at or above this level to be logged.
  117. log_min_messages | Sets the message levels that are logged.
  118. log_parser_stats | Writes parser performance statistics to the server log.
  119. log_planner_stats | Writes planner performance statistics to the server log.
  120. log_rotation_age | Automatic log file rotation will occur after N minutes.
  121. log_rotation_size | Automatic log file rotation will occur after N kilobytes.
  122. log_statement | Sets the type of statements logged.
  123. log_statement_stats | Writes cumulative performance statistics to the server log.
  124. log_temp_files | Log the use of temporary files larger than this number of kilobytes.
  125. log_timezone | Sets the time zone to use in log messages.
  126. log_truncate_on_rotation | Truncate existing log files of same name during log rotation.
  127. logging_collector | Start a subprocess to capture stderr output and/or csvlogs into log files.
  128. maintenance_work_mem | Sets the maximum memory to be used for maintenance operations.
  129. max_connections | Sets the maximum number of concurrent connections.
  130. max_files_per_process | Sets the maximum number of simultaneously open files for each server process.
  131. max_function_args | Shows the maximum number of function arguments.
  132. max_identifier_length | Shows the maximum identifier length.
  133. max_index_keys | Shows the maximum number of index keys.
  134. max_locks_per_transaction | Sets the maximum number of locks per transaction.
  135. max_prepared_transactions | Sets the maximum number of simultaneously prepared transactions.
  136. max_stack_depth | Sets the maximum stack depth,in kilobytes.
  137. password_encryption | Encrypt passwords.
  138. port | Sets the TCP port the server listens on.
  139. post_auth_delay | Waits N seconds on connection startup after authentication.
  140. pre_auth_delay | Waits N seconds on connection startup before authentication.
  141. random_page_cost | Sets the planner's estimate of the cost of a nonsequentially fetched disk page.
  142. regex_flavor | Sets the regular expression "flavor".
  143. search_path | Sets the schema search order for names that are not schema-qualified.
  144. segment_size | Shows the number of pages per disk file.
  145. seq_page_cost | Sets the planner's estimate of the cost of a sequentially fetched disk page.
  146. server_encoding | Sets the server (database) character set encoding.
  147. server_version | Shows the server version.
  148. server_version_num | Shows the server version as an integer.
  149. session_replication_role | Sets the session's behavior for triggers and rewrite rules.
  150. shared_buffers | Sets the number of shared memory buffers used by the server.
  151. shared_preload_libraries | Lists shared libraries to preload into server.
  152. silent_mode | Runs the server silently.
  153. sql_inheritance | Causes subtables to be included by default in varIoUs commands.
  154. ssl | Enables SSL connections.
  155. standard_conforming_strings | Causes '...' strings to treat backslashes literally.
  156. statement_timeout | Sets the maximum allowed duration of any statement.
  157. stats_temp_directory | Writes temporary statistics files to the specified directory.
  158. superuser_reserved_connections | Sets the number of connection slots reserved for superusers.
  159. synchronize_seqscans | Enable synchronized sequential scans.
  160. synchronous_commit | Sets immediate fsync at commit.
  161. syslog_facility | Sets the syslog "facility" to be used when syslog enabled.
  162. syslog_ident | Sets the program name used to identify Postgresql messages in syslog.
  163. tcp_keepalives_count | Maximum number of TCP keepalive retransmits.
  164. tcp_keepalives_idle | Time between issuing TCP keepalives.
  165. tcp_keepalives_interval | Time between TCP keepalive retransmits.
  166. temp_buffers | Sets the maximum number of temporary buffers used by each session.
  167. temp_tablespaces | Sets the tablespace(s) to use for temporary tables and sort files.
  168. TimeZone | Sets the time zone for displaying and interpreting time stamps.
  169. timezone_abbreviations | Selects a file of time zone abbreviations.
  170. trace_notify | Generates debugging output for LISTEN and NOTIFY.
  171. trace_sort | Emit information about resource usage in sorting.
  172. track_activities | Collects information about executing commands.
  173. track_activity_query_size | Sets the size reserved for pg_stat_activity.current_query,in bytes.
  174. track_counts | Collects statistics on database activity.
  175. track_functions | Collects function-level statistics on database activity.
  176. transaction_isolation | Sets the current transaction's isolation level.
  177. transaction_read_only | Sets the current transaction's read-only status.
  178. transform_null_equals | Treats "expr=NULL" as "expr IS NULL".
  179. unix_socket_directory | Sets the directory where the Unix-domain socket will be created.
  180. unix_socket_group | Sets the owning group of the Unix-domain socket.
  181. unix_socket_permissions | Sets the access permissions of the Unix-domain socket.
  182. update_process_title | Updates the process title to show the active sql command.
  183. vacuum_cost_delay | Vacuum cost delay in milliseconds.
  184. vacuum_cost_limit | Vacuum cost amount available before napping.
  185. vacuum_cost_page_dirty | Vacuum cost for a page dirtied by vacuum.
  186. vacuum_cost_page_hit | Vacuum cost for a page found in the buffer cache.
  187. vacuum_cost_page_miss | Vacuum cost for a page not found in the buffer cache.
  188. vacuum_freeze_min_age | Minimum age at which VACUUM should freeze a table row.
  189. vacuum_freeze_table_age | Age at which VACUUM should scan whole table to freeze tuples.
  190. wal_block_size | Shows the block size in the write ahead log.
  191. wal_buffers | Sets the number of disk-page buffers in shared memory for WAL.
  192. wal_segment_size | Shows the number of pages per write ahead log segment.
  193. wal_sync_method | Selects the method used for forcing WAL updates to disk.
  194. wal_writer_delay | WAL writer sleep time between WAL flushes.
  195. work_mem | Sets the maximum memory to be used for query workspaces.
  196. xmlbinary | Sets how binary values are to be encoded in XML.
  197. xmloption | Sets whether XML data in implicit parsing and serialization operations is to be considered as documents or content fragment
  198. s.
  199. zero_damaged_pages | Continues processing past damaged page headers.
  200. (193 rows)
  201.  
  202. kevin_test=#


4.记住选项值的改动并不一定对整个数据库生效,有时仅对当前session在效,在执行SET语句时,可通过LOCAL选项明确指定。

  1. kevin_test=# SET LOCAL work_mem='16MB';
  2. SET
  3. kevin_test=#

5.查看系统中哪些选项被修改过。

  1. kevin_test=# SELECT name,source,setting FROM pg_settings
  2. WHERE source != 'default'
  3. AND source != 'override'
  4. ORDER BY 2,1;
  5. name | source | setting
  6. ----------------------------+----------------------+-------------------
  7. log_timezone | command line | PRC
  8. TimeZone | command line | PRC
  9. timezone_abbreviations | command line | Default
  10. DateStyle | configuration file | ISO,YMD
  11. default_text_search_config | configuration file | pg_catalog.simple
  12. lc_messages | configuration file | zh_CN.UTF-8
  13. lc_monetary | configuration file | zh_CN.UTF-8
  14. lc_numeric | configuration file | zh_CN.UTF-8
  15. lc_time | configuration file | zh_CN.UTF-8
  16. listen_addresses | configuration file | *
  17. max_connections | configuration file | 100
  18. shared_buffers | configuration file | 3584
  19. max_stack_depth | environment variable | 2048
  20. (13 rows)
  21.  
  22. kevin_test=#

猜你在找的Postgre SQL相关文章