在大型表上运行 VACUUM FULL之前,是否可以检查表空间中有多少可用的可重用空间?
我有一个很大的 postgres 表(大约 20G),偶尔会出现 VACUUM FULL 的情况。该驱动器上的可用空间在 15-25 GB 之间变化。在尝试每次真空之前,我都会记录表大小(使用 postgres 查询)和可用磁盘空间(使用操作系统工具)。
我知道 VACUUM FULL 需要对表进行完整复制。因此,如果表为 20G,则需要 20G 的可用空间。
有时表有 20G,只有 15G 操作系统空间可用,真空将起作用。我猜所需的额外 5G 是从表空间内部恢复的。
其他时候,vacuum 会由于空间不足而失败,我猜在这些情况下,表空间中找不到所需的额外 5G。
我希望能够事先检查是否有足够的空间用于 VACUUM FULL,我该怎么做?我知道表有多大,我知道操作系统有多少可用空间,但我不知道表空间中有多少可重复利用的空间。
我正在查看表(pg_stat_user_indexes和pg_stat_user_tables)并发现许多未使用的索引。
但在我考虑执行任何操作来删除这些索引之前,我需要了解此数据的分析时间段(idx_scan),是自数据库创建以来的时间段吗?
在pg_stat_database ( stats_reset ) 表中,有一个日期通常是今天或最多 15 天前,但是这个过程是否会干扰我上面提到的表?
%reset() 命令是否清除表(pg_stat_user_indexes和pg_stat_user_tables)?
我的目标是了解收集数据的时间段,以便我可以做出决定。
我的表中有带有时区的时间戳(timestamptz)。我可以将它们转换为 unix 时间戳格式的数字或 int 吗?
我发现了这个问题,但它只显示了如何将没有时区的时间戳转换为unix时间戳。
在主键“id”且默认值为 nextval() 的表上,插入一行会引发错误
Query 1 ERROR: ERROR: duplicate key value violates unique constraint "table_pkey"
DETAIL: Key (id)=(6) already exists.
Run Code Online (Sandbox Code Playgroud)
除了这个表有 200 行。为什么 Postgres 不将新行 id 设置为 201?
我有这张桌子
create table testing(
c1 text,
c2 text);
Run Code Online (Sandbox Code Playgroud)
我同时打开两笔交易。t0 t1..表示按递增顺序排列的一系列时间快照 事务 1
BEGIN; -- t0
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- t2
SELECT * FROM testing where c2 = 'rand'; -- t4
INSERT INTO testing VALUES ('rand', 'xyz'); -- t6
COMMIT; -- t8
Run Code Online (Sandbox Code Playgroud)
交易2
BEGIN; -- t1
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; -- t3
SELECT * FROM testing where c1 = 'rand1'; -- t5
INSERT INTO testing VALUES ('rand1', 'abc'); -- t7
COMMIT; -- t9
Run Code Online (Sandbox Code Playgroud)
t9当我收到这个错误
后 …
我正在使用 pg_trgm 运行查询,但在使用符号差异进行搜索时得到很多 1 匹配。我有以下查询:
SELECT my_column, similarity('$ Hello', my_column) AS sml
FROM my_table
WHERE my_column % '$ Hello'
ORDER BY sml DESC, my_column;
Run Code Online (Sandbox Code Playgroud)
在 中my_table,我有以下内容:
- Hello
? Hello
| Hello
$ Hello
! Hello
!? Hello
Run Code Online (Sandbox Code Playgroud)
它们都以 1 的相似性匹配返回。我是否需要转义“$”或类似的内容?
太长了;博士
以下查询在执行时会阻止 postgres 服务器
ALTER TABLE "image_data" ADD COLUMN "user_status" varchar(30) NULL
Run Code Online (Sandbox Code Playgroud)
引擎版本10.6
行数 1.5 mil
你好,
我的生产数据库服务器有一个有点奇怪的问题(我无法在本地服务器上重现)。作为部署过程的一部分,我想添加一个默认值为null的新列。当我运行上述查询(由 dajngo 的迁移过程生成)时,它失败了,并且在此过程中锁定了数据库并阻止了其他查询(非锁定选择)的执行。由于是生产环境,所以几分钟后(大约10分钟)就回滚了。我发现最不寻常的是,相同的查询在我的本地数据库复制(具有与生产完全相同的数据)上执行不到一秒。
编辑:当我运行此事务时,它会阻止所有其他选择查询对数据库的访问 - 我认为(如果我错了,请纠正我)意味着数据库实际上被此事务阻止了。
我有一个Shipment表,其中包含有关货件的一些基本数据,还有一个ShipmentItem表,其中包含有关该货件的附加属性以及foreignKey表Shipment的主键。到Shipment表ShipmentItem是OneToMany关系。
我们需要包含一个文本搜索选项,该选项采用给定的输入文本字符串,Shipment除了三个特定types的ShipmentItem名称列之外,还搜索 (make) 的超过 2 个列。这是我当前的查询:
select *
from Shipment shipment
where shipment.deliveryRequestedDate >= '2019-06-09T00:00:00Z'
and shipment.deliveryRequestedDate <= '2019-12-06T23:59:59Z'
and (
shipment.identifierkeyvalues = '12345'
or shipment.carrierReferenceNumber = '12345'
or shipment.uuid in (
select shipmentItem.resultId
from ShipmentItem shipmentItem
where (
shipmentItem.type in (
'poNumber', 'deliveryNoteNumber', 'salesOrderNumber'
)
)
and shipmentItem.name = '12345'
and shipmentItem.deliveryRequestedDate >= '2019-06-09T00:00:00Z'
and shipmentItem.deliveryRequestedDate <= '2019-12-06T23:59:59Z'
) …Run Code Online (Sandbox Code Playgroud) Postgres 中的完整计数可能会很慢,其原因众所周知且经过多次讨论。因此,在可能的情况下,我一直在使用估计技术。对于行, pg_stats 似乎很好,对于视图,提取由 工作返回的估计也EXPLAIN可以。
https://www.cybertec-postgresql.com/en/count-made-fast/
但不同的价值观又如何呢?在这里,我的运气要差很多。有时估计是 100% 正确的,有时会偏离 2 或 20 倍。截断的表似乎特别有严重过时的估计(?)。
我刚刚运行了这个测试并提供了一些结果:
analyze assembly_prods; -- Doing an ANLYZE to give pg_stats every help.
select 'count(*) distinct' as method,
count(*) as count
from (select distinct assembly_id
from assembly_prods) d
union all
select 'n_distinct from pg_stats' as method,
n_distinct as count
from pg_stats
where tablename = 'assembly_prods' and
attname = 'assembly_id';
Run Code Online (Sandbox Code Playgroud)
结果:
method count
count(*) distinct 28088
n_distinct from pg_stats 13805
Run Code Online (Sandbox Code Playgroud)
虽然只相差了 2 倍,但我的数据似乎更糟糕。到了我不会使用估计的地步。我还有什么可以尝试的吗?这是PG 12改进的吗?
如何终止特定 PostgreSQL 用户的所有数据库活动?
我知道如何选择它们:SELECT * FROM pg_stat_activity WHERE usename='foo_user'?
我在这里找到了这个:/sf/answers/2472371891/
我所做的是首先检查正在运行的进程
Run Code Online (Sandbox Code Playgroud)SELECT * FROM pg_stat_activity WHERE state = 'active';找到您要终止的进程,然后键入:
Run Code Online (Sandbox Code Playgroud)SELECT pg_cancel_backend(<pid of the process>)如果无法杀死进程,请尝试:
Run Code Online (Sandbox Code Playgroud)SELECT pg_terminate_backend(<pid of the process>)
但如何将其应用于多个进程呢?
有时 CI 中的测试花费太长时间,系统应该被破坏。
postgresql ×10
index ×2
alter-table ×1
date ×1
disk-space ×1
distinct ×1
index-tuning ×1
performance ×1
statistics ×1
tablespaces ×1
transaction ×1
vacuum ×1