Fixed an issue where the wrong SQL is showing for the child partition tables. Fixes #6329

This commit is contained in:
Rahul Shirsat
2021-04-26 14:32:34 +05:30
committed by Akshay Joshi
parent abdd3190fe
commit b853bda2a2
10 changed files with 77 additions and 19 deletions

View File

@@ -17,5 +17,6 @@ Housekeeping
Bug fixes
*********
| `Issue #6329 <https://redmine.postgresql.org/issues/6329>`_ - Fixed an issue where the wrong SQL is showing for the child partition tables.
| `Issue #6341 <https://redmine.postgresql.org/issues/6341>`_ - Fixed an issue where CSV download quotes the numeric columns.
| `Issue #6385 <https://redmine.postgresql.org/issues/6385>`_ - Ensure that Backup and Restore should work on shared servers.

View File

@@ -1,5 +1,5 @@
SELECT c.oid, conname as name,
NOT convalidated as convalidated
NOT convalidated as convalidated, conislocal
FROM pg_catalog.pg_constraint c
WHERE contype = 'c'
AND conrelid = {{ tid }}::oid

View File

@@ -1,6 +1,6 @@
SELECT c.oid, conname as name, relname, nspname, description as comment,
pg_catalog.pg_get_expr(conbin, conrelid, true) as consrc,
connoinherit, NOT convalidated as convalidated
connoinherit, NOT convalidated as convalidated, conislocal
FROM pg_catalog.pg_constraint c
JOIN pg_catalog.pg_class cl ON cl.oid=conrelid
JOIN pg_catalog.pg_namespace nl ON nl.oid=relnamespace

View File

@@ -1,4 +1,4 @@
SELECT c.oid, conname as name
SELECT c.oid, conname as name, conislocal
FROM pg_catalog.pg_constraint c
WHERE contype = 'c'
AND conrelid = {{ tid }}::oid

View File

@@ -1,5 +1,5 @@
SELECT c.oid, conname as name, relname, nspname, description as comment ,
pg_catalog.pg_get_expr(conbin, conrelid, true) as consrc
pg_catalog.pg_get_expr(conbin, conrelid, true) as consrc, conislocal
FROM pg_catalog.pg_constraint c
JOIN pg_catalog.pg_class cl ON cl.oid=conrelid
JOIN pg_catalog.pg_namespace nl ON nl.oid=relnamespace

View File

@@ -16,7 +16,8 @@ SELECT ct.oid,
nr.nspname as refnsp,
cr.relname as reftab,
description as comment,
convalidated
convalidated,
conislocal
FROM pg_catalog.pg_constraint ct
JOIN pg_catalog.pg_class cl ON cl.oid=conrelid
JOIN pg_catalog.pg_namespace nl ON nl.oid=cl.relnamespace

View File

@@ -17,7 +17,8 @@ SELECT
cl.relname as fktab,
nr.nspname as refnsp,
cr.relname as reftab,
description as comment
description as comment,
conislocal
FROM pg_catalog.pg_constraint ct
JOIN pg_catalog.pg_class cl ON cl.oid=conrelid
JOIN pg_catalog.pg_namespace nl ON nl.oid=cl.relnamespace

View File

@@ -14,6 +14,7 @@ SELECT cls.oid,
END AS comment,
condeferrable,
condeferred,
conislocal,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fillfactor=([0-9]*)') AS fillfactor
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class cls ON cls.oid=indexrelid

View File

@@ -14,6 +14,7 @@ SELECT cls.oid,
END AS comment,
condeferrable,
condeferred,
conislocal,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fillfactor=([0-9]*)') AS fillfactor
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class cls ON cls.oid=indexrelid

View File

