TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Bug story: Sorting by timestamp

68 pointsby adam-pover 1 year ago

7 comments

aljarryover 1 year ago
Interesting note on the NOW() (or CURRENT_TIMESTAMP), they are equivalent to transaction_timestamp(), which means - start time of the current transaction.<p>So, if you&#x27;d insert multiple items in a single transaction, all of them would end up with the same value in the &quot;created&quot; column.
评论 #38760923 未加载
评论 #38762932 未加载
jupp0rover 1 year ago
I found it to be a generally useful rule to never &quot;ORDER BY created&quot; but instead &quot;ORDER BY created,id&quot; instead to achieve stable sorting.<p>I recently added some indices to a few tables to speed up a complicated query with lots of subqueries and joins and ran into many unit test failures because usage of the new indices changed the order in which items with the same &quot;created&quot; values were returned.
评论 #38774566 未加载
评论 #38760152 未加载
dotancohenover 1 year ago
Another common reason that time goes backwards is DST. The amount of DST-related bugs that I&#x27;ve fixed over the years amazes me, because every single developer has moved clocks back and forth twice a year for their entire lives, bar a few years in the beginning. And even this fine article mentions the time-has-gone-back possibility yet ignores DST.
评论 #38748867 未加载
评论 #38747823 未加载
评论 #38748464 未加载
评论 #38760558 未加载
评论 #38748607 未加载
评论 #38748894 未加载
WirelessGigabitover 1 year ago
Sidenote on showing (or not showing) timestamps.<p>Consider a page with 10 rows, and in each row you show the relative date.<p>2 issues with that:<p>1) If I see a whole page and I see 1 week ago the range is 7 days. If I see 1 year ago the range is 365 days. That is too much for most of the things. 2) If I am on a page without visual indication of sort order and I&#x27;m on a page that shows 10 entries with &#x27;1 year ago&#x27; I have no clue about the sort order.<p>I hate relative dates.
评论 #38748945 未加载
评论 #38748320 未加载
senderistaover 1 year ago
Timestamp resolution doesn&#x27;t just depend on the storage granularity of the timestamp type; it also depends on the actual accuracy of whatever system call is used to populate the timestamp.
评论 #38755034 未加载
neonsunsetover 1 year ago
This title is instant PTSD flashback - at one place I worked there was a system that would order events by their timestamps and multiple downstream systems would rely on that.<p>One day, I fixed an issue in the message producer that was causing the routine to take unreasonable amount of time and resources so that latency went from 1s to ~50ms.<p>Three hours later P1 is raised and the entire architecture had to be refactored. I still have the screenshot of the latency graph saved somewhere :)
vivzkestrelover 1 year ago
CREATE INDEX IF NOT EXISTS feed_items_pubdate_id ON public.feed_items USING btree (pubdate DESC NULLS FIRST, id DESC NULLS FIRST) TABLESPACE pg_default;<p>create an index on both the pubdate (timestamptz column with potential duplicates like your post mentions) and a uuid primary key column (id in my case) problem solved
评论 #38801031 未加载