Empty tables after pg_upgrade from v9.6 to v12 - postgresql

I used this command to upgrade the postgresql database from v9.6 to v12:
/opt/rh/rh-postgresql12/root/usr/bin/pg_upgrade -b /opt/rh/rh-postgresql96/root/usr/bin/ -B /opt/rh/rh-postgresql12/root/usr/bin/ -d /var/opt/rh/rh-postgresql96/lib/pgsql/data -D /var/opt/rh/rh-postgresql12/lib/pgsql/data
I got no errors and the upgrade was done successfully, but when I tried to fetch some data from my database, I realized all the database tables are empty.
The size of the database is somehow the same as the old database and running the below command returns all the tables but with 0 rows:
select n.nspname as table_schema,c.relname as table_name, c.reltuples as rows
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'r'
and n.nspname not in ('information_schema','pg_catalog')
order by c.reltuples desc;
Can you please let me know why the tables have no rows?

running the suggested script by postgresql: '/var/lib/pgsql/analyze_new_cluster.sh'
or the command
sudo -u postgres /opt/rh/rh-postgresql12/root/usr/bin/vacuumdb --all --analyze-in-stages -p 5433
would solve the issue.

Related

Postgres order by difference

I am comparing query result set between PostgreSql 9.6 and PostgreSql 12 version. Noticed a very strange behavior in query result. Running below query from psql
SELECT current_database(),table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_name;
PostgreSql 9.6 Output
current_database | table_name
------------------+------------------------------
mydb |key_request_sum
mydb |key_req_user
PostgreSql 12 output
current_database | table_name
------------------+------------------------------
mydb |key_req_user
mydb |key_request_sum
This is strange to see the different output for same query for different version. please suggest what to change in query to make Postgres 12 result same as 9.6
If you want the 9.6 version, you can have it collate by the locality that worked in the days before UTF finally got fixed to the point of being usable:
create table key_req_user ();
create table key_request_user ();
select current_database(), table_name, pg_typeof(table_name)
from information_schema.tables
where table_type = 'BASE TABLE'
and table_schema not in ('pg_catalog', 'information_schema')
order by table_name collate "en_US.utf8";
db<>fiddle here

PostgreSql , extract schema objects DDL to separate SQL file

I want to export all objects DDL to separate file example (table_a_create.sql, view_b_create.sql, trigger_c_create.sql, table_contraints.sql ...)
I was trying with pg_dump but it only exports to one file for the whole schema.
I read some questions about this on stackoverflow but still not enough for my requirement
Ex: How to dump PostgreSQL database structure (each object in separate file)
Is there any way to do it? I'm using Windows
If you are on the client machine, you can put this in a SQL script (e.g. export_plpgsql.sql) :
\pset tuples_only on
\pset footer off
\set QUIET on
\pset format unaligned
\set QUIET off
SELECT '\echo ''* Export '||(CASE proKind WHEN 'f' THEN 'Function' ELSE 'Procedure' END)||' : '||proName||''''
||chr(10)||'\copy (SELECT pg_get_functiondef('||p.oid||')) TO '''||:'export_path'||'/'||upper(proName)
||(CASE proKind WHEN 'f' THEN '.fct' ELSE '.prc' END)||''' WITH CSV;' as export_routine
FROM pg_proc p
WHERE proNamespace = (SELECT oid FROM pg_namespace WHERE nspName = lower(:'schema_name'))
ORDER BY proName;
and call it using 2 arguments : schema_name and export_path, for example :
psql -U my_ -d my_db -v schema_name=my_schema -v export_path=C:/temp/export_PG -f export_plpgsql.sql > C:\temp\export_plpgsql.gen.sql
This will generate a script containing all the exports command for your plpgsql routines, e.g.
\copy (SELECT pg_get_functiondef(51296)) TO 'C:/temp/export_PG/my_procedure.prc' WITH CSV;
Last step : run the generated script
psql -U my_ -d my_db -f C:\temp\export_plpgsql.gen.sql
It will generate a .prc file for each procedure and a .fct file for each function.
NB: You may have to refine the script as you can have other kind of functions (proKind) in pg_proc view.

How to modify a DB2 view query without dropping and creating view

I need to modify the query of a DB2 view without dropping and creating that view.
I use DB Visualizer and I tried the ALTER VIEW command but I got the error "unexpected token 'VIEW' was found following 'ALTER'.
Any idea on how to resolve this problem?
Check out CREATE or REPLACE view functionaliy - it is also call soft invalidation and desribed in detail on the page referenced.
Here would be a sample script, how to modify a view. It works fine without any error on V11.5 GA, AIX 7.1.
#!/bin/sh
db2 -v "drop db db1"
db2 -v "create db db1"
db2 -v "connect to db1"
db2 -v "create table t1 (c1 int, c2 char(10), c3 char(10))"
db2 -v "insert into t1 values (1, 'aaa', 'AAA')"
db2 -v "insert into t1 values (2, 'bbb', 'BBB')"
db2 -v "create view v1 as select c1, c2 from t1"
db2 -v "select * from v1"
db2 -v "create or replace view v1 as select c1, c2, c3 from t1"
db2 -v "select * from v1"
db2 -v "terminate"
The first select * from v1 returns 2 columns but second one returns 3 columns sice it's replaced with a new v1.
Hope this helps.

Postgresql dynamic query in COPY stament with psql command

I am trying to get maintenance query from windows command line psql with COPY statement.(Windows 2012). The query produces maintenance query as output file. After that I wish to import maintenance query (reindex.sql) and execute with a scheduler. But I couldn't find it out why I couldnt execute script. There should be a comma trick which I couldn't solve.
psql -U postgres -c "COPY( SELECT 'REINDEX TABLE "' || schemaname || '"."' || relname || '";'FROM pg_stat_all_tables ORDER BY n_dead_tup DESC)TO 'E:\scripts\maintenance\reindex.sql';"
Use format():
psql -U postgres -c ^
"COPY ( ^
SELECT format('REINDEX TABLE %s.%s', schemaname, relname) ^
FROM pg_stat_all_tables ^
ORDER BY n_dead_tup DESC) ^
TO 'E:\scripts\maintenance\reindex.sql'"

Different views to list users in postgresql 9.5

Somebody could tell me the difference between the views pg_users, users and \du+ command to display users in Postgresql 9.5.
select *
from users
display users that are not in pg_user views
"users" is not a catalog table - it is yours. to check waht happens when you use meta-command in psql - use -E switch, eg:
~]$ psql t -E
Timing is on.
psql (9.5.4)
Type "help" for help.
t=# \du+
********* QUERY **********
SELECT r.rolname, r.rolsuper, r.rolinherit,
r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,
r.rolconnlimit, r.rolvaliduntil,
ARRAY(SELECT b.rolname
FROM pg_catalog.pg_auth_members m
JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)
WHERE m.member = r.oid) as memberof
, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description
, r.rolreplication
, r.rolbypassrls
FROM pg_catalog.pg_roles r
ORDER BY 1;
**************************
List of roles
Role name | Attributes | Member of | Description
------------------+------------------------------------------------------------+-------------+---------------------------------------------------------------------------------------------
vao | | {} |
If you dig further you will find out that in 9.5 at least both pg_user and pg_roles query from pg_authid