How to migrate "as of timestamp" query in PostgreSQL - postgresql

I want to migrate or write an equivalent query to get the data from table one hr before the current time in PostgreSQL.
oracle query:
select *
from T_DATA as of timestamp (systimestamp - interval '60' minute);

select * from T_DATA where timestamp_column >= now() - interval '1 hour'

Since flashback queries are not supported in postgresql, One approach I tried with temporal tables extension.

Related

How to use oracle sys_sxtract_utc like function in postgresql?

I have an oracle query that uses sys_sxtract_utc function in where clause:
select * from my_table where sys_sxtract_utc(timestmp) > sys_sxtract_utc(last_stable_date)
But I could not use this in postgresql query. I could not find any function like this.
How can I use in postgresql?
You can use at time zone in your query to get the current GMT timestamp
select CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
PostgreSQL Community Doc

Delayed Postgresql Backup table

Is there an easy way to create a replica table on the same postgresql server that is delayed by 24 hours?
I have tried creating a cron that does an insert with a command like this
INSERT INTO TABLE_BACKUP
SELECT * from TABLE_new WHERE time< (NOW() - INTERVAL '1 day');
I have found that doing the above some records are missing.
Is there a better way?

Yesterday - in Redshift & PostgreSQL - date addition compatibility

Attempting to write queries that will be compatible with both PostgreSQL and Amazon Redshift.
Reason: Syncing data from PG to RS to perform complex queries, but in dev/QA environments budget (and DB size) dictates to stay with PG only.
Request: return yesterday's date
In PostgreSQL:
SELECT DATE((NOW() - '1 DAY'::INTERVAL));
In Redshift:
SELECT DATE(DATEADD(DAY, -1, GETDATE()));
Problem: Neither works in the other platform.
Is there a compatible way to achieve requested action?
ORM is an option we'd like to avoid.
The following works in Postgres and Redshift:
ANSI standard SQL:
SELECT current_date - interval '1' day;
-- 2018-06-19 00:00:00
SELECT current_timestamp - interval '1' day;
-- 2018-06-19 13:40:06.509337+00
Postgres (and I believe Redshift as well) also supports the alternative (non-standard) interval syntax: interval '1 day'
Or more compact (not 100% ANSI SQL but works in both):
SELECT current_date - 1;
-- 2018-06-19 00:00:00
SELECT current_timestamp - 1;
-- 2018-06-19 13:40:06.509337+00

Huge PostgreSQL table - Select, update very slow

I am using PostgreSQL 9.5. I have a table which is almost 20GB's. It has a primary key on the ID column which is an auto-increment column, however I am running my queries on another column which is a timestamp... I am trying to select/update/delete on the basis of a timestamp column but the queries are very slow. For example: A select on this table `where timestamp_column::date (current_date - INTERVAL '10 DAY')::date) is taking more than 15 mins or so..
Can you please help on what kind of Index should I add to this table (if needed) to make it perform faster?
Thanks
You can create an index with your clause expression:
CREATE INDEX ns_event_last_updated_idx ON ns_event (CAST(last_updated AT TIME ZONE 'UTC' AS DATE));
But, keep in mind that you're using timestamp with timezone, cast this type to date can let you get undesirable side effects.
Also, remove all casting in your sql:
select * from ns_event where Last_Updated < (current_date - INTERVAL '25 DAY');

Using BETWEEN or comparison operators in PostgreSQL

I'm having trouble with using the between operator in where clause. I have the following queries:
SELECT *
FROM table
WHERE timestamp_column BETWEEN (current_date - interval '1 day')::date
AND current_date => 500k rows
SELECT *
FROM table
WHERE timestamp_column >= (current_date - interval '1 day')::date => 1 mil rows
Does anyone have any idea why the result set is different? Shouldn't it be the same? I am trying to compare the number of rows from a database in PostgreSQL with data from Sybase ASE. The same 2 queries ran in Sybase give the same results. The expected result set is 1 milion rows. This should be the number of rows that I have between yesterday and today.
PostgreSQL v9.5, timestamp_column = timestamp without time zone
I tried using now(), current_timestamp but same result.
What am I missing here? If I didn't made myself clear let me know.