mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-21 19:03:25 +02:00
This allows tools that read the WAL sequentially to identify (possible) redo points when they're reached, rather than only being able to detect them in retrospect when XLOG_CHECKPOINT_ONLINE is found, possibly much later in the WAL stream. There are other possible applications as well; see the discussion links below. Any redo location that precedes the checkpoint location should now point to an XLOG_CHECKPOINT_REDO record, so add a cross-check to verify this. While adjusting the code in CreateCheckPoint() for this patch, I made it call WALInsertLockAcquireExclusive a bit later than before, since there appears to be no need for it to be held while checking whether the system is idle, whether this is an end-of-recovery checkpoint, or what the current timeline is. Bump XLOG_PAGE_MAGIC. Patch by me, based in part on earlier work from Dilip Kumar. Review by Dilip Kumar, Amit Kapila, Andres Freund, and Michael Paquier. Discussion: http://postgr.es/m/CA+TgmoYy-Vc6G9QKcAKNksCa29cv__czr+N9X_QCxEfQVpp_8w@mail.gmail.com Discussion: http://postgr.es/m/20230614194717.jyuw3okxup4cvtbt%40awork3.anarazel.de Discussion: http://postgr.es/m/CA+hUKG+b2ego8=YNW2Ohe9QmSiReh1-ogrv8V_WZpJTqP3O+2w@mail.gmail.com
274 lines
9.0 KiB
Plaintext
274 lines
9.0 KiB
Plaintext
CREATE EXTENSION pg_walinspect;
|
|
-- Mask DETAIL messages as these could refer to current LSN positions.
|
|
\set VERBOSITY terse
|
|
-- Make sure checkpoints don't interfere with the test.
|
|
SELECT 'init' FROM pg_create_physical_replication_slot('regress_pg_walinspect_slot', true, false);
|
|
?column?
|
|
----------
|
|
init
|
|
(1 row)
|
|
|
|
CREATE TABLE sample_tbl(col1 int, col2 int);
|
|
-- Save some LSNs for comparisons.
|
|
SELECT pg_current_wal_lsn() AS wal_lsn1 \gset
|
|
INSERT INTO sample_tbl SELECT * FROM generate_series(1, 2);
|
|
SELECT pg_current_wal_lsn() AS wal_lsn2 \gset
|
|
INSERT INTO sample_tbl SELECT * FROM generate_series(3, 4);
|
|
-- ===================================================================
|
|
-- Tests for input validation
|
|
-- ===================================================================
|
|
-- Invalid input LSN.
|
|
SELECT * FROM pg_get_wal_record_info('0/0');
|
|
ERROR: could not read WAL at LSN 0/0
|
|
-- Invalid start LSN.
|
|
SELECT * FROM pg_get_wal_records_info('0/0', :'wal_lsn1');
|
|
ERROR: could not read WAL at LSN 0/0
|
|
SELECT * FROM pg_get_wal_stats('0/0', :'wal_lsn1');
|
|
ERROR: could not read WAL at LSN 0/0
|
|
SELECT * FROM pg_get_wal_block_info('0/0', :'wal_lsn1');
|
|
ERROR: could not read WAL at LSN 0/0
|
|
-- Start LSN > End LSN.
|
|
SELECT * FROM pg_get_wal_records_info(:'wal_lsn2', :'wal_lsn1');
|
|
ERROR: WAL start LSN must be less than end LSN
|
|
SELECT * FROM pg_get_wal_stats(:'wal_lsn2', :'wal_lsn1');
|
|
ERROR: WAL start LSN must be less than end LSN
|
|
SELECT * FROM pg_get_wal_block_info(:'wal_lsn2', :'wal_lsn1');
|
|
ERROR: WAL start LSN must be less than end LSN
|
|
-- LSNs with the highest value possible.
|
|
SELECT * FROM pg_get_wal_record_info('FFFFFFFF/FFFFFFFF');
|
|
ERROR: WAL input LSN must be less than current LSN
|
|
-- Success with end LSNs.
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', 'FFFFFFFF/FFFFFFFF');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_stats(:'wal_lsn1', 'FFFFFFFF/FFFFFFFF');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_block_info(:'wal_lsn1', 'FFFFFFFF/FFFFFFFF');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- Failures with start LSNs.
|
|
SELECT * FROM pg_get_wal_records_info('FFFFFFFF/FFFFFFFE', 'FFFFFFFF/FFFFFFFF');
|
|
ERROR: WAL start LSN must be less than current LSN
|
|
SELECT * FROM pg_get_wal_stats('FFFFFFFF/FFFFFFFE', 'FFFFFFFF/FFFFFFFF');
|
|
ERROR: WAL start LSN must be less than current LSN
|
|
SELECT * FROM pg_get_wal_block_info('FFFFFFFF/FFFFFFFE', 'FFFFFFFF/FFFFFFFF');
|
|
ERROR: WAL start LSN must be less than current LSN
|
|
-- ===================================================================
|
|
-- Tests for all function executions
|
|
-- ===================================================================
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_record_info(:'wal_lsn1');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', :'wal_lsn2');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_stats(:'wal_lsn1', :'wal_lsn2');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_block_info(:'wal_lsn1', :'wal_lsn2');
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- ===================================================================
|
|
-- Test for filtering out WAL records of a particular table
|
|
-- ===================================================================
|
|
SELECT oid AS sample_tbl_oid FROM pg_class WHERE relname = 'sample_tbl' \gset
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', :'wal_lsn2')
|
|
WHERE block_ref LIKE concat('%', :'sample_tbl_oid', '%') AND resource_manager = 'Heap';
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- ===================================================================
|
|
-- Test for filtering out WAL records based on resource_manager and
|
|
-- record_type
|
|
-- ===================================================================
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_records_info(:'wal_lsn1', :'wal_lsn2')
|
|
WHERE resource_manager = 'Heap' AND record_type = 'INSERT';
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- ===================================================================
|
|
-- Tests to get block information from WAL record
|
|
-- ===================================================================
|
|
-- Update table to generate some block data.
|
|
SELECT pg_current_wal_lsn() AS wal_lsn3 \gset
|
|
UPDATE sample_tbl SET col1 = col1 + 1 WHERE col1 = 1;
|
|
SELECT pg_current_wal_lsn() AS wal_lsn4 \gset
|
|
-- Check if we get block data from WAL record.
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_block_info(:'wal_lsn3', :'wal_lsn4')
|
|
WHERE relfilenode = :'sample_tbl_oid' AND block_data IS NOT NULL;
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- Force a checkpoint so that the next update will log a full-page image.
|
|
SELECT pg_current_wal_lsn() AS wal_lsn5 \gset
|
|
CHECKPOINT;
|
|
-- Verify that an XLOG_CHECKPOINT_REDO record begins at precisely the redo LSN
|
|
-- of the checkpoint we just performed.
|
|
SELECT redo_lsn FROM pg_control_checkpoint() \gset
|
|
SELECT start_lsn = :'redo_lsn'::pg_lsn AS same_lsn, resource_manager,
|
|
record_type FROM pg_get_wal_record_info(:'redo_lsn');
|
|
same_lsn | resource_manager | record_type
|
|
----------+------------------+-----------------
|
|
t | XLOG | CHECKPOINT_REDO
|
|
(1 row)
|
|
|
|
-- This update should produce a full-page image because of the checkpoint.
|
|
UPDATE sample_tbl SET col1 = col1 + 1 WHERE col1 = 2;
|
|
SELECT pg_current_wal_lsn() AS wal_lsn6 \gset
|
|
-- Check if we get FPI from WAL record.
|
|
SELECT COUNT(*) >= 1 AS ok FROM pg_get_wal_block_info(:'wal_lsn5', :'wal_lsn6')
|
|
WHERE relfilenode = :'sample_tbl_oid' AND block_fpi_data IS NOT NULL;
|
|
ok
|
|
----
|
|
t
|
|
(1 row)
|
|
|
|
-- ===================================================================
|
|
-- Tests for permissions
|
|
-- ===================================================================
|
|
CREATE ROLE regress_pg_walinspect;
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_record_info(pg_lsn)', 'EXECUTE'); -- no
|
|
has_function_privilege
|
|
------------------------
|
|
f
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_records_info(pg_lsn, pg_lsn) ', 'EXECUTE'); -- no
|
|
has_function_privilege
|
|
------------------------
|
|
f
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_stats(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- no
|
|
has_function_privilege
|
|
------------------------
|
|
f
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_block_info(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- no
|
|
has_function_privilege
|
|
------------------------
|
|
f
|
|
(1 row)
|
|
|
|
-- Functions accessible by users with role pg_read_server_files.
|
|
GRANT pg_read_server_files TO regress_pg_walinspect;
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_record_info(pg_lsn)', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_records_info(pg_lsn, pg_lsn) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_stats(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_block_info(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
REVOKE pg_read_server_files FROM regress_pg_walinspect;
|
|
-- Superuser can grant execute to other users.
|
|
GRANT EXECUTE ON FUNCTION pg_get_wal_record_info(pg_lsn)
|
|
TO regress_pg_walinspect;
|
|
GRANT EXECUTE ON FUNCTION pg_get_wal_records_info(pg_lsn, pg_lsn)
|
|
TO regress_pg_walinspect;
|
|
GRANT EXECUTE ON FUNCTION pg_get_wal_stats(pg_lsn, pg_lsn, boolean)
|
|
TO regress_pg_walinspect;
|
|
GRANT EXECUTE ON FUNCTION pg_get_wal_block_info(pg_lsn, pg_lsn, boolean)
|
|
TO regress_pg_walinspect;
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_record_info(pg_lsn)', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_records_info(pg_lsn, pg_lsn) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_stats(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
SELECT has_function_privilege('regress_pg_walinspect',
|
|
'pg_get_wal_block_info(pg_lsn, pg_lsn, boolean) ', 'EXECUTE'); -- yes
|
|
has_function_privilege
|
|
------------------------
|
|
t
|
|
(1 row)
|
|
|
|
REVOKE EXECUTE ON FUNCTION pg_get_wal_record_info(pg_lsn)
|
|
FROM regress_pg_walinspect;
|
|
REVOKE EXECUTE ON FUNCTION pg_get_wal_records_info(pg_lsn, pg_lsn)
|
|
FROM regress_pg_walinspect;
|
|
REVOKE EXECUTE ON FUNCTION pg_get_wal_stats(pg_lsn, pg_lsn, boolean)
|
|
FROM regress_pg_walinspect;
|
|
REVOKE EXECUTE ON FUNCTION pg_get_wal_block_info(pg_lsn, pg_lsn, boolean)
|
|
FROM regress_pg_walinspect;
|
|
-- ===================================================================
|
|
-- Clean up
|
|
-- ===================================================================
|
|
DROP ROLE regress_pg_walinspect;
|
|
SELECT pg_drop_replication_slot('regress_pg_walinspect_slot');
|
|
pg_drop_replication_slot
|
|
--------------------------
|
|
|
|
(1 row)
|
|
|
|
DROP TABLE sample_tbl;
|
|
DROP EXTENSION pg_walinspect;
|