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 1f6K0k-0004PA-EW for pgsql-general@arkaria.postgresql.org; Wed, 11 Apr 2018 17:58:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f6K0j-0003dY-CZ for pgsql-general@arkaria.postgresql.org; Wed, 11 Apr 2018 17:58:21 +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 1f6K0i-0003dO-7h for pgsql-general@lists.postgresql.org; Wed, 11 Apr 2018 17:58:21 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f6K0d-0006CF-Cv for pgsql-general@postgresql.org; Wed, 11 Apr 2018 17:58:19 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id A307720AF0; Wed, 11 Apr 2018 13:58:12 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 11 Apr 2018 13:58:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=YQNKEmMmrfdET6Q3yF+okUBalL3wr cYpObNM351z/bg=; b=J3uPbcdRW2M0wKSeAqu6HIkTnxXTtAdEtIr7ciq04jE+X n1ZEMSDHsClPor8YsQD0HnhaH/rVqkoCrDrTxSkQyVwyXZ1XSUzUAiuLv7vmi7xS RjkNL4rzgy3qD8aUOe5aZbCLATOuIip3ENQ+OT7A82UnfPCEYV9g9CT3pSs2NCDE nqIC7G3r8/i0D9zQjpdcw2vTb3SSeEnRaEqODUD46YiHgWOBadoHoht47SQWVc6J Fke1apvvzpF5KniwTTEM6Koqp9pT7vX/RtRv3gdjVCv6DMYyCl1bt2Ctagh97fZ0 GXJSaq8esfPlnWo7qn6wM/o2whtuFwiXFpGoYP8xg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=YQNKEm MmrfdET6Q3yF+okUBalL3wrcYpObNM351z/bg=; b=luk5jXbYZXZ+4pRu+QgQjL paBQecEhmxjsKZqzKd/Y57+/zW3e55fVX3vu4ZgGg6frz2h3T5zODTApQqdc+uLN a/+VtQS1BV7NwnRXZrYbchIohOpBlp8O926ceYsQXOXPmQyt6S20WQOPO6Iam+Kx Pdv66Nx/Zesy87WxylpEd8zjNgdc8y6LnBrPPCQH3HcHKHbvGVqwmM/u/pFteg3W wkUZLrABPfC5ntMNDANJ6qmTYH3MroQiqpzTbxfHVmtcn+/obnZkTuB9j7OBuYUS IEOYpdiuWNQCzDZAS0mAIh8OxefIiQrho9ZqQWPm9x6XmzhWjSJm4X4rksfpKxgQ == X-ME-Sender: Received: from ranger.suse (74-93-105-186-washington.hfc.comcastbusiness.net [74.93.105.186]) by mail.messagingengine.com (Postfix) with ESMTPA id DC29A10253; Wed, 11 Apr 2018 13:58:11 -0400 (EDT) Subject: Re: Archiving Data to Another DB? To: Don Seiler , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: Date: Wed, 11 Apr 2018 10:58:10 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/11/2018 09:15 AM, Don Seiler wrote: > Let's say I have two DBs: main (9.6.6) and archive (10.2). > > I have a table in main where I want to archive data older then 60 days. > For various reasons, the table is not partitioned, so for now we must > use DELETE. The destination table in the archive DB is partitioned with > the new Pg10 partitioning. > > My initial plan was to have a stored procedure on the archive DB use > postgres_fdw to do an INSERT INTO / SELECT to select the data remotely > and insert into the local archive table. It would then issue a single > DELETE command to remotely remove the data from the main DB. However I > found that doing this resulted in the main DB calling thousands (perhaps > millions if it's one-per-row) of individual DELETE statements based on a > ctid column. Aside from WAL behavior concerns, it is flooding my > postgresql server logs since I log any DML. > > On top of that, I'm told that a remote DELETE wouldn't be transactional, > so if I were to compare inserted rows vs deleted rows and found a > mismatch, I couldn't just rollback the DELETE. I plan to verify this > with a small test case later but for now I'll assume this to be true. That would be worth testing: https://www.postgresql.org/docs/9.6/static/postgres-fdw.html "F.33.3. Transaction Management During a query that references any remote tables on a foreign server, postgres_fdw opens a transaction on the remote server if one is not already open corresponding to the current local transaction. The remote transaction is committed or aborted when the local transaction commits or aborts. Savepoints are similarly managed by creating corresponding remote savepoints. ..." > > Right now I'm thinking of falling back to the far-less-elegant method of > dumping the data to a flat file via COPY, running psql to connect to the > archive DB remotely and running a COPY to load the data (or maybe > transferring the flat file to the archive DB to load it there, > offloading that part of the workload), then deleting the data from the > main DB. I could capture the rows dumped in a control table and compare > the rows deleted against that and then rollback the delete if necessary. I may be missing something, but why not reverse your original set up? Assuming transactional behavior works as expected something like: 1) Setup postgres_fdw in main database. 2) Create FOREIGN TABLE pointing to table in archive database. 3) INSERT INTO/SELECT from main table to archive table. 4) DELETE FROM main table. > > Like I said, not elegant, but I don't want to risk losing data that > wasn't successfully archived to the archive DB. I'm very interested to > hear what others might be doing for tasks like this. > > Don. > > -- > Don Seiler > www.seiler.us -- Adrian Klaver adrian.klaver@aklaver.com