pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Auto generate number in Postgres-9.1.
6+ messages / 4 participants
[nested] [flat]

* Auto generate number in Postgres-9.1.
@ 2017-03-20 13:38 Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 13:43 ` Re: Auto generate number in Postgres-9.1. Josh Berkus <josh@berkus.org>
  0 siblings, 1 reply; 6+ messages in thread

From: Dinesh Chandra 12108 @ 2017-03-20 13:38 UTC (permalink / raw)
  To: pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>; +Cc: pgsql-performance

Dear expert,

I have to add one column  "ID" in postgres table which will generate Auto Increment<http://www.davidghedini.com/pg/entry/postgresql_auto_increment>ed number .

Example:
Suppose I have five records and if I insert 1 new record It should auto generate 6.
If I truncate the same table and then again insert rows should start with 1 in "ID" column.

Regards,
Dinesh Chandra
|Database administrator (Oracle/PostgreSQL)| Cyient Ltd. Noida.

________________________________

DISCLAIMER:

This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. Check all attachments for viruses before opening them. All views or opinions presented in this e-mail are those of the author and may not reflect the opinion of Cyient or those of our affiliates.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Auto generate number in Postgres-9.1.
  2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
@ 2017-03-20 13:43 ` Josh Berkus <josh@berkus.org>
  2017-03-20 13:48   ` Re: Auto generate number in Postgres-9.1. Andreas Karlsson <andreas@proxel.se>
  2017-03-20 13:50   ` Re: Auto generate number in Postgres-9.1. John Gorman <jgorman@eldocomp.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Josh Berkus @ 2017-03-20 13:43 UTC (permalink / raw)
  To: Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>; pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>; +Cc: pgsql-performance

Dinesh,

> I have to add one column  “ID” in postgres table which will generate
> Auto Increment
> <http://www.davidghedini.com/pg/entry/postgresql_auto_increment>ed number .
> 
>  
> 
> Example:
> 
> Suppose I have five records and if I insert 1 new record It should auto
> generate 6.

https://www.postgresql.org/docs/9.6/static/sql-createsequence.html
also SERIAL on this page:
https://www.postgresql.org/docs/9.6/static/datatype-numeric.html


> 
> If I truncate the same table and then again insert rows should start
> with 1 in “ID” column.

That's not how it works, normally.  I'd suggest adding an ON TRUNCATE
trigger to the table.


-- 
Josh Berkus
Containers & Databases Oh My!


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Auto generate number in Postgres-9.1.
  2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 13:43 ` Re: Auto generate number in Postgres-9.1. Josh Berkus <josh@berkus.org>
@ 2017-03-20 13:48   ` Andreas Karlsson <andreas@proxel.se>
  1 sibling, 0 replies; 6+ messages in thread

From: Andreas Karlsson @ 2017-03-20 13:48 UTC (permalink / raw)
  To: Josh Berkus <josh@berkus.org>; Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>; pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>; +Cc: pgsql-performance

On 03/20/2017 02:43 PM, Josh Berkus wrote:
>> If I truncate the same table and then again insert rows should start
>> with 1 in “ID” column.
>
> That's not how it works, normally.  I'd suggest adding an ON TRUNCATE
> trigger to the table.

Actually that may not be necessary as long as you make sure to use the 
RESTART IDENTITY option when running TRUNCATE. I would argue that is a 
cleaner solution than using triggers, if you can get away with it.

https://www.postgresql.org/docs/9.6/static/sql-truncate.html

