# LogicTest: local

statement ok
CREATE TABLE xy (x INT, y INT);
INSERT INTO xy VALUES (1, 2);

subtest protect_mvcc_history

user root

statement ok
SET CLUSTER SETTING jobs.registry.interval.adopt = '10ms'

statement ok
SET CLUSTER SETTING jobs.registry.interval.cancel = '10ms'

statement ok
SET CLUSTER SETTING sql.history_retention_job.poll_interval = '10ms'

statement ok
GRANT admin TO testuser

user testuser

statement ok
select crdb_internal.protect_mvcc_history(1709131929793796000.0000000000, '24 hours'::interval, 'molt fetch');

query TTT colnames
select ts as timestamp, meta_type, crdb_internal.pb_to_json( 'cockroach.protectedts.Target', target ) from system.protected_ts_records
----
timestamp  meta_type  crdb_internal.pb_to_json
1709131929793796000.0000000000  jobs       {"cluster": {}}

query T
SELECT description FROM [SHOW JOBS] WHERE job_type = 'HISTORY RETENTION'
----
History Retention for molt fetch

# Cancel the session to force the cleanup the PTS.
statement ok
CANCEL JOB (SELECT id FROM system.jobs WHERE job_type = 'HISTORY RETENTION');

# Confirm that the PTS is gone.
query TTT retry
select ts as timestamp, meta_type, crdb_internal.pb_to_json('cockroach.protectedts.Target', target ) from system.protected_ts_records
----

# Confirm PTS expires after the given expiration without cancellation.
statement ok
select crdb_internal.protect_mvcc_history(1709131929793796000.0000000000, '10ms'::interval, 'molt fetch');

# Confirm that the PTS is gone.
query TTT retry
select ts as timestamp, meta_type, crdb_internal.pb_to_json( 'cockroach.protectedts.Target', target ) from system.protected_ts_records
----

subtest clear_query_plan_cache

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# The first execution should miss the query cache.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%query cache%';
----
query cache miss
query cache add

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# The second execution should hit the query cache.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%query cache%';
----
query cache hit

statement ok
SELECT crdb_internal.clear_query_plan_cache();

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# After running the builtin, the query cache should be cleared.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%query cache%';
----
query cache miss
query cache add

subtest clear_table_stats_cache

# Add a fresh set of table stats.
statement ok
ALTER TABLE xy INJECT STATISTICS '[
    {
        "columns": [ "x" ],
        "created_at": "2021-07-13 14:04:42.014148",
        "distinct_count": 20000,
        "histo_buckets": [
            {
                "distinct_range": 0,
                "num_eq": 60000,
                "num_range": 0,
                "upper_bound": "1"
            },
            {
                "distinct_range": 20000,
                "num_eq": 20000,
                "num_range": 20000,
                "upper_bound": "40000"
            }
        ],
        "histo_col_type": "INT8",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 100000
    },
    {
        "columns": [ "y" ],
        "created_at": "2021-07-13 14:04:42.014148",
        "distinct_count": 7000,
        "histo_col_type": "",
        "name": "__auto__",
        "histo_buckets": [
            {
                "distinct_range": 0,
                "num_eq": 7000,
                "num_range": 0,
                "upper_bound": "1"
            },
            {
                "distinct_range": 7000,
                "num_eq": 86000,
                "num_range": 7000,
                "upper_bound": "10000"
            }
        ],
        "histo_col_type": "INT8",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 100000
    },
    {
        "columns": [ "x", "y" ],
        "created_at": "2021-07-13 14:04:42.014148",
        "distinct_count": 20000,
        "histo_col_type": "",
        "name": "__auto__",
        "null_count": 0,
        "row_count": 100000
    }
]';

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# The first execution should miss the stats cache.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%reading statistics%';
----
reading statistics for table 106
finished reading statistics for table 106

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# The second execution should hit the stats cache.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%reading statistics%';
----

statement ok
SELECT crdb_internal.clear_table_stats_cache();

statement ok
SET TRACING = "on", cluster;

statement ok
SELECT * FROM xy;

statement ok
SET TRACING = "off";

# After executing the builtin, the last execution should miss the stats cache.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE '%reading statistics%';
----
reading statistics for table 106
finished reading statistics for table 106

subtest end
