'Last entries in any table of the database

While testing a website by adding records via the UI, I cannot always tell which tables are being updated. I would like a query - in MSSQL and a version for PostgreSQL - which returns the last entry/entries added/modified in the database, without knowing the table, so I can figure out which tables are related to the feature I am looking at.

In this case I cannot provide an example because I cannot tell which table is being updated and how.



Solution 1:[1]

If you are just trying to track "what table(s) is this UI writing to" without wanting to use Extended Events or Query Store to see what commands are actually running, and the service hasn't been restarted since the UI did its thing, and nobody else is using the database, you can do something like this:

SELECT TOP (10) -- or some other arbitrary number, or no TOP at all
   [Schema]  = s.name, 
   [Table]   = t.name, 
   LastWrite = MAX(ius.last_user_update)
 FROM sys.schemas AS s
 INNER JOIN sys.objects AS t
 ON s.[schema_id] = t.[schema_id]
 INNER JOIN sys.dm_db_index_usage_stats AS ius
 ON ius.[object_id] = t.[object_id]
 GROUP BY s.name, t.name
 ORDER BY LastWrite DESC;

But it seems like a narrow use case and can be invalidated by a lot of variables. If you want to know what your UI is doing, look at the code, or use Extended Events to monitor.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aaron Bertrand