Compare commits
6 Commits
424b9882bb
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 13e48717eb | |||
| 5dec90aa2a | |||
| 7a5099188e | |||
| 7b1465fc4e | |||
| aa2318b5ea | |||
| 1b849b8fa1 |
@@ -1,7 +1,5 @@
|
|||||||
-- 修改用户的密码
|
-- search_path
|
||||||
alter user postgres with password '123456';
|
SHOW search_path;
|
||||||
-- 查询所有用户
|
|
||||||
select * from pg_catalog.pg_user;
|
|
||||||
|
|
||||||
-- 查询表的 COLLATION VERSION
|
-- 查询表的 COLLATION VERSION
|
||||||
SELECT
|
SELECT
|
||||||
@@ -21,3 +19,39 @@ ALTER DATABASE fortern REFRESH COLLATION VERSION;
|
|||||||
|
|
||||||
REINDEX DATABASE kt_shop;
|
REINDEX DATABASE kt_shop;
|
||||||
ALTER DATABASE kt_shop REFRESH COLLATION VERSION;
|
ALTER DATABASE kt_shop REFRESH COLLATION VERSION;
|
||||||
|
|
||||||
|
-- 重置某表的某列的自增序列 [表名]_[列名]_seq
|
||||||
|
ALTER SEQUENCE person_id_seq RESTART WITH 4284858;
|
||||||
|
|
||||||
|
-- ctid 会返回一个元组,元组中第一个数字是 page number, 第二个是 slot number。
|
||||||
|
select r.ctid, r.* from study.r;
|
||||||
|
-- 得到
|
||||||
|
-- "(0,1)",1,101
|
||||||
|
-- "(0,2)",2,102
|
||||||
|
-- "(0,3)",3,103
|
||||||
|
|
||||||
|
-- 删除一条
|
||||||
|
delete from study.r where id = 2;
|
||||||
|
-- 再重新查询,得到
|
||||||
|
-- "(0,1)",1,101
|
||||||
|
-- "(0,3)",3,103
|
||||||
|
-- PostgreSQL 会保留空槽
|
||||||
|
|
||||||
|
-- 插入新数据
|
||||||
|
insert into study.r values (104, 4);
|
||||||
|
-- 再重新查询,得到
|
||||||
|
-- "(0,1)",1,101
|
||||||
|
-- "(0,3)",3,103
|
||||||
|
-- "(0,4)",4,104
|
||||||
|
-- 可以看到使用了末尾的新槽位,而不是使用中间的新槽位
|
||||||
|
|
||||||
|
-- 执行清理
|
||||||
|
vacuum full study.r;
|
||||||
|
-- 再次扫面,得到
|
||||||
|
-- "(0,1)",1,101
|
||||||
|
-- "(0,2)",3,103
|
||||||
|
-- "(0,3)",4,104
|
||||||
|
-- 看到空间已经回收
|
||||||
|
|
||||||
|
-- 查看数据类型
|
||||||
|
select pg_typeof(EXTRACT(EPOCH FROM current_timestamp(0)))
|
||||||
|
|||||||
6
PostgreSQL/function.sql
Normal file
6
PostgreSQL/function.sql
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-- ROW 关键字生成一个元组
|
||||||
|
SELECT ROW (1, 2, 3);
|
||||||
|
|
||||||
|
-- pg_column_size函数获取存储数据需要的字节数
|
||||||
|
SELECT pg_column_size(ROW (1,2,3)); -- 36
|
||||||
|
SELECT pg_column_size(ROW (1::int2, 2::int2, 3::int2)); -- 30
|
||||||
2
PostgreSQL/index.sql
Normal file
2
PostgreSQL/index.sql
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
-- 不阻塞的情况下创建索引
|
||||||
|
create index concurrently idx_prism_timestamp on activities (timestamp);
|
||||||
13
PostgreSQL/time.sql
Normal file
13
PostgreSQL/time.sql
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
-- 设置会话时区
|
||||||
|
SET TIME ZONE 'Asia/Shanghai';
|
||||||
|
|
||||||
|
-- AT TIME ZONE 在 PostgreSQL 中是重载的,行为取决于左侧的类型:
|
||||||
|
-- timestamp WITHOUT time zone AT TIME ZONE zone → 把该 wall time 视为 zone 的本地时间,然后转换为 UTC,结果为 timestamptz(显示为 UTC 偏移后的时间)。
|
||||||
|
-- timestamptz AT TIME ZONE zone → 把该瞬间转换成 zone 的本地 wall time,结果为 timestamp WITHOUT time zone。
|
||||||
|
select msg_time at time zone 'Asia/Shanghai' from msg where id = 1;
|
||||||
|
select msg_time at time zone 'UTC+8' from msg where id = 1;
|
||||||
|
|
||||||
|
select to_timestamp(1761850339) AT TIME ZONE 'Asia/Shanghai';
|
||||||
|
|
||||||
|
-- 将当前时间转换为Unix时间戳
|
||||||
|
select EXTRACT(EPOCH FROM current_timestamp(0)) - 60;
|
||||||
4
PostgreSQL/user.sql
Normal file
4
PostgreSQL/user.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
-- 修改用户的密码
|
||||||
|
alter user postgres with password '123456';
|
||||||
|
-- 查询所有用户
|
||||||
|
select * from pg_catalog.pg_user;
|
||||||
22
PostgreSQL/windows_fun.sql
Normal file
22
PostgreSQL/windows_fun.sql
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
-- 窗口函数
|
||||||
|
-- row_number(): 对相等的值【不区分】,相当于行号,相等的值对应的排名不同,序号从1到n连续
|
||||||
|
-- rank(): 相等的值排名相同,但若有相等的值,则序号从1到n不连续。符合一般人思维中的排名
|
||||||
|
-- dense_rank(): 对相等的值排名相同,但序号从1到n连续
|
||||||
|
|
||||||
|
-- 先按num分区,每个区内按id排序
|
||||||
|
select id, num, id - row_number() over (partition by num order by id) as rnk
|
||||||
|
from employee;
|
||||||
|
|
||||||
|
-- 找出所有至少连续出现三次的数字
|
||||||
|
-- 先按num分区,每个区内按id排序
|
||||||
|
-- 一个分区内,如果数字连续,那么id会不断+1,row_number()也会不断+1,id - row_number()差值不变
|
||||||
|
-- 在按照id - row_number()聚合,使用count(*)统计,count(*)的结果就是连续出现的次数
|
||||||
|
select r.num, r.rnk, count(*)
|
||||||
|
from (select num, id - row_number() over (partition by num order by id) as rnk from employee) as r
|
||||||
|
group by r.num, r.rnk;
|
||||||
|
|
||||||
|
select distinct r.num as ConsecutiveNums
|
||||||
|
from (select num, id - row_number() over (partition by num order by id) as rnk from employee) as r
|
||||||
|
group by r.num, r.rnk
|
||||||
|
having count(*) >= 3
|
||||||
|
limit 1;
|
||||||
Reference in New Issue
Block a user