Re: Serial Vs Sequence

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Serial Vs Sequence
Date: 2011-02-01 07:13:40
Message-ID: AANLkTikeDYv0L31i7ykFzH8Bp4vYYq5c2VGLaUWHTL1H@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello

SEQUENCE isn't datatype. It is a database object. Simple object, that
can to generate unique integer numbers.

SERIAL is a fictive datatype. It can create a own SEQUENCE object and
can create a reference on this object.

pavel=# create sequence aaaa;
CREATE SEQUENCE
Time: 461.883 ms
pavel=# select nextval('aaaa');
nextval
─────────
1
(1 row)

Time: 78.413 ms
pavel=# select nextval('aaaa');
nextval
─────────
2
(1 row)

Time: 12.761 ms

pavel=# create table ggg(a serial);
NOTICE: CREATE TABLE will create implicit sequence "ggg_a_seq" for
serial column "ggg.a"
CREATE TABLE
Time: 91.866 ms
pavel=# \d ggg
Table "public.ggg"
Column │ Type │ Modifiers
────────┼─────────┼─────────────────────────────────────────────────
a │ integer │ not null default nextval('ggg_a_seq'::regclass)

pavel=#

regards

Pavel Stehule

2011/2/1 Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>:
> Dear all,
>
> I am not able to distinct these two datatypes ( Serial and Sequence ) in
> Postgresql which resembles like auto-increment in Mysql.
>
> Which one gets priority and When ?
>
> The only thing I am able to find is to use SERIAL because if a drop table
> occurs , still SEQUENCE memory is not freed, its garbage remains.
>
> However if we define a SERIAL column in a table , implicitly it makes a
> sequence.
>
> Could anyone Please describe me the difference and Which to Use When ?
>
>
> Thanks & Regards
>
> Adarsh Sharma
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nicos Panayides 2011-02-01 10:09:28 Re: Weird performance issue with custom function with a for loop.
Previous Message John R Pierce 2011-02-01 07:08:20 Re: Serial Vs Sequence