Andreas


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Auto generate number in Postgres-9.1.
  2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 13:43 ` Re: Auto generate number in Postgres-9.1. Josh Berkus <josh@berkus.org>
@ 2017-03-20 13:50   ` John Gorman <jgorman@eldocomp.com>
  2017-03-20 14:08     ` Re: Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  1 sibling, 1 reply; 6+ messages in thread

From: John Gorman @ 2017-03-20 13:50 UTC (permalink / raw)
  To: pgsql-performance; pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>

Sequences are stored as a separate object in PostgreSQL.

Here in this example table and you can see that rec_id is a sequence number and that the object name is: whiteboards_rec_id_seq

mydb=> \d whiteboards

                                         Table "public.whiteboards"
    Column     |            Type             |                          Modifiers                           
---------------+-----------------------------+--------------------------------------------------------------
 rec_id        | integer                     | not null default nextval('whiteboards_rec_id_seq'::regclass)
 board_name    | character varying(24)       | not null
 board_content | text                        | not null
 updatets      | timestamp without time zone | default now()
Indexes:
    "whiteboards_pkey" PRIMARY KEY, btree (rec_id)

Now I can display the whiteboards_rec_id_seq object

mydb=> \dS whiteboards_rec_id_seq 
     Sequence "public.whiteboards_rec_id_seq"
    Column     |  Type   |         Value          
---------------+---------+------------------------
 sequence_name | name    | whiteboards_rec_id_seq
 last_value    | bigint  | 12
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 31
 is_cycled     | boolean | f
 is_called     | boolean | t

-----Original Message-----
From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Josh Berkus
Sent: Monday, March 20, 2017 6:43 AM
To: Dinesh Chandra 12108; pgsql-performance-owner@postgresql.org
Cc: pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Auto generate number in Postgres-9.1.

Dinesh,

> I have to add one column  "ID" in postgres table which will generate
> Auto Increment
> <http://www.davidghedini.com/pg/entry/postgresql_auto_increment>ed number .
> 
>  
> 
> Example:
> 
> Suppose I have five records and if I insert 1 new record It should auto
> generate 6.

https://www.postgresql.org/docs/9.6/static/sql-createsequence.html
also SERIAL on this page:
https://www.postgresql.org/docs/9.6/static/datatype-numeric.html


> 
> If I truncate the same table and then again insert rows should start
> with 1 in "ID" column.

That's not how it works, normally.  I'd suggest adding an ON TRUNCATE
trigger to the table.


-- 
Josh Berkus
Containers & Databases Oh My!


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Auto generate number in Postgres-9.1.
  2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 13:43 ` Re: Auto generate number in Postgres-9.1. Josh Berkus <josh@berkus.org>
  2017-03-20 13:50   ` Re: Auto generate number in Postgres-9.1. John Gorman <jgorman@eldocomp.com>
@ 2017-03-20 14:08     ` Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 15:40       ` Re: Auto generate number in Postgres-9.1. Andreas Karlsson <andreas@proxel.se>
  0 siblings, 1 reply; 6+ messages in thread

From: Dinesh Chandra 12108 @ 2017-03-20 14:08 UTC (permalink / raw)
  To: John Gorman <jgorman@eldocomp.com>; +Cc: pgsql-performance; pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>

Hi,

Thanks for your immediate response!!!!

Its working fine when we insert a new row.

But on deletion it's not automatically re-adjusting the id's.

Do I need to create trigger for this??

Regards,
Dinesh Chandra
|Database administrator (Oracle/PostgreSQL)| Cyient Ltd. Noida.


-----Original Message-----
From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of John Gorman
Sent: 20 March, 2017 7:20 PM
To: pgsql-performance@postgresql.org; pgsql-performance-owner@postgresql.org
Subject: Re: [PERFORM] Auto generate number in Postgres-9.1.

Sequences are stored as a separate object in PostgreSQL.

Here in this example table and you can see that rec_id is a sequence number and that the object name is: whiteboards_rec_id_seq

mydb=> \d whiteboards

                                         Table "public.whiteboards"
    Column     |            Type             |                          Modifiers
---------------+-----------------------------+--------------------------
---------------+-----------------------------+--------------------------
---------------+-----------------------------+----------
 rec_id        | integer                     | not null default nextval('whiteboards_rec_id_seq'::regclass)
 board_name    | character varying(24)       | not null
 board_content | text                        | not null
 updatets      | timestamp without time zone | default now()
Indexes:
    "whiteboards_pkey" PRIMARY KEY, btree (rec_id)

Now I can display the whiteboards_rec_id_seq object

mydb=> \dS whiteboards_rec_id_seq
     Sequence "public.whiteboards_rec_id_seq"
    Column     |  Type   |         Value
---------------+---------+------------------------
 sequence_name | name    | whiteboards_rec_id_seq
 last_value    | bigint  | 12
 start_value   | bigint  | 1
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 31
 is_cycled     | boolean | f
 is_called     | boolean | t

-----Original Message-----
From: pgsql-performance-owner@postgresql.org [mailto:pgsql-performance-owner@postgresql.org] On Behalf Of Josh Berkus
Sent: Monday, March 20, 2017 6:43 AM
To: Dinesh Chandra 12108; pgsql-performance-owner@postgresql.org
Cc: pgsql-performance@postgresql.org
Subject: Re: [PERFORM] Auto generate number in Postgres-9.1.

Dinesh,

> I have to add one column  "ID" in postgres table which will generate
> Auto Increment
> <http://www.davidghedini.com/pg/entry/postgresql_auto_increment>ed number .
>
>
>
> Example:
>
> Suppose I have five records and if I insert 1 new record It should
> auto generate 6.

https://www.postgresql.org/docs/9.6/static/sql-createsequence.html
also SERIAL on this page:
https://www.postgresql.org/docs/9.6/static/datatype-numeric.html


>
> If I truncate the same table and then again insert rows should start
> with 1 in "ID" column.

That's not how it works, normally.  I'd suggest adding an ON TRUNCATE trigger to the table.


--
Josh Berkus
Containers & Databases Oh My!


--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance


--
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance

________________________________

DISCLAIMER:

This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. Check all attachments for viruses before opening them. All views or opinions presented in this e-mail are those of the author and may not reflect the opinion of Cyient or those of our affiliates.


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Auto generate number in Postgres-9.1.
  2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
  2017-03-20 13:43 ` Re: Auto generate number in Postgres-9.1. Josh Berkus <josh@berkus.org>
  2017-03-20 13:50   ` Re: Auto generate number in Postgres-9.1. John Gorman <jgorman@eldocomp.com>
  2017-03-20 14:08     ` Re: Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
