Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMHkc-0003P5-9L for pgsql-performance@arkaria.postgresql.org; Tue, 05 Dec 2017 18:15:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eMHkb-0001bS-NE for pgsql-performance@arkaria.postgresql.org; Tue, 05 Dec 2017 18:15:25 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eMHkb-0001bJ-Bl for pgsql-performance@lists.postgresql.org; Tue, 05 Dec 2017 18:15:25 +0000 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eMHkX-0007eT-Qt for pgsql-performance@lists.postgresql.org; Tue, 05 Dec 2017 18:15:23 +0000 Received: by mail-qt0-x242.google.com with SMTP id u42so2927772qte.7 for ; Tue, 05 Dec 2017 10:15:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding:content-language; bh=K19M24ouRRp1kErsbO4S01v9rdcuT5Ipny3MLyu2c+s=; b=Qrc+ZSx9YclqoAgIcm9og4R0mRcJbV7rqtnYjwcNsvuCVlsFAgByz/eT9BMRMpSqcX O46h4jvUMMtBuvhXqU2tQ12FPmsxSG1jG6MK0b0HbvUJh+74md/7+Fh33ak37UWXQGoG A4KpYsbF+gUgQCD+jVAsWQgsnKnXO8x8na8/QF60nEWRZIbS+qfDkDU8pLkjpwsYm5NB HRC+qtmtYJJQoZglrksUbErbrb/65l+0GELa1ibivhvY30klBTYDO+hujcZwBuJ/2kkw 5QlzUkfPikFYUXf9oJo92aZj7OPT3mfs0x1fRxCA20BJPkgNa2e6LolC01P5cik7+Gt9 q3eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=K19M24ouRRp1kErsbO4S01v9rdcuT5Ipny3MLyu2c+s=; b=bRzLqJjlPy4x+q4F23xcKCYSzoHq3+eTA34MW2XrYBjIBhCDDHlyW93MPZf7EIulu3 HDP7NE/wMBUsiL1tkGXgcoN0u48Vcng7CiJ4OvAri7/EJX66Wy/3p/KqsCLuxPw0wPPm lr/Ff3moF3wwcuCaEInyC8FZn/Wse8kkDb6Q9qSNOc5Q2qJanXbeNRUmmqTth59RCdUp wdV/1zVsQ1GQVZWi8CsAq5w2qYW3Oq+z7wH2UlEb2WP49qiT511jNIWInRuB0X/7zfK4 R/9BsjRWQjbqp0Y0ygJIxQjgpEOOxJuchuLQSTrMEWhRxwM+Wwr3g5cQvfZgFyB3rlZ8 Ohpw== X-Gm-Message-State: AKGB3mKyX7WUNM1nPhFmY6zIMA/wJM26j6xs8tWN8kl9iwHrXpQdB35r Pw36y3ronU4pugAS2FbQIQnYgQ== X-Google-Smtp-Source: AGs4zMaxJHEqmR+gHrfgv0e4RwIl9SAa/huOFVFZ3w0oJwVI6mm3/ohQ9yhHsUWfIwKOnEUnW3HIqw== X-Received: by 10.200.46.149 with SMTP id h21mr2556191qta.73.1512497720152; Tue, 05 Dec 2017 10:15:20 -0800 (PST) Received: from [192.168.1.6] ([181.213.88.188]) by smtp.gmail.com with ESMTPSA id f79sm451940qkh.62.2017.12.05.10.15.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Dec 2017 10:15:19 -0800 (PST) Subject: Re: Extremely slow DELETE with cascade foreign keys To: Alvaro Herrera Cc: Tom Lane , pgsql-performance@lists.postgresql.org References: <20171205174914.7q4x56gii3f626zg@alvherre.pgsql> From: Rodrigo Rosenfeld Rosas Message-ID: <833f6794-3a02-ca8a-5c95-3f1bcaf686b4@gmail.com> Date: Tue, 5 Dec 2017 16:15:14 -0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: <20171205174914.7q4x56gii3f626zg@alvherre.pgsql> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: pt-BR List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Em 05-12-2017 15:49, Alvaro Herrera escreveu: > Rodrigo Rosenfeld Rosas wrote: >> Em 05-12-2017 15:25, Tom Lane escreveu: >>>> Normally this is because you lack indexes on the referencing columns, so >>>> the query that scans the table to find the referencing rows is a >>>> seqscan. >>> Actually though ... the weird thing about this is that I'd expect to >>> see a separate line in the EXPLAIN output for time spent in the FK >>> trigger. Where'd that go? >> Yes, I was also hoping to get more insights through the EXPLAIN output :) > It normally does. Can you show \d of the table containing the FK? \d highlighted_text                                          Tabela "public.highlighted_text"     Coluna    |            Tipo             | Collation | Nullable |                   Default --------------+-----------------------------+-----------+----------+----------------------------------------------  id           | integer                     |           | not null | nextval('highlighted_text_id_seq'::regclass)  date_created | timestamp without time zone |           | not null |  last_updated | timestamp without time zone |           | not null |  reference_id | integer                     |           | not null |  highlighting | text                        |           | | Índices:     "highlighted_text_pkey" PRIMARY KEY, btree (id)     "highlighted_text_reference_id_idx" btree (reference_id) Restrições de chave estrangeira:     "fk_highlighted_text_reference" FOREIGN KEY (reference_id) REFERENCES "references"(id) ON DELETE CASCADE The highlighted_text_reference_id_idx was previously missing. begin; explain analyze delete from "references" where id=966539; rollback; BEGIN Tempo: 0,466 ms                                                              QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------  Delete on "references"  (cost=0.43..8.45 rows=1 width=6) (actual time=2.683..2.683 rows=0 loops=1)    ->  Index Scan using references_pkey on "references" (cost=0.43..8.45 rows=1 width=6) (actual time=2.609..2.612 rows=1 loops=1)          Index Cond: (id = 966539)  Planning time: 0.186 ms  Trigger for constraint fk_highlighted_text_reference: time=0.804 calls=1  Execution time: 3.551 ms (6 registros) Tempo: 4,791 ms ROLLBACK Tempo: 0,316 ms drop index highlighted_text_reference_id_idx; DROP INDEX Tempo: 35,938 ms begin; explain analyze delete from "references" where id=966539; rollback; BEGIN Tempo: 0,494 ms                                                              QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------  Delete on "references"  (cost=0.43..8.45 rows=1 width=6) (actual time=0.112..0.112 rows=0 loops=1)    ->  Index Scan using references_pkey on "references" (cost=0.43..8.45 rows=1 width=6) (actual time=0.071..0.074 rows=1 loops=1)          Index Cond: (id = 966539)  Planning time: 0.181 ms  Trigger for constraint fk_highlighted_text_reference: time=2513.816 calls=1  Execution time: 2513.992 ms (6 registros) Time: 2514,801 ms (00:02,515) ROLLBACK Tempo: 0,291 ms It displayed the spent on the trigger this time. How about deleting the field values? begin; explain analyze delete from field_values where transaction_id=2479; rollback; BEGIN Tempo: 0,461 ms QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------  Delete on field_values  (cost=0.43..364.98 rows=453 width=6) (actual time=4.732..4.732 rows=0 loops=1)    ->  Index Scan using index_field_values_on_transaction_id on field_values (cost=0.43..364.98 rows=453 width=6) (actual time=0.137..0.949 rows=624 loops=1)          Index Cond: (transaction_id = 2479)  Planning time: 0.210 ms  Trigger for constraint field_value_booleans_field_value_id_fkey on field_values: time=7.953 calls=624  Trigger for constraint field_value_currencies_field_value_id_fkey on field_values: time=5.548 calls=624  Trigger for constraint field_value_jurisdictions_field_value_id_fkey on field_values: time=6.376 calls=624  Trigger for constraint fk_field_value_date_range_field_value_id on field_values: time=5.735 calls=624  Trigger for constraint fk_field_value_dates_field_value_id on field_values: time=6.316 calls=624  Trigger for constraint fk_field_value_numerics_field_value_id on field_values: time=6.368 calls=624  Trigger for constraint fk_field_value_options_field_value_id on field_values: time=6.503 calls=624  Trigger for constraint fk_field_value_strings_field_value_id on field_values: time=6.794 calls=624  Trigger for constraint fk_field_value_time_spans_field_value_id on field_values: time=6.332 calls=624  Trigger for constraint fk_references_field_value_id on field_values: time=7.382 calls=624  Trigger for constraint fk_highlighted_text_reference on references: time=644994.047 calls=390  Execution time: 645065.326 ms (16 registros) Time: 645066,726 ms (10:45,067) ROLLBACK Tempo: 0,300 ms Yeah, for some reason, now I got the relevant trigger hints :) Go figure out why it didn't work the last time I tried before subscribing to this list :) Glad it's working now anyway :) Thanks, Rodrigo. > > alvherre=# begin; explain analyze delete from pk where a = 505; rollback; > BEGIN > Duración: 0,207 ms > QUERY PLAN > ────────────────────────────────────────────────────────────────────────────────────────────────────────────────── > Delete on pk (cost=0.00..8.27 rows=1 width=6) (actual time=0.023..0.023 rows=0 loops=1) > -> Index Scan using pk_pkey on pk (cost=0.00..8.27 rows=1 width=6) (actual time=0.012..0.013 rows=1 loops=1) > Index Cond: (a = 505) > Trigger for constraint fk_a_fkey: time=201.580 calls=1 > Total runtime: 201.625 ms > (5 filas) > > alvherre=# \d fk > Tabla «public.fk» > Columna │ Tipo │ Modificadores > ─────────┼─────────┼─────────────── > a │ integer │ > Restricciones de llave foránea: > "fk_a_fkey" FOREIGN KEY (a) REFERENCES pk(a) ON DELETE CASCADE > >