Re: [HACKERS] ALTER TABLE parent SET WITHOUT OIDS and the oid column - Mailing list pgsql-hackers

From Ashutosh Bapat
Subject Re: [HACKERS] ALTER TABLE parent SET WITHOUT OIDS and the oid column
Date
Msg-id CAFjFpRekA26arLk9_nZ6EO8CM7Hq0eqxno9wKAkFnWTzL4wLiQ@mail.gmail.com
Whole thread Raw
In response to [HACKERS] ALTER TABLE parent SET WITHOUT OIDS and the oid column  (Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>)
Responses Re: [HACKERS] ALTER TABLE parent SET WITHOUT OIDS and the oid column  (Amit Langote <Langote_Amit_f8@lab.ntt.co.jp>)
List pgsql-hackers
On Mon, Dec 26, 2016 at 3:36 PM, Amit Langote
<Langote_Amit_f8@lab.ntt.co.jp> wrote:
> I suspect the following is a bug:
>
> create table foo (a int) with oids;
> CREATE TABLE
> create table bar (a int);
> CREATE TABLE
> alter table bar inherit foo;
> ERROR:  table "bar" without OIDs cannot inherit from table "foo" with OIDs
>
> alter table bar set with oids;
> ALTER TABLE
> alter table bar inherit foo;
> ALTER TABLE
>
> alter table foo set without oids;
> ERROR:  relation 16551 has non-inherited attribute "oid"
>
> Because:
>
> select attinhcount from pg_attribute where attrelid = 'bar'::regclass and
> attname = 'oid';
>  attinhcount
> -------------
>            0
> (1 row)
>
> Which also means "oid" can be safely dropped from bar breaking the
> invariant that if the parent table has oid column, its child tables must too:
>
> alter table bar drop oid;  -- or, alter table bar set without oids;
> ALTER TABLE
>
> Attached patches modifies MergeAttributesIntoExisting() such that we
> increment attinhcount not only for user attributes, but also for the oid
> system column if one exists.
>
> Thoughts?

Yes, if a child inherits from a parent with OID, child gets OID column
and its inhcount is set to 1.
postgres=# create table foo (a int) with oids;
CREATE TABLE
postgres=# create table bar() inherits (foo);
CREATE TABLE
postgres=# \d+ foo                                   Table "public.foo"Column |  Type   | Collation | Nullable |
Default| Storage | Stats
 
target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------a      | integer |
|         |         | plain   |              |
 
Child tables: bar
Has OIDs: yes

postgres=# \d+ bar                                   Table "public.bar"Column |  Type   | Collation | Nullable |
Default| Storage | Stats
 
target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------a      | integer |
|         |         | plain   |              |
 
Inherits: foo
Has OIDs: yes

postgres=# select attname, attinhcount from pg_attribute where
attrelid = 'bar'::regclass;attname  | attinhcount
----------+-------------tableoid |           0cmax     |           0xmax     |           0cmin     |           0xmin
|           0oid      |           1ctid     |           0a        |           1
 
(8 rows)

So, I think your patch is on the right track.

We allow creating user attribute with name "oid" so you do not want to
search system attribute oid by name. Instead search by attribute id
ObjectIdAttributeNumber.

-- 
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company



pgsql-hackers by date:

Previous
From: Pavan Deolasee
Date:
Subject: Re: [HACKERS] Patch: Write Amplification Reduction Method (WARM)
Next
From: Anastasia Lubennikova
Date:
Subject: Re: [HACKERS] Vacuum: allow usage of more than 1GB of work mem