Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uedbU-00Cj2j-RH for pgsql-general@arkaria.postgresql.org; Wed, 23 Jul 2025 17:50:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uedbS-003g5Z-P8 for pgsql-general@arkaria.postgresql.org; Wed, 23 Jul 2025 17:50:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uedbS-003g5R-CW for pgsql-general@lists.postgresql.org; Wed, 23 Jul 2025 17:50:06 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uedbR-000QNG-0P for pgsql-general@postgresql.org; Wed, 23 Jul 2025 17:50:05 +0000 Received: by mail-lf1-x12d.google.com with SMTP id 2adb3069b0e04-553d52cb80dso230644e87.1 for ; Wed, 23 Jul 2025 10:50:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753293001; x=1753897801; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XbG0KwbFDRvsQLsljk3UWANOCW9ZjkxFW2DBbn+83xg=; b=Z45ZHyYk1+uMzpOKi7GJTrnSjtizdm3cgaEOsa8pnVVJydWVVc73s7i744Hsw3ANJB 6ZH2PyxwxYYiA1Y0KCqAMlPZbiqQyq4r5vumMbQ6j4QyhcywA+yBbeQYgKE6NIwqL/al 2t7Iwe7rJRNgiHoWKN/3rBefHijRaHiNqCrppr4LoG5/tm9FNdMnaH9lF5t6/N25EzDx l4VvFJ2fIxe2sLg06Cf88hcLZN3o3V8tFSGhBvecSF9Yy6MxiY1lq/Y3NLeiFnxrJJUR SBWQjKV19d31hQy5lZ2ikem4KmGq8jycVZxVDIxdylkP2NkpcRTFGdD8Gj7F8Tk0KMjR 0UBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753293001; x=1753897801; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XbG0KwbFDRvsQLsljk3UWANOCW9ZjkxFW2DBbn+83xg=; b=HtFTobSnlEI/ApHJPI8ZZd5nfbJZ0F/qC6dOzQLQgu7Oz4UjPPTlGgboEpGBCPgoBI 6PpoqS+3fO9esAPUFjK4QmiRRMkgYj5Xw2H0/wXGB0VcMBOkiB4k5FyhzdGBC0y+h7xu 1kRIwaNIro5Hh9aDTgx/ymVC2oP0W4RejWz+l0EKcuOgoqHea6nTTAUwVNXvRpEx89rB Vi+5XMbL89JP22JVdBbfSROEQZZzKUy1zt/lZOOdPbPeBr5krd2IdRSM+Ox5dbJOqlre zUhiZk7F9lSLGoVVkaMqigVMmVy+WlI2T9nN6O0dtzWkFbdVuDs9Sr8zSz4wZFR90iYw b/Sg== X-Gm-Message-State: AOJu0Yx3lGiGJW31yO/Zylqx67/14ZcMVch0yHLMqG5d6zbOd0sTEUnh 6hO/CaYbX8G9wUkw3owD8K+qFO6aPc4HE+qOEi4/Qj31IEADSZa6cNkAtWdTap3TWV5BPUu6ZBi eeuMndWSRVZ3BTLeI5Nw0utR48Z7Zsrw= X-Gm-Gg: ASbGncs/2nLFftnLX7FqU/QV1hUkJv+1UZx9U1nPGBjnjPIRc2UlU5jWzMok1o2qFP5 pzF39p1IkaXvqCs1DyFU4msw9H3pyf3dABlN+K3K+d5YBUPmFe7gqlMdy/tWEyn3/LpoJgjkAQH 9DGPHDFuVH7CP1IE2NQStbvWVr72/nTkdSD/FXV08Pz/fgDBkgF1Oj9WhpF/WqpmuDo/79f8ffV JeXiifL X-Google-Smtp-Source: AGHT+IH0fSrcrpoTmn6Ery2MtSbEHLvIE0t0ofhN/PNCz8HzvOAIx6COKFkXATQ6m1qw/NzxTw9ZN6rhPEAM26bMYlY= X-Received: by 2002:a05:6512:239b:b0:553:2969:1d6d with SMTP id 2adb3069b0e04-55a516aa90bmr1252117e87.13.1753293001091; Wed, 23 Jul 2025 10:50:01 -0700 (PDT) MIME-Version: 1.0 References: <1453510076.1900935.1753260637232.ref@mail.yahoo.com> <1453510076.1900935.1753260637232@mail.yahoo.com> In-Reply-To: <1453510076.1900935.1753260637232@mail.yahoo.com> From: Merlin Moncure Date: Wed, 23 Jul 2025 11:49:49 -0600 X-Gm-Features: Ac12FXxmbSOn8NA1XPj5GaJJYmZd34BxEr3DeNrqQyHX-hzN0EaQwi7OOUg-cVc Message-ID: Subject: Re: Is there any limit on the number of rows to import using copy command To: "sivapostgres@yahoo.com" Cc: Pgsql-general Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jul 23, 2025 at 2:51=E2=80=AFAM sivapostgres@yahoo.com wrote: > > Tried in PostgreSQL 11.11 , PostgreSQL 15.2 in Windows 10 > > Here we try to transfer data from one database to another (remote) databa= se. > > Tables do have records ranging from 85000 to 3600000 along with smaller s= ized tables. > No issues while transferring smaller sized tables. > > I here take one particular table [table1] which has 85000 records. > The table got Primary Key, Foreign Key(s), Triggers. Trigger updates ano= ther table [table2] > Table2 have 2 triggers, one to arrive a closing value and other to delete= , if the closing value is zero. > > 1. Transfer the data from source database to a csv file. 85000 records = transferred. No issues. > 2. Transfer the file to the remote location. No issues. > 3. Transfer the contents of the file to the table using Copy From comman= d. - Fails when try to transfer all the 85000 records at once. > > Copy from command is > > Copy public.table1 From 'E:\temp\file1.csv' (FORMAT CSV, DELIMITER ',', H= EADER TRUE) > > The above command succeeds, when > 1. The trigger in Table1 is disabled with all other constraints on. > 2. The no. of rows is within 16000 or less, with Trigger enabled. We ha= ven't tried with higher no of rows. > > The above command goes on infinite loop, when > 1. We try to transfer all 85000 rows at once, with Trigger and other con= straints in table1 enabled. We waited for 1.5 hrs first time and 2.5 hrs s= econd time before cancelling the operation. > > I read in the documentation that the fastest way to transfer data is to u= se Copy command. And I couldn't find any limit in transferring data using = that command. One could easily transfer millions of rows using this comman= d. Most likely, you are getting yourself into trouble with the trigger dependencies. Triggers are powerful, but also can be dangerous, and this could be 'wrong tool for the job' situation. Here are some general tips: * pg_trigger_depth(): can tell you if trigger A calls trigger B and back to trigger A, etc. you can use it with raise notify, and also use it to guard execution on CREATE TRIGGER * reconfiguring your logic to statement level triggers can be a good idea. this can take some thinking, but can be much more efficient when bulk processing since trigger execution can be deferred until the load completes. (one trick is to use now() to check for records inserted since it is stable though the transaction) * reconfiguring your logic to a procedure can be a better idea; COPY your data into some staging tables (perhaps temp, and indexed), then write to various tables with joins, upserts, etc. merlin