By digoal
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.
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 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.
PostgreSQL v12: How pg_stat_statements Causes High-concurrency Performance Issues
How to Write into a Database Using reWriteBatchedInserts Parameter
digoal - February 20, 2020
digoal - September 6, 2019
Alibaba Clouder - July 5, 2019
Alibaba Cloud MaxCompute - June 2, 2021
digoal - May 28, 2019
digoal - September 20, 2019
Alibaba Cloud PolarDB for PostgreSQL is an in-house relational database service 100% compatible with PostgreSQL and highly compatible with the Oracle syntax.
Learn MoreLeverage cloud-native database solutions dedicated for FinTech.
Learn MoreMigrate 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 MoreMigrating to fully managed cloud databases brings a host of benefits including scalability, reliability, and cost efficiency.
Learn MoreMore Posts by digoal