DEV Community

Cover image for Dead Tuples in PostgreSQL.
Sandeep
Sandeep

Posted on

10

Dead Tuples in PostgreSQL.

Today we are going to learn about Dead Tuples in PostgreSQL. As we know PostgreSQL is based on MVCC Architecture.

MVCC is a Multi Version Concurrency Control basically "A single rows has multiple versions."
E.g, If a user insert a record of a Person whose AGE is 21. Now, he update the record with AGE 22, so what PostgreSQL does here, PostgreSQL stored those two records with the different versions.
Old version: Age = 21
New version: Age = 22
By default user can only select the new version of the record, and old version marked as an unused record.
That types of record called Dead Tuple or Dead Row.

How to find Live Tuples or Dead Tuples using pg_stat_user_tables.



select n_live_tup, n_dead_tup, relname from pg_stat_user_tables;


Enter fullscreen mode Exit fullscreen mode

Find tuples from a specific table.



SELECT n_live_tup, n_dead_tup, relname FROM
pg_stat_user_tables where relname = 'users';


Enter fullscreen mode Exit fullscreen mode

using Alias



SELECT relname AS Table_Name, n_live_tup AS Live_Tuples,
n_dead_tup AS Dead_Tuples FROM
pg_stat_user_tables where relname = 'users';


Enter fullscreen mode Exit fullscreen mode

SQL Result:
Dead Tuples

How to avoid to create Dead Tuple while Inserting the duplicate data.

If a table have some unique constraint and a user trying to Insert the same data again, PostgreSQL will return an error >
Uniqueness violation. duplicate key value violates unique constraint. We can avoid this by using ON CONFLICT DO NOTHING clause.



INSERT INTO users VALUES ('name', 'email@gmail.com')
ON CONFLICT DO NOTHING;


Enter fullscreen mode Exit fullscreen mode

It'll return INSERT 0 0 indicates that nothing was inserted in the table, the query didn't error out. In the case of ON CONFLICT DO NOTHING no dead tuples are generated because of the pre-check.

How to delete Dead Tuples using VACUUM -



VACUUM users;


Enter fullscreen mode Exit fullscreen mode

It'll free up the space within the users table and only users table can use this space. If you want to use this free space by system then this command should be run.



VACUUM FULL users;


Enter fullscreen mode Exit fullscreen mode

Read More about VACUUM.

Thank you <πŸ‘¨β€πŸ’»/>

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free β†’

Top comments (0)

Image of Stellar post

πŸš€ Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay