Question

I am using psql 8.4.7. I am trying to delete from a log table with over 4 million rows. Every time the process starts, it hangs and I stop it. I tried deleting 1 row, using the id to select it, but no luck. No error, just hangs. I do have free space on my disk. The log table has a child table with almost 2 mill rows, and ON DELETE CASCADE is in place. Other tables with more dependents and row delete just fine.

Here is the create statement:

  CREATE TABLE log(
  id serial NOT NULL,
  userid integer,
  itemid integer NOT NULL,
  itemtype integer NOT NULL,
  date timestamp with time zone NOT NULL DEFAULT now(),
  actiontype integer NOT NULL,
  log_detail text,
  dtype integer,
  created timestamp with time zone DEFAULT now(),
  modified timestamp with time zone DEFAULT now(),
  CONSTRAINT log_pkey PRIMARY KEY (id),
  CONSTRAINT fk_log_user_id FOREIGN KEY (userid)
  REFERENCES appuser (id) MATCH SIMPLE
  ON UPDATE CASCADE ON DELETE SET NULL
)
CREATE INDEX log_itemid_index
 ON log
 USING btree
 (itemid);

CREATE INDEX log_itemtype_index
  ON log
  USING btree
  (itemtype);

CREATE INDEX log_userid_index
  ON log
  USING btree
  (userid);

Any idea what could be wrong?

EDIT: The table definition for the child is:

CREATE TABLE log_snapshot ( 
id serial NOT NULL, 
log_id integer, 
item_field character varying(255) DEFAULT NULL::character varying, 
item_value character varying(255) DEFAULT NULL::character varying, 
created timestamp with time zone DEFAULT now(),
modified timestamp with time zone DEFAULT now(), 
CONSTRAINT fk_log_snapshot FOREIGN KEY () REFERENCES log () MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE ).
Was it helpful?

Solution

Someone else already mentioned. But try creating an index on log_snapshot(log_id) I tried it (inserted 10000000 records into log and log_snapshot). See the execution time difference.

test=# delete from log where id = 196;
DELETE 1
Time: 1232.772 ms
test=# create index myindex on log_snapshot(log_id);
CREATE INDEX
Time: 10143.934 ms
test=# delete from log where id = 496;
DELETE 1
Time: 65.293 ms

OTHER TIPS

I faced this similar issue and I later found that my server was turned on on my local machine and my table might be somehow locked. The issue resolved when I stopped the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top