patternsqlModerate
Why postgresql pg_dump exports a view as a table?
Viewed 0 times
postgresqlwhypg_dumpviewexportstable
Problem
I am using PostgreSQL 9.3
but when I check
But, if I
then
So, why is this happening? Thank you guys!
pg_dump tool for extracting only the public schema definition using:pg_dump -s -n public -h host -U postgres --dbname=db > ./schema.sqlbut when I check
schema.sql one of our views appears in a CREATE TABLE statement instead of a CREATE VIEW statement.But, if I
pg_dump the specific view using:pg_dump -s -t myview -h host -U postgres --dbname=db > ./schema.sqlthen
schema.sql contains the actual view definition.So, why is this happening? Thank you guys!
Solution
Internally, a view is just a table with a rule, so this makes sense.
See here: https://postgresql.org/docs/9.5/static/rules-views.html
Views in PostgreSQL are implemented using the rule system. In fact,
there is essentially no difference between:
compared against the two commands:
because this is exactly what the
This has some side effects. One of them is that the information about
a view in the PostgreSQL system catalogs is exactly the same as it is
for a table. So for the parser, there is absolutely no difference
between a table and a view. They are the same thing: relations.
See here: https://postgresql.org/docs/9.5/static/rules-views.html
Views in PostgreSQL are implemented using the rule system. In fact,
there is essentially no difference between:
CREATE VIEW myview AS SELECT * FROM mytab;compared against the two commands:
CREATE TABLE myview (same column list as mytab);
CREATE RULE "_RETURN" AS ON SELECT TO myview DO INSTEAD
SELECT * FROM mytab;because this is exactly what the
CREATE VIEW command does internally.This has some side effects. One of them is that the information about
a view in the PostgreSQL system catalogs is exactly the same as it is
for a table. So for the parser, there is absolutely no difference
between a table and a view. They are the same thing: relations.
Code Snippets
CREATE VIEW myview AS SELECT * FROM mytab;CREATE TABLE myview (same column list as mytab);
CREATE RULE "_RETURN" AS ON SELECT TO myview DO INSTEAD
SELECT * FROM mytab;Context
StackExchange Database Administrators Q#102579, answer score: 14
Revisions (0)
No revisions yet.