1

It's becoming common to see this problem in the development environment. My application is not in production yet, but I'm worried this problem could happen.

As I develop new features, I commonly clear some tables when necessary. I started to get into this problem with one specific table I try to clean. I delete everything from other tables that could have foreign constraints, but when I try to delete from that specific table, pgAdmin loses the connection and the database enters in recovery mode.

First I get a warming saying:

"The application has lost the database connection".

If I try several times to connect and execute again, then I get a message

"FATAL: the database system is in recovery mode".

One thing that worked was to execute an update setting some field to it's own value on all rows (i.e update table set field = field). After this I could delete all rows.


I'm actually using c# and EntityFramework 6 with the DevArt.PostgreSql plugin to control the database creation, but I generated the table script from pgAdmin to help understand the table structure. I also added one other table that has a foreign key to the table in question.

The application domain, code and database structure is written in pt-BR, so I tried to translate the table and fields names to help. It's an application to control order requests and charges.

Table (request_event):

CREATE TABLE public.request_event
(
    id integer NOT NULL DEFAULT nextval('request_event_id_seq'::regclass),
    id_request integer NOT NULL,
    type integer NOT NULL,
    date timestamp without time zone NOT NULL,
    time_informed boolean NOT NULL,
    id_user integer,
    message text COLLATE pg_catalog."default" NOT NULL,
    visible boolean NOT NULL,
    occurrence timestamp without time zone NOT NULL DEFAULT '0001-01-01 00:00:00'::timestamp without time zone,
    CONSTRAINT request_event_pkey PRIMARY KEY (id),
    CONSTRAINT "FK_request_event_request_id_request" FOREIGN KEY (id_request) REFERENCES public.request (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT "FK_request_event_user_id_user" FOREIGN KEY (id_user) REFERENCES public.user (id_person) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.request_event OWNER to postgres;

CREATE INDEX "IX_request_event_id" ON public.request_event USING btree (id) TABLESPACE pg_default;
CREATE INDEX "IX_request_event_id_request" ON public.request_event USING btree (id_request) TABLESPACE pg_default;
CREATE INDEX "IX_request_event_id_user" ON public.request_event USING btree (id_user) TABLESPACE pg_default;

Table (charge_event):

CREATE TABLE public.charge_event
(
    id integer NOT NULL,
    id_charge integer NOT NULL,
    CONSTRAINT charge_event_pkey PRIMARY KEY (id),
    CONSTRAINT "FK_charge_event_charge_id_charge" FOREIGN KEY (id_charge) REFERENCES public.charge (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION,
    CONSTRAINT "FK_charge_event_request_event_id" FOREIGN KEY (id) REFERENCES public.request_event (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public.charge_event OWNER to postgres;
CREATE INDEX "IX_charge_event_id" ON public.charge_event USING btree (id) TABLESPACE pg_default;
CREATE INDEX "IX_charge_event_id_charge" ON public.charge_event USING btree (id_charge) TABLESPACE pg_default;

Delete SQL (fail):

DELETE FROM charge_event;
DELETE FROM request_event;

Delete SQL (worked):

DELETE FROM charge_event;
UPDATE request_event SET type = type;
DELETE FROM request_event;
  • Could you share the create script for the table, and the code to delete the table? – Anthony Genovese Jan 2 at 16:26
  • Hi Anthony, I added the Scripts to the question. Let me know if you need something else. – Thiago Romam Jan 2 at 17:37
  • I guess you'll need to investigate why your Postgres instance crashes during the second delete. How many rows do you delete at once? Any particular reason for not using truncate? Are you running a supported version? Anything interesting in the error log? – mustaccio Jan 2 at 19:20
  • I didn't use truncate because of the foreign keys of other tables. – Thiago Romam Jan 3 at 10:23
  • 2
    Did you try 11.1? There is one bugfix related to crashes with DELETE statements. See here and here – a_horse_with_no_name Jan 4 at 11:57
2

As suggested by @a_horse_with_no_name, there is one bugfix related to crashes with DELETE statements here and here.

I waited for the problem to occur again. After I updated to version 11.1, the delete statement worked as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.