Hola Avaro

Hice el correspondiente laboratorio en una instancia dedicada de PostgreSQL
11.11, uno con una tabla "padre" con primariy key definido y otra con tabla
"padre" sin el primary key, aqui los resultados

-- copia para crear las dos particiones con datos a partir de tabla original

create table public.llamadas_with_pk as select * from public.llamadas;
alter table public.llamadas_with_pk alter column id set not null;
alter table public.llamadas_with_pk alter column fecha set not null;
alter table public.llamadas_with_pk alter column uniqueid set not null;
alter table public.llamadas_with_pk alter column tipo_evento set not null;

create table public.llamadas_without_pk as select * from public.llamadas;
alter table public.llamadas_without_pk alter column id set not null;
alter table public.llamadas_without_pk alter column fecha set not null;
alter table public.llamadas_without_pk alter column uniqueid set not null;
alter table public.llamadas_without_pk alter column tipo_evento set not
null;


-- tabla with pk


CREATE TABLE public.llamadas_particion_with_pk
(
  id bigserial NOT NULL,
  fecha timestamp with time zone NOT NULL,
  uniqueid character varying(255) NOT NULL,
  tipo_evento character varying(255) NOT NULL,
  CONSTRAINT llamadas_particion_with_pk_pkey PRIMARY KEY (id,fecha)
)PARTITION BY RANGE(fecha);

checkpoint;

/*
bash-4.1$ du  --max-depth=1
12      ./log
16      ./pg_logical
4       ./pg_commit_ts
910232  ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1114120 ./pg_wal
44      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2025164 .


*/

ALTER TABLE public.llamadas_particion_with_pk ATTACH PARTITION
public.llamadas_with_pk  FOR VALUES FROM ('2020-11-01 00:00:00' ) TO
('2021-03-30 23:59:59');
-- Query returned successfully with no result in 3.9 secs.

checkpoint;


/*
Genero WALs (diferencia 49152 )

bash-4.1$ du  --max-depth=1
12      ./log
16      ./pg_logical
4       ./pg_commit_ts
966504  ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1163272 ./pg_wal
44      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2130588 .

*/


CREATE TABLE public.llamadaswith_2021_04 PARTITION OF
public.llamadas_particion_with_pk FOR VALUES FROM ('2021-04-01 00:00:00' )
TO ('2021-04-30 23:59:59');
CREATE TABLE public.llamadaswith_2021_05 PARTITION OF
public.llamadas_particion_with_pk FOR VALUES FROM ('2021-05-01 00:00:00' )
TO ('2021-05-31 23:59:59');




-- tabla without pk


CREATE TABLE public.llamadas_particion_without_pk
(
  id bigserial NOT NULL,
  fecha timestamp with time zone NOT NULL,
  uniqueid character varying(255) NOT NULL,
  tipo_evento character varying(255) NOT NULL
)PARTITION BY RANGE(fecha);

checkpoint;

/*
bash-4.1$ du  --max-depth=1
12      ./log
16      ./pg_logical
4       ./pg_commit_ts
966600  ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1163272 ./pg_wal
48      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2130688 .


*/


ALTER TABLE public.llamadas_particion_without_pk ATTACH PARTITION
public.llamadas_without_pk  FOR VALUES FROM ('2020-11-01 00:00:00' ) TO
('2021-03-30 23:59:59');
-- Query returned successfully with no result in 6.3 secs.

checkpoint;

/*
NO Genero WALs

bash-4.1$ du  --max-depth=1
12      ./log
16      ./pg_logical
4       ./pg_commit_ts
966600  ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1163272 ./pg_wal
48      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2130688 .



*/

create unique index concurrently llamadas_particion_without_pk on
llamadas_particion_without_pk(id,fecha);
-- ERROR:  cannot create index on partitioned table
"llamadas_particion_without_pk" concurrently

-- genera bloqueo sobre la tabla
alter table llamadas_particion_without_pk add  primary key (id,fecha)
-- Query returned successfully with no result in 5.6 secs.

checkpoint;

/*
Genero WALs  (dif: 49152)

bash-4.1$ du  --max-depth=1
16      ./log
16      ./pg_logical
4       ./pg_commit_ts
1022820 ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1212424 ./pg_wal
48      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2236064 .

*/


CREATE TABLE public.llamadaswithout_2021_04 PARTITION OF
public.llamadas_particion_without_pk FOR VALUES FROM ('2021-04-01 00:00:00'
) TO ('2021-04-30 23:59:59');
CREATE TABLE public.llamadaswithout_2021_05 PARTITION OF
public.llamadas_particion_without_pk FOR VALUES FROM ('2021-05-01 00:00:00'
) TO ('2021-05-31 23:59:59');



