diff options
| author | Amit Kapila | 2019-02-04 02:19:15 +0000 |
|---|---|---|
| committer | Amit Kapila | 2019-02-04 02:19:15 +0000 |
| commit | b0eaa4c51bbff3e3c600b11e5d104d6feb9ca77f (patch) | |
| tree | fcb1465d0d691fd9271414b03d53166e4e931132 /src/test | |
| parent | be12aa47e60c45f331e91693efdbc94497d4e9b0 (diff) | |
Avoid creation of the free space map for small heap relations, take 2.
Previously, all heaps had FSMs. For very small tables, this means that the
FSM took up more space than the heap did. This is wasteful, so now we
refrain from creating the FSM for heaps with 4 pages or fewer. If the last
known target block has insufficient space, we still try to insert into some
other page before giving up and extending the relation, since doing
otherwise leads to table bloat. Testing showed that trying every page
penalized performance slightly, so we compromise and try every other page.
This way, we visit at most two pages. Any pages with wasted free space
become visible at next relation extension, so we still control table bloat.
As a bonus, directly attempting one or two pages can even be faster than
consulting the FSM would have been.
Once the FSM is created for a heap we don't remove it even if somebody
deletes all the rows from the corresponding relation. We don't think it is
a useful optimization as it is quite likely that relation will again grow
to the same size.
Author: John Naylor, Amit Kapila
Reviewed-by: Amit Kapila
Tested-by: Mithun C Y
Discussion: https://www.postgresql.org/message-id/CAJVSVGWvB13PzpbLEecFuGFc5V2fsO736BsdTakPiPAcdMM5tQ@mail.gmail.com
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/fsm.out | 48 | ||||
| -rw-r--r-- | src/test/regress/parallel_schedule | 6 | ||||
| -rw-r--r-- | src/test/regress/serial_schedule | 1 | ||||
| -rw-r--r-- | src/test/regress/sql/fsm.sql | 41 |
4 files changed, 96 insertions, 0 deletions
diff --git a/src/test/regress/expected/fsm.out b/src/test/regress/expected/fsm.out new file mode 100644 index 00000000000..b02993188cd --- /dev/null +++ b/src/test/regress/expected/fsm.out @@ -0,0 +1,48 @@ +-- +-- Free Space Map test +-- +CREATE TABLE fsm_check_size (num int, str text); +-- With one block, there should be no FSM +INSERT INTO fsm_check_size VALUES(1, 'a'); +VACUUM fsm_check_size; +SELECT pg_relation_size('fsm_check_size', 'main') AS heap_size, +pg_relation_size('fsm_check_size', 'fsm') AS fsm_size; + heap_size | fsm_size +-----------+---------- + 8192 | 0 +(1 row) + +-- Extend table with enough blocks to exceed the FSM threshold +DO $$ +DECLARE curtid tid; +num int; +BEGIN +num = 11; + LOOP + INSERT INTO fsm_check_size VALUES (num, 'b') RETURNING ctid INTO curtid; + EXIT WHEN curtid >= tid '(4, 0)'; + num = num + 1; + END LOOP; +END; +$$; +VACUUM fsm_check_size; +SELECT pg_relation_size('fsm_check_size', 'fsm') AS fsm_size; + fsm_size +---------- + 24576 +(1 row) + +-- Add long random string to extend TOAST table to 1 block +INSERT INTO fsm_check_size +VALUES(0, (SELECT string_agg(md5(chr(i)), '') + FROM generate_series(1,100) i)); +VACUUM fsm_check_size; +SELECT pg_relation_size(reltoastrelid, 'main') AS toast_size, +pg_relation_size(reltoastrelid, 'fsm') AS toast_fsm_size +FROM pg_class WHERE relname = 'fsm_check_size'; + toast_size | toast_fsm_size +------------+---------------- + 8192 | 0 +(1 row) + +DROP TABLE fsm_check_size; diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index cc0bbf5db9f..4051a4ad4e1 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -69,6 +69,12 @@ test: create_aggregate create_function_3 create_cast constraints triggers inheri test: sanity_check # ---------- +# fsm does a delete followed by vacuum, and running it in parallel can prevent +# removal of rows. +# ---------- +test: fsm + +# ---------- # Believe it or not, select creates a table, subsequent # tests need. # ---------- diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule index 0c10c7100c6..ac1ea622d65 100644 --- a/src/test/regress/serial_schedule +++ b/src/test/regress/serial_schedule @@ -80,6 +80,7 @@ test: roleattributes test: create_am test: hash_func test: sanity_check +test: fsm test: errors test: select test: select_into diff --git a/src/test/regress/sql/fsm.sql b/src/test/regress/sql/fsm.sql new file mode 100644 index 00000000000..332c3e2b2d2 --- /dev/null +++ b/src/test/regress/sql/fsm.sql @@ -0,0 +1,41 @@ +-- +-- Free Space Map test +-- + +CREATE TABLE fsm_check_size (num int, str text); + +-- With one block, there should be no FSM +INSERT INTO fsm_check_size VALUES(1, 'a'); + +VACUUM fsm_check_size; +SELECT pg_relation_size('fsm_check_size', 'main') AS heap_size, +pg_relation_size('fsm_check_size', 'fsm') AS fsm_size; + +-- Extend table with enough blocks to exceed the FSM threshold +DO $$ +DECLARE curtid tid; +num int; +BEGIN +num = 11; + LOOP + INSERT INTO fsm_check_size VALUES (num, 'b') RETURNING ctid INTO curtid; + EXIT WHEN curtid >= tid '(4, 0)'; + num = num + 1; + END LOOP; +END; +$$; + +VACUUM fsm_check_size; +SELECT pg_relation_size('fsm_check_size', 'fsm') AS fsm_size; + +-- Add long random string to extend TOAST table to 1 block +INSERT INTO fsm_check_size +VALUES(0, (SELECT string_agg(md5(chr(i)), '') + FROM generate_series(1,100) i)); + +VACUUM fsm_check_size; +SELECT pg_relation_size(reltoastrelid, 'main') AS toast_size, +pg_relation_size(reltoastrelid, 'fsm') AS toast_fsm_size +FROM pg_class WHERE relname = 'fsm_check_size'; + +DROP TABLE fsm_check_size; |
