I have to translate this Oracle stmt:
SELECT DATA_PRECISION, DATA_SCALE
FROM USER_TAB_COLUMNS
Into PostgreSQL.
I have managed so far to gather that USER_TAB_COLUMNS becomes information_schema.columns.
What about DATA_SCALE and DATA_PRECISION?
Related
I am running below query in Oracle and Postgres, both shows different output with respect to ordering of the values.
with test as (
select 'Summary-Account by User (Using Contact ID)' col1 from dual
union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1 from dual
)
select * from test
order by col1 desc;
Below is Oracle one
Postgres
with test as (
select 'Summary-Account by User (Using Contact ID)' col1
union all
select 'Summary-Account by User by Client by Day (Using Contact ID)' col1
)
select * from test
order by col1 desc;
Oracle collation is AL32UTF8
Postgres has LC_CTYPS is en_US.UTF-8
Both of them look same from how database should behave. How to fix this?
I have read few posts on stackoverflow about POSIX and C, after changing the query order by to order by col1 collate "C" desc; The result matches Oracle output.
Is there anyway to apply this permanently?
AL32UTF8 is not a collation, but an encoding (character set).
Oracle uses the “binary collation” by default, which corresponds the the C or POSIX collation in PostgreSQL.
You have several options to get a similar result in PostgreSQL:
create the database with LOCALE "C"
if you are selecting from a table, define the column to use the "C" collation:
ALTER TABLE tab ALTER col1 TYPE text COLLATE "C";
add an explicit COLLATE clause:
ORDER BY col1 COLLATE "C"
Are there multiple ways to set n_distinct in PostgreSQL? Both of these seem to be doing the same thing but end up changing a different value within pg_attribute. What is the difference between these two commands?
alter table my_table alter column my_column set (n_distinct = 500);
alter table my_table alter column my_column set statistics 1000;
select
c.relname,
a.attname,
a.attoptions,
a.attstattarget
from
pg_class c
inner join
pg_attribute a
on c.oid = a.attrelid
where
c.relname = 'my_table'
and
a.attname = 'my_column'
order by
c.relname,
a.attname;
Name |Value
-------------|----------------
relname |my_table
attname |my_column
attoptions |{n_distinct=500}
attstattarget|1000
Both of these seem to be doing the same thing
Why would you say that? Both commands are obviously distinct. Both are related to column statistics and query planning. But they do very different things.
The statistics target ...
controls the level of detail of statistics accumulated for this column by ANALYZE. See:
Check statistics targets in PostgreSQL
Basics in the manual.
Setting n_distinct is something completely different. It means hard-coding the number (or ratio) of distinct values to expect for the given column. (But only effective after the next ANALYZE.)
Related answer on dba.SE with more on n_distinct:
Very bad query plan in PostgreSQL 9.6
I am trying to list all the created Tables,Indexes, Views, Procedures, Materialize views from all Databases. By 'created' I mean not all the postgres default tables,views etc. I found a way to list all tables but it's per database and couldn't find a way to list all tables from all databases.
SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema';
SELECT viewname FROM pg_views WHERE schemaname NOT IN ('pg_catalog', 'information_schema') AND viewname !~ '^pg_';
Can't find a way to list all from all databases , hope someone has an easy solution for this.
You will have to connect to each database seperatly, these details are not revealed in other databases as this might reveal sensitive information. You can query pg_database to retrieve a list of all databases and use that information to write a program that connects to all the databases (if you have the privileges to do so).
How can I find the number of User-defined functions and Stored Procedures in PostgreSQL database?
It will work for you :
(editted by #a_horse_with_no_name 's warn)
SELECT count(*)
FROM information_schema.routines
WHERE routines.specific_schema='schema_name'
This excludes metadata schemas of postgres:
select count(*) from information_schema.routines t where t.routine_schema not in ('pg_catalog', 'information_schema');
If you're only interested in the number of procedures then:
select count(*) from information_schema.routines t where t.routine_schema not in ('pg_catalog', 'information_schema') and t.routine_type = 'PROCEDURE';
Which Postgres query is equivalent to the following Oracle query?
select CID,
max(cus.customer_id) keep (dense_rank first order by decode(acct.CUS_Type,'X',4,'M',5,'F',6,'G',7,'I',5,6), start_date) best_CUS
from cusomer cus
group by CID ;