Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres create view as not a view

Tags:

sql

postgresql

Maintaining code from a former employee found a piece of SQL i cannot understand:

CREATE OR REPLACE VIEW my_view AS Not a view

There's no info on official documentation and I've beenseeking for information on this query but to no avail.

any hint?

like image 460
Forge Avatar asked Jul 30 '26 06:07

Forge


1 Answers

The view was deleted.

Your script was generated by an application which uses pg_get_viewdef(view_oid). When there is no a view with a given oid then the function returns the string not a view. Simple test:

create view my_view as select 1;

select oid 
from pg_class 
where relname = 'my_view';

  oid   
--------
 151388
(1 row)

select pg_get_viewdef(151388);

 pg_get_viewdef 
----------------
  SELECT 1;
(1 row)

drop view my_view;

select pg_get_viewdef(151388);

 pg_get_viewdef 
----------------
 Not a view
(1 row) 

Note that it does not mean that my_view does not exist. If you recreate the view it'll have another oid. The only certain conclusion is that your script is not up-to-date (It's inconsistent with the current content of the database). As a remedy you should dump the schema in SQL format, e.g.

pg_dump --schema-only --format=plain my_database > my_database.dump
like image 74
klin Avatar answered Jul 31 '26 20:07

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!