Skip to content

Commit

Permalink
[columnar] turn off caching when building an index, vacuum, and analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
JerrySievert committed Mar 29, 2024
1 parent a935675 commit 107778e
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
48 changes: 48 additions & 0 deletions columnar/src/backend/columnar/columnar_tableam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,10 @@ static void
columnar_vacuum_rel(Relation rel, VacuumParams *params,
BufferAccessStrategy bstrategy)
{
/* Capture the cache state and disable it for a vacuum. */
bool old_cache_mode = columnar_enable_page_cache;
columnar_enable_page_cache = false;

pgstat_progress_start_command(PROGRESS_COMMAND_VACUUM,
RelationGetRelid(rel));

Expand Down Expand Up @@ -1779,6 +1783,9 @@ columnar_vacuum_rel(Relation rel, VacuumParams *params,
Max(new_live_tuples, 0),
0);
pgstat_progress_end_command();

/* Reenable the cache state. */
columnar_enable_page_cache = old_cache_mode;
}


Expand Down Expand Up @@ -2074,6 +2081,10 @@ columnar_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin,
double *liverows, double *deadrows,
TupleTableSlot *slot)
{
/* Capture the cache state and disable it for a vacuum. */
bool old_cache_mode = columnar_enable_page_cache;
columnar_enable_page_cache = false;

/*
* Currently we don't do anything smart to reduce number of rows returned
* for ANALYZE. The TableAM API's ANALYZE functions are designed for page
Expand All @@ -2088,9 +2099,16 @@ columnar_scan_analyze_next_tuple(TableScanDesc scan, TransactionId OldestXmin,
if (columnar_getnextslot(scan, ForwardScanDirection, slot))
{
(*liverows)++;

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

return true;
}

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

return false;
}

Expand Down Expand Up @@ -2126,6 +2144,10 @@ columnar_index_build_range_scan(Relation columnarRelation,
elog(ERROR, "parallel scans on columnar are not supported");
}

/* Disable the page cache for the index build. */
bool old_cache_mode = columnar_enable_page_cache;
columnar_enable_page_cache = false;

/*
* In a normal index build, we use SnapshotAny to retrieve all tuples. In
* a concurrent build or during bootstrap, we take a regular MVCC snapshot
Expand Down Expand Up @@ -2199,6 +2221,9 @@ columnar_index_build_range_scan(Relation columnarRelation,
indexInfo->ii_ExpressionsState = NIL;
indexInfo->ii_PredicateState = NULL;

/* Reset the cache mode. */
columnar_enable_page_cache = old_cache_mode;

return reltuples;
}

Expand Down Expand Up @@ -3481,6 +3506,10 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)
bool completelyDone = false;
struct sigaction action;

/* Capture the cache state and disable it for a vacuum. */
bool old_cache_mode = columnar_enable_page_cache;
columnar_enable_page_cache = false;

/*
* Set up signal handlers for any incoming signals during the vacuum,
* killing during a write could cause corruption. Give us time to
Expand Down Expand Up @@ -3512,6 +3541,9 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)

MemoryContextSwitchTo(oldcontext);

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_VOID();
}

Expand Down Expand Up @@ -3541,6 +3573,9 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)

MemoryContextSwitchTo(oldcontext);

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_VOID();
}

Expand Down Expand Up @@ -3691,6 +3726,9 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)
kil_action.sa_handler(SIGKILL);
}

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_NULL();
}

Expand All @@ -3716,6 +3754,10 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)
relation_close(rel, NoLock);
TruncateColumnar(rel, DEBUG3);
UnlockRelation(rel, ExclusiveLock);

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_UINT32(progress);
}

Expand Down Expand Up @@ -3764,6 +3806,9 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)
kil_action.sa_handler(SIGKILL);
}

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_NULL();
}

Expand Down Expand Up @@ -3857,6 +3902,9 @@ vacuum_columnar_table(PG_FUNCTION_ARGS)
sigaction(SIGABRT, &abt_action, NULL);
sigaction(SIGKILL, &kil_action, NULL);

/* Reset cache to previous state. */
columnar_enable_page_cache = old_cache_mode;

PG_RETURN_UINT32(progress + relocationProgress);
}

