Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oIx7H-0006CF-L2 for pgsql-general@arkaria.postgresql.org; Tue, 02 Aug 2022 18:59:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oIx7G-00047C-Eh for pgsql-general@arkaria.postgresql.org; Tue, 02 Aug 2022 18:59:42 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oIx7F-00046v-SP for pgsql-general@lists.postgresql.org; Tue, 02 Aug 2022 18:59:41 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oIx7D-0000KH-Go for pgsql-general@lists.postgresql.org; Tue, 02 Aug 2022 18:59:40 +0000 Received: by mail-pg1-x534.google.com with SMTP id h23so923256pgv.5 for ; Tue, 02 Aug 2022 11:59:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:from:to:cc; bh=QdB0w5Yvn+Ccv1j1ul9hquvHmWvP6VfYSXRIq0uIpOo=; b=gnBFOtToL28YTwllTcQ1fGKcQWGkJJN06Pw5K9aY0TQshEnbZ2H6W54TYceOYj5hyp RjeRsRzB4fnX1G/h/MMV6bn8uzkh36ewKdpTea6qIo6P53BSeUsVUAuLhu5cdG0jciTl ihzbWw/G0OB7Ah3La2dKsz/Mx7zJbsLGwThu9BHMREp+MPCR+/f8jX6mRdK4JqzJG2Ct QAk554Ngg09QHOCp6UxgpxM97QL+B+yI+O7/Ty2VKL631/+tT6hlZVK67fnQAxlRITvf 7jk35JvyeKvBpVCZo+qzqqUDdHSyFqD6NpGtnbGZHtjVcfOgA/BKWTF0J3i4JHb4AWt/ Rsqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:references:to:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc; bh=QdB0w5Yvn+Ccv1j1ul9hquvHmWvP6VfYSXRIq0uIpOo=; b=ddGec24iLWc/4Ko1TpG7hciB9NV6GaALTG/+2ArEOVrjVPpzrYni23L9qCoUTyZIT/ c7GdtVohAbeKdYFXo0ui/DAqhBXsndYe8EUfQL8C63UCgWaSpHBiAfCHxxfvv7M3m36k +qPDqeB+NpeZFw3Ey1u7XE2AWqAaSV8hWvZdo3Wa4sd803jRCzyPemDlxQB9m7Gpjcsn pMZRcHJwqEI+jRVXMLyGL1FeoapXBlAjCmOq2/hxMTePJXUz7WJiSIcXXcAI2YLXSMCz 939uW3D2gBw0K/llw3lIBYxyFpj19ptzK5gMKCX/iCWlI5XSkWLdWDLybPSP3ic5o4GU nZrw== X-Gm-Message-State: AJIora9r8jGZfOoRtXDD5yIdJJOZvNsm0oYrnPREygjg5RcAJSzniRsI o6L3GE7NNZBOqw8sN+DtKUBIjx5qyAk= X-Google-Smtp-Source: AGRyM1uJQ3GKm5NDaTePQX5v/ssjlzGWRdRKtSGhqlbBI8zYK1ZAmd0OMXRq3AbHV7Z0gPCyz1GDQA== X-Received: by 2002:a05:6a00:c91:b0:52a:cad7:d755 with SMTP id a17-20020a056a000c9100b0052acad7d755mr22470972pfv.66.1659466778187; Tue, 02 Aug 2022 11:59:38 -0700 (PDT) Received: from [10.128.56.11] ([155.98.131.3]) by smtp.gmail.com with ESMTPSA id d9-20020a17090a628900b001f207d1860fsm11171384pjj.45.2022.08.02.11.59.37 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 Aug 2022 11:59:37 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------NAtURHJvgqVL94k2aLRHwbm9" Message-ID: Date: Tue, 2 Aug 2022 12:59:36 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Copying records from TABLE_A to TABLE_B (in the same database) Content-Language: en-CA To: pgsql-general@lists.postgresql.org References: <5adcc923-889d-bb68-5a04-60125b6f0e96@gmail.com> <3e06c774-e323-db92-0cea-8600e1155600@gmx.net> From: Rob Sargent In-Reply-To: <3e06c774-e323-db92-0cea-8600e1155600@gmx.net> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------NAtURHJvgqVL94k2aLRHwbm9 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 8/2/22 12:51, Thomas Kellerer wrote: > Ron schrieb am 02.08.2022 um 20:37: >> AWS RDS Postgresql 12.10 >> >> There are no indices or constraints (except for NOT NULL) on table_a. >> >> The two ways that I know are: >>      INSERT INTO table_a SELECT * FROM table_b; >> and >>      \COPY table_a TO '/tmp/table_a.tsv' WITH (FORMAT BINARY); >>      \COPY table_b FROM '/tmp/table_a.tsv' WITH (FORMAT BINARY); >> >> Is there a faster/better way? > > The INSERT is most probably faster then \copy > > Another option is to have a trigger on table_a > to automatically replay all DML on table_b > > > Logical replication might be another option. > Although I am not sure if that is even possible inside > the samme database. > I know it's tricky inside the same server > (between different databases) > > > If you can get outside sql, the bulk copy facilities (CopyManager in java) is blindingly fast for me. --------------NAtURHJvgqVL94k2aLRHwbm9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 8/2/22 12:51, Thomas Kellerer wrote:
Ron schrieb am 02.08.2022 um 20:37:
AWS RDS Postgresql 12.10

There are no indices or constraints (except for NOT NULL) on table_a.

The two ways that I know are:
     INSERT INTO table_a SELECT * FROM table_b;
and
     \COPY table_a TO '/tmp/table_a.tsv' WITH (FORMAT BINARY);
     \COPY table_b FROM '/tmp/table_a.tsv' WITH (FORMAT BINARY);

Is there a faster/better way?

The INSERT is most probably faster then \copy

Another option is to have a trigger on table_a
to automatically replay all DML on table_b


Logical replication might be another option.
Although I am not sure if that is even possible inside
the samme database.
I know it's tricky inside the same server
(between different databases)



If you can get outside sql, the bulk copy facilities (CopyManager in java) is blindingly fast for me.
--------------NAtURHJvgqVL94k2aLRHwbm9--