Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default;
;

ALTER TABLE IF EXISTS public.test_table_for_indexes
CLUSTER ON "Idx1_$%{}[]()&*^!@""'`\/#";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
TABLESPACE pg_default;
;

COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx3_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=10)
TABLESPACE pg_default;
;

ALTER TABLE IF EXISTS public.test_table_for_indexes
CLUSTER ON "Idx1_$%{}[]()&*^!@""'`\/#";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
TABLESPACE pg_default;
;

COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx3_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE UNIQUE INDEX "Idx3_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, (lower(name)) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10)
TABLESPACE pg_default

WHERE id < 100;

COMMENT ON INDEX public."Idx3_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
CREATE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING hash
(id)
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING hash
(id)
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=False)
TABLESPACE pg_default

WHERE id < 100;

ALTER INDEX public."Idx_$%{}[]()&*^!@""'`\/#"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE INDEX
ON public.test_table_for_indexes USING hash
(id)
TABLESPACE pg_default;
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2026, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################

"""Regression test for #10341: CREATE INDEX ... TABLESPACE pg_default is
rejected by PostgreSQL when the index's table is partitioned ('cannot
specify default tablespace for partitioned relations'). pgAdmin's index
create.sql templates always emitted the TABLESPACE clause for any
selected tablespace, including the pre-filled 'pg_default' default,
which is invalid for a partitioned table and a no-op for anything else.

The index_constraint/exclusion_constraint create.sql templates already
guard against this (data.spcname != "pg_default"); this test asserts the
plain index templates apply the same guard, without requiring a running
PostgreSQL server.
"""

import os

import pgadmin
from flask import Flask, render_template
from jinja2 import FileSystemLoader

from pgadmin.utils.driver import get_driver
from pgadmin.utils.route import BaseTestGenerator
from config import PG_DEFAULT_DRIVER


class _FakeConn:
"""Stand-in for a psycopg connection so qtIdent resolves without a
live server."""

conn = None

def __bool__(self):
return True


class _FakeApp(Flask):
"""Minimal Flask app mirroring the production Jinja filters."""

def __init__(self, template_root):
super().__init__('')
driver = get_driver(PG_DEFAULT_DRIVER, self)
self.jinja_env.filters['qtIdent'] = driver.qtIdent
self.jinja_env.loader = FileSystemLoader([template_root])


_TEMPLATE_ROOT = os.path.join(
os.path.dirname(pgadmin.__file__), 'browser', 'server_groups', 'servers',
'databases', 'schemas', 'tables', 'templates')


def _base_data(spcname):
return dict(
name='my_index', schema='public', table='my_partitioned_table',
indisunique=False, isconcurrent=False, indisonly=False,
amname='btree', columns=[dict(colname='my_column', is_exp=False)],
include=[], storage_parameters=None, spcname=spcname,
indconstraint=None, dependsonextensions=[],
)


class IndexCreateSQLPartitionedTablespaceTestCase(BaseTestGenerator):
"""CREATE INDEX SQL must omit a redundant TABLESPACE pg_default
clause, since it is rejected outright for partitioned tables and a
no-op for everything else."""

scenarios = [
('default templates omit TABLESPACE pg_default', dict(
template='indexes/sql/default/create.sql',
spcname='pg_default',
)),
('15_plus templates omit TABLESPACE pg_default', dict(
template='indexes/sql/15_plus/create.sql',
spcname='pg_default',
)),
('default templates keep an explicit non-default tablespace',
dict(
template='indexes/sql/default/create.sql',
spcname='custom_ts',
)),
('15_plus templates keep an explicit non-default tablespace',
dict(
template='indexes/sql/15_plus/create.sql',
spcname='custom_ts',
)),
]

def setUp(self):
self.app_under_test = _FakeApp(_TEMPLATE_ROOT)

def runTest(self):
conn = _FakeConn()
data = _base_data(self.spcname)

with self.app_under_test.app_context():
rendered = render_template(
self.template, data=data, conn=conn, mode='create',
add_not_exists_clause=False)

if self.spcname == 'pg_default':
self.assertNotIn(
'TABLESPACE', rendered,
msg=('Generated CREATE INDEX SQL must not name the '
'default tablespace explicitly, since PostgreSQL '
'rejects it for a partitioned table.\nRendered:\n{}'
.format(rendered)))
else:
self.assertIn(
'TABLESPACE {}'.format(self.spcname), rendered,
msg=('An explicitly chosen, non-default tablespace must '
'still be emitted.\nRendered:\n{}'.format(rendered)))

def tearDown(self):
pass
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FIRST{% else %}LAST{% endif %}{% endif %}{% endfor %})
{% endif %}
{% if data.storage_parameters %}
WITH ({% for key, value in data.storage_parameters.items() %}{% if loop.index != 1 %}, {% endif %}{{key}}={{value}}{% endfor %})
{% endif %}{% if data.spcname %}
{% endif %}{% if data.spcname and data.spcname != "pg_default" %}
TABLESPACE {{conn|qtIdent(data.spcname)}}{% endif %}{% if data.indconstraint %}

WHERE {{data.indconstraint}}{% endif %};
Expand Down
Loading
Loading