From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Paquier <michael.paquier@gmail.com>
Cc: Andres Freund <andres@2ndquadrant.com>
Cc: cg@osss.net
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #11638: Transaction safety fails when constraints are dropped and analyze is done
Date: Tue, 28 Oct 2014 19:28:05 -0400
Message-ID: <20972.1414538885@sss.pgh.pa.us> (raw)
In-Reply-To: <20648.1413943949@sss.pgh.pa.us>
References: <20141010181433.25463.5856@wrigleys.postgresql.org>
<20141010191401.GA18020@awork2.anarazel.de>
<CAB7nPqRsXPPNaFXDHZPbBESYR_EJPvQP6eKmhp-p13mJDpGgXw@mail.gmail.com>
<20141015051254.GC7242@alap3.anarazel.de>
<CAB7nPqTsZUnDNYgLZ8OO5T4rQY+=XSe7uomiv=OVQnJvVzzHJg@mail.gmail.com>
<20141015061815.GF7242@alap3.anarazel.de>
<CAB7nPqR5mpKM6HWh1jE9crWa-8mjri_J9exD3AyYuEEnW0cTQg@mail.gmail.com>
<20648.1413943949@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
I wrote:
> I think that a better answer is to continue to do this update
> nontransactionally, but to not let the code clear relhasindex etc
> if we're inside a transaction block. It is certainly safe to put
> off clearing those flags if we're not sure that we're seeing a
> committed state of the table's schema.
Attached is a proposed patch to do it that way. I borrowed Michael's
test case.
> An interesting question is whether it is ever possible for this function
> to be told to *set* relhasindex when it was clear (or likewise for the
> other flags). Offhand I would say that that should never happen, because
> certainly neither VACUUM nor ANALYZE should be creating indexes etc.
> Should we make it throw an error if that happens, or just go ahead and
> apply the update, assuming that it's correcting somehow-corrupted data?
After looking more closely, the existing precedent for the other similar
fields is just to make sure the code only clears the flags, never sets
them, so I think relhasindex should be treated the same.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:fix-bug-11638.patch (text/x-diff, 7.6K) downloaddiff --git a/src/backend/commands/vacuum.c b/src/backend/commands/vacuum.cindex e5fefa3..cfa4a1d 100644
*** a/src/backend/commands/vacuum.c
--- b/src/backend/commands/vacuum.c
*************** vac_estimate_reltuples(Relation relation
*** 648,670 ****
*
* We violate transaction semantics here by overwriting the rel's
* existing pg_class tuple with the new values. This is reasonably
! * safe since the new values are correct whether or not this transaction
! * commits. The reason for this is that if we updated these tuples in
! * the usual way, vacuuming pg_class itself wouldn't work very well ---
! * by the time we got done with a vacuum cycle, most of the tuples in
! * pg_class would've been obsoleted. Of course, this only works for
! * fixed-size never-null columns, but these are.
! *
! * Note another assumption: that two VACUUMs/ANALYZEs on a table can't
! * run in parallel, nor can VACUUM/ANALYZE run in parallel with a
! * schema alteration such as adding an index, rule, or trigger. Otherwise
! * our updates of relhasindex etc might overwrite uncommitted updates.
*
* Another reason for doing it this way is that when we are in a lazy
! * VACUUM and have PROC_IN_VACUUM set, we mustn't do any updates ---
! * somebody vacuuming pg_class might think they could delete a tuple
* marked with xmin = our xid.
*
* This routine is shared by VACUUM and ANALYZE.
*/
void
--- 648,677 ----
*
* We violate transaction semantics here by overwriting the rel's
* existing pg_class tuple with the new values. This is reasonably
! * safe as long as we're sure that the new values are correct whether or
! * not this transaction commits. The reason for doing this is that if
! * we updated these tuples in the usual way, vacuuming pg_class itself
! * wouldn't work very well --- by the time we got done with a vacuum
! * cycle, most of the tuples in pg_class would've been obsoleted. Of
! * course, this only works for fixed-size not-null columns, but these are.
*
* Another reason for doing it this way is that when we are in a lazy
! * VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
! * Somebody vacuuming pg_class might think they could delete a tuple
* marked with xmin = our xid.
*
+ * In addition to fundamentally nontransactional statistics such as+ * relpages and relallvisible, we try to maintain certain lazily-updated+ * DDL flags such as relhasindex, by clearing them if no longer correct.+ * It's safe to do this in VACUUM, which can't run in parallel with+ * CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.+ * However, it's *not* safe to do it in an ANALYZE that's within a+ * transaction block, because the current transaction might've dropped+ * the last index; we'd think relhasindex should be cleared, but if the+ * transaction later rolls back this would be wrong. So we refrain from+ * updating the DDL flags if we're inside a transaction block. This is+ * OK since postponing the flag maintenance is always allowable.+ *
* This routine is shared by VACUUM and ANALYZE.
*/
void
*************** vac_update_relstats(Relation relation,
*** 689,695 ****
relid);
pgcform = (Form_pg_class) GETSTRUCT(ctup);
! /* Apply required updates, if any, to copied tuple */
dirty = false;
if (pgcform->relpages != (int32) num_pages)
--- 696,702 ----
relid);
pgcform = (Form_pg_class) GETSTRUCT(ctup);
! /* Apply statistical updates, if any, to copied tuple */
dirty = false;
if (pgcform->relpages != (int32) num_pages)
*************** vac_update_relstats(Relation relation,
*** 707,738 ****
pgcform->relallvisible = (int32) num_all_visible_pages;
dirty = true;
}
- if (pgcform->relhasindex != hasindex)- {- pgcform->relhasindex = hasindex;- dirty = true;- }
! /*
! * If we have discovered that there are no indexes, then there's no
! * primary key either. This could be done more thoroughly...
! */
! if (pgcform->relhaspkey && !hasindex)
! {
! pgcform->relhaspkey = false;
! dirty = true;
! }
! /* We also clear relhasrules and relhastriggers if needed */
! if (pgcform->relhasrules && relation->rd_rules == NULL)
! {
! pgcform->relhasrules = false;
! dirty = true;
! }
! if (pgcform->relhastriggers && relation->trigdesc == NULL)
{
! pgcform->relhastriggers = false;
! dirty = true;
}
/*
--- 714,754 ----
pgcform->relallvisible = (int32) num_all_visible_pages;
dirty = true;
}
! /* Apply DDL updates, but not inside a transaction block (see above) */
! if (!IsTransactionBlock())
{
! /*
! * If we didn't find any indexes, reset relhasindex.
! */
! if (pgcform->relhasindex && !hasindex)
! {
! pgcform->relhasindex = false;
! dirty = true;
! }
!
! /*
! * If we have discovered that there are no indexes, then there's no
! * primary key either. This could be done more thoroughly...
! */
! if (pgcform->relhaspkey && !hasindex)
! {
! pgcform->relhaspkey = false;
! dirty = true;
! }
!
! /* We also clear relhasrules and relhastriggers if needed */
! if (pgcform->relhasrules && relation->rd_rules == NULL)
! {
! pgcform->relhasrules = false;
! dirty = true;
! }
! if (pgcform->relhastriggers && relation->trigdesc == NULL)
! {
! pgcform->relhastriggers = false;
! dirty = true;
! }
}
/*
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.outindex 10f45f2..f5ae511 100644
*** a/src/test/regress/expected/alter_table.out
--- b/src/test/regress/expected/alter_table.out
*************** ALTER TABLE logged1 SET UNLOGGED; -- sil
*** 2517,2519 ****
--- 2517,2539 ----
DROP TABLE logged3;
DROP TABLE logged2;
DROP TABLE logged1;
+ -- check presence of foreign key with transactional ANALYZE+ CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);+ CREATE TABLE check_fk_presence_2 (id int REFERENCES check_fk_presence_1, t text);+ BEGIN;+ ALTER TABLE check_fk_presence_2 DROP CONSTRAINT check_fk_presence_2_id_fkey;+ COPY check_fk_presence_2 FROM stdin;+ ANALYZE check_fk_presence_2;+ ALTER TABLE check_fk_presence_2 ADD FOREIGN KEY (table1_fk) REFERENCES check_fk_presence_1 (id);+ ERROR: column "table1_fk" referenced in foreign key constraint does not exist+ ROLLBACK;+ \d check_fk_presence_2+ Table "public.check_fk_presence_2"+ Column | Type | Modifiers + --------+---------+-----------+ id | integer | + t | text | + Foreign-key constraints:+ "check_fk_presence_2_id_fkey" FOREIGN KEY (id) REFERENCES check_fk_presence_1(id)+ + DROP TABLE check_fk_presence_1, check_fk_presence_2;diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sqlindex 12fd7c2..05d8226 100644
*** a/src/test/regress/sql/alter_table.sql
--- b/src/test/regress/sql/alter_table.sql
*************** ALTER TABLE logged1 SET UNLOGGED; -- sil
*** 1676,1678 ****
--- 1676,1691 ----
DROP TABLE logged3;
DROP TABLE logged2;
DROP TABLE logged1;
+ -- check presence of foreign key with transactional ANALYZE+ CREATE TABLE check_fk_presence_1 (id int PRIMARY KEY, t text);+ CREATE TABLE check_fk_presence_2 (id int REFERENCES check_fk_presence_1, t text);+ BEGIN;+ ALTER TABLE check_fk_presence_2 DROP CONSTRAINT check_fk_presence_2_id_fkey;+ COPY check_fk_presence_2 FROM stdin;+ 1 test+ \.+ ANALYZE check_fk_presence_2;+ ALTER TABLE check_fk_presence_2 ADD FOREIGN KEY (table1_fk) REFERENCES check_fk_presence_1 (id);+ ROLLBACK;+ \d check_fk_presence_2+ DROP TABLE check_fk_presence_1, check_fk_presence_2;
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, michael.paquier@gmail.com, andres@2ndquadrant.com, cg@osss.net
Subject: Re: BUG #11638: Transaction safety fails when constraints are dropped and analyze is done
In-Reply-To: <20972.1414538885@sss.pgh.pa.us>
References: <20141010181433.25463.5856@wrigleys.postgresql.org> <20141010191401.GA18020@awork2.anarazel.de> <CAB7nPqRsXPPNaFXDHZPbBESYR_EJPvQP6eKmhp-p13mJDpGgXw@mail.gmail.com> <20141015051254.GC7242@alap3.anarazel.de> <CAB7nPqTsZUnDNYgLZ8OO5T4rQY+=XSe7uomiv=OVQnJvVzzHJg@mail.gmail.com> <20141015061815.GF7242@alap3.anarazel.de> <CAB7nPqR5mpKM6HWh1jE9crWa-8mjri_J9exD3AyYuEEnW0cTQg@mail.gmail.com> <20648.1413943949@sss.pgh.pa.us> <20972.1414538885@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox