Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQdcT-0007xG-FU for pgsql-admin@arkaria.postgresql.org; Wed, 06 Jun 2018 18:57:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fQdcS-0004Nh-3H for pgsql-admin@arkaria.postgresql.org; Wed, 06 Jun 2018 18:57:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQdcR-0004NX-SD for pgsql-admin@lists.postgresql.org; Wed, 06 Jun 2018 18:57:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fQdcO-0001mG-Kn for pgsql-admin@postgresql.org; Wed, 06 Jun 2018 18:57:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w56Iv7jJ021524; Wed, 6 Jun 2018 14:57:07 -0400 From: Tom Lane To: Wells Oliver cc: nunks , Scott Ribe , pgsql-admin Subject: Re: [pgsql-admin] "Soft-hitting" the 1600 column limit In-reply-to: References: <3C61E858-5CA7-405D-B32A-B4C89D1AC62E@elevated-dev.com> Comments: In-reply-to Wells Oliver message dated "Wed, 06 Jun 2018 11:43:22 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21522.1528311427.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 06 Jun 2018 14:57:07 -0400 Message-ID: <21523.1528311427@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Wells Oliver writes: > Is there a pointer to some deeper explanation of this? It's news to me a= nd > kind of fascinating that dropped columns don't disappear. I did this stu= pid > test, which obviously failed: > mydb=3D# create table wells.foo (col1 text, col2 text); > CREATE TABLE > mydb=3D# insert into wells.foo values ('a','b'),('c','d'); > INSERT 0 2 > mydb=3D# alter table wells.foo drop column col2; > ALTER TABLE > mydb=3D# insert into wells.foo (col1,col2) values('a','b'); > ERROR: column "col2" of relation "foo" does not exist > LINE 1: insert into wells.foo (col1,col2) values('a','b'); > Just curious then, in what meaningful way do dropped columns persist, wh= at > are the reasons? I don't recall if it's documented explicitly in any user-facing places, but poking into the source code or the system catalogs will show you what happens: regression=3D# create table wells.foo (col1 text, col2 text); CREATE TABLE regression=3D# select attname, attnum, attisdropped from pg_attribute wher= e attrelid =3D 'wells.foo'::regclass and attnum > 0; attname | attnum | attisdropped = ---------+--------+-------------- col1 | 1 | f col2 | 2 | f (2 rows) regression=3D# alter table wells.foo drop column col2; ALTER TABLE regression=3D# select attname, attnum, attisdropped from pg_attribute wher= e attrelid =3D 'wells.foo'::regclass and attnum > 0; attname | attnum | attisdropped = ------------------------------+--------+-------------- col1 | 1 | f ........pg.dropped.2........ | 2 | t (2 rows) Most SQL operations ignore "attisdropped" entries in pg_attribute, which is why those seem to be hidden. But they're still valid as far as the physical representation of the table is concerned. As for why it's like this, the most obvious practical benefit is that it makes ALTER TABLE DROP COLUMN cheap: we just have to change that one entry in pg_attribute, not rewrite the entire table to physically remove the column from each table row. There are also some more-theoretical benefits involving having a stable identifier (a/k/a primary key) for a column. While that could be done in different ways, the way we do it is that attrelid (the table's OID) plus attnum is the unique identifier for a column. regards, tom lane