Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions index_is_partial #342

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/pgtap.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -5144,6 +5144,29 @@ Tests whether an index is unique.

Tests whether an index is on a primary key.

### `index_is_partial()` ###

SELECT index_is_partial( :schema, :table, :index, :description );
SELECT index_is_partial( :schema, :table, :index );
SELECT index_is_partial( :table, :index );
SELECT index_is_partial( :index );

**Parameters**

`:schema`
: Schema in which to find the table.

`:table`
: Name of a table containing the index.

`:index`
: Name of the index.

`:description`
: A short description of the test.

Tests whether an index is a partial index or not.

### `is_partitioned()` ###

SELECT is_partitioned( :schema, :table, :description );
Expand Down
72 changes: 71 additions & 1 deletion sql/pgtap--1.3.3--1.3.4.sql
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
-- TBD
-- index_is_partial( schema, table, index, description )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME, NAME, NAME, text )
RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace
WHERE ct.relname = $2
AND ci.relname = $3
AND n.nspname = $1
INTO res;

RETURN ok( COALESCE(res, false), $4 );
END;
$$ LANGUAGE plpgsql;
rodo marked this conversation as resolved.
Show resolved Hide resolved

-- index_is_primary( schema, table, index )
rodo marked this conversation as resolved.
Show resolved Hide resolved
CREATE OR REPLACE FUNCTION index_is_primary ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT index_is_partial(
$1, $2, $3,
'Index ' || quote_ident($3) || ' should be partial'
);
$$ LANGUAGE sql;

-- index_is_partial( table, index )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME, NAME )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these can be pure SQL I think.

RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
WHERE ct.relname = $1
AND ci.relname = $2
AND pg_catalog.pg_table_is_visible(ct.oid)
INTO res;

RETURN ok(
COALESCE(res, false),
'Index ' || quote_ident($2) || ' should be partial'
);
END;
$$ LANGUAGE plpgsql;

-- index_is_partial( index )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME )
RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
WHERE ci.relname = $1
AND pg_catalog.pg_table_is_visible(ct.oid)
INTO res;

RETURN ok(
COALESCE(res, false),
'Index ' || quote_ident($1) || ' should be partial'
);
END;
$$ LANGUAGE plpgsql;
76 changes: 74 additions & 2 deletions sql/pgtap.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -869,11 +869,11 @@ BEGIN
SELECT GREATEST((return_percent * iterations)::int, 1) INTO limit_it;

FOR a_time IN SELECT times[i]
FROM generate_series(array_lower(times, 1), array_upper(times, 1)) i
FROM generate_series(array_lower(times, 1), array_upper(times, 1)) i
rodo marked this conversation as resolved.
Show resolved Hide resolved
ORDER BY 1
OFFSET offset_it
LIMIT limit_it LOOP
RETURN NEXT a_time;
RETURN NEXT a_time;
END LOOP;
END;
$$ LANGUAGE plpgsql;
Expand Down Expand Up @@ -3261,6 +3261,78 @@ BEGIN
END;
$$ LANGUAGE plpgsql;

-- index_is_partial( schema, table, index, description )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME, NAME, NAME, text )
RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
JOIN pg_catalog.pg_namespace n ON n.oid = ct.relnamespace
WHERE ct.relname = $2
AND ci.relname = $3
AND n.nspname = $1
INTO res;

RETURN ok( COALESCE(res, false), $4 );
END;
$$ LANGUAGE plpgsql;

-- index_is_partial( schema, table, index )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT index_is_partial(
$1, $2, $3,
'Index ' || quote_ident($3) || ' should be partial'
);
$$ LANGUAGE sql;

-- index_is_partial( table, index )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME, NAME )
RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
WHERE ct.relname = $1
AND ci.relname = $2
AND pg_catalog.pg_table_is_visible(ct.oid)
INTO res;

RETURN ok(
COALESCE(res, false),
'Index ' || quote_ident($2) || ' should be partial'
);
END;
$$ LANGUAGE plpgsql;

-- index_is_partial( index )
CREATE OR REPLACE FUNCTION index_is_partial ( NAME )
RETURNS TEXT AS $$
DECLARE
res boolean;
BEGIN
SELECT x.indpred IS NOT NULL
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class ci ON ci.oid = x.indexrelid
JOIN pg_catalog.pg_class ct ON ct.oid = x.indrelid
WHERE ci.relname = $1
AND pg_catalog.pg_table_is_visible(ct.oid)
INTO res;

RETURN ok(
COALESCE(res, false),
'Index ' || quote_ident($1) || ' should be partial'
);
END;
$$ LANGUAGE plpgsql;

-- is_clustered( schema, table, index, description )
CREATE OR REPLACE FUNCTION is_clustered ( NAME, NAME, NAME, text )
RETURNS TEXT AS $$
Expand Down
Loading