How to Use PostgreSQL log_fdw to Query Logs
By digoal
Background
Usually, you query logs such as SQL audit, error logs, and slow SQL directly in the $PGDATA/log directory file. However, you can only query logs using the GUI in case of ApsaraDB RDS, which can be inconvenient.
The plug-in log_fdw is now available for direct log queries in ApsaraDB RDS for PostgreSQL. The usage is simple as follows. Please refer to the official guide of ApsaraDB RDS for PostgreSQL and the list of plug-ins supported.
How to Use log_fdw?
1) Create a plug-in.
postgres=> create extension log_fdw cascade;
NOTICE: installing required extension "file_fdw"
CREATE EXTENSION
2) Check the features of the plug-in.
postgres=> \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-----------------------------------+-------------------------------+-----------------------------------------------------------------------+------
public | create_foreign_table_for_log_file | boolean | table_name text, log_server text, log_file text | func
public | file_fdw_handler | fdw_handler | | func
public | file_fdw_validator | void | text[], oid | func
public | list_postgres_log_files | SETOF record | OUT file_name text, OUT file_size_bytes bigint | func
3) List the log files.
postgres=> select * from list_postgres_log_files();
WARNING: 1 temporary files and directories not closed at end-of-transaction
file_name | file_size_bytes
----------------------------------+-----------------
postgresql-2019-12-19_054325.csv | 5386
(1 row)
4) Create a server, which may be provided, and you needn’t manually create in the future.
i=> create server log foreign data wrapper file_fdw ;
CREATE SERVER
5) Create the external table corresponding to the specified log file.
postgres=> select create_foreign_table_for_log_file('ft1','log','postgresql-2019-12-19_054325.csv') ;
create_foreign_table_for_log_file
-----------------------------------
t
(1 row)
6) Query the external table, namely to query the current log content in real time.
postgres=> select * from ft1;
-[ RECORD 1 ]----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------
log_time | 2019-12-19 13:43:25.275+08
user_name |
database_name |
process_id | 56
connection_from |
session_id | 5dfb0dfd.38
session_line_num | 1
command_tag |
session_start_time | 2019-12-19 13:43:25+08
virtual_transaction_id |
transaction_id | 0
error_severity | LOG
sql_state_code | 00000
message | database system was shut down at 2019-12-19 05:43:18 UTC
detail |
hint |
internal_query |
internal_query_pos |
context |
query |
query_pos |
location | StartupXLOG, xlog.c:6362
application_name |
.......
Configure the Number of Logs Retained
Configure the rds_max_log_files parameter to set the number of files to be retained. The default value is 20.
The size of a single file is 100MB by default.
postgres=> select name,setting,unit from pg_settings where name ~ 'log_ro';
name | setting | unit
-------------------+---------+------
log_rotation_age | 0 | min
log_rotation_size | 102400 | kB
(2 rows)
Therefore, logs of 2 GB are retained by default.
References
Read previous post:
PostgreSQL v12: How pg_stat_statements Causes High-concurrency Performance Issues
Read next post:
How to Write into a Database Using reWriteBatchedInserts Parameter
Comments
Related Products
-
PolarDB for PostgreSQL
Alibaba Cloud PolarDB for PostgreSQL is an in-house relational database service 100% compatible with PostgreSQL and highly compatible with the Oracle syntax.
Learn More -
Database for FinTech Solution
Leverage cloud-native database solutions dedicated for FinTech.
Learn More -
Oracle Database Migration Solution
Migrate your legacy Oracle databases to Alibaba Cloud to save on long-term costs and take advantage of improved scalability, reliability, robust security, high performance, and cloud-native features.
Learn More -
Database Migration Solution
Migrating to fully managed cloud databases brings a host of benefits including scalability, reliability, and cost efficiency.
Learn More
More Posts by digoal
- Tips for Executing SQL Statements in Batch: Which Is Better, gexec or Dynamic SQL?
- What Could Be the Factor Leading to the Slow Execution of the First SQL Statement?
- How to Remove Duplicate and Similar Contents in Vector Databases
- How to Implement Parallelism and Concurrency Control (Queue) in Shell
- How to Determine Whether the Current PostgreSQL Database is in a Consistent State?
- PolarDB for PostgreSQL | Retrieval and Matching-Degree Sorting by Tags on Blogs and Websites
- Three Key Details and Principles of PostgreSQL Online Backup & Recovery
- PolarDB for PostgreSQL | Database Performance Optimization for High-Concurrency Queue Processing
- Implementing PostgreSQL Hook: Stats on Tables with Full Scans & Corresponding SQLs
- Optimization on Memory Usage During Rust Cargo Code Compiling