How to generate time in milliseconds in PostgreSQL stored procedure?
I understand one could use something like EXTRACT on CURRENT_TIMESTAMP when selecting rows


You don't need extract in order to get the data:

select *
from your_table
where timestamp_column &gt;= timestamp '2007-08-24 18:34:01';


will happily use an index on moment_in_time and will be very efficient - in fact there will be no difference in performance compared to

select *
from your_table
where milliseconds_column &gt;= 1187906400000;


Now look at the two statements and tell me for which one you immediately understand what the query will return.

If you do need those milliseconds in your application then you can always do something like this:

select some_col, 
       other_col,
       extract(epoch from timestamp_column) * 1000 as millis
from your_table
where timestamp_column &gt;= timestamp '2007-08-24 18:34:01';


And again, I'm almost 100% certain that you won't see any performance difference to:

select some_col, 
       other_col,
       milliseconds_column
from your_table
where  milliseconds_column &gt;= 1187906400000;


Plus: if you do store the milliseconds and you want to display the real date, you always need to apply an additional conversion:

select some_col, 
       other_col,
       to_char(timestamp_column, 'yyyy-mm-dd') as the_date
from your_table
where timestamp_column &gt;= timestamp '2007-08-24 18:34:01';


vs.

select some_col, 
       other_col,
       to_char(to_timestamp(milliseconds_column / 1000), 'yyyy-mm-dd) as the_date
from your_table
where  milliseconds_column &gt;= 1187906400000;


The second one one be slower than the first one, but it's more typing and makes the code harder to read. 

Edit:


   how to create a stored procedure that will convert this timestamp into the milliseconds I want (and the reverse function too)?


You don't need to write your own function:


convert timestamp to milliseconds: extract(epoch from current_timestamp) * 1000 + extract(milliseconds from current_timestamp)
convert milliseconds to timestamp: to_timestamp(1187980443530 / 1000)




So to wrap it up:

Go with a timestamp column and don't worry about performance.