Re: Getting The Last Entry

From: Lew <noone(at)lewscanon(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Getting The Last Entry
Date: 2010-09-12 14:44:16
Message-ID: i6iovt$db7$1@news.albasani.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

(Top-posting corrected.)

Thom Brown wrote:
> On 10 September 2010 16:31, Francisco Leovey <fleovey(at)yahoo(dot)com
> <mailto:fleovey(at)yahoo(dot)com>> wrote:
> >
> > if by "last" you mean the last row inserted, use
> >
> > select *,oid from table order by oid DESC limit 1
> >
> > (asuming you use OID's)
>
> (copying in list with your response)

Luiz Eduardo Cantanhede Neri wrote:
>> Once I suggested to use oid, but some guys here said was a poor design.

It is, but you don't just throw the baby out with the bathwater. Use the
"ORDER BY ... DESC LIMIT 1" trick with a column that is not bad design, one
that matches your (as yet unrevealed) definition of "last record", as Thom
suggested.

Within themselves tables have no notion of order or "last" vs. "first" other
than the OID. The reason that OID use is bad is its role as an internal,
DB-only mechanism - it is not connected to the data model and therefore
creates risks that real columns do not.

"It's bad design to use OID" can be countered with, "Yeah, I know, I'm using
it anyway" if you elucidate and ameliorate the risks involved, and show why
the better alternatives (use of a data-model-relevant column to order your
data) cannot be used this time.

But your colleagues do have a point - use the better alternatives unless you
really, really, really, really cannot and are able to fully explain and
justify why not.

Don't let good rules ("Avoid use of the OID") become cargo-cult superstitious
dogma.

--
Lew

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Machiel Richards 2010-09-13 09:50:09 Database integrity ?
Previous Message Sim Zacks 2010-09-12 06:50:42 Re: Change view with dependencies function: PG 8.3