Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 0250C1337BD0 for ; Thu, 31 Mar 2011 07:07:03 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 67171-09 for ; Thu, 31 Mar 2011 10:06:55 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-yi0-f46.google.com (mail-yi0-f46.google.com [209.85.218.46]) by mail.postgresql.org (Postfix) with ESMTP id EFEDC1337999 for ; Thu, 31 Mar 2011 07:06:54 -0300 (ADT) Received: by yia27 with SMTP id 27so904539yia.19 for ; Thu, 31 Mar 2011 03:06:54 -0700 (PDT) Received: by 10.151.134.30 with SMTP id l30mr2657868ybn.136.1301566012966; Thu, 31 Mar 2011 03:06:52 -0700 (PDT) Received: from tornado.gateway.2wire.net (adsl-242-203-69.rmo.bellsouth.net [74.242.203.69]) by mx.google.com with ESMTPS id b3sm1039305ana.50.2011.03.31.03.06.51 (version=SSLv3 cipher=OTHER); Thu, 31 Mar 2011 03:06:51 -0700 (PDT) Date: Thu, 31 Mar 2011 06:06:49 -0400 From: Noah Misch To: Robert Haas Cc: Naoya Anzai , pgsql-hackers@postgresql.org Subject: Re: BUG #5856: pg_attribute.attinhcount is not correct. Message-ID: <20110331100649.GA7286@tornado.gateway.2wire.net> References: <201101311142.p0VBgVGd019556@wwwmaster.postgresql.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.18 (2008-05-17) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201103/1876 X-Sequence-Number: 185126 [moving to pgsql-hackers] On Thu, Feb 03, 2011 at 11:24:42AM -0500, Robert Haas wrote: > On Mon, Jan 31, 2011 at 6:42 AM, Naoya Anzai > wrote: > > In PostgreSQL8.4.5, I found that the catalog pg_attribute.attinhcount is not > > correct. > > > > I executed the following queries. > > > > -------------------------------------------------------------------------- > > create table "3_grandchild"(); > > create table "2_child"(); > > create table "1_parent"(); > > > > alter table "3_grandchild" inherit "2_child"; > > alter table "2_child" inherit "1_parent"; > > > > alter table "3_grandchild" add column c1 text; > > alter table "2_child" add column c1 text; > > alter table "1_parent" add column c1 text; > > > > select c.relname,a.attname,a.attinhcount from pg_attribute a,pg_class c > > where a.attrelid=c.oid > > and relname in ('1_parent','2_child','3_grandchild') > > and attname not in('xmax','xmin','cmin','cmax','tableoid','ctid') > > order by relname; > > > > ? ?relname ? ?| attname | attinhcount > > ?--------------+---------+------------- > > ?1_parent ? ? | c1 ? ? ?| ? ? ? ? ? 0 > > ?2_child ? ? ?| c1 ? ? ?| ? ? ? ? ? 1 > > ?3_grandchild | c1 ? ? ?| ? ? ? ? ? 2 > > ?(3 rows) > > -------------------------------------------------------------------------- > > > > "3_grandchild"'s attinhcount should be 1. > > I think this is a manifestation the same problem mentioned here: > > http://git.postgresql.org/gitweb?p=postgresql.git;a=commit;h=31b6fc06d83c6de3644c8f2921eb7de0eb92fac3 > > I believe this requires some refactoring to fix. It would be good to do that. The best way I can see is to make ATExecAddColumn more like ATExecDropColumn, ATAddCheckConstraint, and ATExecDropConstraint. Namely, recurse at Exec-time rather than Prep-time, and cease recursing when we satisfy the ADD COLUMN with a merge. Did you have something else in mind? Incidentally, when we satisfy an ADD COLUMN with a merge, we do not check or update attnotnull: create table parent(); create table child(c1 text) inherits (parent); alter table parent add column c1 text not null; \d child We could either update attnotnull (and schedule a phase-3 scan of the table) or throw an error. For ALTER TABLE ... INHERIT, we throw the error. For CREATE TABLE ... INHERITS, we add the NOT NULL (and no scan is needed). I'd weakly lean toward throwing the error. Opinions? nm