Page inspection using heap_page_item() after pruning the page

I was reading the Postgres Internals book and in Page Pruning section it was showing an example on how Postgres cleans up the page when it accommodates enough dead tuples, I tried the example it was showing the expected result.

For context, the example was very similar to this

CREATE TABLE messages (
id INTEGER,
content CHAR(2000));

CREATE INDEX messages_id ON messages(id);
CREATE INDEX messages_content ON messages(content);

INSERT INTO messages (id,content) VALUES (1, 'A');

UPDATE messages SET content = 'B' WHERE id = 1;
UPDATE messages SET content = 'C' WHERE id = 1;
UPDATE messages SET content = 'D' WHERE id = 1;
UPDATE messages SET content = 'E' WHERE id = 1;

SELECT lp, t_ctid, t_data, CASE
       WHEN t_xmax = 0 THEN 'live'
       ELSE 'dead'
       END AS status
FROM heap_page_items(get_raw_page('messages', 0));

SELECT * FROM bt_page_items('messages_id', 1);

Output:

The book mentions that Postgres did prune the page as it reached the maximum page size and need to clean up dead rows, also says that the remaining heap tuples are physically moved towards the highest addresses so the free space is aggregated, and the index page stayed the same because pruning only operates on a single page at a time, which are all fine.

My problem is when I tried to modify the example and did the following:

CREATE TABLE messages (
id SERIAL PRIMARY KEY,
content CHAR(2000));

INSERT INTO messages (content) VALUES ('A');

UPDATE messages SET content = 'B' WHERE id = 1;
UPDATE messages SET content = 'C' WHERE id = 1;
UPDATE messages SET content = 'D' WHERE id = 1;
UPDATE messages SET content = 'E' WHERE id = 1;

SELECT lp, t_ctid, t_data, CASE
       WHEN t_xmax = 0 THEN 'live'
       ELSE 'dead'
       END AS status
FROM heap_page_items(get_raw_page('messages', 0));

SELECT * FROM bt_page_items('messages_pkey', 1);

Output:

I have modified the id to be an incremental primary key, which by default also creates a primary key index, the result is so different as you can see, and it does not follow anything the book mentioned, the ctid and htid in the index is different from the one in the table, free space is not aggregated, and the index does not have any dead tuples. I have no idea why this happened and I’m looking for some clarification.

3

The difference is the index on content in the first example.

Because of that index, the updates cannot be HOT updates. Each new row version gets an index entry, which cannot be cleaned up until VACUUM runs. That explains the five index tuples you can see.

The five line pointers to which the index entries point must be retained, but the last update goes ahead and reclaims the space of the first three table tuples as it finds that the page is over 90% full.


In the second example, where you have no index on content, all updates are HOT updates, so they create no new index entry. the new “heap-only tuple” created by such an update is only referenced from the ctid of the precious tuple in the HOT chain.

Since there is no external reference to the line pointers of such a heap-only tuple, each update can “prune” any dead tuples and the line pointers of dead heap-only tuples from the previous updates. The situation you see in your second example is that the last update reused line pointer 2 for the new tuple (line pointer 1 cannot be pruned and reused, since it is referenced from the index).

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật