Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will multiple calls to `now()` in a single postgres query always give same result?

I want to insert one row in a table; e.g.:

INSERT INTO some_table VALUES (now(), now()); 

I want the date value in both the columns to be equal. Is the above query safe for this requirement? Or should I use other alternatives like sub-query/CTE:

INSERT INTO some_table (select t.now, t.now from (select now()) as t); 

In general, how do these functions get invoked in SQL internally? How is the sequence (left to right/right to left) of functions to be invoked decided? Is a given function just called once and the return value cached for a single query? Is it vendor-specific?

like image 229
Vikas Prasad Avatar asked Apr 20 '18 06:04

Vikas Prasad


People also ask

Can Postgres use multiple indexes in one query?

Fortunately, PostgreSQL has the ability to combine multiple indexes (including multiple uses of the same index) to handle cases that cannot be implemented by single index scans. The system can form AND and OR conditions across several index scans.

How many queries can Postgres handle per second?

In terms of business transactions, each business transactions is around 30-35 queries hitting the database. We are able to achieve ~ 150 business transactions with 4,500-5,000 QPS ( query per second ).

Is used in PostgreSQL to retrieve rows from more than one table?

Using Joins Joins are used to retrieve rows from two or more tables, based on a related column between those tables.


1 Answers

The documentation says about now():

now() is a traditional PostgreSQL equivalent to transaction_timestamp()

And about transaction_timestamp():

These SQL-standard functions all return values based on the start time of the current transaction

So within one SQL statement, now() will always return the same value.

like image 76
Andomar Avatar answered Sep 23 '22 15:09

Andomar