r/programming • • Sep 24 '20

PostgreSQL 13 Released!

https://www.postgresql.org/about/news/2077/
1.3k Upvotes

236 comments sorted by

View all comments

289

u/little_blue_teapot Sep 24 '20

The major highlights:

PostgreSQL 13 contains many new features and enhancements, including:

  • Space savings and performance gains from de-duplication of B-tree index entries

  • Improved performance for queries that use aggregates or partitioned tables

  • Better query planning when using extended statistics

  • Parallelized vacuuming of indexes

  • Incremental sorting

51

u/I_AM_GODDAMN_BATMAN Sep 24 '20

I'm curious about reverse b-tree index to support LIKE '%something' query. Maybe it's better to use GIN.

28

u/Tavi2k Sep 24 '20

You probably could define an index on reverse(column) for that, then the wildcard is at the end of the string again and a B-tree index works. That index won't be good for anything else, though.

A heavier option is a trigram index, that should be able to speed up this kind of query as well.

4

u/sbrick89 Sep 25 '20

Usually for those full search capabilities, inverted indexes are the structure of choice. Sadly those systems essentially try to break text into words, so certain code / numbering convention type of situations get fucked.

15

u/krum Sep 24 '20

GIN?

29

u/TheMightyMegazord Sep 24 '20

11

u/krum Sep 24 '20 edited Sep 24 '20

So I took a look at this, and I'm not sure how GIN would help with string wildcard prefix matches. It did just occur to me that you could have a b-tree comparator that sorted strings backwards to achieve that but of course that would incur two b-tree writes per value.

7

u/mrweck Sep 24 '20

You can look at the pg_trgm extension, on contrib. With it, you can do wildcard searches like '%test%'

1

u/LordSeptum Oct 02 '20

DUDE I'm going to have to try this. We work with millions of records and while we implement full-text indexing for human-readable fields, for something such as partial part number searches, this would be great.

46

u/kernel_dev Sep 24 '20

A type of alcohol usually served with tonic water and ice.

8

u/krum Sep 24 '20

Oh haha yeah I've got my bottle on Hendrick's right here on my desk.

3

u/tias Sep 24 '20

The international Gin & Tonic day is coming up in three weeks. An excellent time to have a look in your gin index.

1

u/LordSeptum Oct 02 '20

I'm wondering if maybe full-text indexing would fits your needs.. It transliterates words into their base word and indexes them. Then when testing against the index, your query is also transliterated into the base words so that matches may be found.

If you're looking for true "starts with" or "ends with" functionality, I'd have to do some digging but seems possible.

3

u/Alchestbreach_ModAlt Sep 24 '20

Can someone e plain the parallel vacuum thing? New to sql and trying to learn here.

20

u/BinaryRockStar Sep 24 '20

It's a bit complicated but I'll give you my layman explanation.

Most (all?) database systems these days use Multi-Version Concurrency Control (MVCC). This means if you have one connection UPDATEing a row while another connection is SELECTing that row the database will keep before and after copies of the row so that the SELECTing connection sees the row as it was when the transaction started and the UPDATEing connection sees the updated row. Once both connections/transactions end, the updated row is now the latest version and all future connections will see that.

Now, what happens to the old row? You don't want to delete it and reorganise the physical data on disk during either of the above transactions as that would add a variable performance degradation to each mutation (UPDATE/DELETE/INSERT) which isn't ideal. Postgres deals with this by having the DB administrator perform a manual VACUUM command while traffic on the DB is low. This cleans up the table itself and any indexes, removing dead rows and index entries.

The feature added in Postgres 13 is parallel vacuuming. This means if a table has multiple indexes they can all be vacuumed at the same time, seriously reducing the time it takes to vacuum a table with multiple indexes.

16

u/infrul Sep 25 '20

The vacuum is normally performed automatically by a special process which weighs system load versus urgency and is generally pretty good at keeping things running smoothly without manual intervention being required.

https://www.postgresql.org/docs/current/routine-vacuuming.html#AUTOVACUUM

5

u/BinaryRockStar Sep 25 '20

Right, I knew that I'm not sure why I made it out to be a manual process. I'm going to hide behind simplifying it for a beginner ;-)

5

u/infrul Sep 25 '20

No need to be shy, you posted a really good explanation. Note that although autovacuum is enabled by default, it isn't necessarily so.

2

u/SGBotsford Sep 25 '20

So... Roomba, not Electrolux... <grin, duck, run>

1

u/BinaryRockStar Sep 25 '20

My understanding is that vacuuming is a bugbear of PostgreSQL as with a constant high write load it can impact performance substantially. How do other RDBMSes handle MVCC stale row cleanup?

5

u/infrul Sep 25 '20

http://rhaas.blogspot.com/2017/12/mvcc-and-vacuum.html

A constant high write load will impact anything's performance substantially and an RDBMS isn't the best solution for every problem. There are other ways to handle MVCC but they aren't better than the way PostgreSQL does it and they are probably worse. PostgreSQL gives you the ability to fine tune how garbage collection runs to suit a wide variety of uses.

1

u/BinaryRockStar Sep 25 '20

Great, thanks for the info

3

u/ants_a Sep 25 '20

That's a pretty generous description of autovacuum throttling logic. In reality it's "if we hit X% garbage start cleaning up, but rest Y ms every Z blocks cleaned up. Works surprisingly ok enough given how simplistic it is.

1

u/Alchestbreach_ModAlt Sep 25 '20

That helped explain alittle bit! Thank you

2

u/randomdudefromutah Sep 25 '20

Or in other words EXPLAIN ANALYZE VACCUM (Sorry the chance to combine a dumb dad joke with SQL nerdery appeared and I had to take it)