From: alemagox@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #13034: Inconsistent attrelid field in pg_attribute table after adding columns to table.
Date: Sun, 12 Apr 2015 11:56:30 +0000
Message-ID: <20150412115630.26380.58037@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
The following bug has been logged on the website:
Bug reference: 13034
Logged by: Alejandro
Email address: alemagox@gmail.com
PostgreSQL version: 9.3.5
Operating system: Ubuntu 14.04 64 bits
Description:
Hello!
At my work we are trying to automatically generate some tables based on
already existing ones. For that reason, we need to check different system
tables such as 'pg_attribute', 'pg_class' and 'pg_namespace' so we can
retrieve the datatype of the existing columns.
This error is easily reproducible from scratch by simply connecting the
postgreSQL server as postgres user.
Firstly, I will show the query we are trying to make work. Simply run the
following queries to retrieve the type of 'id' field form a 'test' table
inside a 'test' database:
CREATE DATABASE test;
\c test
CREATE SCHEMA ab;
CREATE TABLE ab.test
(
id integer NOT NULL
);
SELECT format_type(a.atttypid, a.atttypmod)
FROM pg_attribute a
JOIN pg_class b ON (a.attrelid = b.relfilenode)
JOIN pg_namespace c ON (c.oid = b.relnamespace)
WHERE b.relname = 'test' AND c.nspname = 'ab' AND a.attname = 'id';
After this last SELECT we receive the following output as expected:
format_type
-------------
integer
The problem comes when we now try to add an extra column to 'test' table. We
do it with the following query:
ALTER TABLE ab.test ADD COLUMN time TIMESTAMP WITH TIME ZONE DEFAULT now();
Now, the query with 'format_type' returns empty set.
I have run also a couple of extra queries and the issue seems to be that
'attrelid' field inside 'pg_attribute' table does not get update after the
ALTER TABLE. For example, before running the ALTER TABLE we can run these
queries:
SELECT b.relfilenode FROM pg_class b, pg_namespace c WHERE c.oid =
b.relnamespace AND b.relname='test' AND c.nspname='ab';
Here we receive as output that 'refilenode' has a value of '1861610'. If we
now run the ALTER TABLE query and repeat the previous one we receive a value
of '1861614'. If we try now the query below, we will receive empty set:
SELECT a.* FROM pg_attribute a WHERE a.attrelid=1861614;
But if we repeat the previous query using the number 1861610, we will
receive all the fields of the table including the added 'time' column.
If we change the attrelid field manually we can rerun the 'format_type'
query and we will receive the integer output as expected:
UPDATE pg_attribute SET attrelid=1861614 WHERE attrelid=1861610;
SELECT format_type(a.atttypid, a.atttypmod)
FROM pg_attribute a
JOIN pg_class b ON (a.attrelid = b.relfilenode)
JOIN pg_namespace c ON (c.oid = b.relnamespace)
WHERE b.relname = 'test' AND c.nspname = 'ab' AND a.attname = 'id';
format_type
-------------
integer
As far as I understand, the field should be automatically updated as the
user should not be touching these system tables.
Thank you very much in advance for you attention.
Best regards,
Alejandro
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
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: alemagox@gmail.com
Subject: Re: BUG #13034: Inconsistent attrelid field in pg_attribute table after adding columns to table.
In-Reply-To: <20150412115630.26380.58037@wrigleys.postgresql.org>
References: <20150412115630.26380.58037@wrigleys.postgresql.org>
* 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