Is there some means of querying the system tables to establish which tables are using what locking schemes? I took a look at the columns in sysobjects but nothing jumped out.
aargh, just being an idiot:
SELECT name, lockscheme(name)
FROM sysobjects
WHERE type="U"
ORDER BY name
take a look at the syslockinfo and syslocks system tables
you can also run the sp_lock proc
Related
While I am doing monitoring with my PostgreSql server, I found out that this query frequently shown up.
select t.oid, t.typname from pg_type t where t.typtype='b'
and because I have a lot of table, that query will take long time to process.
Is it possible for me to custom that query into another query like
select t.oid, t.typname
from pg_type t
left join pg_type base_type on t.typelem=base_type.oid
where t.typtype='b'
and (base_type.oid is null or base_type.typtype='b')
or maybe another suggestion will be good
thank you
Try to find out what issues this query, then you can fix the problem. It may well be some ORM, framework or other tool.
Having extremely many tables, users, schemas or other objects is a bad idea, because the catalogs are not built and indexed for that.
At my work, I needed to build a new join table in a postgresql database that involved doing a lot of computations on two existing tables. The process was supposed to take a long time so I set it up to run over the weekend before I left on Friday. Now, I want to check to see if the query finished or not.
How can I check if an INSERT command has finished yet while not being at the computer I ran it on? (No, I don't know how many rows it was suppose to add.)
Select * from pg_stat_activity where state not ilike 'idle%' and query ilike 'insert%'
This will return all non-idle sessions where the query begins with insert, if your query does not show in this list then it is no longer running.
pg_stat_activity doc
You can have a look at the table pg_stat_activity which contains all database connections including active query, owner etc.
At https://gist.github.com/rgreenjr/3637525 there is a copy-able example how such a query could look like.
I want to retrieve the last time table was updated(insert,delete,update).
I tried this query.
SELECT last_user_update
FROM sys.dm_db_index_usage_stats
WHERE object_id=object_id('T')
but the data there is not persisted across service restarts.
I want to preserve the stats even if the service restarts. How can I achieve it?
If you're talking about last time the table was updated in terms of its structured has changed (new column added, column changed etc.) - use this query:
SELECT name, [modify_date] FROM sys.tables
If you're talking about DML operations (insert, update, delete), then you either need to persist what that DMV gives you on a regular basis, or you need to create triggers on all tables to record that "last modified" date - or check out features like Change Data Capture in SQL Server 2008 and newer.
If you want to see data updates you could use this technique with required permissions:
SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'DATABASE')
AND OBJECT_ID=OBJECT_ID('TABLE')
Find last time of update on a table
SELECT
tbl.name
,ius.last_user_update
,ius.user_updates
,ius.last_user_seek
,ius.last_user_scan
,ius.last_user_lookup
,ius.user_seeks
,ius.user_scans
,ius.user_lookups
FROM
sys.dm_db_index_usage_stats ius INNER JOIN
sys.tables tbl ON (tbl.OBJECT_ID = ius.OBJECT_ID)
WHERE ius.database_id = DB_ID()
http://www.sqlserver-dba.com/2012/10/sql-server-find-last-time-of-update-on-a-table.html
To persist audit data regarding data modifications, you will need to implement a DML Trigger on each table that you are interested in. You will need to create an Audit table, and add code to your triggers to write to this table.
For more details on how to implement DML triggers, refer to this MDSN article http://msdn.microsoft.com/en-us/library/ms191524%28v=sql.105%29.aspx
SELECT so.name,so.modify_date
FROM sys.objects as so
INNER JOIN INFORMATION_SCHEMA.TABLES as ist
ON ist.TABLE_NAME=so.name where ist.TABLE_TYPE='BASE TABLE' AND
TABLE_CATALOG='DbName' order by so.modify_date desc;
this is help to get table modify with table name
SELECT UPDATE_TIME
FROM information_schema.tables WHERE TABLE_SCHEMA = 'your_dbname' AND TABLE_NAME = 'your_tablename'
Why not just run this: No need for special permissions
SELECT
name,
object_id,
create_date,
modify_date
FROM
sys.tables
WHERE
name like '%yourTablePattern%'
ORDER BY
modify_date
I have seen that PostgreSQL have several statistics depending on every table's OID (like the number of inserts, and so) 1.
What I am looking for is some kind of SELECT query or something that would sum up everything for every table.
It would be something like:
SELECT SUM(pg_stat_get_db_tuples_returned(SELECT oid FROM pg_class));
Or something like that. I would apreciate any help in here.
Do you mean this:
SELECT SUM(pg_stat_get_db_tuples_returned(oid))
from pg_class;
Something like this, with your choice of stats function, aggregates the stats function on all tables except temporary and system catalog tables:
SELECT
sum(pg_stat_get_db_tuples_returned(c.oid))
FROM pg_catalog.pg_class c
INNER JOIN pg_namespace n ON (c.relnamespace = n.oid)
WHERE NOT (n.nspname LIKE ANY(ARRAY['pg_temp%','pg_catalog','information_schema']));
Note that pg_toast schemas are included in this, as I presume you want your stats to include any TOAST side-tables. If you don't, add pg_toast% to the exclusions.
Edit: I was using the construct:
(quote_ident(n.nspname)||'.'||quote_ident(c.relname))::regclass
to get the table oid, but that's just silly when it's right there in pg_class; it's ridiculously roundabout as shown by a_horse_with_no_name.
In PostgreSQL, is there a way to get all of the tables that a view/table depends on based on its use of foreign keys and access to a given table?
Basically, I want to be able to copy the structure of a view/table using a script and want to be able to automatically get the list of tables that I would also need to copy in order for everything to still work right.
This response appears to be headed in the right direction, but doesn't give me the results that I expect/need. Any suggestions?
Using the info from Andy Lester, I was able to come up with the following queries to retrieve the information that I needed.
Get Tables that Foreign Keys refer to:
SELECT cl2.relname AS ref_table
FROM pg_constraint as co
JOIN pg_class AS cl1 ON co.conrelid=cl1.oid
JOIN pg_class AS cl2 ON co.confrelid=cl2.oid
WHERE co.contype='f' AND cl1.relname='TABLENAME'
ORDER BY cl2.relname;
Get Tables that a View or Rules from a Table refer to:
SELECT cl_d.relname AS ref_table
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
JOIN pg_class AS cl_d ON d.refobjid=cl_d.oid
WHERE cl_d.relkind IN ('r','v') AND cl_r.relname='TABLENAME'
GROUP BY cl_d.relname
ORDER BY cl_d.relname;
Assuming you have your foreign keys set up correctly, use pg_dump to dump the table definitions.
pg_dump -s -t TABLENAME
I think it is a quite bad idea. Just copy the whole database, I think that the application wants to have all data, not only data from one table.
What's more, there are also triggers, that could depend on some tables, but to know that you'd have to make not so easy code analysis.
In psql, adding + to the usual \d gives you a "Referenced by" list along with the table definition.
\d+ tablename