Aquí está mi código de ejemplo para la partición: t_master es una vista que seleccionar/insertar/actualizar/borrar en su aplicación t_1 y t_2 es las tablas subyacentes en realidad el almacenamiento datos.
create or replace view t_master(id, col1)
as
select id, col1 from t_1
union all
select id, col1 from t_2
CREATE TABLE t_1
(
id bigint PRIMARY KEY,
col1 text
);
CREATE TABLE t_2
(
id bigint PRIMARY KEY,
col1 text
);
CREATE OR REPLACE FUNCTION t_insert_partition_function()
returns TRIGGER AS $$
begin
raise notice '%s', 'hello';
execute 'insert into t_'
|| (mod(NEW.id, 2)+ 1)
|| ' values ($1, $2)' USING NEW.id, NEW.col1 ;
RETURN NULL;
end;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION t_update_partition_function()
returns TRIGGER AS $$
begin
raise notice '%s', 'hello';
execute 'update t_'
|| (mod(NEW.id, 2)+ 1)
|| ' set id = $1, col1 = $2 where id = $1'
USING NEW.id, NEW.col1 ;
RETURN NULL;
end;
$$
LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION t_delete_partition_function()
returns TRIGGER AS $$
begin
raise notice '%s', 'hello';
execute 'delete from t_'
|| (mod(OLD.id, 2)+ 1)
|| ' where id = $1'
USING OLD.id;
RETURN NULL;
end;
$$
LANGUAGE plpgsql;
CREATE TRIGGER t_insert_partition_trigger instead of INSERT
ON t_master FOR each row
execute procedure t_insert_partition_function();
CREATE TRIGGER t_update_partition_trigger instead of update
ON t_master FOR each row
execute procedure t_update_partition_function();
CREATE TRIGGER t_delete_partition_trigger instead of delete
ON t_master FOR each row
execute procedure t_delete_partition_function();
Gracias mucho por su gran comprensión! – Brambo76
+1 ¡Muy informativo! –
Al igual que un punto de datos, nuestra tienda cuenta con más de 300 millones de filas en una de nuestras tablas con mayor acceso, sin particiones ni fragmentación, y funciona bien.Para reafirmar algo de lo anterior, los factores críticos para hacer que el particionamiento valga la pena es tener una clave de partición que a menudo se usa para limitar las filas de interés en las consultas, y que desean eliminar periódicamente una partición completa. (Es mucho más rápido eliminar una partición que eliminar 1/12 de sus filas). – kgrittn