Skip to content

Commit 2087d24

Browse files
author
xiongcc
committed
add chapter 81~93
1 parent 1ac71ec commit 2087d24

13 files changed

+1365
-0
lines changed

How to change a Postgres parameter.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# How to change a Postgres parameter
2+
3+
> 我每天都会发布一篇新的 PostgreSQL "howto" 文章。加入我的旅程吧 — 订阅、提供反馈、分享!
4+
5+
如果你需要更改 Postgres 参数 (又称之为 GUC,Grand Unified Configuration) 以实现永久效果,请按照以下步骤操作。
6+
7+
参考文档:[设置参数](https://postgresql.org/docs/current/config-setting.html.)
8+
9+
## 1) 确认是否需要重启
10+
11+
快速检查是否需要重启的两种方法:
12+
13+
- 使用 [postgresqlco.nf](https://postgresqlco.nf/) 并查看 `Restart: xxx` 字段。例如,对于 [max_wal_size](https://postgresqlco.nf/doc/en/param/max_wal_size/)`Restart: false`,而对于 [shared_buffers](https://postgresqlco.nf/doc/en/param/shared_buffers) 则为 `true`
14+
- 检查 `pg_settings` 中的 `context` 字段 — 如果是 `postmaster`,则需要重启,否则不需要 (例外情况:`internal` — 这类参数无法更改)。例如:
15+
16+
```sql
17+
nik=# select name, context
18+
from pg_settings
19+
where name in ('max_wal_size', 'shared_buffers');
20+
name | context
21+
----------------+------------
22+
max_wal_size | sighup
23+
shared_buffers | postmaster
24+
(2 rows)
25+
```
26+
27+
## 2) 进行更改
28+
29+
在 Postgres 配置文件中应用更改 (`postgresql.conf` 或其依赖项,如果使用了 `include` 命令的话)。除非必要,建议避免使用 `ALTER SYSTEM`,以免将来产生混淆 (它会写入 `postgresql.auto.conf`,且可能很容易被忽视;参见[相关讨论](https://postgresql.org/message-id/flat/CA%2BVUV5rEKt2%2BCdC_KUaPoihMu%2Bi5ChT4WVNTr4CD5-xXZUfuQw%40mail.gmail.com))。
30+
31+
## 3) 应用更改
32+
33+
如果需要重启,则重启 Postgres。如果忘记重启,稍后可以在 `pg_settings` 中查看 `pending_restart` 来检测未应用的更改。
34+
35+
如果不需要重启,在超级用户下执行:
36+
37+
```sql
38+
select pg_reload_conf();
39+
```
40+
41+
或者使用以下方法之一:
42+
43+
- `pg_ctl reload $PGDATA`
44+
45+
- 发送 `SIGHUP` 信号给 postmaster 进程,例如:
46+
47+
~~~bash
48+
kill -HUP $(cat "${PGDATA}/postmaster.pid")
49+
~~~
50+
51+
当应用了无需重启的更改时,Postgres 日志中会显示类似以下内容:
52+
53+
```bash
54+
LOG: received SIGHUP, reloading configuration files
55+
LOG: parameter "max_wal_size" changed to "2GB"
56+
```
57+
58+
## 4) 验证更改
59+
60+
使用 `SHOW``current_setting(...)` 确认更改已生效,例如:
61+
62+
```sql
63+
nik=# show max_wal_size;
64+
max_wal_size
65+
--------------
66+
2GB
67+
(1 row)
68+
```
69+
70+
或者
71+
72+
```sql
73+
nik=# select current_setting('max_wal_size');
74+
current_setting
75+
-----------------
76+
2GB
77+
(1 row)
78+
```
79+
80+
## 额外信息:数据库级、用户级和表级设置
81+
82+
如果 `pg_settings` 中的 `context``user``superuser`,那么可以在数据库或用户级别调整设置,例如:
83+
84+
```sql
85+
alter database verbosedb set log_statement = 'all';
86+
alter user hero set statement_timeout = 0;
87+
```
88+
89+
这些结果可以在 `pg_db_role_setting` 中查看:
90+
91+
```sql
92+
nik=# select * from pg_db_role_setting;
93+
setdatabase | setrole | setconfig
94+
-------------+---------+-----------------------
95+
24580 | 0 | {log_statement=all}
96+
0 | 24581 | {statement_timeout=0}
97+
(2 rows)
98+
```
99+
100+
当使用 `CREATE TABLE ``ALTER TABLE` 时,某些设置还可以在表级别进行调整,参照 [CREATE TABLE / Storage Parameters](https://postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS)。请注意命名差异:`autovacuum_enabled` 用于启用或禁用特定表的 autovacuum 守护进程,而全局设置名称为 `autovacuum`
101+
102+
## 我见
103+
104+
在 15 中,还支持了 `\dconfig` 命令,也很方便。
105+
106+
>The \dconfig command can display the parameter settings of the instance. If no option is specified, a list of parameters that have changed from the default values is displayed. If a parameter name is specified, the specific parameter setting value can be checked. Wildcards can be used for parameter names.
107+
108+
~~~sql
109+
postgres=# \dconfig *mem*
110+
List of configuration parameters
111+
Parameter | Value
112+
----------------------------------+-------
113+
autovacuum_work_mem | -1
114+
dynamic_shared_memory_type | posix
115+
enable_memoize | on
116+
hash_mem_multiplier | 2
117+
logical_decoding_work_mem | 64MB
118+
maintenance_work_mem | 64MB
119+
min_dynamic_shared_memory | 0
120+
multixact_member_buffers | 256kB
121+
shared_memory_size | 145MB
122+
shared_memory_size_in_huge_pages | 73
123+
shared_memory_type | mmap
124+
work_mem | 4MB
125+
(12 rows)
126+
~~~
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# How to change ownership of all objects in a database
2+
3+
>我每天都会发布一篇新的 PostgreSQL "howto" 文章。加入我的旅程吧 — 订阅、提供反馈、分享!
4+
5+
如果需要更改当前数据库中所有对象的所有权,可以使用以下匿名 DO 块 (或从[此处](https://gitlab.com/postgres-ai/database-lab/-/snippets/2075222)粘贴):
6+
7+
~~~sql
8+
do $$
9+
declare
10+
new_owner text := 'NEW_OWNER_ROLE_NAME';
11+
object_type record;
12+
r record;
13+
sql text;
14+
begin
15+
-- New owner for all schemas
16+
for r in select * from pg_namespace loop
17+
sql := format(
18+
'alter schema %I owner to %I;',
19+
r.nspname,
20+
new_owner
21+
);
22+
23+
raise debug 'Execute SQL: %', sql;
24+
25+
execute sql;
26+
end loop;
27+
28+
-- Various DB objects
29+
-- c: composite type
30+
-- p: partitioned table
31+
-- i: index
32+
-- r: table
33+
-- v: view
34+
-- m: materialized view
35+
-- S: sequence
36+
for object_type in
37+
select
38+
unnest('{type,table,table,view,materialized view,sequence}'::text[]) type_name,
39+
unnest('{c,p,r,v,m,S}'::text[]) code
40+
loop
41+
for r in
42+
execute format(
43+
$sql$
44+
select n.nspname, c.relname
45+
from pg_class c
46+
join pg_namespace n on
47+
n.oid = c.relnamespace
48+
and not n.nspname in ('pg_catalog', 'information_schema')
49+
and c.relkind = %L
50+
order by c.relname
51+
$sql$,
52+
object_type.code
53+
)
54+
loop
55+
sql := format(
56+
'alter %s %I.%I owner to %I;',
57+
object_type.type_name,
58+
r.nspname,
59+
r.relname,
60+
new_owner
61+
);
62+
63+
raise debug 'Execute SQL: %', sql;
64+
65+
execute sql;
66+
end loop;
67+
end loop;
68+
69+
-- Functions, procedures
70+
for r in
71+
select
72+
p.proname,
73+
n.nspname,
74+
pg_catalog.pg_get_function_identity_arguments(p.oid) as args
75+
from pg_catalog.pg_namespace as n
76+
join pg_catalog.pg_proc as p on p.pronamespace = n.oid
77+
where
78+
not n.nspname in ('pg_catalog', 'information_schema')
79+
and p.proname not ilike 'dblink%' -- We do not want dblink to be involved (exclusion)
80+
loop
81+
sql := format(
82+
'alter function %I.%I(%s) owner to %I', -- todo: check support CamelStyle r.args
83+
r.nspname,
84+
r.proname,
85+
r.args,
86+
new_owner
87+
);
88+
89+
raise debug 'Execute SQL: %', sql;
90+
91+
execute sql;
92+
end loop;
93+
94+
-- Full text search dictionary
95+
-- TODO: text search configuration
96+
for r in
97+
select *
98+
from pg_catalog.pg_namespace n
99+
join pg_catalog.pg_ts_dict d on d.dictnamespace = n.oid
100+
where not n.nspname in ('pg_catalog', 'information_schema')
101+
loop
102+
sql := format(
103+
'alter text search dictionary %I.%I owner to %I',
104+
r.nspname,
105+
r.dictname,
106+
new_owner
107+
);
108+
109+
raise debug 'Execute SQL: %', sql;
110+
111+
execute sql;
112+
end loop;
113+
114+
-- Domains
115+
for r in
116+
select typname, nspname
117+
from pg_catalog.pg_type
118+
join pg_catalog.pg_namespace on pg_namespace.oid = pg_type.typnamespace
119+
where typtype = 'd' and not nspname in ('pg_catalog', 'information_schema')
120+
loop
121+
sql := format(
122+
'alter domain %I.%I owner to %I',
123+
r.nspname,
124+
r.typname,
125+
new_owner
126+
);
127+
128+
raise debug 'Execute SQL: %', sql;
129+
130+
execute sql;
131+
end loop;
132+
end
133+
$$;
134+
~~~
135+
136+
不要忘记更改 `new_owner` 的值。
137+
138+
该查询不包括模式 `pg_catalog``information_schema`,它涵盖:模式对象、表、视图、物化视图、函数、文本搜索字典和域。根据你的 PG 版本,可能还有其他对象需要处理。根据需要调整代码。
139+
140+
要查看调试消息,请在运行之前更改 `client_min_messages`
141+
142+
~~~sql
143+
set client_min_messages to debug;
144+
~~~
145+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# How to draw frost patterns using SQL ❄
2+
3+
>我每天都会发布一篇新的 PostgreSQL "howto" 文章。加入我的旅程吧 — 订阅、提供反馈、分享!
4+
5+
此创意和实现最初由Kirill Borovikov (kilor) 完成 — 我只是重新格式化并稍微调整了一下,扩展了字符集。
6+
7+
下面是查询代码:
8+
9+
~~~sql
10+
with recursive t as (
11+
select
12+
0 as x,
13+
0 as y,
14+
'{"{0,0}"}'::text[] as c,
15+
0 as i
16+
17+
union all
18+
19+
(
20+
with z as (
21+
select
22+
dn.x,
23+
dn.y,
24+
t.c,
25+
t.i
26+
from t,
27+
lateral (
28+
select
29+
((random() * 2 - 1) * 100)::integer as x,
30+
((random() * 2 - 1) * 100)::integer as y
31+
) as p,
32+
lateral (
33+
select *
34+
from (
35+
select
36+
(unnest::text[])[1]::integer as x,
37+
(unnest::text[])[2]::integer as y
38+
from unnest(t.c::text[])
39+
) as t
40+
order by sqrt((x - p.x) ^ 2 + (y - p.y) ^ 2)
41+
limit 1
42+
) as n,
43+
lateral (
44+
select
45+
n.x + dx as x,
46+
n.y + dy as y
47+
from
48+
generate_series(-1, 1) as dx,
49+
generate_series(-1, 1) as dy
50+
where (dx, dy) <> (0, 0)
51+
order by
52+
case
53+
when (p.x, p.y) = (n.x, n.y) then 0
54+
else abs(
55+
acos(
56+
((p.x - n.x) * dx + (p.y - n.y) * dy)
57+
/ sqrt((p.x - n.x) ^ 2 + (p.y - n.y) ^ 2)
58+
/ sqrt(dx ^ 2 + dy ^ 2)
59+
)
60+
)
61+
end
62+
limit 1
63+
) as dn
64+
)
65+
select
66+
z.x,
67+
z.y,
68+
z.c || array[z.x, z.y]::text,
69+
z.i + 1
70+
from z
71+
where z.i < (1 << 10)
72+
)
73+
),
74+
map as (
75+
select
76+
gx as x,
77+
gy as y,
78+
(
79+
select sqrt((gx - T.x) ^ 2 + (gy - T.y) ^ 2) v
80+
from t
81+
order by v
82+
limit 1
83+
) as v
84+
from
85+
generate_series(-40, 40) as gx,
86+
generate_series(-30, 30) as gy
87+
),
88+
gr as (
89+
select regexp_split_to_array('@%#*+=-:. ', '') as s
90+
)
91+
select
92+
string_agg(
93+
coalesce(s[(v * (array_length(s, 1) - 1))::integer + 1], ' '),
94+
' '
95+
order by x
96+
) as frozen
97+
from
98+
(
99+
select
100+
x,
101+
y,
102+
v::double precision / max(v) over() as v
103+
from map
104+
) as t,
105+
gr
106+
group by y
107+
order by y;
108+
~~~
109+
110+
每次它都会绘制一个新的霜花图案,此处是几个例子:
111+
112+
![frozen pattern 1](https://gitlab.com/postgres-ai/postgresql-consulting/postgres-howtos/-/raw/main/files/0082_01.png)
113+
114+
![frozen pattern 2](https://gitlab.com/postgres-ai/postgresql-consulting/postgres-howtos/-/raw/main/files/0082_02.png)
115+
116+
![frozen pattern 3](https://gitlab.com/postgres-ai/postgresql-consulting/postgres-howtos/-/raw/main/files/0082_03.png)
117+
118+
节日快乐🎅

0 commit comments

Comments
 (0)