Según esto, en la generación del  Primary Key es cuando  generan los WAL (y
seguro de cualquier unique o índice que tenga), incluso lei que agregando
un check en la tabla con datos con el rango de la partición antes de
agregarla con ATTACH omitía  el full scan que tenia que hacer para
verificar  si los datos cumplen  con el rango de partición:


create table public.llamadas_without_pk_with_check as select * from
public.llamadas;
alter table public.llamadas_without_pk_with_check alter column id set not
null;
alter table public.llamadas_without_pk_with_check alter column fecha set
not null;
alter table public.llamadas_without_pk_with_check alter column uniqueid set
not null;
alter table public.llamadas_without_pk_with_check alter column tipo_evento
set not null;


alter table public.llamadas_without_pk_with_check add CONSTRAINT
valid_range_check CHECK  (fecha between ('2020-11-01 00:00:00' ) and
('2021-03-30 23:59:59'));

checkpoint


-- tabla without pk with check


CREATE TABLE public.llamadas_particion_without_pk_with_check
(
  id bigserial NOT NULL,
  fecha timestamp with time zone NOT NULL,
  uniqueid character varying(255) NOT NULL,
  tipo_evento character varying(255) NOT NULL,
  opcion character varying(255),
  dato1 character varying(255),
  dato2 character varying(4096),
  dato3 character varying(255),
  dato4 character varying(255),
  dato5 character varying(2048),
  dato6 character varying(255),
  dato7 character varying(255),
  dato8 character varying(255),
  dato9 character varying(255),
  dato10 character varying(255)
)PARTITION BY RANGE(fecha);


checkpoint

/*
bash-4.1$ du  --max-depth=1
16      ./log
16      ./pg_logical
4       ./pg_commit_ts
1291696 ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1523720 ./pg_wal
48      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2816236 .



*/


ALTER TABLE public.llamadas_particion_without_pk_with_check ATTACH
PARTITION public.llamadas_without_pk_with_check  FOR VALUES FROM
('2020-11-01 00:00:00' ) TO ('2021-03-30 23:59:59');
-- Query returned successfully with no result in 2.0 secs.

checkpoint


/*
bash-4.1$ du  --max-depth=1
16      ./log
16      ./pg_logical
4       ./pg_commit_ts
1291704 ./base
28      ./pg_multixact
4       ./pg_serial
12      ./pg_subtrans
12      ./pg_notify
4       ./pg_tblspc
1523720 ./pg_wal
48      ./pg_stat_tmp
4       ./pg_stat
4       ./pg_replslot
4       ./pg_snapshots
4       ./pg_twophase
584     ./global
4       ./pg_dynshmem
12      ./pg_xact
2816244 .

*/

Lo único es al agregar el primary key (sea por índice unique o con
constraint primary key) no se puede hacer con concurrently y generaría un
bloqueo.




El lun, 3 de may. de 2021 a la(s) 07:04, Alvaro Herrera (
alvhe...@alvh.no-ip.org) escribió:

> Hellmuth Vargas escribió:
> > Hola Álvaro
> >
> > El dom., 2 de mayo de 2021 10:00 p. m., Alvaro Herrera <
> > alvhe...@alvh.no-ip.org> escribió:
> >
> > > Hellmuth Vargas escribió:
> > >
> > > > La instrucciones que ejecute básicamente fueron:
> > > >
> > > > CREATE TABLE llamadas (
> > > >    campos
> > > > ) PARTITION BY RANGE (fecha);
> > >
> > > ¿hay índice o PK en estos campos?
> >
> > Si señor,  PK
> >
> > CREATE TABLE llamadas (
> > id bigserial not null,
> > fecha timestamp,
> > constraint pk_llamadas primary key (id, fecha)
> > ) PARTITION BY RANGE (fecha);
>
> Prueba creando el índice en las tablas antes de hacer el attach. (No
> recuerdo si basta con CREATE UNIQUE INDEX o es necesario ADD PRIMARY
> KEY).
>
> --
> Álvaro Herrera                            39°49'30"S 73°17'W
> "En las profundidades de nuestro inconsciente hay una obsesiva necesidad
> de un universo lógico y coherente. Pero el universo real se halla siempre
> un paso más allá de la lógica" (Irulan)
>


-- 
Cordialmente,

Ing. Hellmuth I. Vargas S.
Esp. Telemática y Negocios por Internet
Oracle Database 10g Administrator Certified Associate
EnterpriseDB Certified PostgreSQL 9.3 Associate

Reply via email to