如果某个表上的concurednt锁重命名锁定表,会发生什么?

时间:2017-03-23 13:09:14

标签: database postgresql

我每分钟都要插入数百个:

BEGIN ISOLATION LEVEL SERIALIZABLE;

  INSERT INTO table1 VALUES (...);

COMMIT;

我将进行以下交易:

BEGIN ISOLATION LEVEL SERIALIZABLE;

  LOCK TABLE table1 IN ACCESS EXCLUSIVE MODE;

  /* Rename the locked table */
  ALTER TABLE table1 RENAME TO table1_renamed;

  /* And I recreate the table */
  CREATE TABLE table1 (
    ...
  );

COMMIT;

第二个事务将阻止所有INSERT,直到它提交为止。

提交时,是否会在新table1或重命名的Settings -> Build, Execution, Deployment -> Build tools -> Gradle 上继续插入?

在其他世界中,对象的名称获取了锁(因此它在重命名操作时具有弹性)或对象的引用?

2 个答案:

答案 0 :(得分:0)

我试图嘲笑你的疑惑。第一个会话t=# \set PROMPT1 1>,第二个会话t=# \set PROMPT1 2>。分号后的--N表示顺序语句编号符合您描述的情况:

第一节:

1>BEGIN ISOLATION LEVEL SERIALIZABLE; --2
BEGIN
1>ALTER TABLE so32 rename to so33;--4
ALTER TABLE
1>create table so32(i int,t timestamptz default now());--7
CREATE TABLE
1>end;--9
COMMIT
第二节:

2>BEGIN ISOLATION LEVEL SERIALIZABLE; --1
BEGIN
2>insert into so32 (i) select 2;--3
INSERT 0 1
2>end;--5
COMMIT
2>BEGIN ISOLATION LEVEL SERIALIZABLE; --6
BEGIN
2>insert into so32 (i) select 2;--8
INSERT 0 1
2>end;--10
COMMIT
2>select * from so32;
 i |               t
---+-------------------------------
 2 | 2017-03-23 15:31:41.770094+00
(1 row)

2>select * from so33;
 i |               t
---+-------------------------------
 2 | 2017-03-23 15:30:50.761371+00
(1 row)

答案 1 :(得分:0)

LOCK TABLE之后排队的所有交易都会获得新表格INSERT

s1=> CREATE TABLE table1 (id integer PRIMARY KEY, val text NOT NULL);
s1=> BEGIN ISOLATION LEVEL SERIALIZABLE;
s1=> INSERT INTO table1 VALUES (1, 'one');

s2=> BEGIN ISOLATION LEVEL SERIALIZABLE;
s2=> LOCK TABLE table1 IN ACCESS EXCLUSIVE MODE;  -- blocks

s2=> BEGIN ISOLATION LEVEL SERIALIZABLE;
s3=> INSERT INTO table1 VALUES (2, 'two');  -- blocks, queues behind s2

s1=> COMMIT;  -- unblocks s2

s2=> ALTER TABLE table1 RENAME TO table1_renamed;
s2=> CREATE TABLE table1 (id integer PRIMARY KEY, val text NOT NULL);
s2=> COMMIT;  --unblocks s3

s3=> INSERT INTO table1 VALUES (2, 'two');
s3=> COMMIT;

会话s3插入的行位于新表中:

s1=> SELECT * FROM table1;
┌────┬─────┐
│ id │ val │
├────┼─────┤
│  2 │ two │
└────┴─────┘
(1 row)