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 1mfIPF-0002Vm-CS for pgsql-general@arkaria.postgresql.org; Tue, 26 Oct 2021 09:06:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mfIPE-0000jy-7w for pgsql-general@arkaria.postgresql.org; Tue, 26 Oct 2021 09:06:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mfIPD-0000iy-UQ for pgsql-general@lists.postgresql.org; Tue, 26 Oct 2021 09:06:03 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mfIP7-0005Cd-Gi for pgsql-general@lists.postgresql.org; Tue, 26 Oct 2021 09:06:03 +0000 Received: by mail-oi1-x236.google.com with SMTP id y128so38326oie.8 for ; Tue, 26 Oct 2021 02:05:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=yk8GADDZ8fV8WWQj5KjJfV62OnxbJ1NdIZJNx16Necc=; b=S48XkwhImN8spYDorfPQYflFCzytt5NnIOErzjeJZi6ONqap1hsqSbwTJahciuwXzX ydIMiIWdzUIYl/QnvnZLKfxsAwkGW4VTnHfB2OyDNsb5UYDGHn0MBaLtaMll8QcWre9W 5Td3a8DEgyuDZwfToybRYgaUBxT0W1LXFFZsUhe5ULZm28VFMhWbmv3igRftOeuBbJt5 IEkpYF3HjguD0LMuNuVZCEDX1nIb1V5xhgv5ihb9lMq2cBdHSAvHKgYLybpzAjaFafE4 qlyfTDnEMZQW8YfAm0C3AXMa0w+O9hbXfoYxpDJAvDCQm6CIcGTCVKFC/qSZfp0tiqc8 oHyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=yk8GADDZ8fV8WWQj5KjJfV62OnxbJ1NdIZJNx16Necc=; b=w3haAo6h6LcMZWSVYgGXug4SGl0l25kzI3VN0Wk2SY1d5jH5ht2OnBqUF9rNelrkIx AmzZgEMtbr02vOh+22OjqceG6c58HjaPeyo9tPFrZcUzrE1IchyYYDQkgqys/P9p6Zi4 i3TXSPg/+DXoo41Z06Cuq7Jzxz3+G6acUFkg5fhcKx7mjWu9TlR2ICMhHDHNtB2ejYLw iLq6LQQnUFqU8pm6sNqGKuGswqtqdn/3DU3WWNuMmZvjqbZIVO6HbXo42ezvEXNV+6w5 p0a1cfNzKqTJV6VEKaNsP1Nw4XXDfTMU9ZhYJsZ7iGHPiTedVBuvXyYWvA/G+jF+i0Tv foow== X-Gm-Message-State: AOAM531D1k6HNx2TwTNiDojJIvyxN1MBzuFS5vW9BdaSDH2hcWm4Iw5c lDHCtluhtmGzdr5+YLefVMk3Vc7Mobs= X-Google-Smtp-Source: ABdhPJyFOkI62wdKTZxOXBHpM/puQEZq4wl0gVYnj4BXujDqmKwZlArFYdw4eZs5cp8B2C0InAtwbQ== X-Received: by 2002:a05:6808:2086:: with SMTP id s6mr16893723oiw.1.1635239155648; Tue, 26 Oct 2021 02:05:55 -0700 (PDT) Received: from [192.168.88.10] (ip68-11-68-85.no.no.cox.net. [68.11.68.85]) by smtp.googlemail.com with ESMTPSA id j23sm2582022oou.5.2021.10.26.02.05.54 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 26 Oct 2021 02:05:54 -0700 (PDT) Subject: Re: How to copy rows into same table efficiently To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <982cf46b-c112-82cb-3f34-6904305761bb@gmail.com> Date: Tue, 26 Oct 2021 04:05:53 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/26/21 1:04 AM, Arun Suresh wrote: > > Dear PG experts, > > We have a tenant discriminator column in our tables to enable storage of > data from multiple > tenants. This column is also part of the composite primary key. > The customers may request creation of a tenant copy, which means if they > currently have a > tenant id "ABCD", they would like to copy all data "where tenant_id = > 'ABCD'" to a new copy tenant "XYZ". > The copy must also be done on the same table. > > Current approach taken is to build a query like below: > INSERT INTO mytable (col1, col2, col3, col4) SELECT col1, 'XYZ', col3, > col4 FROM mytable WHERE col2 = 'ABCD' > > Is there a better way to do this? > There could be other tables with foreign key reference, would a simple > ordering of the copy based on table relationship suffice? That would be my first thought.  Of course, FK dependencies quickly become a rat's nest, so beware. > Also if the table has millions of records, what are the things to take > care of? Take care of? Anyway, for millions of rows, I might use COPY instead of INSERT (depending on how many millions, how many indices, how large the rows, how fast the machine, etc. -- Angular momentum makes the world go 'round.