CREATE_MATERIALIZED_VIEW(7pg13)

πŸ“¦ version 13.23, in opensuse-leap 16.0

πŸ’… CREATE MATERIALIZED VIEW (7pg13) β€” the PostgreSQL 13.23 glow-up guide

okay bestie, so we're talking CREATE MATERIALIZED VIEW from the PostgreSQL 13.23 Documentation and honestly? she's iconic. let's get into it πŸŽ€


✨ NAME ✨

CREATE_MATERIALIZED_VIEW β€” define a new materialized view πŸ’– (yes, like a view but she remembers things, we love a girl with memory)


πŸ¦‹ SYNOPSIS πŸ¦‹

CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] table_name
    [ (column_name [, ...] ) ]
    [ USING method ]
    [ WITH ( storage_parameter [= value] [, ... ] ) ]
    [ TABLESPACE tablespace_name ]
    AS query
    [ WITH [ NO ] DATA ]

look at all those optional brackets β€” she's flexible, she's giving main character energy, she lets you skip the extras if you're not feeling them πŸ’…


🌸 DESCRIPTION 🌸

so CREATE MATERIALIZED VIEW defines a materialized view of a query. fr fr. and here's the tea: the query actually gets executed and used to populate the view at the exact moment you issue the command (unless you hit it with WITH NO DATA, more on that later). then you can refresh her later any time using REFRESH MATERIALIZED VIEW. she's lowkey self-care for your query results ✨

CREATE MATERIALIZED VIEW is super similar to CREATE TABLE AS β€” like, sisters β€” EXCEPT that she also remembers the query you used to initialize her, so she can be refreshed later on demand. that's the whole vibe. iykyk.

a materialized view has many of the same properties as a table, but heads up: there is no support for temporary materialized views. not a thing. don't try it. πŸ™ˆ


πŸ’œ PARAMETERS πŸ’œ

okay this is the part where we go through every single one, no skipping, because every detail matters bestie 🫢

  • IF NOT EXISTS β€” do not throw an error if a materialized view with the same name already exists. a notice gets issued in this case instead. but real talk: note that there is no guarantee that the existing materialized view is anything like the one that would have been created. she might be a completely different girl. 😭 proceed with caution.
  • table_name β€” the name (optionally schema-qualified) of the materialized view to be created. that's your girl's name πŸ’–
  • column_name β€” the name of a column in the new materialized view. if you don't provide column names, they're taken from the output column names of the query. so she just inherits them, cute πŸŽ€
  • USING method β€” this optional clause specifies the table access method to use to store the contents for the new materialized view. the method needs be an access method of type TABLE. see Chapter 60 for more info. if you don't specify this option, the default table access method is chosen for the new materialized view. see default_table_access_method for more info. slay, defaults πŸ’…
  • WITH ( storage_parameter [= value] [, ... ] ) β€” this clause specifies optional storage parameters for the new materialized view. see Storage Parameters in the CREATE TABLE (CREATE_TABLE(7)) documentation for more info. literally all parameters supported for CREATE TABLE are also supported for CREATE MATERIALIZED VIEW. see CREATE TABLE (CREATE_TABLE(7)) for more information. she's generous like that ✨
  • TABLESPACE tablespace_name β€” the tablespace_name is the name of the tablespace in which the new materialized view is to be created. if not specified, default_tablespace is consulted. she'll find a home either way 🏠
  • query β€” a SELECT(7), TABLE, or VALUES(7) command. this query will run within a security-restricted operation; in particular, calls to functions that themselves create temporary tables will fail. ⚠️ so like, keep that in mind β€” no sneaky temp tables in there.
  • WITH [ NO ] DATA β€” this clause specifies whether or not the materialized view should be populated at creation time. if not, the materialized view will be flagged as unscannable and cannot be queried until REFRESH MATERIALIZED VIEW is used. so she exists but she's not ready yet β€” a vibe check she'll fail until you refresh her. 😭πŸ”₯

🌈 COMPATIBILITY 🌈

CREATE MATERIALIZED VIEW is a PostgreSQL extension. that's it, that's the whole section. homegrown, proprietary to the fam, periodt. πŸ‘‘


⭐ SEE ALSO ⭐

go read these next, bestie, they're all connected 🧚:


and that's the whole page, no cap πŸ’– this was PostgreSQL 13.23, dated 2025 β€” go forth and materialize, you absolute icon πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from