News for hackers. Stuff that matters.
Re:One word in PostgreSQL unlocked a 9x performance improvement by oefrha on Sunday June 14, 2020 @12:51PM (#23519200)
Long as hell post with a clickbait title documenting how the author found out (1) one can insert multiple rows with a single INSERT statement; (2) the RETURNING clause. Spend your time elsewhere if you’re already aware; maybe pick up a SQL textbook if you aren’t...

This is not an uncharitable read btw. Actual quotes:

> The first thing I learned was how to insert multiple rows with a single INSERT statement: ...

> Scouring the docs I discovered the RETURNING clause of an INSERT statement.

Re:One word in PostgreSQL unlocked a 9x performance improvement by quasarj on Sunday June 14, 2020 @12:58PM (#23519278)
I enjoyed trying to guess at what the "one word" was he was going to discover. I was mostly right!

That said, I was unaware of the big discovery in the post: Doing INSERT ... RETURNING ... ON CONFLICT DO NOTHING, and passing an array of rows to insert, the returning clause will return only the values from the rows that were actually inserted. IMO this is the "obvious" behavior, but it is still worthwhile to know it works!

Re:One word in PostgreSQL unlocked a 9x performance improvement by nicoburns on Sunday June 14, 2020 @01:03PM (#23519319)
> IMO this is the "obvious" behavior, but it is still worthwhile to know it works!

I think this is why people love Postgres so much. 99 times out of 100, the "obvious" behaviour turns out to be the behaviour that Postgres actually implements.

Re:One word in PostgreSQL unlocked a 9x performance improvement by egeozcan on Sunday June 14, 2020 @01:56PM (#23519730)
But your battle-trained mined, injured from weird RDBMSs, is always expecting the worst, and you still open the docs, searching for the gotcha.
Re:One word in PostgreSQL unlocked a 9x performance improvement by teraku on Sunday June 14, 2020 @03:02PM (#23520342)
Or like my colleagues: find behaviour that 99 out of 100 db-people find intuitive weird because it's not like WhAt ThEy ArE uSeD tO
Re:One word in PostgreSQL unlocked a 9x performance improvement by dkersten on Sunday June 14, 2020 @06:08PM (#23521786)
What are they used to?
Re:One word in PostgreSQL unlocked a 9x performance improvement by fphhotchips on Sunday June 14, 2020 @06:33PM (#23521952)
Depends on the industry segment, but I find that MySQL/MariaDB users tend to be the worst for this.

All the RDBMS systems are guilty of it to some degree though.

Re:One word in PostgreSQL unlocked a 9x performance improvement by teraku on Monday June 15, 2020 @06:28AM (#23525835)
Oracle
Re:One word in PostgreSQL unlocked a 9x performance improvement by alexbanks on Sunday June 14, 2020 @03:03PM (#23520348)
The actual Python "pit of success" is postgres.
Re:One word in PostgreSQL unlocked a 9x performance improvement by Kwantuum on Sunday June 14, 2020 @01:53PM (#23519694)
Well it's not just the obvious behaviour, it seems like the only useful behaviour, just like the default is returning the number of affected rows (which for insert, would be the number of inserts). You know ahead of time how many items you're trying to insert, it would be completely pointless to return that.
Re:One word in PostgreSQL unlocked a 9x performance improvement by xeonoex on Sunday June 14, 2020 @03:53PM (#23520774)
> You know ahead of time how many items you're trying to insert, it would be completely pointless to return that.

In simple CRUD cases yes. In many other scenarios, you might not. If your insert is a select statement, with joins and clauses, you can easily insert too much data, or no data at all.

Re:One word in PostgreSQL unlocked a 9x performance improvement by OJFord on Sunday June 14, 2020 @02:31PM (#23520013)
Is it? Returning the conflicts that weren't inserted seems more obviously useful to me.
Re:One word in PostgreSQL unlocked a 9x performance improvement by saltcured on Sunday June 14, 2020 @03:14PM (#23520466)
The RETURNING semantics produce content of the resulting row after insertion. For the ones that have been skipped, this could be ill-defined. The content is not just the content of the row data that was supplied, but potentially server-generated content from sequences, row-level triggers, etc.
Re:One word in PostgreSQL unlocked a 9x performance improvement by ivoras on Sunday June 14, 2020 @03:20PM (#23520513)
There's one HUGE upside for doing it the current way: autogenerated columns.

E.g.

    CREATE TABLE t (id SERIAL PRIMARY KEY, x INTEGER UNIQUE);

    INSERT INTO t (x) VALUES (42), (43) ON CONFLICT DO NOTHING RETURNING *;
This does EXACTLY the most useful thing if one of '42' or '43' conflicts.
Re:One word in PostgreSQL unlocked a 9x performance improvement by magicalhippo on Sunday June 14, 2020 @04:51PM (#23521255)
> You know ahead of time how many items you're trying to insert, it would be completely pointless to return that.

I often use "insert into mytable(...) select ... from ... where ...", and I know I only want one row inserted.

In such cases checking that exactly one row was inserted is a very nice safeguard against buggy code/query, or in the freak case someone messed with the DB.

Re:One word in PostgreSQL unlocked a 9x performance improvement by chinhodado on Sunday June 14, 2020 @05:42PM (#23521629)
I've been looking for way to bulk insert for postgres, but with the ability to ignore any error for any single row (including FK/unique constraint/check constraint/post insert trigger/whatever) and after it finishes give me the list of the rows that failed to insert. I haven't found any way to do this yet. Anyone has any idea?
Re:One word in PostgreSQL unlocked a 9x performance improvement by biddlesby on Sunday June 14, 2020 @01:42PM (#23519629)
I didn't know about RETURNING and enjoyed the post. It's a much nicer way to learn than via a textbook. A blog post doesn't have to be novel to have value.

That said, I'm with you on the clickbait title. That really grinds my gears.

Re:One word in PostgreSQL unlocked a 9x performance improvement by oefrha on Sunday June 14, 2020 @01:57PM (#23519733)
If you never had a formal introduction to SQL, you can probably get a lot of value from a run-of-the-mill SQL textbook by scanning the table of contents and skimming the chapters/sections on topics new to you. (For the record, I learned SQL myself through fumbling. My first schema designs didn’t even have relations.)

The problem with this blog post isn’t lack of novelty; rather, with the clickbait title, it piqued my interest but I ended up learning nothing, other than being reminded of my own terrible designs and queries back when I was a noob.

Re:One word in PostgreSQL unlocked a 9x performance improvement by badrequest on Sunday June 14, 2020 @03:28PM (#23520579)
All that is well and good, but RETURNING is a Postgres feature, not part of the SQL standard. I don't think a standard SQL textbook (especially one targeting something like MySQL/Sqlite) would have taught the author about it.
Re:One word in PostgreSQL unlocked a 9x performance improvement by fabian2k on Sunday June 14, 2020 @03:34PM (#23520621)
I was quite surprised when I found out that RETURNING is actually a Postgres feature, and not standard. It just felt like such an obvious and extremely useful thing to have.
Re:One word in PostgreSQL unlocked a 9x performance improvement by AdrianB1 on Sunday June 14, 2020 @04:11PM (#23520900)
RETURNING exists also in Oracle PL/SQL and it is called OUTPUT in T-SQL. It may exist in other SQL variants.
Re:One word in PostgreSQL unlocked a 9x performance improvement by branko_d on Sunday June 14, 2020 @08:29PM (#23522685)
> My first schema designs didn’t even have relations.

I'm pretty sure your first schema designs had relations (tables). I can't speak for relationSHIPS (foreign keys) though...

;)

Re:One word in PostgreSQL unlocked a 9x performance improvement by Swizec on Sunday June 14, 2020 @01:59PM (#23519750)
But would any of us have seen this blogpost without the clickbait title? Probably not
Re:One word in PostgreSQL unlocked a 9x performance improvement by devmunchies on Sunday June 14, 2020 @02:14PM (#23519883)
Agreed. I would expect learning about more edge cases here on HN. These are basic PG features you can see when looking at the INSERT definition in the docs.

I second what you said about reading a text book. I read through the entire PG documentation and a PG book my second year as an engineer and 7 years later its paid off big time. It’s one of those things where if you do it early, you get compounding benefits over your career.

Re:One word in PostgreSQL unlocked a 9x performance improvement by revskill on Sunday June 14, 2020 @02:22PM (#23519947)
The title is fine. The problem is structure of the post. If author puts actual that word with a story to follow then all is good.
Re:One word in PostgreSQL unlocked a 9x performance improvement by aaomidi on Sunday June 14, 2020 @03:04PM (#23520362)
What SQL Textbook should I read?
Re:One word in PostgreSQL unlocked a 9x performance improvement by sgk284 on Sunday June 14, 2020 @04:04PM (#23520847)
The Postgres docs are some of the best reading you can do. Super digestible, great examples, and all around excellent for learning.
Re:One word in PostgreSQL unlocked a 9x performance improvement by ant6n on Sunday June 14, 2020 @03:08PM (#23520405)
"Become a Select St*r!" (https://wizardzines.com/zines/sql/)
Re:One word in PostgreSQL unlocked a 9x performance improvement by wiml on Sunday June 14, 2020 @04:49PM (#23521237)
Once you have a basic understanding of SQL, I found it really informative to go closer to the source, e.g. C. J. Date and Hugh Darwen's "A Guide to the SQL Standard", or perhaps other books by them.

There's a lot of incomplete and misunderstood information floating around in blog tutorials or potboiler textbooks.

Re:One word in PostgreSQL unlocked a 9x performance improvement by aaomidi on Monday June 15, 2020 @04:33PM (#23532632)
This sounds great!
Re:One word in PostgreSQL unlocked a 9x performance improvement by 3pt14159 on Sunday June 14, 2020 @03:51PM (#23520763)
I found reading the postgres documentation and playing around with each of the examples useful.
Re:One word in PostgreSQL unlocked a 9x performance improvement by jmalicki on Monday June 15, 2020 @12:54AM (#23523918)
Re:One word in PostgreSQL unlocked a 9x performance improvement by xiphias2 on Sunday June 14, 2020 @02:16PM (#23519899)
Thanks, flagged it