Skip to content

Commit 389be50

Browse files
authored
Merge pull request #2080 from joto/timestamps-in-expire-tables
Add first/last timestamps to expire tables
2 parents 6746e54 + fb0d5d5 commit 389be50

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)