Upgrade to Pro — share decks privately, control downloads, hide ads and more …

One TOAST fits all - by Oleg Bartunov, Teodor S...

One TOAST fits all - by Oleg Bartunov, Teodor Sigaev, Nikita Malakhov, Nikita Glukhov

This presentation covers the recent Postgres Professional innovations related to the suggested TOAST improvements - TOASTER API and custom toasters for JSONB and BYTEA data types.

Postgres Professional

May 13, 2022
Tweet

More Decks by Postgres Professional

Other Decks in Programming

Transcript

  1. Postgres breathed a second life into relational databases db-engines.com/en/ranking JSONB

    • Postgres innovation - the first relational database with NoSQL support • NoSQL Postgres attracts the NoSQL users • JSON became a part of SQL Standard 2016 PG15: SQL/JSON/TABLE 2022 by #postgrespro
  2. Why this talk ? •Blossom of Microservice architecture •Startups want/need

    JSON[B] •JSONB is one of the main driver of Postgres popularity •One-Type-Fits-All •Client app — Backend - Database use JSON •All server side languages support JSON •JSON relaxed ORM (Object-Relational Mismatch), mitigate contradictions between code-centric and data-centric paradigms. •Performance of JSONB (not only) can be improved by several orders of magnitude with proper modification of TOAST. How to integrate improvements into PG CORE !?
  3. The Curse of TOAST: Unpredictable performance CREATE TABLE test (jb

    jsonb); ALTER TABLE test ALTER COLUMN jb SET STORAGE EXTERNAL; INSERT INTO test SELECT jsonb_build_object( 'id', i, 'foo', (select jsonb_agg(0) from generate_series(1, 1960/12)) ) jb -- [0,0,0, ...] FROM generate_series(1, 10000) i; =# EXPLAIN(ANALYZE, BUFFERS) SELECT jb->'id' FROM test; QUERY PLAN ------------------------------------------------- Seq Scan on test (actual rows=10000 loops=1) Buffers: shared hit=2500 Planning Time: 0.050 ms Execution Time: 6.147 ms (4 rows) =# UPDATE test SET jb = jb || '{"bar": "baz"}'; =# VACUUM FULL test; -- remove old versions =# EXPLAIN (ANALYZE, BUFFERS) SELECT jb->'id' FROM test; QUERY PLAN --------------------------------------------- Seq Scan on test (actual rows=10000 loops=1) Buffers: shared hit=30064 Planning Time: 0.105 ms Execution Time: 38.719 ms (4 rows) Small update cause significant slowdown ! Pageinspect: 64 pages with 157 tuples per page WHY 30064 pages !!!!
  4. TOAST Explained The Oversized-Attribute Storage Technique •TOASTed (large field) values

    are compressed, then splitted into the fixed-size TOAST chunks (1996B for 8KB page) •TOAST chunks (along with generated Oid chunk_id and sequnce number chunk_seq) stored in special TOAST relation pg_toast.pg_toast_XXX, created for each table containing TOASTable attributes •Attribute in the original heap tuple is replaced with TOAST pointer (18 bytes) containing chunk_id, toast_relid, raw_size, compressed_size https://www.postgresql.org/docs/current/storage-toast.html
  5. TOAST access •TOAST pointers does not refer to heap tuples

    with chunks directly. Instead they contains Oid chunk_id, so one need to descent by index (chunk_id, chunk_seq). Overhead to read only a few bytes from the first chunk is 3,4 or even 5 additional index blocks.
  6. The Curse of TOAST • Access to TOASTed JSONB requires

    reading at least 3 additional buffers: • 2 TOAST index buffers (B-tree height is 2) • 1 TOAST heap buffer • 2 chunks read from the same page, if JSONB size > Page size (8Kb), then more TOAST heap buffers =# EXPLAIN (ANALYZE, BUFFERS) SELECT jb->'id' FROM test; QUERY PLAN --------------------------------------------- Seq Scan on test (actual rows=10000 loops=1) Buffers: shared hit=30064 Planning Time: 0.105 ms Execution Time: 38.719 ms (4 rows) Table TOAST 64 buffers + 3 buffers*10000
  7. Motivational example (synthetic test) •A table with 100 jsonbs of

    different sizes (130B-13MB, compressed to 130B-247KB): CREATE TABLE test_toast AS SELECT i id, jsonb_build_object( 'key1', i, 'key2', (select jsonb_agg(0) from generate_series(1, pow(10, 1 + 5.0 * i / 100.0)::int)),-- 10-100k elems 'key3', i, 'key4', (select jsonb_agg(0) from generate_series(1, pow(10, 0 + 5.0 * i / 100.0)::int)) -- 1-10k elems ) jb FROM generate_series(1, 100) i; •Each jsonb looks like: key1, loooong key2[], key3, long key4[]. •We measure execution time of operator ->(jsonb, text) for each row by repeating it 1000 times in the query: SELECT jb -> 'keyN', jb -> 'keyN', … jb -> 'keyN' FROM test_toast WHERE id = ?;
  8. Motivational example (synthetic test) Key access time for TOASTed (raw

    size > 100 Kb) jsonbs linearly increase with jsonb size, regardless of key size and position. Inline Compressed Inline Inline Toasted Inline+ Toasted Large jsonb is TOASTed !
  9. Jsonb deTOAST improvements goal Ideal goal: no dependency on jsonb

    size and position • Access time ~ O(level) • Update time ~ O(level) + O(key size) • Original TOAST doesn’t use inline, only TOAST pointers are stored. Utilize inline (fast access) as much as possible: • Keep inline as much as possible uncompressed short fields and compressed medium-size fields • Keep compressed long fields in TOAST chunks separately for independent access and update.
  10. Jsonb deTOAST improvements (root level) • Partial (prefix) decompression -

    eliminates overhead of pglz decompression of the whole jsonb – FULL deTOAST and partial decompression: Decompress(offset) + Detoast(jsonb compressed size), offset depends on key position • Sort jsonb object key by their length – good for short keys Decompress(key_rank * key size) + Detoast(jsonb compressed size), offset depends on key size • Partial deTOAST and partial decompression (deTOASTing iterator) Decompress(key_rank * key size) + Detoast(key_rankc * key size) • Inline TOAST – store inline prefix of compressed data (jsonb header and probably some short keys) Decompress(key_rank * key size) -- great benefit for inline short keys ! Decompress(key_rank * key size) + Detoast(key_rankc * key size)
  11. Jsonb deTOAST improvements • Compress_fields – compress fields sorted by

    size until jsonb fits inline, fallback to Inline TOAST. O(1) – short keys Decompress(key size) – mid size keys • Shared TOAST – compress fields sorted by size until jsonb fits inline, fallback to store compressed fields separately in chunks, fallback to Inline TOAST if inline overfilled by toast pointers (too many fields). • Access O(1) – short keys Decompress(key size) – mid size keys Decompress(key size) + Detoast(key size) – long keys • Update O(inline size) – short keys (inline size < 2KВ) O(inline size) + O(key size) – keys in chunks O(jsonb size) – inline TOAST
  12. Jsonb deTOAST improvements • In-place updates for TOASTed jsonb: •

    Store new element values, their offsets and lengths together with TOAST pointer (some kind of diff) instead of rewriting TOAST chunk chains, if element’s size and type is not changed (in-place update) and new value fits into inline. • Old values are replaced with new ones during deTOASTing. • Update: • O(element size) – if in-place update and new value fits into inline • O(array size) – otherwise
  13. JSONB partial update TOAST was originally designed for atomic data

    types, it knows nothing about internal structure of composite data types like jsonb, hstore, and even ordinary arrays. TOAST works only with binary BLOBs, it does not try to find differencies between old and new values of updated attributes. So, when the TOASTed attribute is being updated (does not matter at the beginning or at the end and how much data is changed), its chunks are simply fully copied. The consequences are: •TOAST storage is duplicated •WAL traffic is increased in comparison with updates of non-TOASTED attributes, because the whole TOASTed values is logged •Performance is too low
  14. UPDATE JSONB: Query execution time Update time of array elements

    depends on their position: •First elements updated very fast (like inline fields) •Last elements updated slower (need to read the whole JEntry array) shared toast + in-place updates
  15. UPDATE JSONB: Blocks read Number of blocks read depends on

    element position: •First elements do not require reading of additional blocks •Last elements require reading the whole JEntry array (4В * array size) shared toast + in-place updates
  16. UPDATE JSONB: WAL traffic •WAL size of in-place updates is

    almost independent on element position •Only inline data with TOAST pointer diff are logged shared toast + in-place updates
  17. TODO • Random access to objects keys and array elements

    of TOAST-ed jsonb • Physical level — add compression to the sliced detoast (easy) • Logical level - shared toast with array support (difficult, require jsonb modification — new storage for array, JSONB API + range support )
  18. Motivational example •A table with 100 MB bytea (uncompressed): CREATE

    TABLE test (data bytea); ALTER TABLE test ALTER COLUMN data SET STORAGE EXTERNAL; INSERT INTO test SELECT repeat('a', 100000000)::bytea data; •Append 1 byte to bytea: EXPLAIN (ANALYZE, BUFFERS, COSTS OFF) UPDATE test SET data = data || 'x'::bytea; Update on test (actual time=1359.229..1359.232 rows=0 loops=1) Buffers: shared hit=238260 read=12663 dirtied=25189 written=33840 -> Seq Scan on test (actual time=155.499..166.509 rows=1 loops=1) Buffers: shared hit=12665 Planning Time: 0.127 ms Execution Time: 1382.959 ms •>1 second to append 1 byte !!! Table size doubled to 200 MB, 100 MB of WAL generated.
  19. Motivational example (explanation) • Current TOAST is not sufficient for

    partial updates • All data is deTOASTed before in-memory modification • Updated data is TOASTed back after modification with new TOAST oid
  20. Solution • Special datum format: TOAST pointer + inline data

    • Inline data serves as a buffer for TOASTing • Operator || does not deTOAST data, it appends inline data producing datum in the new format
  21. Solution • When size of inline data exceeds 2 KB,

    TOASTer recognizes changes in old and new datums and TOASTs only the new inline data with the same TOAST oid • Last not filled chunk can be rewritten with creation of new tuple version • First unmodified chunks are shared
  22. TOAST optimized •Append 1 byte to bytea: EXPLAIN (ANALYZE, BUFFERS,

    COSTS OFF) UPDATE test SET data = data || 'x'::bytea; Update on test (actual time=0.060..0.061 rows=0 loops=1) Buffers: shared hit=2 -> Seq Scan on test (actual time=0.017..0.020 rows=1 loops=1) Buffers: shared hit=1 Planning Time: 0.727 ms Execution Time: 0.496 ms (was 1382 ms) •2750x speed up! •Table size remains 100 MB •Only 143 bytes of WAL generated (was 100 MB) •No unnecessary buffer reads and writes
  23. Appendable bytea: stream Stream organized as follows: •1 row (id,

    bytea) grows from 0 up to 1Mb UPDATE test SET data = data || repeat('a', append_size)::bytea; COMMIT; •append_size = 10b, 100b,…,100Kb •pg_stat_statements: time, blocks r/rw, wal
  24. Conclusions •TOAST in Postgres can be improved for specific data

    types: jsonb, appendable bytea •How integrate them to the Postgres — that is the question ! •Data type aware TOAST — Pluggable TOAST Extend Postgres Extensibility !
  25. Huge values — how to store? • Page limit —

    8KB by default (up to 64KB) •Obviously, not enough • Large object •2nd class citizen (no structure, isn’t accessible at SQL-level, issues with backup) • TOAST — slice to chain of small chunks •Could be compressed •Store in heap table with index (hidden) •TOAST doesn’t know internals of data type, works with as just a long byte array. •Any update make a copy of whole value •TOAST is integrated in heap TAM •The single advantage - versatility
  26. Extend Postgres Extensibility •Let us extend Postgres even further! •Create

    extension … •Toaster is «class» encapsulated all work with huge values •Toaster could be specific for datatype (ex jsonb) •Toaster could be specific for workload (ex append only) •Toaster could be specific for column (different columns in table could use different toasters) •Default toaster — compatibility with previous versions
  27. Toaster - basis CREATE TOASTER nameHNDeLh dAlr;ER =N#\pLhg _t o\

    sbmeHNDeLh TN"pL csbm.NeNpHbusbmeHNDeLhc CHp|n# y TasL y CHppNeiH# y l|ppN"pL y rLfN|pe ------------+---------+-----------+----------+--------- Hi\ y Hi\ y y #He #|pp y eDh#NnL y #NnL y y #He #|pp y eDh=N#\pLh y hLbshH. y y #He #|pp y I#\LxLD: csbmeHNDeLhmHi\mi#\Lxc PRIMARY KEY, "ehLL (Hi\) csbmeHNDeLhm#NnLmi#\Lxc UlIQUE COlSTRAIlT, "ehLL (eDh#NnL)
  28. Toaster - basis CREATE TAB;E eDe1 ( fHH eLxe GSTORA4E

    spNi#{, "Nh eLxe STORAtE eoserna| TOASTER midsualser, i\ i#e/ )g A;TER TAB;E eDe1 A;TER CO;UMl #NnL SET TOASTER nameHNDeLhg _t o\+ eDe1 CHp|n# y TasL y CHppNeiH# y l|ppN"pL y rLfN|pe y SeHhNbL y THNDeLh yuuu --------+---------+-----------+----------+---------+----------+------------+uuu fHH y eLxe y y y y spNi# y \LfeHNDeLh yuuu "Nh y eLxe y y y y LxeLh#Np y nameHNDeLh yuuu i\ y i#eLbLh y y y y spNi# y yuuu A..LDD nLe=H\: =LNs
  29. Toaster - basis _t o\ sbmNeehi"|eL TN"pL csbm.NeNpHbusbmNeehi"|eLc CHp|n# y

    TasL y CHppNeiH# y l|ppN"pL y rLfN|pe ----------------+-----------+-----------+----------+--------- NeehLpi\ y Hi\ y y #He #|pp y Nee#NnL y #NnL y y #He #|pp y Neeeasi\ y Hi\ y y #He #|pp y uuu NeeDeHhNbL y c.=Nhc y y #He #|pp y asssualser _ ufc _ _ nus n|| _ Nee.HnshLDDiH# y c.=Nhc y y #He #|pp y uuuu
  30. Toaster storage in tuple Custom toaster (>=10 bytes, alignment 2)

    Tuple’s header ... Toast pointer int32 rawsize int32 extinfo Oid valueid Oid toastrelid Tuple’s header ... Toast pointer int16 toasterdatalen uint32 rawsize Oid toaster ... ... ... Default (current) toaster (16 bytes, alignment 4)
  31. Сonsequences Toast pointer is aware of toaster id it was

    toasted by • toaster could not be dropped (is it really necessary?) • In one column could be data toasted by different toasters
  32. Toaster - API easL\Lf Deh|.e TDhRH|ei#L * lH\LTNb easLg v?

    i#eLhfN.L f|#.eiH#D ?v sualsdfnfs fnfs; sualsdnsfun suals; |s\NeLmeHNDemf|#.eiH# |s\NeLmeHNDeg .HsameHNDemf|#.eiH# .HsameHNDeg cesualsdnsfun cesuals; \LpmeHNDemf|#.eiH# \LpeHNDeg bLem}eN"pLmf|#.eiH# bLem}eN"pLg vvk eHNDeLh}Npi\NeLmf|#.eiH# eHNDeLh}Npi\NeLg vvk 0
  33. Toaster — validate? v? }Npi\NeL \Lfi#ieiH# Hf N eHNDeLh Oi\

    ?v easL\Lf "HHp (?eHNDeLh}Npi\NeLmf|#.eiH#) (Oi\ easLHi\,.=Nh DeHhNbL, .=Nh .HnshLDDiH#, Oi\ NnHi\, "HHp fNpDLmHz)g • Validate method is required for Toaster compatibility check. • Toaster is specific for datatype and workload, it depends on compression and storage.
  34. Toaster — vtable? v? RLe|h# }ihe|Np eN"pL Hf f|#.eiH#D, HseiH#Np

    ?v easL\Lf }Hi\ ? (?bLem}eN"pLmf|#.eiH#) (rNe|n eHNDemseh)g
  35. Current Toast/Detoast • Part of the Heap AM • Single

    Toast/Detoast strategy - full Toast/Detoast only • Not extensible
  36. Toaster API Magic • Detached from Heap AM, Independent •

    Possibility extend with any Custom Toaster •Does not affect performance
  37. Virtual Table of User-Defined Functions vtable is an inner API

    of Toaster API. It allows Custom Toasters to have any user- defined function a developer wants – just put it into TsrRoutine virtual function table, and it is ready to use! For example, bytea Toaster has append() function to append two bytea Datums instead of creating new (third) copy. There functions may not be directly used for toasting/detoasting, but could provide additional operations DeNei. }Hi\ ? "aeLNmeHNDeLhm}eN"pL(rNe|n eHNDemseh) * BaeLNTHNDeRH|ei#L ?hH|ei#L _ sNppH.>(DiDLHf(?hH|ei#L))g hH|ei#L-EnNbi. _ BYTEAmTOASTERmMA4ICg hH|ei#L-ENssL#\ _ "aeLNmeHNDeLhmNssL#\g hLe|h# hH|ei#Lg 0
  38. BYTEA and JSONB Toasters • We are glad to present

    two Toasters developed by PostgresPro Team – bytea Appendable Toaster and Jsonb Toaster. Both types could store huge amounts of data, and current Toast mechanics does not perform well with accessing and updating: - bytea type is suitable for streaming, which require special fast update mode – “append” without re-writing full data record; - Values stored in JSONb objects are often accessed “by-key”, and full detoast to fetch just one key is very ineffective
  39. Bytea Update Performance • Update time depends on the size

    of the updated and appended values •Huge WAL traffic is generated – full record is placed in WAL with update
  40. The Solution is – bytea Appendable Toaster! • Toaster package

    specially designed for bytea datatype. • Fast, effective, extensible. The main difference, along with the toast/detoast and update functions, is Custom ByteaAppendablePointer - Modified tail is stored as inline data; - When inline tail exceeds Toast size limit, it is toasted, but unmodified chunks are left as-is
  41. Bytea Toaster Extension •Special datum format: TOAST pointer + inline

    data •“append” operation - operator || does not deTOAST data, it appends inline data producing datum in new format •TOASTer recognizes changes and TOASTs only the new inline data, possibly rewriting last chunk in chain
  42. Toasting Appended Data • Toaster API Bytea Toaster is called

    via Toast •Last not filled chunk can be rewritten with creation of new tuple version •First unmodified chunks are shared
  43. Bytea Toaster Performance MASTER • T ~ OLD SIZE +

    NEW SIZE • WAL ~ OLD SIZE + NEW SIZE Old tuple and New tuple are written to WAL BYTEA TOASTER • T ~ APPEND SIZE • WAL ~ INLINE + NEW SIZE Only updated part and appended value is written to WAL
  44. JSONb •Json is very popular datatype used by many applications;

    •People want SQL/JSON and want it to be fast an effective; •Json objects are mostly accessed by keys; •JSONb is a PostgreSQL internal binary representation of Json objects; •Full JSONb object needs to be detoasted to access single key-value pair – very ineffective;
  45. Default JSONb Access Performance Key access time linearly increases with

    jsonb size, regardless of value size and position
  46. Jsonb Toaster Extension Jsonb Toaster is plugged in with Toaster

    API. Along with Toast/Detoast Jsonb Toaster provides new functions to work with Jsonb Containers and Iterators
  47. TODO •Random access to objects keys and array elements of

    TOAST-ed jsonb •Physical level — add compression to the sliced detoast (easy) •Logical level - shared toast with array support (difficult, require jsonb modification — new storage for array, JSONB API + range support ) •Additional Access Methods
  48. References • Our experiments: • Understanding Jsonb performance http://www.sai.msu.su/~megera/postgres/talks/jsonb-pgconfnyc-2021.pdf •

    Details - http://www.sai.msu.su/~megera/postgres/talks/jsonb-pgvision-2021.pdf • Slides of this talk http://www.sai.msu.su/~megera/postgres/talks/toast-highload-2022.pdf • Борьба с TOAST или будущее JSONB в PostgreSQL https://habr.com/ru/company/oleg-bunin/blog/646987/ • Pluggable TOAST at Commitfest https://commitfest.postgresql.org/38/3490/ • Jsonb is ubiquitous and is continuously developing • JSON[B] Roadmap V2, Postgres Professional Webinar, Sep 17, 2020 • JSON[B] Roadmap V3, Postgres Build 2020, Dec 8, 2020
  49. When children climb trees and tear their pants off, we

    can forbid them to do so or teach them climbing techniques.
  50. Let’s not say that json is the wrong technology, Let’s

    make json a first class citizen instead.