From 31465c5bba69ae0405ed2d5bc7fa133e0803f162 Mon Sep 17 00:00:00 2001 From: Vijay Prasanna Date: Mon, 24 May 2021 17:25:35 +0530 Subject: [PATCH] console/postgres: fix broken table view when clicking on inherited tables ### Description - Fixes https://github.com/hasura/graphql-engine/issues/6926 - This is to resolve the issue where inherited tables get listed into the array that's used to filter off partitions. - In the check, we compare if the partition's parent is a "partitioned table" or not before adding to the filter array. ### Steps to verify - Create the following tables on a postgres db ```SQL CREATE TABLE cities ( id SERIAL PRIMARY KEY, name text, population real, elevation int -- (in ft) ); CREATE TABLE capitals ( state char(2) UNIQUE NOT NULL ) INHERITS (cities); ``` - Navigate to the data tab, and click on the datasource > schema, and verify that both `cities` and `capitals` are present. - Click on `capitals` and see if it loads properly. - Create the following table and its partitions ```sql CREATE TABLE measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (logdate); CREATE TABLE measurement_y2006m02 PARTITION OF measurement FOR VALUES FROM ('2006-02-01') TO ('2006-03-01'); CREATE TABLE measurement_y2006m03 PARTITION OF measurement FOR VALUES FROM ('2006-03-01') TO ('2006-04-01'); ``` - Verify that the data tab nav does not show `measurement_y2006m02` and `measurement_y2006m03` GitOrigin-RevId: 1cc988fff03f289e6ea36d0b1e9f8e54b8741be9 --- .../services/postgresql/sqlUtils.ts | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/console/src/dataSources/services/postgresql/sqlUtils.ts b/console/src/dataSources/services/postgresql/sqlUtils.ts index b99d2eae4ab..344c0942f16 100644 --- a/console/src/dataSources/services/postgresql/sqlUtils.ts +++ b/console/src/dataSources/services/postgresql/sqlUtils.ts @@ -65,11 +65,12 @@ export const getFetchTablesListQuery = (options: { SELECT COALESCE(Json_agg(Row_to_json(info)), '[]' :: json) AS tables FROM ( - with partitions as ( - select array( + WITH partitions AS ( + SELECT array( + WITH partitioned_tables AS (SELECT array(SELECT oid FROM pg_class WHERE relkind = 'p') AS parent_tables) SELECT child.relname AS partition - FROM pg_inherits + FROM partitioned_tables, pg_inherits JOIN pg_class child ON pg_inherits.inhrelid = child.oid JOIN pg_namespace nmsp_child ON nmsp_child.oid = child.relnamespace ${generateWhereClause( @@ -78,18 +79,19 @@ export const getFetchTablesListQuery = (options: { 'nmsp_child.nspname', 'where' )} - ) as names + AND pg_inherits.inhparent = ANY (partitioned_tables.parent_tables) + ) AS names ) SELECT - pgn.nspname as table_schema, - pgc.relname as table_name, - case - when pgc.relkind = 'r' then 'TABLE' - when pgc.relkind = 'f' then 'FOREIGN TABLE' - when pgc.relkind = 'v' then 'VIEW' - when pgc.relkind = 'm' then 'MATERIALIZED VIEW' - when pgc.relkind = 'p' then 'PARTITIONED TABLE' - end as table_type, + pgn.nspname AS table_schema, + pgc.relname AS table_name, + CASE + WHEN pgc.relkind = 'r' THEN 'TABLE' + WHEN pgc.relkind = 'f' THEN 'FOREIGN TABLE' + WHEN pgc.relkind = 'v' THEN 'VIEW' + WHEN pgc.relkind = 'm' THEN 'MATERIALIZED VIEW' + WHEN pgc.relkind = 'p' THEN 'PARTITIONED TABLE' + END AS table_type, obj_description(pgc.oid) AS comment, COALESCE(json_agg(DISTINCT row_to_json(isc) :: jsonb || jsonb_build_object('comment', col_description(pga.attrelid, pga.attnum))) filter (WHERE isc.column_name IS NOT NULL), '[]' :: json) AS columns, COALESCE(json_agg(DISTINCT row_to_json(ist) :: jsonb || jsonb_build_object('comment', obj_description(pgt.oid))) filter (WHERE ist.trigger_name IS NOT NULL), '[]' :: json) AS triggers,