Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eqGa5-00058c-Nl for pgsql-general@arkaria.postgresql.org; Mon, 26 Feb 2018 11:04:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eqGa3-0000NU-FZ for pgsql-general@arkaria.postgresql.org; Mon, 26 Feb 2018 11:04:27 +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 1eqGa3-0000NK-1Q for pgsql-general@lists.postgresql.org; Mon, 26 Feb 2018 11:04:27 +0000 Received: from mail-qk0-x236.google.com ([2607:f8b0:400d:c09::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eqGZz-0003Za-Mz for pgsql-general@postgresql.org; Mon, 26 Feb 2018 11:04:25 +0000 Received: by mail-qk0-x236.google.com with SMTP id w142so6705293qkb.8 for ; Mon, 26 Feb 2018 03:04:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=7hLpdQAFM1YXALtu5t7ixNwbn48oDnMDZkMZYje0ckE=; b=LS+xI5VoN0ychxUqziYKTWYSy55gDEJ22CdWO+3Rzj1/yNi1iK2LVfbhjOCGStJghg RHyEXBkREQBiE8N6GdnL7eKy8CFjYxBWjsqWwCjFHoxdO1rM0Y9Mgk5L5o35rIoc7dgO 1xIdnR7fq1PBmd9uZ/P++0qzLXa01oABifGUdx0xH+26vDPgRImoAmDVFNXoPAJH0sX6 kbrfSGqTK3e/z6fAMH4rtAY//AKB9ZLATW93RHV5dia5wnjdeJGEEVDQZLSak+oz3qaN lmN8G1h5YiEuhm/xV0wR7Ei5znRe74oklT34W8cep+3K+Xi+CbddV9Pu5jE4V+u0IFJU fVDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=7hLpdQAFM1YXALtu5t7ixNwbn48oDnMDZkMZYje0ckE=; b=VrIAKiGxFtQowmX+0ckUzLVy4UNhtyYVqkQe02enBkalWuzOvL3a7UJPYH7DF+IZO8 YRdZ+UGwUcTdCPq7lKgzHdPGLRHDKFsSQ4bf7jgM59UMAmKOiTZ7v+IoyM+b6IW7LLmU RmlB5SuDlfXFd3UlKvOnIBmZgvBuwTfmrWxX+lKiR3XMYFGlgJw8Jv0tmvP9R7KnIc81 OOZJuqpiy5uSLC1tbswQepGIQzZfGMtwX+CSGGNzdbKtlszpg/tBJIH59TxI7nbCCOq9 aLCdzzM04Cg9/HogOxclLlxW5B3UuO1BE2irzC4vgA+PbXhr3fACFj/m9jFlhqHhfo0L CrIw== X-Gm-Message-State: APf1xPBmo/TzLLooMtOdrouCNWNpchD46NJGqaWZovpLXQ84W7V2itRz FUxI855aTir7R8mcGd/nYmEoDcnpsSwlH7Acqrw= X-Google-Smtp-Source: AG47ELvsvDEZYz4+w2hbHzVNVacGru8G3/ZAAEm5cVtZ5MKB+BHJdNbZxfxlGETBd3FQaiC9/mwF71ZY4+JaYVuiRcE= X-Received: by 10.55.120.5 with SMTP id t5mr15388458qkc.14.1519643062453; Mon, 26 Feb 2018 03:04:22 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: =?UTF-8?Q?Manuel_G=C3=B3mez?= Date: Mon, 26 Feb 2018 11:04:11 +0000 Message-ID: Subject: Re: Creating complex track changes database - challenge! To: =?UTF-8?Q?=C5=81ukasz_Jarych?= Cc: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="94eb2c05e7caec4d5d05661b78d6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --94eb2c05e7caec4d5d05661b78d6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 26, 2018 at 11:44 AM =C5=81ukasz Jarych wr= ote: > I have to: > > 1. Keep all changes within table including: > -adding rows > -deleting > -editing > > 2. Save table with specific state and recover specific state (so go back > to previous table versions) including comparing tables. > > 3. Track all DLL and DML changes with possibility to ho back to previous > version. > Hi, I had similar needs long ago, so I wrote this tool I called Squealer, which would transform a specification of a SQL database schema into some PostgreSQL DDL to create a database that implements the same schema in a logical sense, but actually stores all historical rows, and even permits existing rows to have references to (soft-)deleted rows, all while providing modifiable views that simulate the behavior of a regular table as specified in the input schema through generous use of INSTEAD OF triggers. It works somewhat like having version control for your database. You may find the source code here: https://github.com/mgomezch/squealer Unfortunately, it has hardly any comments, it is completely unmaintained and probably unused anywhere, I have no idea whether it builds with today's libraries, and it does not necessarily break the tradeoffs in this space in a way that fits your use case. Note there are major caveats with keeping all historical data around forever, and the semantics of querying historical data can get complicated, let alone having current data refer to deleted, historical data. I built this for a very specific use case where this was the right design, but please consider very carefully whether this is what you want. Storing your database history forever would take a lot of space. Consider whether you can instead keep a record of changes stored outside the database in some cheap cold storage. Also consider just keeping a set of tables with dynamically structured event records (e.g. JSON fields) partitioned by time ranges and retained only temporarily, perhaps even in a separate database. Any such solution will have significant cost and performance impact if your database bears a nontrivial load, so be careful. You could also just place your database on a PostgreSQL cluster by itself and then keep all WAL segments archived forever, so you could just do point-in-time recovery to any point in the history of your database. The space required would grow very quickly, though, so if you don't really need the full history forever, but only a fixed retention period, you can surely use any of the well-known solutions for PostgreSQL backups that allow for this through WAL archiving and periodic basebackups: e.g. WAL-E, WAL-G, pgBackRest=E2=80=A6 If all you need is saving and restoring specific table states, logical dumps with pg_dump should probably be enough for your needs. --94eb2c05e7caec4d5d05661b78d6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Mon, Feb 26= , 2018 at 11:44 AM =C5=81ukasz Jarych <jaryszek@gmail.com> wrote:
I have to:

1. Keep a= ll changes within table including:
-adding rows
-deleti= ng
-editing

2. Save table with specific = state and recover specific state (so go back to previous table versions) in= cluding comparing tables.

3. Track all DLL and DML= changes with possibility to ho back to previous version.

Hi,

I had similar needs= long ago, so I wrote this tool I called Squealer, which would transform a = specification of a SQL database schema into some PostgreSQL DDL to create a= database that implements the same schema in a logical sense, but actually = stores all historical rows, and even permits existing rows to have referenc= es to (soft-)deleted rows, all while providing modifiable views that simula= te the behavior of a regular table as specified in the input schema through= generous use of INSTEAD OF triggers.=C2=A0 It works somewhat like having v= ersion control for your database.

You may find the= source code here: https:/= /github.com/mgomezch/squealer=C2=A0 Unfortunately, it has hardly any co= mments, it is completely unmaintained and probably unused anywhere, I have = no idea whether it builds with today's libraries, and it does not neces= sarily break the tradeoffs in this space in a way that fits your use case.<= /div>

Note there are major caveats with keeping all hist= orical data around forever, and the semantics of querying historical data c= an get complicated, let alone having current data refer to deleted, histori= cal data.=C2=A0 I built this for a very specific use case where this was th= e right design, but please consider very carefully whether this is what you= want.

Storing your database history forever would= take a lot of space.=C2=A0 Consider whether you can instead keep a record = of changes stored outside the database in some cheap cold storage.=C2=A0 Al= so consider just keeping a set of tables with dynamically structured event = records (e.g. JSON fields) partitioned by time ranges and retained only tem= porarily, perhaps even in a separate database.=C2=A0 Any such solution will= have significant cost and performance impact if your database bears a nont= rivial load, so be careful.

You could also just pl= ace your database on a PostgreSQL cluster by itself and then keep all WAL s= egments archived forever, so you could just do point-in-time recovery to an= y point in the history of your database.=C2=A0 The space required would gro= w very quickly, though, so if you don't really need the full history fo= rever, but only a fixed retention period, you can surely use any of the wel= l-known solutions for PostgreSQL backups that allow for this through WAL ar= chiving and periodic basebackups: e.g. WAL-E, WAL-G, pgBackRest=E2=80=A6

If all you need is saving and restoring specific tab= le states, logical dumps with pg_dump should probably be enough for your ne= eds.
--94eb2c05e7caec4d5d05661b78d6--