Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4fns-00077m-3y for pgsql-general@arkaria.postgresql.org; Tue, 25 Sep 2018 05:22:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4fnp-0004Oq-3r for pgsql-general@arkaria.postgresql.org; Tue, 25 Sep 2018 05:22:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4fno-0004Oj-HK for pgsql-general@lists.postgresql.org; Tue, 25 Sep 2018 05:22:28 +0000 Received: from mail.alteeve.ca ([65.39.153.71] helo=zimbra.alteeve.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4fne-0000p3-UD for pgsql-general@lists.postgresql.org; Tue, 25 Sep 2018 05:22:23 +0000 Received: from localhost (localhost [127.0.0.1]) by zimbra.alteeve.com (Postfix) with ESMTP id 0AD11BFAF8F for ; Tue, 25 Sep 2018 01:22:12 -0400 (EDT) Received: from zimbra.alteeve.com ([127.0.0.1]) by localhost (zimbra.alteeve.com [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id CuHgxQbkLAR7 for ; Tue, 25 Sep 2018 01:22:11 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.alteeve.com (Postfix) with ESMTP id AEE88BFAF90 for ; Tue, 25 Sep 2018 01:22:11 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 zimbra.alteeve.com AEE88BFAF90 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alteeve.ca; s=3658BF78-91B3-11E8-A999-AEFD2914AE28; t=1537852931; bh=L4D9CeKeYc7/GgXRketUDEMbQYhJ1QZwEHbstmIqldI=; h=To:From:Message-ID:Date:MIME-Version; b=FgGfwxcFsMkywbzdPp3V73q18yA+Q4MUcnxi4GckY5/FFycBOVEwOZj4OBA5gBbl+ 0auCOP3h9orPkJWvxv6RnRCyUYlmPD5MJTGlTfUvTKS2K8ys6fIbd8XGnxGz4rRZXW zhxz17l/qIBhUWSXbmoAOhT/hl+tZYe+xgHJ6bUPz46bCm+2DwyD4oP6fSAW02p+02 q+CWz0EvBSfDb5QwFenhVM6dlDFyUJIktoUZoRA3m4L4gBBBfay0PBiMWxAEWoNi5E YRQNkib7cedDnqBZeTediERmU8uHUc5KGoSHJLr3u3WEgbG72uJDZ394VAUcm3HF9q 5sfNANRuv6Rog== X-Virus-Scanned: amavisd-new at zimbra.alteeve.com Received: from zimbra.alteeve.com ([127.0.0.1]) by localhost (zimbra.alteeve.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id h18zjPL8obq0 for ; Tue, 25 Sep 2018 01:22:11 -0400 (EDT) Received: from neutron.alteeve.com (072-185-148-175.res.spectrum.com [72.185.148.175]) by zimbra.alteeve.com (Postfix) with ESMTPSA id 7D24ABFAF8F for ; Tue, 25 Sep 2018 01:22:11 -0400 (EDT) To: pgsql-general@lists.postgresql.org From: digimer Subject: Weird procedure question Message-ID: <95e561cf-c098-4648-0ec5-030b47c995ba@alteeve.ca> Date: Tue, 25 Sep 2018 01:22:11 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi all, =C2=A0 I've got an interesting use case that I am stuck on. It's a bit o= f a=20 complicated environment, but I'll try to keep it simple. =C2=A0 In short; I have a history schema that has tables that match the=20 public schema, plus one 'history_id' column that has a simple sequential=20 bigserial value. Then I have a procedure and trigger that, on UPDATE or=20 INSERT, copies the data to history. Example use case is that I can=20 UPDATE a sensor value in the public table and it's also INSERTs the data=20 into history. So public shows just the most recent values, but I can see=20 changes over time in the history schema. =C2=A0 I have built my system to support writing to one or more DBs. I k= eep=20 a list of connected DBs and send INSERT/UPDATE calls to a method that=20 then runs the UPDATE/INSERT against all connected databases, as a form=20 of redundancy. This all works fine. =C2=A0 The problem I've hit is that the 'history_id' differs between the= =20 various databases. So I want to switch this to 'history_uuid' and use=20 UUIDs instead of bigserial. =C2=A0 Now the question; =C2=A0 Can I tell a produce to use a specific UUID? =C2=A0 The idea is to generate a UUID for 'history_uuid' so that I have=20 consistency across databases. Of course, if an UPDATE will change=20 multiple rows, then I'll need to predefine multiple UUIDs. This is where=20 things start to get really complicated I think... Maybe I could pass an=20 array of UUIDs? I don't care if I find out which UUID was used for which=20 record, just that the same UUID was used for the same record when the=20 procedure is (re)run on other DBs. =C2=A0 The databases are not clustered, on purpose. I've been trying to=20 handle all the HA stuff in my application for various reasons. If it helps, here is an example pair of tables, the procedure and the=20 trigger I currently use; =3D=3D=3D=3D CREATE TABLE host_variable ( =C2=A0=C2=A0=C2=A0 host_variable_uuid uuid=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 not null=C2=A0=C2=A0=C2=A0 primary key, =C2=A0=C2=A0=C2=A0 host_variable_host_uuid=C2=A0=C2=A0=C2=A0 uuid=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 not null, =C2=A0=C2=A0=C2=A0 host_variable_name text=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 not null, =C2=A0=C2=A0=C2=A0 host_variable_value text=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 not null, =C2=A0=C2=A0=C2=A0 modified_date=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 timestamp with time zone=C2=A0=C2=A0= =C2=A0 not null ); ALTER TABLE host_variable OWNER TO admin; CREATE TABLE history.host_variable ( =C2=A0=C2=A0=C2=A0 history_id=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bigserial, =C2=A0=C2=A0=C2=A0 host_variable_uuid=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 uuid, =C2=A0=C2=A0=C2=A0 host_variable_host_uuid=C2=A0=C2=A0=C2=A0 uuid, =C2=A0=C2=A0=C2=A0 host_variable_name=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 text, =C2=A0=C2=A0=C2=A0 host_variable_value=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 text, =C2=A0=C2=A0=C2=A0 modified_date=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 timestamp with time zone=C2=A0=C2=A0= =C2=A0 not null ); ALTER TABLE history.host_variable OWNER TO admin; CREATE FUNCTION history_host_variable() RETURNS trigger AS $$ DECLARE =C2=A0=C2=A0=C2=A0 history_host_variable RECORD; BEGIN =C2=A0=C2=A0=C2=A0 SELECT INTO history_host_variable * FROM host_variabl= e WHERE=20 host_uuid =3D new.host_uuid; =C2=A0=C2=A0=C2=A0 INSERT INTO history.host_variable =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (host_variable_uuid, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 host_variable_host_uuid= , =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 host_variable_name, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 host_variable_value, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 modified_date) =C2=A0=C2=A0=C2=A0 VALUES =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (history_host_variable.host_v= ariable_uuid, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 history_host_variable.h= ost_variable_host_uuid, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 history_host_variable.h= ost_variable_name, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 history_host_variable.h= ost_variable_value, =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 history_host_variable.m= odified_date); =C2=A0=C2=A0=C2=A0 RETURN NULL; END; $$ LANGUAGE plpgsql; ALTER FUNCTION history_host_variable() OWNER TO admin; CREATE TRIGGER trigger_host_variable =C2=A0=C2=A0=C2=A0 AFTER INSERT OR UPDATE ON host_variable =C2=A0=C2=A0=C2=A0 FOR EACH ROW EXECUTE PROCEDURE history_host_variable(= ); =3D=3D=3D=3D =C2=A0 I know this might sound odd, but I didn't want to complicate thin= gs=20 with how my system works. However, if it would help solve the problem,=20 I'm happy to dig into more detail. =C2=A0 Thanks!