Skip to content

Commit fb0d5d5

Browse files
committed
Add first/last timestamps to expire tables
This adds two timestamp (with timezone) columns to all expire tables called "first" and "last". When an entry is added, both are set to the current (transaction) timestamp. When an entry already exists a new insert will result in the "last" timestamp being updated. Having these two timestamps allows various expire/updating strategies, for instance: * update oldest entry * update entry that didn't change for the longest time * update older entries but only if there are no recent changes, which indicates that there might be more changes coming For backwards compatibility the code detects which table format is used and falls back to the old behaviour if the timestamp columns aren't there.
1 parent ca7a1df commit fb0d5d5

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/expire-output.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,22 @@ expire_output_t::output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
5959

6060
pg_conn_t connection{conninfo};
6161

62-
connection.exec("PREPARE insert_tiles(int4, int4, int4) AS"
63-
" INSERT INTO {} (zoom, x, y) VALUES ($1, $2, $3)"
64-
" ON CONFLICT DO NOTHING",
65-
qn);
62+
auto const result = connection.exec("SELECT * FROM {} LIMIT 1", qn);
63+
64+
if (result.num_fields() == 3) {
65+
// old format with fields: zoom, x, y
66+
connection.exec("PREPARE insert_tiles(int4, int4, int4) AS"
67+
" INSERT INTO {} (zoom, x, y) VALUES ($1, $2, $3)"
68+
" ON CONFLICT DO NOTHING",
69+
qn);
70+
} else {
71+
// new format with fields: zoom, x, y, first, last
72+
connection.exec("PREPARE insert_tiles(int4, int4, int4) AS"
73+
" INSERT INTO {} (zoom, x, y) VALUES ($1, $2, $3)"
74+
" ON CONFLICT (zoom, x, y)"
75+
" DO UPDATE SET last = CURRENT_TIMESTAMP(0)",
76+
qn);
77+
}
6678

6779
auto const count = for_each_tile(
6880
tiles_at_maxzoom, m_minzoom, m_maxzoom, [&](tile_t const &tile) {
@@ -76,10 +88,13 @@ expire_output_t::output_tiles_to_table(quadkey_list_t const &tiles_at_maxzoom,
7688
void expire_output_t::create_output_table(pg_conn_t const &connection) const
7789
{
7890
auto const qn = qualified_name(m_schema, m_table);
79-
connection.exec("CREATE TABLE IF NOT EXISTS {} ("
80-
" zoom int4 NOT NULL,"
81-
" x int4 NOT NULL,"
82-
" y int4 NOT NULL,"
83-
" PRIMARY KEY (zoom, x, y))",
84-
qn);
91+
connection.exec(
92+
"CREATE TABLE IF NOT EXISTS {} ("
93+
" zoom int4 NOT NULL,"
94+
" x int4 NOT NULL,"
95+
" y int4 NOT NULL,"
96+
" first timestamp with time zone DEFAULT CURRENT_TIMESTAMP(0),"
97+
" last timestamp with time zone DEFAULT CURRENT_TIMESTAMP(0),"
98+
" PRIMARY KEY (zoom, x, y))",
99+
qn);
85100
}

0 commit comments

Comments
 (0)