Skip to content

Instantly share code, notes, and snippets.

@j-alberto
Last active October 22, 2018 20:19
Show Gist options
  • Save j-alberto/2dab354381de3a00b33ab910b3604b54 to your computer and use it in GitHub Desktop.
Save j-alberto/2dab354381de3a00b33ab910b3604b54 to your computer and use it in GitHub Desktop.
/*** update sequences ***/
select setval('seqname',(select max(id) from tablename));
/*** restore backup ***/
psql.exe -h [host] -p [port] -d [database] -U [user] -f "c:\\path\\to\\file"
/*** find tables with FKs to a table ***/
select R.TABLE_NAME,r.constraint_schema
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u
inner join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS FK
on U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG
and U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT_SCHEMA
and U.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME
inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE R
ON R.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND R.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND R.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
WHERE U.TABLE_SCHEMA = 'table_schema'
AND U.TABLE_NAME = 'table_name'
/*** disable triggers (for quicker delete. Cascading FKs?) ***/
set session_replication_role = replica;
delete from [table] where [condition];
set session_replication_role = default;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment