Skip to content

Commit

Permalink
Add a comment to get_index_columns and tests for repack_indexdef
Browse files Browse the repository at this point in the history
  • Loading branch information
za-arthur committed Oct 24, 2023
1 parent 30f426c commit 4933510
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pg_repack.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ $$
$$
LANGUAGE sql STABLE STRICT SET search_path to 'pg_catalog';

CREATE FUNCTION repack.get_index_columns(oid, text) RETURNS text AS
-- Get a comma-separated column list of the index.
--
-- Columns are quoted as literals because they are going to be passed to
-- the `repack_trigger` function as text arguments. `repack_trigger` will quote
-- them as identifiers later.
CREATE FUNCTION repack.get_index_columns(oid) RETURNS text AS
$$
SELECT coalesce(string_agg(quote_literal(attname), $2), '')
SELECT coalesce(string_agg(quote_literal(attname), ', '), '')
FROM pg_attribute,
(SELECT indrelid,
indkey,
Expand Down Expand Up @@ -97,7 +102,7 @@ $$
SELECT 'CREATE TRIGGER repack_trigger' ||
' AFTER INSERT OR DELETE OR UPDATE ON ' || repack.oid2text($1) ||
' FOR EACH ROW EXECUTE PROCEDURE repack.repack_trigger(' ||
repack.get_index_columns($2, ', ') || ')';
repack.get_index_columns($2) || ')';
$$
LANGUAGE sql STABLE STRICT;

Expand Down
13 changes: 13 additions & 0 deletions regress/expected/tablespace.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ WHERE indrelid = 'testts1'::regclass ORDER BY relname;
CREATE INDEX CONCURRENTLY index_OID ON testts1 USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;
regexp_replace
---------------------------------------------------------------------------------------------------------
CREATE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar" WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar"
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE "foo bar"
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
Expand Down
13 changes: 13 additions & 0 deletions regress/expected/tablespace_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ WHERE indrelid = 'testts1'::regclass ORDER BY relname;
CREATE INDEX CONCURRENTLY index_OID ON testts1 USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;
regexp_replace
---------------------------------------------------------------------------------------------------------
CREATE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar" WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar"
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE "foo bar"
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
Expand Down
13 changes: 13 additions & 0 deletions regress/expected/tablespace_2.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ WHERE indrelid = 'testts1'::regclass ORDER BY relname;
CREATE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;
regexp_replace
---------------------------------------------------------------------------------------------------------
CREATE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar" WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar"
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE "foo bar"
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
Expand Down
13 changes: 13 additions & 0 deletions regress/expected/tablespace_3.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ WHERE indrelid = 'testts1'::regclass ORDER BY relname;
CREATE INDEX CONCURRENTLY index_OID ON testts1 USING btree (id) WITH (fillfactor=80) TABLESPACE foo
(3 rows)

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;
regexp_replace
---------------------------------------------------------------------------------------------------------
CREATE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar" WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar"
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE "foo bar"
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
Expand Down
13 changes: 13 additions & 0 deletions regress/expected/tablespace_4.out
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ WHERE indrelid = 'testts1'::regclass ORDER BY relname;
CREATE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;
regexp_replace
---------------------------------------------------------------------------------------------------------
CREATE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar" WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE "foo bar"
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE "foo bar"
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
Expand Down
7 changes: 7 additions & 0 deletions regress/sql/tablespace.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ SELECT regexp_replace(
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;

-- Test that a tablespace is quoted as an identifier
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo bar', false),
'_[0-9]+', '_OID', 'g')
FROM pg_index i join pg_class c ON c.oid = indexrelid
WHERE indrelid = 'testts1'::regclass ORDER BY relname;

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts

Expand Down

0 comments on commit 4933510

Please sign in to comment.