Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id BBF1F633B75 for ; Mon, 5 Oct 2009 19:23:40 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 67665-08 for ; Mon, 5 Oct 2009 22:23:24 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f182.google.com (mail-iw0-f182.google.com [209.85.223.182]) by mail.postgresql.org (Postfix) with ESMTP id E63B96334C1 for ; Mon, 5 Oct 2009 19:23:29 -0300 (ADT) Received: by iwn12 with SMTP id 12so1684496iwn.15 for ; Mon, 05 Oct 2009 15:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=wRSaJZLUSRW8Vndlod7OnGrBNAe/va376M+7QjD/dGk=; b=C/ScoP9X/bziyENYSynfPyNX/WMINbVnVHubcX/ZyEbTtY+NPG/tyK+x4LbqOQHz5O e8pYTR/nVk9xa8t3HBrUnTkkspBC8zXcDRreroNCMnXoIXx3c9/ClhCk+aK/6nwdjCX/ rLTmw1bh/vWEsEK63+Z/7OYL4KLj8kP9jegkA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=xpHruHMJnWQDpIKm6RzGgKzSRI//fyBqx5aQXYO3pvliQv5Zb/M04h5k8KNMfZPfGs 5WRZGiVXtJYWtpFMfhfyeHbpsHFiUcrI3MgtWPwFI5zDIoFftRum176NPS1ZJagcrIFl AFm0Gs4JtwIbcyExWY73RA+OcNcAr3X5USevY= MIME-Version: 1.0 Received: by 10.231.83.75 with SMTP id e11mr1091859ibl.11.1254781408287; Mon, 05 Oct 2009 15:23:28 -0700 (PDT) In-Reply-To: <4AC8F8BC.2050905@gmail.com> References: <444951.51198.qm@web33806.mail.mud.yahoo.com> <690707f60910041137l340d2873le86d8f3458188471@mail.gmail.com> <4AC8F8BC.2050905@gmail.com> Date: Mon, 5 Oct 2009 16:23:28 -0600 Message-ID: Subject: Re: reading last inserted record withoud any autoincrement field From: Scott Marlowe To: Rob Sargent Cc: Osvaldo Kussama , m_qoreishy@yahoo.com, pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.465 tagged_above=-10 required=5 tests=AWL=0.134, BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200910/23 X-Sequence-Number: 33222 On Sun, Oct 4, 2009 at 1:34 PM, Rob Sargent wrote: > Osvaldo Kussama wrote: >> >> 2009/10/4 mohammad qoreishy >> >>> >>> How can get last inserted record in a table without any autoincrement >>> filed? >>> I need to =A0frequently fetch the last inserted record. >>> If I must use the "Cursor" please explain your solution. >>> >>> >> >> >> RETURNING clause? >> http://www.postgresql.org/docs/current/interactive/sql-insert.html >> >> Osvaldo >> >> > > It took the OP to mean last insert as in randomly in the past, not as par= t > of current transaction. =A0My fear is OP's schema has no way of identifyi= ng > time-of-insert, nor a monotonically increasing record id and is hoping > postgres has a some internal value that will return the most recently > inserted record. Without a table definition it's hard to say. Given that he's mentioning cursors, I'm guessing he's talking about during this session / transaction. So returning would be best. Note that returning returns a SET of results, so that if your insert inserts 10 rows, you'll get back 10 rows from returning.