Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Postgres 9.5 feature highlight: Import Foreign Schema (otacoo.com)
64 points by turrini on July 13, 2014 | hide | past | favorite | 10 comments


This is a huge win for improving the usability of foreign data wrappers. The initial setup today is a bit pain and convenience tools as this will get many more people using them. Though as 9.5 is at minimum a full year a way you could get some similar result of this from the query below. The query will create SQL to run on the one you're trying to create the foreign table to, to establish all tables in the DB as foreign ones.

    SELECT
            'CREATE FOREIGN TABLE '
            || quote_ident('#{prefix}_' || c.relname)
            || '(' || array_to_string(array_agg(quote_ident(a.attname) || ' ' || t.typname), ', ') || ') '
            || ' SERVER #{prefix}_db OPTIONS'
            || ' (schema_name ''' || quote_ident(n.nspname) || ''', table_name ''' || quote_ident(c.relname) || ''');'
          FROM
            pg_class     c,
            pg_attribute a,
            pg_type      t,
            pg_namespace n
          WHERE
            a.attnum > 0
            AND a.attrelid = c.oid
            AND a.atttypid = t.oid
            AND n.oid = c.relnamespace
            AND c.relkind in ('r', 'v')
            AND n.nspname <> 'pg_catalog'
            AND n.nspname <> 'information_schema'
            AND n.nspname !~ '^pg_toast'
            AND pg_catalog.pg_table_is_visible(c.oid)
          GROUP BY c.relname, n.nspname
          ORDER BY c.relname


Unrelated: Most SQL is typed in upper-case, and so I read it in my head as a robotic overlord, commanding the masses to IMPORT FOREIGN SCHEMA OR FACE ANNIHILATION.


I agree. Yell-caps in SQL are gross. I write all my sql in lowercase. It is case insensitive, every keyword. There is no other language that I write in where I don't use camel-case or underscore. SQL isn't going to be the exception.


Yell-caps in SQL are gross. I write all my sql in lowercase.

This may not be the right battle to be fighting. SQL reserved words have been written in all caps for a Very Long Time - it is a standard. From my personal experience, when I encounter legacy code where SQL keywords are lowercase I immediately think that the statements have been written by an inexperienced engineer (probably without any formal education).

The last time I encountered SQL statements with lowercase keywords, the individual included a table named user in their schema, causing all kinds of havoc, and reinforcing the stereotype.


It's not a battle. I won't do it. I never do it. I would name my table `user` or [user] or "user". I'm a big boy.


> There is no other language that I write in where I don't use camel-case or underscore

underscore is standard for user-defined words (variables, table, databases). All-caps distinguishes built-ins to avoid confusion with user-defined. IMO with conventions, consistency is more important than the convention itself.


The keywords are called that because they are few and known. Their key-ness is what distinguishes them.


I used to feel that way and did the same thing. I'll still do lower-case keywords on the command line for one-off queries. But for checked in code or anything that anyone else will look at, the all caps convention is the way to go. I've actually started thinking that it looks nice, now that I'm used to it. Especially when you format your crazy long query so that each indented line starts with the capital SQL keyword…


So, that is so important for you that if the team decides to have a convention in caps, you'll do it in your own way?


The team would likely decide to do it my way if they need a convention. But, if I can read their caps, they can read my no caps.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: