Problema con insert en tabla particionada

From: Juan Carlos Ramirez Zambrano <juancarlosrz(dot)78(at)gmail(dot)com>
To: Ayuda <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Problema con insert en tabla particionada
Date: 2012-03-23 00:26:36
Message-ID: CAKqO2PiC_-sDK50asKKfWn1eLMtrcifq+hnH4EEA1EEEcVBkaQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Hola Lista, quiero consultar y ver si me pueden orientar con un problema
que se me presento con los insert en una tabla particionada

El problema se presento de la nada, se implemento un trigger para insertar
los datos en tablas por dia, esta tabla recibio hasta las 14:00 horas
4,951,257 millones de registros, esto se implemento a las 00:00 horas y
todo iva bien hasta las 14:00 horas cuando los inserte se empezaron hacer
muy lentos, es decir a tardar casi 1 segundo por insert.

Agradezco cualquier ayuda que me puedan proporcionar para poder solucionar
este problema. Aqui dejo el trigger implementado y la tabla padre.

CREATE OR REPLACE FUNCTION monterrey.tabla_x_dia_v1()
RETURNS trigger AS
$BODY$
DECLARE
nombre_tabla text:='';
BEGIN
--Se obtiene la fecha a concatenar al nombre de la tabla
select 'tabla_'||substr(NEW.recibo::text,1,4)
||'_'||substr(NEW.recibo::text,6,2) ||'_'|| substr(NEW.recibo::text,9,2)
into nombre_tabla;

--Validamos si existe la tabla continuamos solo con el insert
IF NOT EXISTS (SELECT * FROM information_schema.tables WHERE table_name =
nombre_tabla) THEN
raise notice 'No existe la tabla, se crea como %',nombre_tabla;

--Se ejecuta la creacion de la tabla correspondiete al dia
EXECUTE 'CREATE TABLE monterrey.' || nombre_tabla || '( CHECK( recibo >=
'''||
substr(NEW.recibo::text,0,11)||' 00:00:00''' ||' and recibo<= '''||
substr(NEW.recibo::text,0,11) ||' 23:59:59'')) INHERITS (esquema.tabla);';

--Se asignan los permisos correspondientes
EXECUTE 'GRANT ALL ON TABLE esquema.'||nombre_tabla||' TO " usuario ";';
EXECUTE 'GRANT ALL ON TABLE esquema .'||nombre_tabla||' TO usuario ;';
EXECUTE 'GRANT ALL ON TABLE esquema .'||nombre_tabla||' TO usuario ;';
EXECUTE 'GRANT ALL ON TABLE esquema .'||nombre_tabla||' TO usuario ;';
EXECUTE 'GRANT SELECT, UPDATE, INSERT ON TABLE esquema .'||nombre_tabla||'
TO usuario;';
EXECUTE 'GRANT SELECT, UPDATE, INSERT ON TABLE esquema .'||nombre_tabla||'
TO usuario ;';
EXECUTE 'GRANT SELECT, UPDATE, INSERT ON TABLE esquema .'||nombre_tabla||'
TO usuario ;';

--Se agrega la llave primaria de tabla
EXECUTE 'ALTER TABLE esquema.'||nombre_tabla||' ADD PRIMARY KEY (unidad,
economico, linea, hreal, recibo) USING INDEX TABLESPACE idx';
--Se agregan sus indices
EXECUTE 'CREATE INDEX idx_fecins'||nombre_tabla||' ON
esquema.'||nombre_tabla||' USING btree
(fecins) TABLESPACE idx;';

EXECUTE 'CREATE INDEX idx_hreal_lin_eco'||nombre_tabla||' ON
esquema.'||nombre_tabla||' USING btree
(hreal, linea, economico) TABLESPACE idx;';

END IF;

--Ejecutamos el insert del regsitro nuevo
EXECUTE 'insert into esquema.'|| nombre_tabla ||
'(campo, campo1,
campo2,fecha,lati,long,paro,distancia,causa,velp,velm,alarma,hora,

recibo,operador,campo1,campo2,pasarela,momento,sub_del,baj_del,blo_del,ala_del,sub_tra,
baj_tra, blo_tra,ala_tra,tipo,xy, panicos, aux_d, aux_t,oreja)
values ('''|| NEW.unidad || ''','|| NEW.economico || ',' || NEW.linea ||
','''|| NEW.hreal || ''','|| NEW.lat || ','|| NEW.lon ||','|| NEW.paro
||','|| NEW.distancia ||
','''|| NEW.causa ||''',' || NEW.velp || ',' || NEW.velm || ',''' ||
NEW.alarma || ''',' || NEW.hora || ',''' || NEW.recibo || ''',' ||
NEW.operador || ',' || NEW.campo1 ||
',' || NEW.campo2 || ',' || NEW.pasarela || ',' || NEW.momento || ',' ||
NEW.sub_del || ',' || NEW.baj_del || ',' || NEW.blo_del || ',' ||
NEW.ala_del || ',' || NEW.sub_tra ||
',' || NEW.baj_tra || ',' || NEW.blo_tra || ',' || NEW.ala_tra || ',''' ||
NEW.tipo || ''',''' || NEW.xy || ''',' || NEW.panicos || ',' || NEW.aux_d
|| ',' || NEW.aux_t || ',' || NEW.oreja ||
');';

RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

CREATE TABLE esquema.tabla
(
unidad character(4) NOT NULL DEFAULT ''::bpchar,
economico integer NOT NULL DEFAULT 0,
linea smallint NOT NULL DEFAULT 0,
hreal timestamp without time zone NOT NULL,
lat integer NOT NULL DEFAULT 0,
lon integer NOT NULL DEFAULT 0,
paro smallint DEFAULT 0,
distancia smallint DEFAULT 0,
causa character(1) NOT NULL DEFAULT ''::bpchar,
velp smallint DEFAULT 0,
velm smallint DEFAULT 0,
alarma character(1) DEFAULT ''::bpchar,
hora integer NOT NULL DEFAULT 0,
recibo timestamp without time zone NOT NULL,
operador numeric(14,0) DEFAULT 0,
campo1 bigint DEFAULT 0,
campo2 bigint DEFAULT 0,
pasarela integer DEFAULT 0,
momento integer DEFAULT 0,
sub_del integer DEFAULT 0,
baj_del integer DEFAULT 0,
blo_del integer DEFAULT 0,
ala_del integer DEFAULT 0,
sub_tra integer DEFAULT 0,
baj_tra integer DEFAULT 0,
blo_tra integer DEFAULT 0,
ala_tra integer DEFAULT 0,
tipo character(1) DEFAULT ''::bpchar,
xy point DEFAULT '(0,0)'::point,
panicos integer DEFAULT 0,
aux_d integer DEFAULT 0,
aux_t integer DEFAULT 0,
oreja integer DEFAULT 0,
fecins timestamp without time zone DEFAULT now(),
CONSTRAINT pkey PRIMARY KEY (unidad , economico , linea , hreal , recibo )
USING INDEX TABLESPACE idx
)
WITH (
OIDS=FALSE
);
ALTER TABLE esquema.tabla
OWNER TO "super";

CREATE INDEX fecins_idx
ON esquema.tabla
USING btree
(fecins )
TABLESPACE idx;

CREATE INDEX hreal_linea_economico_idx
ON esquema.tabla
USING btree
(hreal , linea , economico )
TABLESPACE idx;

CREATE TRIGGER tabla_x_dia
BEFORE INSERT
ON esquema.tabla
FOR EACH ROW
EXECUTE PROCEDURE esquema.tabla_x_dia_v1();

De antemano agradezco la ayuda que se me pueda proporcionar para poder
solucionar este problema

Saludos.

LI. Juan Carlos Ramirez Z.

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Erick Urias 2012-03-23 07:14:00 Ayuda para elegir hardware
Previous Message Juan Carlos Ramirez Zambrano 2012-03-22 23:10:49 Problema con insert en tabla particionada