@ 2017-03-20 15:40       ` Andreas Karlsson <andreas@proxel.se>
  0 siblings, 0 replies; 6+ messages in thread

From: Andreas Karlsson @ 2017-03-20 15:40 UTC (permalink / raw)
  To: Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>; John Gorman <jgorman@eldocomp.com>; +Cc: pgsql-performance; pgsql-performance-owner@postgresql.org <pgsql-performance-owner@postgresql.org>

On 03/20/2017 03:08 PM, Dinesh Chandra 12108 wrote:
> But on deletion it's not automatically re-adjusting the id's.
>
> Do I need to create trigger for this??

It is possible to do but I advice against adjusting the IDs on DELETE 
due to to do so safely would require locking the entire table in the 
trigger.

Note that serial columns will also get holes on ROLLBACK. In general I 
think the right thing to do is accept that your ID columns can get a bit 
ugly.

For example:

CREATE TABLE t (id serial);

INSERT INTO t DEFAULT VALUES;

BEGIN;

INSERT INTO t DEFAULT VALUES;

ROLLBACK;

INSERT INTO t DEFAULT VALUES;

Gives us the following data in the table:

  id
----
   1
   3
(2 rows)

Andreas


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2017-03-20 15:40 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-03-20 13:38 Auto generate number in Postgres-9.1. Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
2017-03-20 13:43 ` Josh Berkus <josh@berkus.org>
2017-03-20 13:48   ` Andreas Karlsson <andreas@proxel.se>
2017-03-20 13:50   ` John Gorman <jgorman@eldocomp.com>
2017-03-20 14:08     ` Dinesh Chandra 12108 <Dinesh.Chandra@cyient.com>
2017-03-20 15:40       ` Andreas Karlsson <andreas@proxel.se>

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