I am trying to figure out what would have caused a table in a PostgreSQL 8.4.16 to get into a state where there is only 1 live tuple and has only ever had one 1 tuple but the size of the table is huge.
CREATE TABLE public.myTable ( myColumn timestamp with time zone NOT NULL ); Note: there is no primary key or index on this table. CREATE OR REPLACE FUNCTION public.myFunc() RETURNS VOID AS $$ BEGIN UPDATE public.myTable SET myColumn = CLOCK_TIMESTAMP(); IF NOT FOUND THEN INSERT INTO public.myTable(myColumn) VALUES (CLOCK_TIMESTAMP()); END IF; END; $$ LANGUAGE plpgsql VOLATILE STRICT SECURITY DEFINER; CREATE OR REPLACE FUNCTION public.wasUpdated(OUT was_updated boolean) RETURNS BOOLEAN AS $$ BEGIN was_updated := COALESCE((SELECT myColumn FROM public.myTable) > (CLOCK_TIMESTAMP() - INTERVAL '5 SECOND'), FALSE); END; $$ LANGUAGE plpgsql VOLATILE STRICT SECURITY DEFINER; SELECT * FROM pg_stat_all_tables WHERE relname = 'myTable';
The stats are very far off with n_live_tup at 949135 when there is only a single row in the table. Autovacuum appears to be running on a regular basis. SELECT * FROM pgstattuple('public.myTable');
The actual size of the table is around 33 MB. The myFunc function is called every 2.5 seconds and the wasUpdated function every 2 seconds by separate processes. I realize that running a FULL VACUUM or CLUSTER command on the table will resolve the issue but I am more interested in a root cause that explains why this table would end up in this state. I have tried to reproduce this issue by running the exact setup and have not been able to get the table to grow like this example. Any plausible cause'es or explanations would be much appreciated. Luke |