Re: Ayuda con inquietud

From: Alejandro Carrillo <fasterzip(at)yahoo(dot)es>
To: Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>, Jorge Toro <jolthgs(at)gmail(dot)com>, Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Ayuda con inquietud
Date: 2012-07-09 16:12:11
Message-ID: 1341850331.26288.YahooMailNeo@web171002.mail.ukl.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Si quieres puedes meter esa función en un trigger de la tabla positions; así cuando se agrege una posicion del GPS, se actualice en la tabla gps. No olvidaer hacer vacuum full periódicamente a la tabla GPS, ya que las actualizaciones realmente son borrados e inserciones de los datos.

>________________________________
> De: Alejandro Carrillo <fasterzip(at)yahoo(dot)es>
>Para: Alejandro Carrillo <fasterzip(at)yahoo(dot)es>; Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>; Jorge Toro <jolthgs(at)gmail(dot)com>; Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
>Enviado: Lunes 9 de julio de 2012 10:55
>Asunto: Re: [pgsql-es-ayuda] Ayuda con inquietud
>
>
>perdon, por favor corregir el is null por is not null
>
>
>
>
>>________________________________
>> De: Alejandro Carrillo <fasterzip(at)yahoo(dot)es>
>>Para: Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>; Jorge Toro <jolthgs(at)gmail(dot)com>; Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
>>Enviado: Lunes 9 de julio de 2012 10:34
>>Asunto: Re: [pgsql-es-ayuda] Ayuda con inquietud
>>
>>
>>Creo que lo mejor es hacer un índice único en la tabla GPS (llave primaria), un campo donde esté la última posición del GPS y otro índice en la llave foránea de GPS en la tabla positions. Y crear una function asi:
>>
>>
>>CREATE OR REPLACE FUNCTION public.fn_guardar_posicion (
>>  gps_id_ integer,
>>  posicion_ varchar,
>>  velocidad_ real,
>>  altura_ real,
>>  fecha_ timestamp,
>>  grados_ real,
>>  satelites_ integer
>>)
>>RETURNS integer AS
>>$body$
>>DECLARE
>>        retorno INTEGER;
>>BEGIN   
>>   
>>        INSERT INTO
positions
>>        (
>>          gps_id,
>>         
posicion,
>>          velocidad,
>>          altura,
>>          fecha,
>>          grados,
>>          satelites
>>        )
>>        VALUES (
>>          gps_id_,
>>          posicion_,
>>          velocidad_,
>>          altura_,
>>          fecha_,
>>          grados_,
>>          satelites_
>>        )RETURNING id INTO retorno;
>>    if (id is null) then
>>       UPDATE gps
>>        SET
>>          posicion = posicion_,
>>        WHERE id = gps_id_;
>>
>>    end if;   
>>;
>>
>>        RETURN retorno;
>>END;
>>$body$
>>LANGUAGE 'plpgsql';
>>
>>
>>Así no necesitas tener más tablas adicionales. No olvidar hacer vacuum full a las tablas periódicamente en horario donde haya pocas transacciones.
>>
>>
>>
>>
>>>________________________________
>>> De: Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu>
>>>Para: Jorge Toro <jolthgs(at)gmail(dot)com>; Foro Postgres <pgsql-es-ayuda(at)postgresql(dot)org>
>>>Enviado: Lunes 9 de julio de 2012 10:21
>>>Asunto: RE: [pgsql-es-ayuda] Ayuda con inquietud
>>>
>>>Entonces podrias tener dos tablas positions, una que se llame hist_positions por ejemplo, y la otra positions, sobre positions almacenas la última ubicación de los gps y sobre hist_positions el historial de posiciones de los gps, para almacenar las posiciones de los gps utililizas una función, y para llevar el historial de los gps, puedes utilizar un trigger sobre positions. Acá te dejo el código de todo, solo falta la referencia de la clave foránea. Espero que te sirva.
>>>
>>>PD: Pudieran existir otras soluciones.
>>>
>>>--Crear la tabla positions
>>>CREATE TABLE public.positions (
>>>  id
SERIAL,
>>>  gps_id INTEGER,
>>>  posicion VARCHAR,
>>>  velocidad REAL,
>>>  altura REAL,
>>>  fecha TIMESTAMP WITHOUT TIME ZONE,
>>>  grados REAL,
>>>  satelites INTEGER,
>>>  CONSTRAINT positions_pkey PRIMARY KEY(id)
>>>) WITHOUT OIDS;
>>>
>>>--Crear la tabla hist_positions
>>>CREATE TABLE public.hist_positions (
>>>  id SERIAL,
>>>  gps_id INTEGER,
>>>  posicion VARCHAR,
>>>  velocidad REAL,
>>>  altura REAL,
>>>  fecha TIMESTAMP WITHOUT TIME ZONE,
>>>  grados REAL,
>>>  satelites INTEGER,
>>>  CONSTRAINT hist_positions_pkey PRIMARY KEY(id)
>>>) WITHOUT OIDS;
>>>
>>>--Función para guardar en positions
>>>CREATE OR REPLACE FUNCTION public.fun_guardar_posicion (
>>>  gps_id_ integer,
>>>  posicion_ varchar,
>>>  velocidad_ real,
>>>  altura_ real,
>>>  fecha_ timestamp,
>>>  grados_ real,
>>>  satelites_ integer
>>>)
>>>RETURNS
integer AS
>>>$body$
>>>DECLARE
>>>        retorno INTEGER;
>>>BEGIN
>>>    UPDATE positions
>>>    SET
>>>      posicion = posicion_,
>>>      velocidad = velocidad_,
>>>      altura = altura_,
>>>      fecha = fecha_,
>>>      grados = grados_,
>>>      satelites = satelites_
>>>    WHERE gps_id = gps_id_
>>>    RETURNING id INTO retorno;
>>>
>>>        IF (retorno IS NULL) THEN
>>>        INSERT INTO positions
>>>        (
>>>          gps_id,
>>>          posicion,
>>>          velocidad,
>>>          altura,
>>>          fecha,
>>>          grados,
>>>         
satelites
>>>        )
>>>        VALUES (
>>>          gps_id_,
>>>          posicion_,
>>>          velocidad_,
>>>          altura_,
>>>          fecha_,
>>>          grados_,
>>>          satelites_
>>>        )RETURNING id INTO retorno;
>>>    END IF;
>>>
>>>        RETURN retorno;
>>>END;
>>>$body$
>>>LANGUAGE 'plpgsql';
>>>
>>>--Función ejecutada por el trigger para guardar en el historial de positions
>>>CREATE OR REPLACE FUNCTION public.fun_trg_hist_positions (
>>>)
>>>RETURNS trigger AS
>>>$body$
>>>DECLARE
>>>BEGIN
>>>  INSERT INTO hist_positions
>>>  (
>>>    gps_id,
>>>    posicion,
>>>    velocidad,
>>>    altura,
>>> 
  fecha,
>>>    grados,
>>>    satelites
>>>  )
>>>  VALUES (
>>>    NEW.gps_id,
>>>    NEW.posicion,
>>>    NEW.velocidad,
>>>    NEW.altura,
>>>    NEW.fecha,
>>>    NEW.grados,
>>>    NEW.satelites
>>>  );
>>>
>>>  RETURN NULL;
>>>END;
>>>$body$
>>>LANGUAGE 'plpgsql';
>>>
>>>--Trigger utilizado
>>>CREATE TRIGGER trg_hist_positions AFTER INSERT OR UPDATE
>>>ON public.positions FOR EACH ROW
>>>EXECUTE PROCEDURE public.fun_trg_hist_positions();
>>>
>>>Saludos a todos.
>>>________________________________________
>>>From: pgsql-es-ayuda-owner(at)postgresql(dot)org [pgsql-es-ayuda-owner(at)postgresql(dot)org] On Behalf Of Jorge Toro [jolthgs(at)gmail(dot)com]
>>>Sent: Monday, July 09, 2012 9:43 AM
>>>To: Foro Postgres
>>>Subject: Re: [pgsql-es-ayuda] Ayuda con inquietud
>>>
>>>Hola Lazáro, gracias por responder.
>>>
>>>Sí gps.id<http://gps.id> es la referencia a una tabla "gps" que se usa para registrar cada uno de los GPS que se registran para ser aceptados por el servidor.
>>>Esta tabla "gps" tiene datos como: id, name, type, active. de cada uno de los GPS. Y la tabla "position" se encarga de almacenar los reportes (históricos) hechos por cada GPS.
>>>
>>>Y lo que quiero lograr es, tener a la mano siempre que consulte un dispositivo, toda la última información de dicho dispositivo que se encuentre en
la tabla "position".
>>>
>>>Pero no quiero realizar una consulta directa a  la tabla "position" porque son alrededor de 1000 GPS reportando cada minuto y esta
tabla "position" tenderá a ser demasiado grande con el pasar del tiempo.
>>>
>>>
>>>Por tu ayuda muchas gracias,
>>>
>>>
>>>Jorge Alonso Toro
>>>Ing. Teleinformático.
>>>
>>>http://jolthgs.wordpress.com/
>>>www.devmicrosystem.com<http://www.devmicrosystem.com>
>>>--------------------------------------------------------------
>>>Powered By Debian.
>>>Developer Bullix GNU/Linux.
>>>--------------------------------------------------------------
>>>-----BEGIN PGP SIGNATURE-----
>>>Version: GnuPG v1.4.6 (GNU/Linux)
>>>
>>>iD8DBQBIWWH6q7mzdgTzI5ARAkX5AJ9TR6hL2ocLMOUDRfhts8DlVl+jpwCeNw5x
>>>p4+4FNUHPDUx1lU9F8WSKCA=
>>>=zRhQ
>>>-----END PGP SIGNATURE-----
>>>Este correo esta protegido bajo los términos de la Licencia Atribución-Compartir Obras Derivadas Igual a 2.5 Colombia de Creative Commons. Observé
la licencia visitando este
sitio http://creativecommons.org/licenses/by-sa/2.5/co/.
>>>
>>>
>>>El 9 de julio de 2012 09:17, Lazáro Rubén García Martínez <lgarciam(at)vnz(dot)uci(dot)cu<mailto:lgarciam(at)vnz(dot)uci(dot)cu>> escribió:
>>>La columna gps_id es única
>>>
>>>
>>>________________________________
>>>Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN PRISIONES DE LOS EEUU!
>>>http://www.antiterroristas.cu
>>>http://justiciaparaloscinco.wordpress.com
>>>
>>>Fin a la injusticia, LIBERTAD AHORA A NUESTROS CINCO COMPATRIOTAS QUE SE ENCUENTRAN INJUSTAMENTE EN
PRISIONES DE LOS EEUU!
>>>http://www.antiterroristas.cu
>>>http://justiciaparaloscinco.wordpress.com
>>>-
>>>Enviado a la lista de correo pgsql-es-ayuda (pgsql-es-ayuda(at)postgresql(dot)org)
>>>Para cambiar tu suscripción:
>>>http://www.postgresql.org/mailpref/pgsql-es-ayuda
>>>
>>>
>>>
>>
>>
>
>

In response to

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Jorge Toro 2012-07-09 16:13:12 Re: Ayuda con inquietud
Previous Message Mario Sileone 2012-07-09 16:04:41 Re: Ayuda con inquietud