extract tx_history_txid() into a CTE-query
This commit is contained in:
parent
6f740c7209
commit
2f198664fa
@ -15,6 +15,30 @@ drop type if exists tx_operation;
|
|||||||
|
|
||||||
create type "tx_operation" as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE');
|
create type "tx_operation" as enum ('INSERT', 'UPDATE', 'DELETE', 'TRUNCATE');
|
||||||
|
|
||||||
|
|
||||||
|
create or replace function tx_history_txid()
|
||||||
|
returns xid8 stable
|
||||||
|
language plpgsql as $$
|
||||||
|
declare
|
||||||
|
historicalTxIdSetting text;
|
||||||
|
historicalTxId xid8;
|
||||||
|
historicalTimestamp timestamp;
|
||||||
|
begin
|
||||||
|
select current_setting('hsadminng.tx_history_txid', true) into historicalTxIdSetting;
|
||||||
|
raise notice 'tx_history_txid(): historicalTxIdSetting=%', historicalTxIdSetting;
|
||||||
|
if historicalTxIdSetting is null or historicalTxIdSetting = '' then
|
||||||
|
select current_setting('hsadminng.tx_history_timestamp', true)::timestamp into historicalTimestamp;
|
||||||
|
raise notice 'tx_history_txid(): historicalTimestamp=%', historicalTimestamp;
|
||||||
|
select max(txc.txid) from tx_context txc where txc.txtimestamp <= historicalTimestamp into historicalTxId;
|
||||||
|
raise notice 'tx_history_txid(): historicalTxId=%', historicalTxId;
|
||||||
|
else
|
||||||
|
historicalTxId = historicalTxIdSetting::xid8;
|
||||||
|
end if;
|
||||||
|
return historicalTxId;
|
||||||
|
end; $$;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
create or replace function tx_historicize_tf()
|
create or replace function tx_historicize_tf()
|
||||||
returns trigger
|
returns trigger
|
||||||
language plpgsql
|
language plpgsql
|
||||||
@ -97,6 +121,8 @@ begin
|
|||||||
createViewSQL = format(
|
createViewSQL = format(
|
||||||
'CREATE OR REPLACE VIEW %1$s AS' ||
|
'CREATE OR REPLACE VIEW %1$s AS' ||
|
||||||
'(' ||
|
'(' ||
|
||||||
|
-- make sure the function is only called once, not for every matching row in tx_context
|
||||||
|
' WITH txh AS (SELECT tx_history_txid() AS txid) ' ||
|
||||||
' SELECT %2$s' ||
|
' SELECT %2$s' ||
|
||||||
' FROM %3$s' ||
|
' FROM %3$s' ||
|
||||||
' WHERE alive = TRUE' ||
|
' WHERE alive = TRUE' ||
|
||||||
@ -105,7 +131,7 @@ begin
|
|||||||
' SELECT max(ex.version_id) AS history_id' ||
|
' SELECT max(ex.version_id) AS history_id' ||
|
||||||
' FROM %3$s AS ex' ||
|
' FROM %3$s AS ex' ||
|
||||||
' JOIN tx_context as txc ON ex.txid = txc.txid' ||
|
' JOIN tx_context as txc ON ex.txid = txc.txid' ||
|
||||||
' WHERE txc.txid = current_setting(''hsadminng.tx_history_txid'', true)::xid8' ||
|
' WHERE txc.txid <= (SELECT txid FROM txh)' ||
|
||||||
' GROUP BY uuid' ||
|
' GROUP BY uuid' ||
|
||||||
' )' ||
|
' )' ||
|
||||||
')',
|
')',
|
||||||
@ -126,8 +152,6 @@ end; $$;
|
|||||||
|
|
||||||
call tx_create_historicization('hs_hosting_asset'); -- FIXME: move to 7010-hosting-asset.sql
|
call tx_create_historicization('hs_hosting_asset'); -- FIXME: move to 7010-hosting-asset.sql
|
||||||
|
|
||||||
-- and expanded:
|
|
||||||
|
|
||||||
-- ===========================================================================================
|
-- ===========================================================================================
|
||||||
|
|
||||||
|
|
||||||
@ -138,19 +162,21 @@ call defineContext('historization testing', null, 'superuser-alex@hostsharing.ne
|
|||||||
-- 'hs_booking_project#D-1000300-mihdefaultproject:ADMIN'); -- prod
|
-- 'hs_booking_project#D-1000300-mihdefaultproject:ADMIN'); -- prod
|
||||||
-- 'hs_booking_project#D-1000300-mimdefaultproject:ADMIN'); -- test
|
-- 'hs_booking_project#D-1000300-mimdefaultproject:ADMIN'); -- test
|
||||||
-- update hs_hosting_asset set caption='lug00 b' where identifier = 'lug00' and type = 'MANAGED_WEBSPACE'; -- prod
|
-- update hs_hosting_asset set caption='lug00 b' where identifier = 'lug00' and type = 'MANAGED_WEBSPACE'; -- prod
|
||||||
update hs_hosting_asset set caption='hsh00 E ' || now()::text where identifier = 'hsh00' and type = 'MANAGED_WEBSPACE'; -- test
|
update hs_hosting_asset set caption='hsh00 C ' || now()::text where identifier = 'hsh00' and type = 'MANAGED_WEBSPACE'; -- test
|
||||||
update hs_hosting_asset set caption='hsh00 F ' || now()::text where identifier = 'hsh00' and type = 'MANAGED_WEBSPACE'; -- test
|
update hs_hosting_asset set caption='hsh00 D ' || now()::text where identifier = 'hsh00' and type = 'MANAGED_WEBSPACE'; -- test
|
||||||
commit;
|
commit;
|
||||||
|
|
||||||
-- single version at point in time
|
-- single version at point in time
|
||||||
-- set hsadminng.tx_history_txid to (select max(txid) from tx_context where txtimestamp<='2024-08-27 12:13:13.450821');
|
-- set hsadminng.tx_history_txid to (select max(txid) from tx_context where txtimestamp<='2024-08-27 12:13:13.450821');
|
||||||
SELECT set_config('hsadminng.tx_history_txid', (select max(txid)::Text from tx_context where txtimestamp<='2024-08-27 12:13:13.450821'), FALSE);
|
set hsadminng.tx_history_txid to '3249';
|
||||||
select uuid, version, identifier, caption from hs_hosting_asset_hv p where identifier in ('hsh00');
|
--set hsadminng.tx_history_timestamp to '2024-08-27 17:52:07.755407';
|
||||||
|
-- all versions
|
||||||
|
select tx_history_txid(), txc.txtimestamp, txc.currentUser, txc.currentTask, haex.*
|
||||||
|
from hs_hosting_asset_ex haex
|
||||||
|
join tx_context txc on haex.txid=txc.txid
|
||||||
|
where haex.identifier in ('hsh00', 'mim00');
|
||||||
|
|
||||||
|
select uuid, version, identifier, caption from hs_hosting_asset_hv p where identifier in ('hsh00', 'mim00');
|
||||||
|
|
||||||
select pg_current_xact_id();
|
select pg_current_xact_id();
|
||||||
|
|
||||||
-- all versions
|
|
||||||
select txc.txtimestamp, txc.currentUser, txc.currentTask, haex.*
|
|
||||||
from hs_hosting_asset_ex haex
|
|
||||||
join tx_context txc on haex.txid=txc.txid
|
|
||||||
where haex.identifier in ('hsh00');
|
|
||||||
|
Loading…
Reference in New Issue
Block a user