How to implement an auto-increment column for a system catalog table?

Started by Xin Wangalmost 17 years ago3 messages
#1Xin Wang
andywx@gmail.com

Hi all,
I am using Postgres to build the prototype in a research project. I need
to create a new system catalog table with an auto-increment column. For
a ordinary table, CREATE SEQUENCE or a serial type can be used to
implement the auto-increment column, but it seems Postgres do not
support creating a sequence or serial types in system catalogs. How to
implement an auto-increment column for a catalog table?

Thanks in advance.

Wang

#2Simon Riggs
simon@2ndQuadrant.com
In reply to: Xin Wang (#1)
Re: How to implement an auto-increment column for a system catalog table?

On Mon, 2009-03-16 at 10:18 +0800, Xin Wang wrote:

I am using Postgres to build the prototype in a research project. I need
to create a new system catalog table with an auto-increment column. For
a ordinary table, CREATE SEQUENCE or a serial type can be used to
implement the auto-increment column, but it seems Postgres do not
support creating a sequence or serial types in system catalogs. How to
implement an auto-increment column for a catalog table?

Other catalog tables use a new oid for each new row/entry. Just do it in
the code for adding each new entry. Look at backend/catalog/heap.c
heap_create_with_catalog(), or something simpler like
backend/commands/dbcommands.c createdb().

--
Simon Riggs www.2ndQuadrant.com
PostgreSQL Training, Services and Support

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Simon Riggs (#2)
Re: How to implement an auto-increment column for a system catalog table?

Simon Riggs <simon@2ndQuadrant.com> writes:

On Mon, 2009-03-16 at 10:18 +0800, Xin Wang wrote:

I am using Postgres to build the prototype in a research project. I need
to create a new system catalog table with an auto-increment column.

Other catalog tables use a new oid for each new row/entry. Just do it in
the code for adding each new entry. Look at backend/catalog/heap.c
heap_create_with_catalog(), or something simpler like
backend/commands/dbcommands.c createdb().

Note that in fact the bottom level heap_insert code will handle
assigning a new OID for you. The only cases where calling code needs to
do that explicitly are when the OID has to be known before creation of
the new row for some reason (usually, circular OID references).

regards, tom lane