This project is archived and is in readonly mode.

#2692 ✓stale
Victor Nawothnig

[PATCH] Fix disable_referential_integrity in postgresql_adapter

Reported by Victor Nawothnig | May 21st, 2009 @ 10:13 PM | in 3.x

This is regarding the disable_referential_integrity method in the postgresql_adapter.rb.

The original approach did the following things:
1. Disable all triggers on all tables.
2. Yield the block.
3. Ensure that all triggers are enabled on all tables with a "begin; ensure; end" block

However, this can leave to several problems:
This disables triggers not related to RI, such as user or system triggers or constraints to ensure data safety/consistency. If a transaction relies on that, safety/consistency is no longer
ensured, the database can be left in an inconsistent state (outside RI).

The intended effect and that side effect would also apply to any concurrent transaction, which is even more dangerous, because
this effectively disables all constraints, triggers (and thus also RI) for concurrent transactions, not just the block.

Furthermore this would effectively enable triggers or constraints that a user has created, but disabled for some reason which might lead to unexpected results. (Even though they should be rather dropped in the first place, I don't think we want to do that).

And another hazardous case, if the database connection fails before the triggers are enabled again ALL triggers would stay disabled.
If we're loading a large bulk of data in that block it would increase the time window something like a disconnect, server or client crash or a simple blackout occurs, and all triggers would be left disabled).

I replaced the code with more a more direct and fine-grained approach,
removing all of those problems, with an optional argument to limit the disabling of RI and locks to specific table(names).

In addition I had to replace supports_disable_referential_integrity with a more detailed method, required to update pg_trigger directly.

Comments and changes to this ticket

  • Victor Nawothnig

    Victor Nawothnig May 23rd, 2009 @ 05:31 AM

    Added a new patch which handles the version checking
    in pg_trigger_enabled_type properly through postgresql_version.

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Dan Pickett

    Dan Pickett May 9th, 2010 @ 07:31 PM

    • Tag changed from patch to bugmash, patch
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:39 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:39 PM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket