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 1g4w6E-0003aq-Tb for pgsql-general@arkaria.postgresql.org; Tue, 25 Sep 2018 22:46:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g4w6C-0007j8-A3 for pgsql-general@arkaria.postgresql.org; Tue, 25 Sep 2018 22:46:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4w2W-00044F-JC for pgsql-general@lists.postgresql.org; Tue, 25 Sep 2018 22:42:44 +0000 Received: from mail.alteeve.ca ([65.39.153.71] helo=zimbra.alteeve.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g4w2T-00071W-1a for pgsql-general@lists.postgresql.org; Tue, 25 Sep 2018 22:42:42 +0000 Received: from localhost (localhost [127.0.0.1]) by zimbra.alteeve.com (Postfix) with ESMTP id EC5153FA94; Tue, 25 Sep 2018 18:42:38 -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 v2-R6ppUpT3b; Tue, 25 Sep 2018 18:42:38 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.alteeve.com (Postfix) with ESMTP id 8A36BC03EB6; Tue, 25 Sep 2018 18:42:38 -0400 (EDT) DKIM-Filter: OpenDKIM Filter v2.10.3 zimbra.alteeve.com 8A36BC03EB6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alteeve.ca; s=3658BF78-91B3-11E8-A999-AEFD2914AE28; t=1537915358; bh=D2ozvprOibqsHSYWiWoJqSH8e7F71wA3Heb+G7uD1Bs=; h=From:To:Message-ID:Date:MIME-Version; b=KYRyLgwLJ+jOvVgn78qiuWP8DZscYc6PpFzzVi4lEz1ULq84Wa0X5Z2kGvfj6wJvP Dy/h4Xav4KzUr1ow7AaxryIfEKwukWI6zxK7NOs16RAXJoNgpYf/AXNNjQayWebATX /hZfnp0IwtQfAx7p79BkC0AF3RRYFLcLEQezqqhJFy7mmpi3v6vkUWFizj91/muBIT 2MylYr9Z64AR+8CZ4T7mMWfaH72NEhOvnS8yALh33NwqzenpVHSk83ZKRMDFJa/28K Nea3FmLbQYma36DiYnMEPKj6+hqJVPr7CjXXZHeXuTkobHAnOx0Ne1MX+INelcdmyA WCmRdnbV4KTnQ== 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 x4aYOWtNpZAo; Tue, 25 Sep 2018 18:42:38 -0400 (EDT) Received: from neutron.alteeve.com (ip-64-134-242-94.public.wayport.net [64.134.242.94]) by zimbra.alteeve.com (Postfix) with ESMTPSA id 6418EC03EBF; Tue, 25 Sep 2018 18:42:38 -0400 (EDT) Subject: Re: Weird procedure question From: digimer To: Tim Cross Cc: pgsql-general@lists.postgresql.org References: <95e561cf-c098-4648-0ec5-030b47c995ba@alteeve.ca> <87h8id2p62.fsf@gmail.com> Message-ID: Date: Tue, 25 Sep 2018 18:42:39 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 In-Reply-To: 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 On 2018-09-25 6:41 p.m., digimer wrote: > On 2018-09-25 6:22 p.m., Tim Cross wrote: >> digimer writes: >> >>> Hi all, >>> >>> =C2=A0=C2=A0 I've got an interesting use case that I am stuck on. It'= s a bit of a >>> complicated environment, but I'll try to keep it simple. >>> >>> =C2=A0=C2=A0 In short; I have a history schema that has tables that m= atch the >>> public schema, plus one 'history_id' column that has a simple=20 >>> sequential >>> bigserial value. Then I have a procedure and trigger that, on UPDATE = or >>> INSERT, copies the data to history. Example use case is that I can >>> UPDATE a sensor value in the public table and it's also INSERTs the=20 >>> data >>> into history. So public shows just the most recent values, but I can=20 >>> see >>> changes over time in the history schema. >>> >>> =C2=A0=C2=A0 I have built my system to support writing to one or more= DBs. I keep >>> a list of connected DBs and send INSERT/UPDATE calls to a method that >>> then runs the UPDATE/INSERT against all connected databases, as a for= m >>> of redundancy. This all works fine. >>> >>> =C2=A0=C2=A0 The problem I've hit is that the 'history_id' differs be= tween the >>> various databases. So I want to switch this to 'history_uuid' and use >>> UUIDs instead of bigserial. >>> >>> =C2=A0=C2=A0 Now the question; >>> >>> =C2=A0=C2=A0 Can I tell a produce to use a specific UUID? >>> >>> =C2=A0=C2=A0 The idea is to generate a UUID for 'history_uuid' so tha= t I have >>> consistency across databases. Of course, if an UPDATE will change >>> multiple rows, then I'll need to predefine multiple UUIDs. This is=20 >>> where >>> things start to get really complicated I think... Maybe I could pass = an >>> array of UUIDs? I don't care if I find out which UUID was used for=20 >>> which >>> record, just that the same UUID was used for the same record when the >>> procedure is (re)run on other DBs. >>> >>> =C2=A0=C2=A0 The databases are not clustered, on purpose. I've been t= rying to >>> 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 >>> trigger I currently use; >>> >>> =3D=3D=3D=3D >>> CREATE TABLE host_variable ( >>> =C2=A0=C2=A0 host_variable_uuid uuid not null primary key, >>> =C2=A0=C2=A0 host_variable_host_uuid uuid not null, >>> =C2=A0=C2=A0 host_variable_name text not null, >>> =C2=A0=C2=A0 host_variable_value text not null, >>> =C2=A0=C2=A0 modified_date timestamp with time zone not null >>> ); >>> ALTER TABLE host_variable OWNER TO admin; >>> >>> CREATE TABLE history.host_variable ( >>> =C2=A0=C2=A0 history_id bigserial, >>> =C2=A0=C2=A0 host_variable_uuid uuid, >>> =C2=A0=C2=A0 host_variable_host_uuid uuid, >>> =C2=A0=C2=A0 host_variable_name text, >>> =C2=A0=C2=A0 host_variable_value text, >>> =C2=A0=C2=A0 modified_date timestamp with time zone not null >>> ); >>> ALTER TABLE history.host_variable OWNER TO admin; >>> >>> CREATE FUNCTION history_host_variable() RETURNS trigger >>> AS $$ >>> DECLARE >>> =C2=A0=C2=A0 history_host_variable RECORD; >>> BEGIN >>> =C2=A0=C2=A0 SELECT INTO history_host_variable * FROM host_variable W= HERE >>> host_uuid =3D new.host_uuid; >>> =C2=A0=C2=A0 INSERT INTO history.host_variable >>> =C2=A0=C2=A0 (host_variable_uuid, >>> =C2=A0=C2=A0 host_variable_host_uuid, >>> =C2=A0=C2=A0 host_variable_name, >>> =C2=A0=C2=A0 host_variable_value, >>> =C2=A0=C2=A0 modified_date) >>> =C2=A0=C2=A0 VALUES >>> =C2=A0=C2=A0 (history_host_variable.host_variable_uuid, >>> =C2=A0=C2=A0 history_host_variable.host_variable_host_uuid, >>> =C2=A0=C2=A0 history_host_variable.host_variable_name, >>> =C2=A0=C2=A0 history_host_variable.host_variable_value, >>> =C2=A0=C2=A0 history_host_variable.modified_date); >>> =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 AFTER INSERT OR UPDATE ON host_variable >>> =C2=A0=C2=A0 FOR EACH ROW EXECUTE PROCEDURE history_host_variable(); >>> =3D=3D=3D=3D >>> >>> =C2=A0=C2=A0 I know this might sound odd, but I didn't want to compli= cate things >>> with how my system works. However, if it would help solve the problem= , >>> I'm happy to dig into more detail. >>> >>> =C2=A0=C2=A0 Thanks! >> I think James has probably given you the input you need - basically, >> don't allow the system to automatically set the modified time - make >> that parameter to your function or set that value before the copy to t= he >> history tables - content would then be the same, so uuid v3 should wor= k. >> >> However, I do think you have another big problem lurking in the >> shadows. What happens if any of your connected databases are unavailab= le >> or unreachable for a period of time? I suspect your going to run into >> update anomalies and depending on your setup/environment, possibly eve= n >> partitioning problems (depending on number of clients and typology >> etc). These are well known problems in distributed or replication >> systems. >> >> You appear to be implementing a 'poor mans' replication system. There >> are lots of complex issues to deal with and I wonder why you want to >> take them on when PG has already got well tested and robust solutions >> for this that would simplify your architecture and avoid the need to >> re-implement functionality which already exists? >> >> regards, >> >> Tim >> > Hi Tim, > > =C2=A0 Last I checked, pgsql couldn't handle this; > > Two DBs up, getting data. > DB1 goes down, DB2 continues to collect data. > DB2 goes down > DB1 comes back up, starts collecting data. > DB2 comes back up, now I need to move data in both directions (DB1 has=20 > data 2 doesn't and vice-versa). > > =C2=A0 I've created a way to resolve this in my application and it's wo= rked=20 > for some time (obviously, in my application only. It's not a general=20 > purpose system nor is it intended to be). > > =C2=A0 For the record, I realized I was looking for a complex solution = to a=20 > simple problem. I do create the 'modified_date' value in my app, and I=20 > just needed to refresh it between UPDATEs/INSERTs on the same column=20 > so that no two records in the history table have the same=20 > 'modified_date'. With that, my resync works again. > > Cheers, > > digimer I should mention, I used two DBs in the example, but it could be 3 or=20 more, and I need to resync in as many directions at once as needed. So=20 it's a "poor person's" N-way replication cluster, as you guessed. I'd=20 love to use existing tools if they actually exist. digimer