Skip to content

Commit

Permalink
Merge remote-tracking branch 'melkij/support_for_postgresql_12'
Browse files Browse the repository at this point in the history
  • Loading branch information
dvarrazzo committed Oct 4, 2019
2 parents d028e98 + aea1115 commit a16b60a
Show file tree
Hide file tree
Showing 7 changed files with 293 additions and 27 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
@@ -1,13 +1,12 @@
# Travis CI configuration file for psycopg2

dist: trusty
dist: xenial
sudo: required

env:
- PGVER=12
- PGVER=11
PGTESTING=1
- PGVER=10
PGTESTING=1
- PGVER=9.6
- PGVER=9.5
- PGVER=9.4
Expand Down
4 changes: 2 additions & 2 deletions META.json
Expand Up @@ -2,7 +2,7 @@
"name": "pg_repack",
"abstract": "PostgreSQL module for data reorganization",
"description": "Reorganize tables in PostgreSQL databases with minimal locks",
"version": "1.4.4",
"version": "1.4.5",
"maintainer": [
"Beena Emerson <memissemerson@gmail.com>",
"Josh Kupershmidt <schmiddy@gmail.com>",
Expand All @@ -15,7 +15,7 @@
"provides": {
"pg_repack": {
"file": "lib/pg_repack.sql",
"version": "1.4.4",
"version": "1.4.5",
"abstract": "Reorganize tables in PostgreSQL databases with minimal locks"
}
},
Expand Down
7 changes: 6 additions & 1 deletion doc/pg_repack.rst
Expand Up @@ -40,7 +40,7 @@ Requirements
------------

PostgreSQL versions
PostgreSQL 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 10, 11
PostgreSQL 9.1, 9.2, 9.3, 9.4, 9.5, 9.6, 10, 11, 12

Disks
Performing a full-table repack requires free disk space about twice as
Expand Down Expand Up @@ -466,6 +466,11 @@ Creating indexes concurrently comes with a few caveats, please see `the document
Releases
--------

* pg_repack 1.4.5

* Added support for PostgreSQL 12
* Fixed parallel processing for indexes with operators from public schema

* pg_repack 1.4.4

* Added support for PostgreSQL 11 (issue #181)
Expand Down
11 changes: 10 additions & 1 deletion lib/Makefile
Expand Up @@ -19,6 +19,14 @@ OBJS = repack.o pgut/pgut-spi.o

SHLIB_EXPORTS = exports.txt


# It is not possible to create tables with OIDs on PostgreSQL 12 or later
ifeq ($(shell echo $$(($(INTVERSION) < 1200))),1)
RELHASOIDS := relhasoids
else
RELHASOIDS := false
endif

# The version number of the program. It should be the same of the library.
REPACK_VERSION = $(shell grep '"version":' ../META.json | head -1 \
| sed -e 's/[ ]*"version":[ ]*"\(.*\)",/\1/')
Expand All @@ -43,7 +51,8 @@ pg_repack.sql: pg_repack.sql.in
echo "COMMIT;" >> $@;

pg_repack--$(REPACK_VERSION).sql: pg_repack.sql.in
sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@;
sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< \
| sed 's,relhasoids,$(RELHASOIDS),g'> $@;

pg_repack.control: pg_repack.control.in
sed 's,REPACK_VERSION,$(REPACK_VERSION),g' $< > $@
28 changes: 23 additions & 5 deletions lib/repack.c
Expand Up @@ -17,6 +17,14 @@
#include "catalog/indexing.h"
#include "catalog/namespace.h"

/*
* heap_open/heap_close was moved to table_open/table_close in 12.0
* table.h has macros mapping the old names to the new ones
*/
#if PG_VERSION_NUM >= 120000
#include "access/table.h"
#endif

/*
* utils/rel.h no longer includes pg_am.h as of 9.6, so need to include
* it explicitly.
Expand Down Expand Up @@ -112,8 +120,18 @@ must_be_superuser(const char *func)
#define RENAME_REL(relid, newrelname) RenameRelationInternal(relid, newrelname, PG_TOAST_NAMESPACE);
#elif PG_VERSION_NUM < 90300
#define RENAME_REL(relid, newrelname) RenameRelationInternal(relid, newrelname);
#else
#elif PG_VERSION_NUM < 120000
#define RENAME_REL(relid, newrelname) RenameRelationInternal(relid, newrelname, true);
#else
#define RENAME_REL(relid, newrelname) RenameRelationInternal(relid, newrelname, true, false);
#endif
/*
* is_index flag was added in 12.0, prefer separate macro for relation and index
*/
#if PG_VERSION_NUM < 120000
#define RENAME_INDEX(relid, newrelname) RENAME_REL(relid, newrelname);
#else
#define RENAME_INDEX(relid, newrelname) RenameRelationInternal(relid, newrelname, true, true);
#endif

#ifdef REPACK_VERSION
Expand Down Expand Up @@ -931,7 +949,7 @@ repack_swap(PG_FUNCTION_ARGS)
snprintf(name, NAMEDATALEN, "pg_toast_%u", oid2);
RENAME_REL(reltoastrelid1, name);
snprintf(name, NAMEDATALEN, "pg_toast_%u_index", oid2);
RENAME_REL(reltoastidxid1, name);
RENAME_INDEX(reltoastidxid1, name);
CommandCounterIncrement();
}
else if (reltoastrelid1 != InvalidOid)
Expand All @@ -943,21 +961,21 @@ repack_swap(PG_FUNCTION_ARGS)
snprintf(name, NAMEDATALEN, "pg_toast_pid%d", pid);
RENAME_REL(reltoastrelid1, name);
snprintf(name, NAMEDATALEN, "pg_toast_pid%d_index", pid);
RENAME_REL(reltoastidxid1, name);
RENAME_INDEX(reltoastidxid1, name);
CommandCounterIncrement();

/* rename Y to X */
snprintf(name, NAMEDATALEN, "pg_toast_%u", oid);
RENAME_REL(reltoastrelid2, name);
snprintf(name, NAMEDATALEN, "pg_toast_%u_index", oid);
RENAME_REL(reltoastidxid2, name);
RENAME_INDEX(reltoastidxid2, name);
CommandCounterIncrement();

/* rename TEMP to Y */
snprintf(name, NAMEDATALEN, "pg_toast_%u", oid2);
RENAME_REL(reltoastrelid1, name);
snprintf(name, NAMEDATALEN, "pg_toast_%u_index", oid2);
RENAME_REL(reltoastidxid1, name);
RENAME_INDEX(reltoastidxid1, name);
CommandCounterIncrement();
}

Expand Down
234 changes: 234 additions & 0 deletions regress/expected/tablespace_4.out
@@ -0,0 +1,234 @@
SET client_min_messages = warning;
--
-- Tablespace features tests
--
-- Note: in order to pass this test you must create a tablespace called 'testts'
--
SELECT spcname FROM pg_tablespace WHERE spcname = 'testts';
spcname
---------
testts
(1 row)

-- If the query above failed you must create the 'testts' tablespace;
CREATE TABLE testts1 (id serial primary key, data text);
CREATE INDEX testts1_partial_idx on testts1 (id) where (id > 0);
CREATE INDEX testts1_with_idx on testts1 (id) with (fillfactor=80);
INSERT INTO testts1 (data) values ('a');
INSERT INTO testts1 (data) values ('b');
INSERT INTO testts1 (data) values ('c');
-- check the indexes definitions
SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, NULL, 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) WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id)
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80')
(3 rows)

SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo', 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 WHERE (id > 0)
CREATE UNIQUE INDEX index_OID ON repack.table_OID USING btree (id) TABLESPACE foo
CREATE INDEX index_OID ON repack.table_OID USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, NULL, true),
'_[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 CONCURRENTLY index_OID ON public.testts1 USING btree (id) WHERE (id > 0)
CREATE UNIQUE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id)
CREATE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id) WITH (fillfactor='80')
(3 rows)

SELECT regexp_replace(
repack.repack_indexdef(indexrelid, 'testts1'::regclass, 'foo', true),
'_[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 CONCURRENTLY index_OID ON public.testts1 USING btree (id) TABLESPACE foo WHERE (id > 0)
CREATE UNIQUE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id) TABLESPACE foo
CREATE INDEX CONCURRENTLY index_OID ON public.testts1 USING btree (id) WITH (fillfactor='80') TABLESPACE foo
(3 rows)

-- can move the tablespace from default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts
INFO: repacking table "public.testts1"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------+---------
testts1 | testts
(1 row)

SELECT * from testts1 order by id;
id | data
----+------
1 | a
2 | b
3 | c
(3 rows)

-- tablespace stays where it is
\! pg_repack --dbname=contrib_regression --no-order --table=testts1
INFO: repacking table "public.testts1"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------+---------
testts1 | testts
(1 row)

-- can move the ts back to default
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 -s pg_default
INFO: repacking table "public.testts1"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------+---------
(0 rows)

-- can move the table together with the indexes
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --tablespace testts --moveidx
INFO: repacking table "public.testts1"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------------------+---------
testts1 | testts
testts1_partial_idx | testts
testts1_pkey | testts
testts1_with_idx | testts
(4 rows)

-- can't specify --moveidx without --tablespace
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 --moveidx
ERROR: cannot specify --moveidx (-S) without --tablespace (-s)
\! pg_repack --dbname=contrib_regression --no-order --table=testts1 -S
ERROR: cannot specify --moveidx (-S) without --tablespace (-s)
-- not broken with order
\! pg_repack --dbname=contrib_regression -o id --table=testts1 --tablespace pg_default --moveidx
INFO: repacking table "public.testts1"
--move all indexes of the table to a tablespace
\! pg_repack --dbname=contrib_regression --table=testts1 --only-indexes --tablespace=testts
INFO: repacking indexes of "testts1"
INFO: repacking index "public.testts1_partial_idx"
INFO: repacking index "public.testts1_pkey"
INFO: repacking index "public.testts1_with_idx"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------------------+---------
testts1_partial_idx | testts
testts1_pkey | testts
testts1_with_idx | testts
(3 rows)

--all indexes of tablespace remain in same tablespace
\! pg_repack --dbname=contrib_regression --table=testts1 --only-indexes
INFO: repacking indexes of "testts1"
INFO: repacking index "public.testts1_partial_idx"
INFO: repacking index "public.testts1_pkey"
INFO: repacking index "public.testts1_with_idx"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------------------+---------
testts1_partial_idx | testts
testts1_pkey | testts
testts1_with_idx | testts
(3 rows)

--move all indexes of the table to pg_default
\! pg_repack --dbname=contrib_regression --table=testts1 --only-indexes --tablespace=pg_default
INFO: repacking indexes of "testts1"
INFO: repacking index "public.testts1_partial_idx"
INFO: repacking index "public.testts1_pkey"
INFO: repacking index "public.testts1_with_idx"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------+---------
(0 rows)

--move one index to a tablespace
\! pg_repack --dbname=contrib_regression --index=testts1_pkey --tablespace=testts
INFO: repacking index "public.testts1_pkey"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
--------------+---------
testts1_pkey | testts
(1 row)

--index tablespace stays as is
\! pg_repack --dbname=contrib_regression --index=testts1_pkey
INFO: repacking index "public.testts1_pkey"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
--------------+---------
testts1_pkey | testts
(1 row)

--move index to pg_default
\! pg_repack --dbname=contrib_regression --index=testts1_pkey --tablespace=pg_default
INFO: repacking index "public.testts1_pkey"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
---------+---------
(0 rows)

--using multiple --index option
\! pg_repack --dbname=contrib_regression --index=testts1_pkey --index=testts1_with_idx --tablespace=testts
INFO: repacking index "public.testts1_pkey"
INFO: repacking index "public.testts1_with_idx"
SELECT relname, spcname
FROM pg_class JOIN pg_tablespace ts ON ts.oid = reltablespace
WHERE relname ~ '^testts1'
ORDER BY relname;
relname | spcname
------------------+---------
testts1_pkey | testts
testts1_with_idx | testts
(2 rows)

--using --indexes-only and --index option together
\! pg_repack --dbname=contrib_regression --table=testts1 --only-indexes --index=testts1_pkey
ERROR: cannot specify --index (-i) and --table (-t)

0 comments on commit a16b60a

Please sign in to comment.