Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 3F3FB1337B5C for ; Sat, 11 Dec 2010 06:18:19 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 81727-08 for ; Sat, 11 Dec 2010 10:18:13 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from smtpcorp.com (smtpcorp.com [67.192.120.76]) by mail.postgresql.org (Postfix) with ESMTP id AACCA1337B43 for ; Sat, 11 Dec 2010 06:18:11 -0400 (AST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=smtpcorp.com; s=main; h=Content-Transfer-Encoding:Content-Type:Mime-Version:References:Message-ID:Date:Subject:From:To; bh=JP1Ru3EI3qr3EC6PKkQi8gAkCyA+7LlyCwBQRwT3ygU=; b=FUW75QDXCGMFxH2/JVbvHCDQtwk6X00OoYlwUK4fawHwCdAZzJEXogy2Kn1Bclq2tIuHqXsf3XnSV8i5bhau5IbrP05DARNex60xxsO9OTNGunHACiuJpg+151Ugh/qQ; Received: from ip-118-90-129-0.xdsl.xnet.co.nz ([118.90.129.0] helo=gonzo) by smtpcorp.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.70) id 1PRMWn-0006QK-72; mid 144 for pgsql-sql@postgresql.org; Sat, 11 Dec 2010 10:18:09 +0000 Received: from news by gonzo with local (Exim 4.72) (envelope-from ) id 1PRMWc-0002FS-CB for pgsql-sql@postgresql.org; Sat, 11 Dec 2010 23:17:58 +1300 To: pgsql-sql@postgresql.org Path: not-for-mail From: Jasen Betts Newsgroups: gmane.comp.db.postgresql.sql Subject: Re: The best option to insert data with primary id Date: 11 Dec 2010 10:17:58 GMT Organization: Dis (not Dat) Organisation Lines: 48 Message-ID: References: NNTP-Posting-Host: localhost Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: reversiblemaps.ath.cx 1292062678 8644 127.0.0.1 (11 Dec 2010 10:17:58 GMT) X-Face: ?)Aw4rXwN5u0~$nqKj`xPz>xHCwgi^q+^?Ri*+R(&uv2=E1Q0Zk(>h!~o2ID@6{uf8s; a +M[5[U[QT7xFN%^gR"=tuJw%TXXR'Fp~W; (T"1(739R%m0Yyyv*gkGoPA.$b,D.w:z+<'"=-lVT?6 {T?=R^:W5g|E2#EhjKCa+nt":4b}dU7GYB*HBxn&Td$@f%.kl^:7X8rQWd[NTc"P"u6nkisze/Q; 8 "9Z{peQF,w)7UjV$c|RO/mQW/NMgWfr5*$-Z%u46"/00mx-,\R'fLPe.)^ User-Agent: slrn/pre1.0.0-18 (Linux) X-Smtpcorp-Track: 18143194.1.2266588 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_SORBS_DUL=0.001 X-Spam-Level: X-Archive-Number: 201012/54 X-Sequence-Number: 34890 On 2010-12-06, - wrote: > --0016364d26cf7fa4970496bf2224 > Content-Type: text/plain; charset=ISO-8859-1 > > Hi everyone, > I have a question about how best to insert and manipulate the table with > primary key id for better productivity. I need to insert data into the table > and get last id. > > 1. First option to take counter Postgres SEQUENCE: > INSERT INTO table (id, ...) VALUES ((SELECT nextval ('seq_table')), ...) > RETURNING (SELECT currval ('seq_table')) AS id > > Only thing I see, that if the row is not inserted, the counter is > incremented every time when called. Then they will have empty unused id in > the table and ID number will grow much. There will be many records. This id > int8 type declared with length 64. > Is there any option to occupy empty sequence records. I have to worry about > this? (assuming the default for id is nextval ('seq_table')) INSERT INTO table ( id, ...) VALUES ( default, ...) RETURNING id; or you can leave id and default out of the left half: INSERT INTO table ( ...) VALUES ( ...) RETURNING id; > 2. Second option is to take control of id and > INSERT INTO table (id, ...) VALUES ((SELECT MAX (id) +1 FROM table), ...) > RETURNING (SELECT MAX (id) +1 FROM table) AS id you run into concurrency issues that way. (two concurrent inserts could pick the same ID, one will fail with an error) > Quero your opinions on how best to insert data to have less maintenance and > better productivity with concurrent users. > Thank you very much. INSERT INTO table ( ...) VALUES ( ...) RETURNING id; Use the sequence, that's what they were designed for. Let id get the default value and pull that from the returning. you will get gaps in the serquence due to failed or cancelled transactions but there will probably not be many gaps. -- ⚂⚃ 100% natural