Expand Down
14 changes: 14 additions & 0 deletions columnar/src/test/regress/expected/columnar_indexes.out
Original file line number Diff line number Diff line change
Expand Up @@ -796,5 +796,19 @@ BEGIN;
(1 row)

ROLLBACK;
CREATE TABLE test_cache (
i INT, t TEXT
) USING columnar;
INSERT INTO test_cache SELECT generate_series(1, 100, 1) AS i, generate_series(1, 100, 1)::TEXT AS t;
SET columnar.enable_column_cache = 't';
CREATE INDEX ON test_cache (i);
-- should be true
SHOW columnar.enable_column_cache;
columnar.enable_column_cache
------------------------------
on
(1 row)

DROP TABLE test_cache;
SET client_min_messages TO WARNING;
DROP SCHEMA columnar_indexes CASCADE;
17 changes: 17 additions & 0 deletions columnar/src/test/regress/expected/columnar_vacuum.out
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,20 @@ SELECT COUNT(*) = 4 FROM columnar.stripe WHERE storage_id = :t_oid;
(1 row)

DROP TABLE t;
CREATE TABLE cache_test (i INT) USING columnar;
INSERT INTO cache_test SELECT generate_series(1, 100, 1);
SET columnar.enable_column_cache = 't';
SELECT columnar.vacuum('cache_test');
vacuum
--------
0
(1 row)

-- should be true
SHOW columnar.enable_column_cache;
columnar.enable_column_cache
------------------------------
on
(1 row)

DROP TABLE cache_test;
20 changes: 20 additions & 0 deletions columnar/src/test/regress/expected/columnar_vacuum_udf.out
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,23 @@ SELECT count(i1), count(i2), count(i3), count(i4) FROM t1;
(1 row)

DROP TABLE t1;
CREATE TABLE cache_test (i INT) USING columnar;
INSERT INTO cache_test SELECT generate_series(1, 100, 1);
SET columnar.enable_column_cache = 't';
VACUUM cache_test;
-- should be true
SHOW columnar.enable_column_cache;
columnar.enable_column_cache
------------------------------
on
(1 row)

ANALYZE cache_test;
-- should be true
SHOW columnar.enable_column_cache;
columnar.enable_column_cache
------------------------------
on
(1 row)

DROP TABLE cache_test;
12 changes: 12 additions & 0 deletions columnar/src/test/regress/sql/columnar_indexes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,18 @@ BEGIN;
SELECT COUNT(*) FROM circles_and_stuff; -- should return 1
ROLLBACK;

CREATE TABLE test_cache (
i INT, t TEXT
) USING columnar;
INSERT INTO test_cache SELECT generate_series(1, 100, 1) AS i, generate_series(1, 100, 1)::TEXT AS t;

SET columnar.enable_column_cache = 't';

CREATE INDEX ON test_cache (i);
-- should be true
SHOW columnar.enable_column_cache;

DROP TABLE test_cache;

SET client_min_messages TO WARNING;
DROP SCHEMA columnar_indexes CASCADE;
11 changes: 11 additions & 0 deletions columnar/src/test/regress/sql/columnar_vacuum.sql
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,14 @@ VACUUM t;
SELECT COUNT(*) = 4 FROM columnar.stripe WHERE storage_id = :t_oid;

DROP TABLE t;

CREATE TABLE cache_test (i INT) USING columnar;
INSERT INTO cache_test SELECT generate_series(1, 100, 1);

SET columnar.enable_column_cache = 't';
SELECT columnar.vacuum('cache_test');

-- should be true
SHOW columnar.enable_column_cache;

DROP TABLE cache_test;
16 changes: 16 additions & 0 deletions columnar/src/test/regress/sql/columnar_vacuum_udf.sql
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ SELECT columnar.vacuum('t1');
SELECT count(i1), count(i2), count(i3), count(i4) FROM t1;

DROP TABLE t1;

CREATE TABLE cache_test (i INT) USING columnar;
INSERT INTO cache_test SELECT generate_series(1, 100, 1);

SET columnar.enable_column_cache = 't';
VACUUM cache_test;

-- should be true
SHOW columnar.enable_column_cache;

ANALYZE cache_test;

-- should be true
SHOW columnar.enable_column_cache;

DROP TABLE cache_test;

0 comments on commit 107778e

Please sign in to comment.