pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Marc Mamin <M.Mamin@intershop.de>
To: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: removing "serial" from table definitions.
Date: Thu, 24 Jun 2021 08:47:14 -0400
Message-ID: <e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com> (raw)
In-Reply-To: <feaeb83188034e9e88fcaeb33225b22c@intershop.de>
References: <feaeb83188034e9e88fcaeb33225b22c@intershop.de>

On 6/24/21 8:33 AM, Marc Mamin wrote:
> Hi,
> 
> Is there  a way to change a data type from serial to int?
> 
> I tried with :
> 
>    ALTER TABLE "admin".db_jobs ALTER COLUMN id TYPE int USING id::int;
> 
> But this seems not to change anything, as if Posgres woud consider the 
> statement as a no-op.

serial is not an actual data type -- it is essentially an integer with a 
default and an automatically created sequence. See:

https://www.postgresql.org/docs/13/datatype-numeric.html#DATATYPE-SERIAL

> My problem is that "serial" is not exported with pg_dump.
> 
> Creating a db from the dump will hence result into a different table 
> definition (which is equivalent tough)
> 
> We are trying a transfer/migration tool on Azure, that check the table 
> definitions between the source and target before starting the data 
> transfer, and it blocks on that difference.

 From the linked doc above:
--------------
The data types smallserial, serial and bigserial are not true types, but 
merely a notational convenience for creating unique identifier columns 
(similar to the AUTO_INCREMENT property supported by some other 
databases). In the current implementation, specifying:

CREATE TABLE tablename (
     colname SERIAL
);

is equivalent to specifying:

CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
     colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
---------------

I haven't checked, but I am guessing that creating the table using the 
former method results in a dump that looks like the latter? In that 
case, just define the table in the second way to begin with and they 
will match from the migration tools standpoint I should think.

Joe
-- 
Crunchy Data - http://crunchydata.com
PostgreSQL Support for Secure Enterprises
Consulting, Training, & Open Source Development





view thread (6+ messages)  latest in thread

Message-ID: <e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com>
Permalink:  ../e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com/
Also on:    postgresql.org/message-id/e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com

 · 

reply

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-general@postgresql.org
  Cc: mail@joeconway.com, M.Mamin@intershop.de
  Subject: Re: removing "serial" from table definitions.
  In-Reply-To: <e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com>
  References: <feaeb83188034e9e88fcaeb33225b22c@intershop.de> <e409f4c5-ff1d-dd6f-b34b-c2383a62f61b@joeconway.com>

* 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