@@ -5,7 +5,11 @@ cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1
5
5
export PGOPTIONS=" -c synchronous_commit=local -c search_path=pg_catalog"
6
6
7
7
PGVER=$( psql -d " $2 " -XtAc " SELECT pg_catalog.current_setting('server_version_num')::int/10000" )
8
- RESET_ARGS=" oid, oid, bigint"
8
+ if [ " $PGVER " -lt 17 ]; then
9
+ RESET_ARGS=" oid, oid, bigint"
10
+ else
11
+ RESET_ARGS=" oid, oid, bigint, bool"
12
+ fi
9
13
10
14
(echo " \set ON_ERROR_STOP on"
11
15
echo " DO \$\$
@@ -213,6 +217,88 @@ while IFS= read -r db_name; do
213
217
UPGRADE_TIMESCALEDB=$( echo -e " SELECT NULL;\nSELECT default_version != installed_version FROM pg_catalog.pg_available_extensions WHERE name = 'timescaledb'" | psql -tAX -d " ${db_name} " 2> /dev/null | tail -n 1)
214
218
if [ " $UPGRADE_TIMESCALEDB " = " t" ]; then
215
219
echo " ALTER EXTENSION timescaledb UPDATE;"
220
+ IS_VERSION_BELOW_215=$( echo -e " SELECT (installed_version < '2.15')::bool FROM pg_catalog.pg_available_extensions WHERE name = 'timescaledb'" | psql -tAX -d " ${db_name} " 2> /dev/null | tail -n 1)
221
+ if [ " $IS_VERSION_BELOW_215 " = " t" ]; then
222
+ echo " " "
223
+ -- Fix compressed hypertables with FOREIGN KEY constraints that were created with TimescaleDB versions before 2.15.0
224
+ CREATE OR REPLACE FUNCTION pg_temp.constraint_columns(regclass, int2[]) RETURNS text[] AS
225
+ $$
226
+ SELECT array_agg(attname) FROM unnest($2 ) un(attnum) LEFT JOIN pg_attribute att ON att.attrelid=$1 AND att.attnum = un.attnum;
227
+ $$ LANGUAGE SQL SET search_path TO pg_catalog, pg_temp;
228
+ DO $$
229
+ DECLARE
230
+ ht_id int;
231
+ ht regclass;
232
+ chunk regclass;
233
+ con_oid oid;
234
+ con_frelid regclass;
235
+ con_name text;
236
+ con_columns text[];
237
+ chunk_id int;
238
+
239
+ BEGIN
240
+
241
+ -- iterate over all hypertables that have foreign key constraints
242
+ FOR ht_id, ht in
243
+ SELECT
244
+ ht.id,
245
+ format('%I.%I',ht.schema_name,ht.table_name)::regclass
246
+ FROM _timescaledb_catalog.hypertable ht
247
+ WHERE
248
+ EXISTS (
249
+ SELECT FROM pg_constraint con
250
+ WHERE
251
+ con.contype='f' AND
252
+ con.conrelid=format('%I.%I',ht.schema_name,ht.table_name)::regclass
253
+ )
254
+ LOOP
255
+ RAISE NOTICE 'Hypertable % has foreign key constraint', ht;
256
+
257
+ -- iterate over all foreign key constraints on the hypertable
258
+ -- and check that they are present on every chunk
259
+ FOR con_oid, con_frelid, con_name, con_columns IN
260
+ SELECT con.oid, con.confrelid, con.conname, pg_temp.constraint_columns(con.conrelid,con.conkey)
261
+ FROM pg_constraint con
262
+ WHERE
263
+ con.contype='f' AND
264
+ con.conrelid=ht
265
+ LOOP
266
+ RAISE NOTICE 'Checking constraint % %', con_name, con_columns;
267
+ -- check that the foreign key constraint is present on the chunk
268
+
269
+ FOR chunk_id, chunk IN
270
+ SELECT
271
+ ch.id,
272
+ format('%I.%I',ch.schema_name,ch.table_name)::regclass
273
+ FROM _timescaledb_catalog.chunk ch
274
+ WHERE
275
+ ch.hypertable_id=ht_id
276
+ LOOP
277
+ RAISE NOTICE 'Checking chunk %', chunk;
278
+ IF NOT EXISTS (
279
+ SELECT FROM pg_constraint con
280
+ WHERE
281
+ con.contype='f' AND
282
+ con.conrelid=chunk AND
283
+ con.confrelid=con_frelid AND
284
+ pg_temp.constraint_columns(con.conrelid,con.conkey) = con_columns
285
+ ) THEN
286
+ RAISE WARNING 'Restoring constraint % on chunk %', con_name, chunk;
287
+ PERFORM _timescaledb_functions.constraint_clone(con_oid, chunk);
288
+ INSERT INTO _timescaledb_catalog.chunk_constraint(chunk_id, dimension_slice_id, constraint_name, hypertable_constraint_name) VALUES (chunk_id, NULL, con_name, con_name);
289
+ END IF;
290
+
291
+ END LOOP;
292
+ END LOOP;
293
+
294
+ END LOOP;
295
+
296
+ END
297
+ $$ ;
298
+
299
+ DROP FUNCTION pg_temp.constraint_columns(regclass, int2[]);
300
+ " " "
301
+ fi
216
302
fi
217
303
UPGRADE_TIMESCALEDB_TOOLKIT=$( echo -e " SELECT NULL;\nSELECT default_version != installed_version FROM pg_catalog.pg_available_extensions WHERE name = 'timescaledb_toolkit'" | psql -tAX -d " ${db_name} " 2> /dev/null | tail -n 1)
218
304
if [ " $UPGRADE_TIMESCALEDB_TOOLKIT " = " t" ]; then
0 commit comments