Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9237883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:38:13+00:00 2026-06-18T07:38:13+00:00

I know that PostgreSQL tables that use a SERIAL primary key end up with

  • 0

I know that PostgreSQL tables that use a SERIAL primary key end up with an implicit index, sequence and constraint being created by PostgreSQL. The question is how to rename these implicit objects when the table is renamed. Below is my attempt at figuring this out with specific questions at the end.

Given a table such as:

CREATE TABLE foo (
    pkey SERIAL PRIMARY KEY,
    value INTEGER
);

Postgres outputs:

NOTICE: CREATE TABLE will create implicit sequence “foo_pkey_seq” for serial column “foo.pkey”
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index “foo_pkey” for table “foo”
Query returned successfully with no result in 52 ms.

pgAdmin III SQL pane shows the following DDL script for the table (decluttered):

CREATE TABLE foo (
  pkey serial NOT NULL,
  value integer,
  CONSTRAINT foo_pkey PRIMARY KEY (pkey )
);
ALTER TABLE foo OWNER TO postgres;

Now rename the table:

ALTER table foo RENAME TO bar;

Query returned successfully with no result in 17 ms.

pgAdmin III:

CREATE TABLE bar (
  pkey integer NOT NULL DEFAULT nextval('foo_pkey_seq'::regclass),
  value integer,
  CONSTRAINT foo_pkey PRIMARY KEY (pkey )
);
ALTER TABLE bar OWNER TO postgres;

Note the extra DEFAULT nextval('foo_pkey_seq'::regclass), this means that renaming the table does not rename the sequence for the primary keys but now we have this explicit nextval().

Now rename the sequence:

I want to keep the database naming consistent so I tried:

ALTER SEQUENCE foo_pkey_seq RENAME TO bar_pkey_seq;

Query returned successfully with no result in 17 ms.

pgAdmin III:

CREATE TABLE bar (
  pkey serial NOT NULL,
  value integer,
  CONSTRAINT foo_pkey PRIMARY KEY (pkey )
);
ALTER TABLE bar OWNER TO postgres;

The DEFAULT nextval('foo_pkey_seq'::regclass), is gone.

QUESTIONS

  1. Why did the DEFAULT nextval('foo_pkey_seq'::regclass) statement appear and disappear?
  2. Is there a way to rename the table and have the primary key sequence renamed at the same time?
  3. Is it safe to rename the table then sequence while clients are connected to the database, are there any concurrency issues?
  4. How does postgres know which sequence to use? Is there a database trigger being used internally? Is there anything else to rename other than the table and the sequence?
  5. What about the implicit index created by a primary key? Should that be renamed? If so, how can that be done?
  6. What about the constraint name above? It is still foo_pkey. How is a constraint renamed?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:38:14+00:00Added an answer on June 18, 2026 at 7:38 am

    serial is not an actual data type. The manual states:

    The data types smallserial, serial and bigserial are not true types,
    but merely a notational convenience for creating unique identifier columns

    The pseudo data type is resolved doing all of this:

    • create a sequence named tablename_colname_seq

    • create the column with type integer (or int2 / int8 respectively for smallserial / bigserial)

    • make the column NOT NULL DEFAULT nextval('tablename_colname_seq')

    • make the column own the sequence, so that it gets dropped with it automatically

    The system does not know whether you did all this by hand or by way of the pseudo data type serial. pgAdmin checks on the listed features and if all are met, the reverse engineered DDL script is simplified with the matching serial type. If one of the features is not met, this simplification does not take place. That is something pgAdmin does. For the underlying catalog tables it’s all the same. There is no serial type as such.

    There is no way to automatically rename owned sequences. You can run:

    ALTER SEQUENCE ... RENAME TO ...
    

    like you did. The system itself doesn’t care about the name. The column DEFAULT stores an OID ('foo_pkey_seq'::regclass), you can change the name of the sequence without breaking that – the OID stays the same. The same goes for foreign keys and similar references inside the database.

    The implicit index for the primary key is bound to the name of the PK constraint, which will not change if you change the name of the table. In Postgres 9.2 or later you can use

    ALTER TABLE ... RENAME CONSTRAINT ..
    

    to rectify that, too.

    There can also be indexes named in reference to the table name. Similar procedure:

    ALTER INDEX .. RENAME TO  ..
    

    You can have all kinds of informal references to the table name. The system cannot forcibly rename objects that can be named anything you like. And it doesn’t care.

    Of course you don’t want to invalidate SQL code that references those names. Obviously, you don’t want to change names while application logic references them. Normally this wouldn’t be a problem for names of indexes, sequences or constraints, since those are not normally referenced by name.

    Postgres also acquires a lock on objects before renaming them. So if there are concurrent transaction open that have any kind of lock on objects in question, your RENAME operation is stalled until those transactions commit or roll back.

    System catalogs and OIDs

    The database schema is stored in tables of the system catalog in the system schema pg_catalog. All details in the manual here. If you don’t know exactly what you are doing, you shouldn’t be messing with those tables at all. One false move and you can break your database. Use the DDL commands Postgres provides.

    For some of the most important tables Postgres provides object identifier types and type casts to get the name for the OID and vice versa quickly. Like:

    SELECT 'foo_pkey_seq'::regclass
    

    If the schema name is in the search_path and the table name is unique, that gives you the same as:

    SELECT oid FROM pg_class WHERE relname = 'foo_pkey_seq';
    

    The primary key of most catalog tables is oid and internally, most references use OIDs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on PostgreSQL 8.4 in read committed mode. I know that for each
I know that when I use range([start], stop[, step]) or slice([start], stop[, step]) ,
I have a simple SQLite table called message: sequence INTEGER PRIMARY KEY type TEXT
I have a PostgreSQL 9.1 database with 100 or so tables that were loaded
I'm using PostgreSQL 9.1. My database is structured so that there is actual tables
I'm working on PostgeSQL with PHP and I know that PosrgeSQL allow columns of
I have a postgres database that I want to know some quick stats. For
I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that if port 443 is open that means the remote host supports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.