difference in postgres and redshift query - postgresql

1> works fine in AWS RedShift :
rep=# \d repdaily
Table "prod.repdaily"
Column | Type | Modifiers
---------------------------+---------+--------------------
timestamp | integer | not null default 0
rep=# SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second') as Date_New FROM repdaily limit 1;
date_new
------------
2016-06-26
(1 row)
rep=# select version();
version
--------------------------------------------------------------------------------------------------------------------------
PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.1096
(1 row)
rep=#
2>I tried running same query in both 9.5.4 and 8.0.2:
I am not sure how to make it generic so that we can run it any where.
rep=# \d repdaily
Table "prod.repdaily"
Column | Type | Modifiers
---------------------------+---------+--------------------
timestamp | integer | not null default 0
rep=#
rep=# SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second') as Date_New FROM repdaily limit 1;
ERROR: function trunc(timestamp without time zone) does not exist
LINE 1: SELECT distinct trunc(TIMESTAMP 'epoch' + ((floor(timestamp/...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
rep=#
rep=# select version();
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 9.5.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17), 64-bit
(1 row)
rep=#
rep=# select version();
version
-----------------------------------------------------------------------------------------------------------
PostgreSQL 8.0.2 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)
(1 row)
rep=#

PostgreSQL does not have a trunc(timestamp) function native. Looking at RedShift's trunc(timestamp) function you can see:
The function can also return the date from a time stamp.
For time stamps, TRUNC returns a date.
So the equivalent for PostgreSQL would be a simple cast to a DATE type:
SELECT distinct
CAST(TIMESTAMP 'epoch' + ((floor(timestamp/86400))*86400) *INTERVAL '1 second' AS date) as Date_New
FROM repdaily limit 1;
If you want, you can simple create this trunc(timestamp) function in PostgreSQL:
CREATE OR REPLACE FUNCTION trunc(timestamp)
RETURNS date
LANGUAGE SQL
IMMUTABLE AS $$
SELECT $1::date;
$$;
Now your original query will also work in PostgreSQL.

Related

PostgreSQL dates function output to a french language

How can I output the result of PostgreSQL dates function as to_char to french language, for example the output of:
select to_char(current_date, 'Day') ;
should be (a french name for a day):
Mardi
instead of english of day (e.g. Monady)
You would need to set the display of date/time (LC_TIME) to french, and to query not the Day but rather the localizable day TMDay using the TM prefix.
show LC_TIME;
SET LC_TIME = 'French';
select to_char(current_date, 'TMDay') ;
to_char
---------
Mardi
(1 row)
The following works on Ubuntu 16.04 Server, with english language set-up
first we need to add system support for French templating with the command:
sudo locale-gen fr_FR.utf8
then restart postgresql service:
sudo systemctl restart postgresql
then log in to psql
SET LC_TIME = 'fr_FR.utf8';
select to_char(current_date, 'TMDay') ;
to_char
---------
Mardi
(1 row)

Getting ERROR: type "period" does not exist, even i have added btree_gist and temporal_tables extensions

I'm getting error even i have added btree_gist and temporal_tables extensions.
postgres=# \dx
Name | Version | Schema | Description
-----------------+---------+------------+-----------------------------------------------
btree_gist | 1.1 | public | support for indexing common datatypes in GiST
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
temporal_tables | 1.2.0 | public | temporal tables
(3 rows)
CREATE TABLE b (p PERIOD);
ERROR: type "period" does not exist
LINE 1: CREATE TABLE b (p PERIOD);
Version details
postgres=# select version();
version
---------------------------------------------------------------------------------------------
PostgreSQL 9.5.12 on x86_64-alpine-linux-musl, compiled by gcc (Alpine 5.3.0) 5.3.0, 64-bit
If you are talking about this extension, I am not surprised.
The extension does not define a data type or domain called period.

running a query with date_column = current_date wont finish executing in postgres

If I run these 2 queries:
select *
from public.table_name
where date_column = '2018-03-28'
limit 10
select *
from public.table_name
where date_column = current_date - 2
limit 10
The first one will return very quick, the second one would take forever. After checking more, it seems as the first one is scanning the whole table, the second one is properly checking the index. How do I use a dynamic date call that would hit the index properly?
version: PostgreSQL 9.6.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.2 20140120 (Red Hat 4.8.2-16), 64-bit

How to find all relative accent in Postgresql select query?

I have a table named contacts
id | name
----+------------------
44 | Aarón
I am trying to execute a query :
select id,name from contacts where name ilike 'Aaro%';
It return (0 rows)
I am trying to search 'o' and also expect to have result include all accent of 'o' like 'ó'.
As I have did some googling and stackoverlfowing I have found that using locale I need to install the collation.
So I have installed the collation in linux by
sudo locale-gen --no-archive de_DE.utf-8
after that I have tried to install collation in the postgresql database.
create collation de (LOCALE='de_DE.utf-8');
collation generated successfully. I have tried to list out this collation by using select * from pg_collation; and it is there.
after doing all this I tried again to get result of all relative accent 'o' by query:
select id,name from contacts where name ilike 'Aaro%';
but again i got (0 rows)
Ultimatly I want the record "Aarón" when I execute above query.
Thanks in Advance.
You can use unaccent module of postgres.
for that you need to install postgresql-contrib in your system. you can install it using below command in debian based linux.
sudo apt-get install postgresql-contrib
after that you can create unaccent in postgres.
postgres_db=# create EXTENSION unaccent;
CREATE EXTENSION
postgres_db=# select name from test where unaccent(name) ilike 'Aaro%';
name
-------
Aarón
(1 row)
Hope this helps!
The extension unaccent is a good solution. If you deal with a small set of characters without ligatures you can alternatively use a simple function like this one for Polish:
create or replace function unaccent_pl(text)
returns text language sql immutable as $$
select translate($1, 'ąćęłńóśźżĄĆĘŁŃÓŚŹŻ', 'acelnoszzACELNOSZZ')
$$;
select unaccent_pl('Zażółć gęślą jaźń');
unaccent_pl
-------------------
Zazolc gesla jazn
(1 row)

How to view active query underlying cursor

In pg_stat_activity I can see that a client is working its way through some query results using a cursor. But how can I see what the original query is?
pipeline=> select pid, query from pg_stat_activity where state = 'active' order by query_start;
pid | query
-------+--------------------------------------------------------------------------------------
6734 | FETCH FORWARD 1000 FROM "c_109886590_1"
26731 | select pid, query from pg_stat_activity where state = 'active' order by query_start;
(2 rows)
I see there is pg_cursors, but it is empty:
pipeline=> select * from pg_cursors;
name | statement | is_holdable | is_binary | is_scrollable | creation_time
------+-----------+-------------+-----------+---------------+---------------
(0 rows)
The server is on AWS RDS.
pipeline=> select version();
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.3.3 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2), 64-bit
(1 row)
You can't.
pg_cursors is backend-local. It doesn't show cursors that aren't part of the current connection.
PostgreSQL has no way to find out what query underlies a cursor from another session.
The only way I can think of to do this is using log analysis, with log_statement = all and a suitable log_line_prefix.