24
24
$$
25
25
LANGUAGE sql STABLE STRICT SET search_path to 'pg_catalog';
26
26
27
- CREATE FUNCTION repack.get_index_columns(oid, text) RETURNS text AS
27
+ -- Get a comma-separated column list of the index.
28
+ --
29
+ -- Columns are quoted as literals because they are going to be passed to
30
+ -- the `repack_trigger` function as text arguments. `repack_trigger` will quote
31
+ -- them as identifiers later.
32
+ CREATE FUNCTION repack.get_index_columns(oid) RETURNS text AS
28
33
$$
29
- SELECT coalesce(string_agg(quote_ident (attname), $2 ), '')
34
+ SELECT coalesce(string_agg(quote_literal (attname), ', ' ), '')
30
35
FROM pg_attribute,
31
36
(SELECT indrelid,
32
37
indkey,
@@ -43,6 +48,37 @@ CREATE FUNCTION repack.get_order_by(oid, oid) RETURNS text AS
43
48
'MODULE_PATHNAME', 'repack_get_order_by'
44
49
LANGUAGE C STABLE STRICT;
45
50
51
+ CREATE FUNCTION repack.create_log_table(oid) RETURNS void AS
52
+ $$
53
+ BEGIN
54
+ EXECUTE 'CREATE TABLE repack.log_' || $1 ||
55
+ ' (id bigserial PRIMARY KEY,' ||
56
+ ' pk repack.pk_' || $1 || ',' ||
57
+ ' row ' || repack.oid2text($1) || ')';
58
+ END
59
+ $$
60
+ LANGUAGE plpgsql;
61
+
62
+ CREATE FUNCTION repack.create_table(oid, name) RETURNS void AS
63
+ $$
64
+ BEGIN
65
+ EXECUTE 'CREATE TABLE repack.table_' || $1 ||
66
+ ' WITH (' || repack.get_storage_param($1) || ') ' ||
67
+ ' TABLESPACE ' || quote_ident($2) ||
68
+ ' AS SELECT ' || repack.get_columns_for_create_as($1) ||
69
+ ' FROM ONLY ' || repack.oid2text($1) || ' WITH NO DATA';
70
+ END
71
+ $$
72
+ LANGUAGE plpgsql;
73
+
74
+ CREATE FUNCTION repack.create_index_type(oid, oid) RETURNS void AS
75
+ $$
76
+ BEGIN
77
+ EXECUTE repack.get_create_index_type($1, 'repack.pk_' || $2);
78
+ END
79
+ $$
80
+ LANGUAGE plpgsql;
81
+
46
82
CREATE FUNCTION repack.get_create_index_type(oid, name) RETURNS text AS
47
83
$$
48
84
SELECT 'CREATE TYPE ' || $2 || ' AS (' ||
66
102
SELECT 'CREATE TRIGGER repack_trigger' ||
67
103
' AFTER INSERT OR DELETE OR UPDATE ON ' || repack.oid2text($1) ||
68
104
' FOR EACH ROW EXECUTE PROCEDURE repack.repack_trigger(' ||
69
- '''INSERT INTO repack.log_' || $1 || '(pk, row) VALUES(' ||
70
- ' CASE WHEN $1 IS NULL THEN NULL ELSE (ROW($1.' ||
71
- repack.get_index_columns($2, ', $1.') || ')::repack.pk_' ||
72
- $1 || ') END, $2)'')';
105
+ repack.get_index_columns($2) || ')';
73
106
$$
74
107
LANGUAGE sql STABLE STRICT;
75
108
@@ -240,13 +273,12 @@ CREATE VIEW repack.tables AS
240
273
N.nspname AS schemaname,
241
274
PK.indexrelid AS pkid,
242
275
CK.indexrelid AS ckid,
243
- repack.get_create_index_type( PK.indexrelid, 'repack.pk_ ' || R.oid) AS create_pktype,
244
- 'CREATE TABLE repack.log_' || R.oid || ' (id bigserial PRIMARY KEY, pk repack.pk_ ' || R.oid || ', row ' || repack.oid2text(R.oid) || ')' AS create_log,
276
+ 'SELECT repack.create_index_type(' || PK.indexrelid || ', ' || R.oid || ')' AS create_pktype,
277
+ 'SELECT repack.create_log_table( ' || R.oid || ')' AS create_log,
245
278
repack.get_create_trigger(R.oid, PK.indexrelid) AS create_trigger,
246
279
repack.get_enable_trigger(R.oid) as enable_trigger,
247
- 'CREATE TABLE repack.table_' || R.oid || ' WITH (' || repack.get_storage_param(R.oid) || ') TABLESPACE ' AS create_table_1,
248
- coalesce(quote_ident(S.spcname), 'pg_default') as tablespace_orig,
249
- ' AS SELECT ' || repack.get_columns_for_create_as(R.oid) || ' FROM ONLY ' || repack.oid2text(R.oid) AS create_table_2,
280
+ 'SELECT repack.create_table($1, $2)' AS create_table,
281
+ coalesce(S.spcname, S2.spcname) AS tablespace_orig,
250
282
'INSERT INTO repack.table_' || R.oid || ' SELECT ' || repack.get_columns_for_create_as(R.oid) || ' FROM ONLY ' || repack.oid2text(R.oid) AS copy_data,
251
283
repack.get_alter_col_storage(R.oid) AS alter_col_storage,
252
284
repack.get_drop_columns(R.oid, 'repack.table_' || R.oid) AS drop_columns,
@@ -270,6 +302,10 @@ CREATE VIEW repack.tables AS
270
302
ON R.oid = CK.indrelid
271
303
LEFT JOIN pg_namespace N ON N.oid = R.relnamespace
272
304
LEFT JOIN pg_tablespace S ON S.oid = R.reltablespace
305
+ CROSS JOIN (SELECT S2.spcname
306
+ FROM pg_catalog.pg_database D
307
+ JOIN pg_catalog.pg_tablespace S2 ON S2.oid = D.dattablespace
308
+ WHERE D.datname = current_database()) S2
273
309
WHERE R.relkind = 'r'
274
310
AND R.relpersistence = 'p'
275
311
AND N.nspname NOT IN ('pg_catalog', 'information_schema')
@@ -281,7 +317,8 @@ LANGUAGE C STABLE;
281
317
282
318
CREATE FUNCTION repack.repack_trigger() RETURNS trigger AS
283
319
'MODULE_PATHNAME', 'repack_trigger'
284
- LANGUAGE C VOLATILE STRICT SECURITY DEFINER;
320
+ LANGUAGE C VOLATILE STRICT SECURITY DEFINER
321
+ SET search_path = pg_catalog, pg_temp;
285
322
286
323
CREATE FUNCTION repack.conflicted_triggers(oid) RETURNS SETOF name AS
287
324
$$
0 commit comments