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

Creating views in dependency order #15

Open
bjeanes opened this issue Mar 23, 2017 · 1 comment
Open

Creating views in dependency order #15

bjeanes opened this issue Mar 23, 2017 · 1 comment

Comments

@bjeanes
Copy link

bjeanes commented Mar 23, 2017

It's possible to query for all views along with any views they depend on:

WITH views AS (
  SELECT viewname
       , definition
  FROM pg_views
  WHERE schemaname = 'public'
)

SELECT viewname
     , children
     , definition
FROM views
  LEFT JOIN (
    SELECT vtu.view_name
         , array_agg(vtu.table_name::text)
    FROM information_schema.view_table_usage vtu
    WHERE vtu.view_schema  = 'public'
      AND vtu.table_schema = 'public'
      AND vtu.table_name IN (SELECT viewname FROM views)
    GROUP BY 1
  ) deps(viewname, children) USING (viewname)
;

(this only considers public schema, but so does existing implementation).

Then, instead of ordering by viewname as is done now, a topological sort could be straight-forwardly provide an iteration order for views that would avoid having to make multiple pgdiff passes.

I expended some effort this evening trying to do the topological sort in the query (which would make it a drop-in replacement) but didn't get anywhere fruitful. It seems possible but it's either too late or my SQL-fu is not strong enough.

Instead, the tsort could be done in Go. Or, someone else could possibly step in and amend the query above to do it in Postgres.

@joncrlsn
Copy link
Owner

joncrlsn commented Nov 7, 2017

Interesting... I'm working on version 0.9.3 right now. The sorting is all done in Go now because I ran into trouble with collation differences between some databases which made for unreliable comparisons. We'll see if I can make the time to do something with topological sorting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants