skip to Main Content

is there a way to add the same unix timestamp to all the rows in a select statement in PostgreSQL?

The purpose is to have the same value for all the rows – regards how long it runs.

When running the following query – if the query takes long (couple of seconds for millions of rows) then some rows will have a different timestamp.

select id, (extract(epoch from now())) as queryId from books;

Edit:
This is actually works

2

Answers


  1. Why don’t you generate the timestamp first and then select it for all the rows from the books table.

    -- Generate the timestamp
    WITH query_time AS (SELECT extract(epoch from now()) AS query_timestamp)
    
    -- Use it in your SELECT statement for all the rows
    SELECT id, query_timestamp FROM books, query_time
    
    
    Login or Signup to reply.
  2. Your SELECT works as is.

    now() is a stable function (volatility STABLE). Meaning, it’s outcome never changes within the same SELECT statement. See:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search