Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jGYj9-0006P1-84 for pgsql-general@arkaria.postgresql.org; Tue, 24 Mar 2020 01:51:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jGYj8-0006cq-32 for pgsql-general@arkaria.postgresql.org; Tue, 24 Mar 2020 01:51:34 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1jGYj7-0006cj-NH for pgsql-general@lists.postgresql.org; Tue, 24 Mar 2020 01:51:33 +0000 Received: from mail-io1-xd2f.google.com ([2607:f8b0:4864:20::d2f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jGYj5-0006qh-F0 for pgsql-general@postgresql.org; Tue, 24 Mar 2020 01:51:33 +0000 Received: by mail-io1-xd2f.google.com with SMTP id o127so16568332iof.0 for ; Mon, 23 Mar 2020 18:51:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=WOLtdeE/s9m605PB4PKe/ej088Jn5t63N5nP7iSziQ4=; b=Gonk/32iq5A0grEKZ2d5lt2/37a9PnZZJKVXWxOCDYOXx9lje/Vix55wquozqmk2aP 9U63jo7/H0bUc2WMyy0ju3I6uEtplOqQEYbfuXYWI1fylYKfA4I39bcTkBZXWw41MKeQ xnfyKr2hxDrYAX/ZE5/uTffMMqfJbHtnp2EeLH7bf48IZExRuYQYyItg+5OSn+YNxOzi 1NFvj7zdifKXtrAJMa0B8ck8zyVrXahRK/k3NelBIJlWHW8AXnS7g1tOql2lBkpL9kR2 JJTPpyfhCYF7ZdanRFo1BICsruyJWX9rOC101JNr06aB5WazvAdSQsA60Szn7BkUM8tg opSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=WOLtdeE/s9m605PB4PKe/ej088Jn5t63N5nP7iSziQ4=; b=UASKXUpUz00KetXKACLf1JLIYOMwx5P5h26AN6KUAFNYq1v2Mh/6cxBTP179SQVF1o TxGOZkhhlUtSomc09aqC/KgcslGcdZSRhJrMxm/NnUkH8QZoWZIhwjUO5AQWi6V/DMwG Fql4ZsQjODfWyMDg4z4vJBM9JboqfU6tNrXANxtYJRCzS1oB+iYNz5e2a2LyurNwrwMJ Yr7bkO15cRQVseLrO6zl7hbGEoCDz3efY5ZmgnTqn1CoTIkUsd2QKjAMgiH/6poY5ni6 qn+fmEPq+wv0zQfozUkhUpHcgj/bCg3YDaUdQPFcO1hGyZAzGS5lZWuf/2fF6t/zJEjM k8Ag== X-Gm-Message-State: ANhLgQ0Vism+AtYLnk3/3REVLlJU7B5wDq8WVJJjwwZBe8/HIicHQA8P 5+v4QWzSu2xg6Y8a7HtqG7c= X-Google-Smtp-Source: ADFU+vv99vNez+/q+0OCa5XUrLi7qFPo6H6nslh2v0/8BOYhvZ5yN6InzTimoxqM5JbLjHLvfkP4gg== X-Received: by 2002:a5e:8214:: with SMTP id l20mr21611538iom.168.1585014689138; Mon, 23 Mar 2020 18:51:29 -0700 (PDT) Received: from [192.168.1.108] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id d70sm5854442ill.57.2020.03.23.18.51.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 23 Mar 2020 18:51:28 -0700 (PDT) From: Rob Sargent Message-Id: <3A9D9AC0-AD61-45C6-87AD-BD8C9E255A37@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_346903C2-E380-4CA2-AEE9-88386EA48801" Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.60.0.2.5\)) Subject: Re: Loading 500m json files to database Date: Mon, 23 Mar 2020 19:51:27 -0600 In-Reply-To: Cc: pinker , "pgsql-general@postgresql.org" To: "David G. Johnston" References: <1584959088557-0.post@n3.nabble.com> X-Mailer: Apple Mail (2.3608.60.0.2.5) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_346903C2-E380-4CA2-AEE9-88386EA48801 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Mar 23, 2020, at 7:11 PM, David G. Johnston = wrote: >=20 > On Mon, Mar 23, 2020 at 3:24 AM pinker > wrote: > time for i in datafiles/*; do > psql -c "\copy json_parts(json_data) FROM $i"& > done >=20 > Don't know whether this is faster but it does avoid spinning up a = connection multiple times. >=20 > #bash, linux > function append_each_split_file_to_etl_load_script() { > for filetoload in ./*; do > ronumber=3D"$(basename $filetoload)" > # only process files since subdirs can be present > if [[ -f "$filetoload" ]]; then > echo "" > echo "\set invoice"' `cat '"'""$filetoload""'"'`' > echo ", ('$ronumber',:'invoice')" > fi >> "$PSQLSCRIPT" > done >=20 > echo "" >> "$PSQLSCRIPT" > echo ";" >> "$PSQLSCRIPT" > echo "" >> "$PSQLSCRIPT" > } >=20 > There is a bit other related code that is needed (for my specific = usage) but this is the core of it. Use psql variables to capture the = contents of each file into a variable and then just perform a normal = insert (specifically, a VALUES (...), (...) variant). Since you can = intermix psql and SQL you basically output a bloody long script, that = has memory issues at scale - but you can divide and conquer - and then = "psql --file bloody_long_script_part_1_of_100000.psql". >=20 > David J. >=20 Can one put 550M files in a single directory? I thought it topped out = at 16M or so. --Apple-Mail=_346903C2-E380-4CA2-AEE9-88386EA48801 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

On Mar 23, 2020, at 7:11 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:

On Mon, Mar = 23, 2020 at 3:24 AM pinker <pinker@onet.eu> wrote:
time for i in datafiles/*; do
  psql -c "\copy json_parts(json_data) FROM $i"&
done

Don't know whether this = is faster but it does avoid spinning up a connection multiple = times.

#bash, linux
    function = append_each_split_file_to_etl_load_script() {
  =       for filetoload in ./*; do
  =           ronumber=3D"$(basename = $filetoload)"
            # = only process files since subdirs can be present
  =           if [[ -f "$filetoload" ]]; then
                echo = ""
                = echo "\set invoice"' `cat '"'""$filetoload""'"'`'
  =               echo ", = ('$ronumber',:'invoice')"
        =     fi >> "$PSQLSCRIPT"
    =     done

      =   echo ""  >> "$PSQLSCRIPT"
    =     echo ";" >> "$PSQLSCRIPT"
  =       echo ""  >> "$PSQLSCRIPT"
    }

There is a bit other = related code that is needed (for my specific usage) but this is the core = of it.  Use psql variables to capture the contents of each file = into a variable and then just perform a normal insert (specifically, a = VALUES (...), (...) variant).  Since you can intermix psql and SQL = you basically output a bloody long script, that has memory issues at = scale - but you can divide and conquer - and then "psql --file = bloody_long_script_part_1_of_100000.psql".

David J.

Can one put 550M files in a single directory?  I = thought it topped out at 16M or so.



= --Apple-Mail=_346903C2-E380-4CA2-AEE9-88386EA48801--