pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Importing a Large .ndjson file
7+ messages / 3 participants
[nested] [flat]

* Importing a Large .ndjson file
@ 2020-06-17 11:21  Sankar P <sankar.curiosity@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Sankar P @ 2020-06-17 11:21 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Hi

I have a .ndjson file. It is a new-line-delimited JSON file. It is
about 10GB and has about 100,000 records.

Some sample records:
```
{ "key11": "value11", "key12": [ "value12.1", "value12.2"], "key13": {
"k111": "v111" } } \n\r
{ "key21": "value21", "key22": [ "value22.1", "value22.2"] }
```
Now I want to INSERT these json records into my postgres table of the
following schema:

```
CREATE TABLE myTable (id BIGSERIAL, content JSONB);
```

Where I want the records to be inserted to the `content` field of my
postgres table.

What is the best way to do this on a postgresql database, deployed in
kubernetes, with a 1 GB RAM allocated ?

I can probably write a that would read this file line-by-line and
INSERT into the database, in a transaction. But that I believe would
take a lot of network traffic and I want to know if there is a better
way to do this.

Thanks.

-- 
Sankar P
http://psankar.blogspot.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-17 15:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sankar P <sankar.curiosity@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Tom Lane @ 2020-06-17 15:18 UTC (permalink / raw)
  To: Sankar P <sankar.curiosity@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Sankar P <sankar.curiosity@gmail.com> writes:
> I have a .ndjson file. It is a new-line-delimited JSON file. It is
> about 10GB and has about 100,000 records.
> Some sample records:
> { "key11": "value11", "key12": [ "value12.1", "value12.2"], "key13": {
> "k111": "v111" } } \n\r
> { "key21": "value21", "key22": [ "value22.1", "value22.2"] }

> What is the best way to do this on a postgresql database, deployed in
> kubernetes, with a 1 GB RAM allocated ?

It looks like plain old COPY would do this just fine, along the lines
of (in psql)

\copy myTable(content) from 'myfile.ndjson'

If the newlines actually are \n\r rather than the more usual \r\n,
you might have to clean that up to stop COPY from thinking they
represent two line endings not one.

I'd advise extracting the first hundred or so lines of the file and doing
a test import into a temporary table, just to verify the process.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-18 07:46  Sankar P <sankar.curiosity@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Sankar P @ 2020-06-18 07:46 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

> It looks like plain old COPY would do this just fine, along the lines
> of (in psql)
>
> \copy myTable(content) from 'myfile.ndjson'
>

Indeed. Thanks.

-- 
Sankar P
http://psankar.blogspot.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-18 17:38  Sankar P <sankar.curiosity@gmail.com>
  parent: Sankar P <sankar.curiosity@gmail.com>
  0 siblings, 2 replies; 7+ messages in thread

From: Sankar P @ 2020-06-18 17:38 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

> > It looks like plain old COPY would do this just fine, along the lines
> > of (in psql)
> >
> > \copy myTable(content) from 'myfile.ndjson'

I spoke too soon. While this worked fine when there were no indexes
and finished within 10 minutes, with GIN index on the jsonb column, it
is taking hours and still not completing.

-- 
Sankar P
http://psankar.blogspot.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-18 18:09  Michael Lewis <mlewis@entrata.com>
  parent: Sankar P <sankar.curiosity@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: Michael Lewis @ 2020-06-18 18:09 UTC (permalink / raw)
  To: Sankar P <sankar.curiosity@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; PostgreSQL General <pgsql-general@lists.postgresql.org>

>
> I spoke too soon. While this worked fine when there were no indexes
> and finished within 10 minutes, with GIN index on the jsonb column, it
> is taking hours and still not completing.
>

It is always recommended to create indexes AFTER loading data. Sometimes it
can be faster to drop all indexes on the table, load huge data, and
re-create the indexes but there's no hard & fast rule. If you are adding
100k records to an empty or near empty table, I would remove all indexes
and create them after. Be sure you have sufficient maintenance_work_mem
also.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-18 18:30  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sankar P <sankar.curiosity@gmail.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Tom Lane @ 2020-06-18 18:30 UTC (permalink / raw)
  To: Sankar P <sankar.curiosity@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

Sankar P <sankar.curiosity@gmail.com> writes:
> I spoke too soon. While this worked fine when there were no indexes
> and finished within 10 minutes, with GIN index on the jsonb column, it
> is taking hours and still not completing.

There's the generic advice that building an index after-the-fact
is often cheaper than updating it incrementally.  For GIN indexes
specifically, see also

https://www.postgresql.org/docs/current/gin-tips.html

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Importing a Large .ndjson file
@ 2020-06-19 03:52  Sankar P <sankar.curiosity@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 7+ messages in thread

From: Sankar P @ 2020-06-19 03:52 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; mlewis@entrata.com; +Cc: pgsql-general@lists.postgresql.org

> Sankar P <sankar.curiosity@gmail.com> writes:
> > I spoke too soon. While this worked fine when there were no indexes
> > and finished within 10 minutes, with GIN index on the jsonb column, it
> > is taking hours and still not completing.
>
> There's the generic advice that building an index after-the-fact
> is often cheaper than updating it incrementally.  For GIN indexes
> specifically, see also
>
> https://www.postgresql.org/docs/current/gin-tips.html

Thanks guys.

-- 
Sankar P
http://psankar.blogspot.com





^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2020-06-19 03:52 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-06-17 11:21 Importing a Large .ndjson file Sankar P <sankar.curiosity@gmail.com>
2020-06-17 15:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-06-18 07:46   ` Sankar P <sankar.curiosity@gmail.com>
2020-06-18 17:38     ` Sankar P <sankar.curiosity@gmail.com>
2020-06-18 18:09       ` Michael Lewis <mlewis@entrata.com>
2020-06-18 18:30       ` Tom Lane <tgl@sss.pgh.pa.us>
2020-06-19 03:52         ` Sankar P <sankar.curiosity@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox