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 1ewPsi-00016F-FH for pgsql-general@arkaria.postgresql.org; Thu, 15 Mar 2018 10:13:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ewPsh-0003q9-KN for pgsql-general@arkaria.postgresql.org; Thu, 15 Mar 2018 10:13:07 +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 1ewPon-0004aJ-R6 for pgsql-general@lists.postgresql.org; Thu, 15 Mar 2018 10:09:05 +0000 Received: from mail-pl0-x241.google.com ([2607:f8b0:400e:c01::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ewPok-0007gD-7Y for pgsql-general@lists.postgresql.org; Thu, 15 Mar 2018 10:09:04 +0000 Received: by mail-pl0-x241.google.com with SMTP id k22-v6so2608497pls.6 for ; Thu, 15 Mar 2018 03:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:mime-version :content-transfer-encoding; bh=jEG6xF5N5WA7IDhmkFajh50KRVGs5l+oh7CEE7ByLHU=; b=q/Qd2sh+dJO8AJ6nLBKf+zJL3pHlIYvuFge/3L5vr0wzyEga/p+/qptqY6K5+5X38x 3YRRKRV+E7FxZm8P226j9foIWEDO3xtcPmVeXSSZX5chBZo8hJ0s9AuLexrBYC72AcX7 cwhr9mZuNzco5eCPO6M4uXDaRTwZnL1ckiKJHTT5ouVJ5smfza1MkGfYDpoYViGcPAF4 1fjs1DfVkHIcziisKGy5MvLG0OOA80+Vv7dZs1kS7f2tTuiYxoG+GRgcD7ncWG8BuiZC P5M5iAYnnn5D8++JRXKHcY3eE97XzQ4HamrStcoeTkLU6VNQx7+DadKpwJE/Ef3LMexI 2b9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=jEG6xF5N5WA7IDhmkFajh50KRVGs5l+oh7CEE7ByLHU=; b=AFXvugipoqbZRPRvM55BZ8qpnqUHNjL/c5A9xWgytn2pEd7QEHOZwJVjziHOV+SGjE 9VixB6JB0RxffSMYAm+w6hNLHcXx6ZH+bfVy266c/HFut9UGo/ynj+tPpaMflIZNkpxS oflcpspEA3Z7pb5PMNdEedFCGhZd8JkyploARkBpOB3z/oGtc+3MEcKQA/LVRd11yD+b c+ZfCE0t+vUhQpuHN5Hwvr9VjmeFHdBF+NNPncs19asWFWAyfVcTYai2k9IA4naBheyW 83jiTNZjY2i+kYxw3+JxDyMxCW8BpkJOgZNO1D9MQzDGSt8wAhnjqRsjnw85rBHG5jTJ gDLg== X-Gm-Message-State: AElRT7FAtzcQtMpF0LzCjC6D0Tx+nejsaKg0oCLhB8n6zlB99Tc2nyTi sv1EHdczmDE9WwchGTwkogw= X-Google-Smtp-Source: AG47ELtlZK8bq6QqUk1JrTsX/Wv3tIltGdluAvobut7Ysb2ZhKlviPLF6OEJCV4ecgmr+5jSguHLzg== X-Received: by 2002:a17:902:5a0b:: with SMTP id q11-v6mr7650400pli.199.1521108539263; Thu, 15 Mar 2018 03:08:59 -0700 (PDT) Received: from roblaptop (d49-188-35-108.sun1.vic.optusnet.com.au. [49.188.35.108]) by smtp.gmail.com with ESMTPSA id v12sm8083278pgs.21.2018.03.15.03.08.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 15 Mar 2018 03:08:58 -0700 (PDT) Message-ID: <1521108533.3331.9.camel@gmail.com> Subject: Re: Snapshot recovery or rolling back commited From: rob stone To: Marc-Antoine =?ISO-8859-1?Q?N=FCssli?= , pgsql-general@lists.postgresql.org Date: Thu, 15 Mar 2018 21:08:53 +1100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.26.2-1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello Marc-Antoine, On Thu, 2018-03-15 at 10:43 +0100, Marc-Antoine Nüssli wrote: > Hi there, > > I saw there was a question about a similar topic recently but my use > case is quite different so there may be a different answer. > Roughly, I have a database which is updated by a single stream of > updates (through jdbc), so I have a single write transaction at any > time. However, sometimes I need to cancel some of the last updates in > order to re-apply different updates, but the readers should always be > able to query the most up-to-date state, including updates that could > potentially be cancelled afteward. > In other words, I need to be able, at any time, to rollback the last > updates up to a certain point (which is moving but always known) and > to be able to query the most up-to-date state (including updates that > could be rollbacked later) > Putting differently, I need two version of the same database, > "consistent" and "latest", with the "consistent" version being some > updates behind latest (or sometimes at the same state) and sometimes > the "latest" version must be restored back to the "consistent" > version. > > An approach would be to have a single transaction for the updates > with a "moving" savepoint, so that we can always rollback to the last > "correct" state. But, as far as I know, there is no way to query the > updated snapshot of an uncommitted transaction outside of it. > Indeed, we cannot do READ UNCOMMITED transaction and exporting > transaction snapshot does not show the updates made by the original > trnasaction. > The more I think and read about this, the more it seems this cannot > be achieved within a MVCC architecture... > > I also thought of a different approach that would use PITR and WAL > features, but as far as I read about it, this would require a lot of > filesytem-level scripting as well as as restarting postgres each time > we want to restore to a previous state. This sounds quite difficult > to accomplish all of this automatically and ot very efficient for a > production system. > > The last solution I thought of is to use an audit history (such as in > https://wiki.postgresql.org/wiki/Audit_trigger_91plus). Then I can > use it to rollback the last changes. And as I know up to which point > I want to restore, I can periodically clean the history to keep only > the last required changes. Currently, this is the only solution that > seems doable to me....but I'm not 100% sure that it would capture all > possible cases and that in some situations, restoring from the audit > history will not restore to the exact same state that it was... > > So, my question is do you think any of the proposed approaches is > doable? And if yes, which one and how? > Otherwise, do you have any thought on how to accomplish such a kind > of use-case using postgres? > > Thanks in advance for your response! > > Best regards, > Marc-Antoine Nüssli Without seeing your schema but assuming that this stream of updates only affects a single table, have you considered adding a column type boolean to that table default value true, and assuming that it could be included in a surrogate primary key, when an "update" is to be processed, lock that row, update the boolean to false and then insert a new row? You could then schedule a cron job to physically delete all rows where the boolean is false and if required, insert them into an archive table just in case you need to review the history. This way, your application is useable 24/7. Just a thought. Cheers, Rob