@@ -294,20 +294,25 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
idxcons_utils.get_index_constraints(self.conn, did, tid, ctype)
if status:
for cons in constraints:
data.setdefault(
index_constraints[ctype], []).append(cons)
if not self.\
_is_partition_and_constraint_inherited(cons, data):
data.setdefault(
index_constraints[ctype], []).append(cons)
# Add Foreign Keys
status, foreign_keys = fkey_utils.get_foreign_keys(self.conn, tid)
if status:
for fk in foreign_keys:
data.setdefault('foreign_key', []).append(fk)
if not self._is_partition_and_constraint_inherited(fk, data):
data.setdefault('foreign_key', []).append(fk)
# Add Check Constraints
status, check_constraints = \
check_utils.get_check_constraints(self.conn, tid)
if status:
data['check_constraint'] = check_constraints
for cc in check_constraints:
if not self._is_partition_and_constraint_inherited(cc, data):
data.setdefault('check_constraint', []).append(cc)
# Add Exclusion Constraint
status, exclusion_constraints = \
@@ -316,6 +321,23 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
for ex in exclusion_constraints:
data.setdefault('exclude_constraint', []).append(ex)
@staticmethod
def _is_partition_and_constraint_inherited(constraint, data):
"""
This function will check whether a constraint is local or
inherited only for partition table
:param constraint: given constraint
:param data: partition table data
:return: True or False based on condition
"""
# check whether the table is partition or not, then check conislocal
if 'relispartition' in data and data['relispartition'] is True:
if 'conislocal' in constraint \
and constraint['conislocal'] is False:
return True
return False
def get_table_dependents(self, tid):
"""
This function get the dependents and return ajax response
@@ -820,7 +842,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
main_sql.append(rules_sql)
def _get_resql_for_partitions(self, data, rset, json_resp,
diff_partition_sql, main_sql):
diff_partition_sql, main_sql, did):
"""
##########################################
# Reverse engineered sql for PARTITIONS
@@ -828,6 +850,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
"""
sql_header = ''
partition_sql_arr = []
if len(rset['rows']):
if json_resp:
sql_header = "\n-- Partitions SQL"
@@ -900,18 +923,48 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
part_data['vacuum_toast'] = \
copy.deepcopy(self.parse_vacuum_data(
self.conn, row, 'toast'))
scid = row['schema_id']
schema = part_data['schema']
table = part_data['name']
# Get all the supported constraints for partition table
self._add_constrints_to_output(part_data, did, row['oid'])
partition_sql += render_template("/".join(
[self.partition_template_path, self._CREATE_SQL]),
data=part_data, conn=self.conn) + '\n'
# Add into main sql
partition_sql = re.sub(self.pattern, self.double_newline,
partition_sql).strip('\n')
partition_main_sql = partition_sql.strip('\n')
partition_sql = re.sub(self.pattern, self.double_newline,
partition_sql).strip('\n')
partition_main_sql = partition_sql.strip('\n')
# Add into partition sql to partition array
partition_sql_arr.append(partition_main_sql)
# Get Reverse engineered sql for ROW SECURITY POLICY
self._get_resql_for_row_security_policy(scid, row['oid'],
json_resp,
partition_sql_arr,
schema, table)
# Get Reverse engineered sql for Triggers
self._get_resql_for_triggers(row['oid'], json_resp,
partition_sql_arr, schema, table)
# Get Reverse engineered sql for Compound Triggers
self._get_resql_for_compound_triggers(row['oid'],
partition_sql_arr,
schema, table)
# Get Reverse engineered sql for Rules
self._get_resql_for_rules(row['oid'], partition_sql_arr, table,
json_resp)
if not diff_partition_sql:
main_sql.append(
sql_header + self.double_newline + partition_main_sql
)
main_sql.append(sql_header + '\n')
main_sql += partition_sql_arr
def get_reverse_engineered_sql(self, **kwargs):
"""
@@ -964,7 +1017,7 @@ class BaseTableView(PGChildNodeView, BasePartitionTable, VacuumSettings):
return internal_server_error(errormsg=rset)
self._get_resql_for_partitions(data, rset, json_resp,
diff_partition_sql, main_sql)
diff_partition_sql, main_sql, did)
sql = '\n'.join(main_sql)