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 1onrFX-0004yo-9M for pgsql-general@arkaria.postgresql.org; Thu, 27 Oct 2022 00:59:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1onrFW-0004Br-6M for pgsql-general@arkaria.postgresql.org; Thu, 27 Oct 2022 00:59:58 +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 1onrFU-0004BG-Dh for pgsql-general@lists.postgresql.org; Thu, 27 Oct 2022 00:59:57 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1onrFR-0004fI-5I for pgsql-general@lists.postgresql.org; Thu, 27 Oct 2022 00:59:55 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 5F0693200940; Wed, 26 Oct 2022 20:59:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 26 Oct 2022 20:59:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1666832390; x= 1666918790; bh=InMViyaM6wi6dsLkGRKKUQk/p4QuSVMfOMuAa6OER+U=; b=Z ZIPvEY6NNgL6MtN7MQRlFAaHu9crU8EqlTlvQFqa3nbnGzxzIarq141Jml+/IXK1 fOSqaOz6RmmvqjzmTg+dsgvReRFCwsBlWCBBg/JPoCeiwt1pFBAbVeHUSzjMSqun pGNxhIqweKYqGdNWkFKrGkoPSe36+lVltCKHpYTWCwPkid6BCN/JTKcPOv0Xg/Sq FBObJqauuVO0hNGfa6y1QfVupnhDf+Bhk7wAS6sXj0zJNnRYAx6qvdnUs9ZHGA2v HuZQZGdV7p8Assogv7BCRQ26/O8mJyAADtcs35QaxGu7X6rhf0ddpkJ75PT2rTxd 6HcJi6EJla6jQTPw9lAKg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1666832390; x=1666918790; bh=I nMViyaM6wi6dsLkGRKKUQk/p4QuSVMfOMuAa6OER+U=; b=kIq4CMZJF+N+9wJB4 HSI9XmMFLr5YlZ33QlWR7JGlb413kET1I9nCcoV8syl9G8+swxMPYqOOUjPBmToV PGsit2DCNU1x6Of1kXfdS4W2VffPTmsh4KRr8Kmy8fKnSW7JPtwc2W72/vhJD44X dwPdVXbAB/mRcJpu5pbo89IME7j7FlDDHH7bEYlJfY9RjA+B4CuDkOfZTnR4+G8J ZDjjf7p31BUF9YqpU9hVHH9lNhxlgEyefqWAoYbgsDapCYN+EZ2Ore+UmOxsR3VM LFcL/wgzKaRLfVdvVbX4c/2gxD2LFfkAtL3S8XOgjjsdnz49XwjZ7QpWywHe049a RXCxA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrtdefgdegvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeelkeegjefhke etheefudevteehiedvudehkeduleetfedvffeuhedtvdegtdeffeenucffohhmrghinhep phhoshhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrg hmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtgho mh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 26 Oct 2022 20:59:50 -0400 (EDT) Message-ID: <8830ee61-cf09-7ba6-0055-efe7a910ac59@aklaver.com> Date: Wed, 26 Oct 2022 17:59:49 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: How to load data from CSV into a table that has array types in its columns? Content-Language: en-US To: Siddharth Jain , pgsql-general@lists.postgresql.org References: From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/26/22 17:30, Siddharth Jain wrote: > Hello, > > Given a long list like this: > > 1,2 > 2,4 --> 2 appears once > 7,9 > 8,9 > 5,3 > 2,5 --> note 2 appears twice > > I want to load it into this table: > > create table tbl ( >   id integer primary key, >   fids integer[] > ) > > so we will have 2 -> [4,5] where 2 is id and [4,5] are the fids > > My actual dataset is very large and has 100M rows in it. How can it be > efficiently loaded into postgres? > > I think I have to use a program for this and am trying to use the pg > library that comes with Node.js. I am reading the data in batches of 1M > or 100k rows for example. I have created a dictionary in Node.js where I > am storing the hashmap. The part where I am stuck is how to generate the > SQL command? > > 1. I want to make one call to the server for the batch of 1M rows, not > 1M calls > 2. Some keys in the hashmap might already exist in the database. For > these keys we want to append to the array > 3. Some keys will not exist and for these we want to insert new rows > > Can someone help me please? I am using Postgres for the first time. The best way to deal with importing large batches of data is to use COPY https://www.postgresql.org/docs/current/sql-copy.html But that will not play well with modifying the data as you input it. What I can see doing is: 1) COPY the data into a staging table: create staging_tbl (id integer, fid integer) 2) Then using SQL statements to move the data to the final table. As example of one possibility, using ON CONFLICT from here: https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT insert into table tbl select id, array[fid] from staging_table on conflict(id) DO UPDATE SET fids = array_append(fids, excluded.fid); I would test with a smaller example data set to vetify. > > -- Adrian Klaver adrian.klaver@aklaver.com