gpt4 book ai didi

postgresql - 创建新表时 Postgres 客户端锁定

转载 作者:行者123 更新时间:2023-11-29 11:15:20 24 4
gpt4 key购买 nike

我正在尝试在 postgres 中创建一个新表,但是当我这样做时它只是在 CREATE TABLE 调用之后挂起。

$ sudo usermod -s /bin/bash postgres
$ sudo su - postgres
postgres@host:~$ psql ranking_analytics
psql (8.4.8)
Type "help" for help.

ranking_analytics=# BEGIN;
BEGIN
ranking_analytics=# CREATE TABLE "about_contactmessage" (
ranking_analytics(# "id" serial NOT NULL PRIMARY KEY,
ranking_analytics(# "user_id" integer NOT NULL REFERENCES "auth_user" ("id") DEFERRABLE INITIALLY DEFERRED,
ranking_analytics(# "subject" text NOT NULL,
ranking_analytics(# "message" text NOT NULL,
ranking_analytics(# "recorded_time" timestamp with time zone NOT NULL
ranking_analytics(# )
ranking_analytics-# ;
NOTICE: CREATE TABLE will create implicit sequence "about_contactmessage_id_seq" for serial column "about_contactmessage.id"

然后它将无限期地坐在这里,直到我 CTRL-C 它。

数据库中还有其他表,而这个表尚不存在:

ranking_analytics=# \d about_contactmessage
Did not find any relation named "about_contactmessage".

我能够毫无问题地对数据库中的其他表执行插入和删除查询:

ranking_analytics=# insert into locations_continent (continent_name) VALUES ('testing');
INSERT 0 1
ranking_analytics=# delete from locations_continent where continent_name = 'testing';
DELETE 1

机器上有足够的驱动器空间:

$ df -H
Filesystem Size Used Avail Use% Mounted on
/dev/xvda 21G 2.3G 18G 12% /
devtmpfs 255M 132k 255M 1% /dev
none 257M 476k 256M 1% /dev/shm
none 257M 54k 257M 1% /var/run
none 257M 0 257M 0% /var/lock

任何想法可能是错误的?

最佳答案

如果重新启动 postgres 是一个选项,那么这很可能会解决问题,并且会让您免于花时间阅读此答案的其余部分:-)

检查 pg_stat_activity View ,可能有一些其他事务阻止了架构更改。

select * from pg_stat_activity 
where
wait_event_type is NULL and xact_start is not NULL order by xact_start;

(pg_stat_activity 在每一个主要的 pg 版本中都有一点改变,对旧版本试试这个):

select * from pg_stat_activity 
where
not waiting and xact_start is not NULL order by xact_start;

出现的第一行可能是导致问题的那一行。它通常是一个“事务中的空闲”——这很可能持有锁,如果它是一个旧事务,它也可能会降低性能。可能是程序员忘记了确保以“提交”或“回滚”结束事务,或者某些数据库 session 由于网络问题而卡住了。

要终止 pid 1234 的事务,请使用 select pg_cancel_backend(1234);,如果失败,select pg_terminate_backend(1234)。对于 shell 访问,等效命令是 kill -INT 1234kill 1234。 (请记住,kill -9 1234 是一个非常糟糕的主意)。

还有一个 View pg_locks 可能会提供一些见解,尽管从中获取任何有用的信息可能并不那么容易。如果 granted 为真,则持有锁,当 granted 为假时,表示查询正在等待锁。这里有一些关于如何从 pg_locks 中提取有用信息的更多提示:http://wiki.postgresql.org/wiki/Lock_Monitoring

如果其他一切都失败了,那么可能是时候寻求简单的解决方案了,重新启动该数据库服务器。

关于postgresql - 创建新表时 Postgres 客户端锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11306583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com