large table - Mailing list pgsql-general

From Luke Coldiron
Subject large table
Date
Msg-id BAY179-W6539997AAF631923CBB501C6B30@phx.gbl
Whole thread Raw
Responses Re: large table
Re: large table
List pgsql-general
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';

relidschemanamerelnameseq_scanseq_tup_readidx_scanidx_tup_fetchn_tup_insn_tup_updn_tup_deln_tup_hot_updn_live_tupn_dead_tuplast_vacuumlast_autovacuumlast_analyzelast_autoanalyze
16713publicmyTable39918333992001  0377540903771173949135183 2014-09-18 11:28:47.63545+00 2014-09-18 11:27:47.134432+00

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');

table_lentuple_counttuple_lentuple_percentdead_tuple_countdead_tuple_lendead_tuple_percentfree_spacefree_precent
34709504132010533600.013075730888.61

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 

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: ALTER TEXT field to VARCHAR(1024)
Next
From: Alan Hodgson
Date:
Subject: Re: large table