YS
Size: a a a
YS
AS
AS
АС
РЗ
Did not find any relations.
AS
postgres=# \d+ l
Partitioned table "public.l"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+----------+--------------+-------------
period | text | | | | extended | |
id | integer | | not null | | plain | |
data | text | | | | extended | |
Partition key: LIST (period)
Indexes:
"l_id_idx" btree (id)
Partitions: l_y2019q4 FOR VALUES IN ('y2019q4'),
l_y2021q1 FOR VALUES IN ('y2021q1'),
l_y2021q2 FOR VALUES IN ('y2021q2'),
l_y2021q3 FOR VALUES IN ('y2021q3'),
l_y2021q4 FOR VALUES IN ('y2021q4')
postgres=# select * from l;
period | id | data
---------+----+--------------
y2019q4 | 1 | text y2019q4
y2021q2 | 1 | some text 1
y2021q3 | 1 | text y2021q3
(4 rows)
begin isolation level serializable;
select * from pg_class limit 1;
begin isolation level serializable;
create table l_y2019q3 (like l including defaults including constraints);
alter table l attach partition l_y2019q3 for values in ('y2019q3');
insert into l values ('y2019q3', 1, 'test y2019q3');
commit;
select * from l;
period | id | data
---------+----+--------------
y2019q4 | 1 | text y2019q4
y2021q2 | 1 | some text 1
y2021q3 | 1 | text y2021q3
(3 rows)
AS
РЗ
AS
YS
DROP TABLE demo_anomaly;
CREATE TABLE demo_anomaly(part_key int, x int, PRIMARY KEY (part_key, x)) PARTITION BY LIST (part_key);
CREATE TABLE demo_anomaly_1 PARTITION OF demo_anomaly FOR VALUES IN (1);
CREATE TABLE demo_anomaly_2 PARTITION OF demo_anomaly FOR VALUES IN (2);
CREATE TABLE demo_anomaly_3 PARTITION OF demo_anomaly FOR VALUES IN (3);
CREATE OR REPLACE FUNCTION check_anomaly()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $function$
DECLARE
BEGIN
IF (SELECT COUNT(*)
FROM demo_anomaly
WHERE demo_anomaly.x = NEW.x) > 2
THEN
RAISE EXCEPTION 'Triplicates X''s are not allowed (%)!', NEW.x;
END IF;
RETURN NEW;
END;
$function$;
CREATE TRIGGER demo_anomaly_ins
AFTER INSERT ON demo_anomaly
FOR EACH ROW EXECUTE PROCEDURE check_anomaly();
INSERT INTO demo_anomaly(part_key, x)
VALUES (1, 1);
--------------------------------------------------------------------------------
-- session 1:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
INSERT INTO demo_anomaly(part_key, x) VALUES (2, 1);
-- session 2:
BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
CREATE TABLE demo_anomaly_4 PARTITION OF demo_anomaly FOR VALUES IN (4);
-- session 1:
COMMIT;
-- session 2:
INSERT INTO demo_anomaly(part_key, x) VALUES (4, 1);
COMMIT;
DO
АС
AS
DO
W
VS
РЖ
ch
s