Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:35:32+00:00 2026-06-04T16:35:32+00:00

I am building a blog and i have a problem. There should be some

  • 0

I am building a blog and i have a problem. There should be some sticky posts. All i want is to get first the sticky posts and then the rest.

One query that works is

select * from 

(
(select *,true as st from blog where "stickyUntil" > current_timestamp) 
UNION  all
(SELECT *,false as st from blog where "stickyUntil" < current_timestamp  or "stickyUntil" is null  ) 
) q 


order by st desc, "stickyUntil" DESC ,publish DESC OFFEST x LIMIT z

Another simpler query that workz is

select * from blog order by case when "stickyUntil" > current_timestamp then "stickyUntil" end desc nulls last, publish desc;

But this would force 200.000 rows to be sorted in memory not very fast..

Is there a way to optimize it ??

Would it be better to use two separate queries ?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:35:34+00:00Added an answer on June 4, 2026 at 4:35 pm

    I would go with a CASE statement to avoid two passes of the table:

    select *, 
       CASE 
       WHEN "stickyUntil" > current_timestamp THEN true
       ELSE false
       END as st
    FROM blog
    ORDER BY st DESC ,publish DESC OFFEST x LIMIT z
    

    Postgres supports creating an INDEX on a computed field which would help here but there is a restriction:

    All functions and operators used in an index definition must be “immutable”,
    that is, their results must depend only on their arguments and never on any
    outside influence (such as the contents of another table or the current time).

    So you cannot index the st calculation. If the calculation of st does not need to be too accurate another option is to add the st field to the table:

    ALTER TABLE blog
    ADD COLUMN st boolean default true
    

    INDEX st as a regular column:

    CREATE INDEX sti ON blog(st)
    

    And periodically run:

    UPDATE blog
    SET st = false
    WHERE st = true
    AND "stickyUntil" < current_timestamp
    

    but having a polling process doing an update is much less attractive than a straight up query. This would only make sense if your query is really slow or there are a lot of reads of the blog table.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a blog using RoR. I have the index.html.erb page for the posts
I'm building a small website with Symfony2 and Doctrine2. There are blog posts, events
Hey guys. I'm building a simple blog and have my data returning from my
I’m building a blog engine in C# Webforms and I have the following code
I'm building an app for a blog site. I have a UINavigationController with a
I'm building a blog that should parse bbcode tags like this: Input: <youtube=http://www.youtube.com/watch?v=VIDEO_ID&feature=channel> Output:
So I'm building a blog engine which has /articles/then-the-article-permalink as it's URL structure. I
I'm building an authors page for my Wordpress blog that lists all the current
I'm building my first website with rails,it consists of a blog, a few static
I'm building a simple code-first MVC 3 blog like application. My model has three

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.