<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="211492" data-post-id="211492">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="nathanl" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nathanl/120/39891_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  nathanl
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="kenny-evitt" data-post="11" data-topic="37126">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/k/e47774/48.png" class="avatar"> kenny-evitt:</div>
<blockquote>
<p>I really like PostgreSQL’s materialized views as an alternative to temporary tables too. Like tables, they can be indexed. And there’s nice concise syntax for ‘refreshing’ one of those views.</p>
</blockquote>
</aside>
<p>You can also build an <code>Ecto.Schema</code> for a view (materialized or otherwise) and treat it like any other table. For example, if you have a system with a <code>Game</code> schema, you could have it <code>has_one(:game_stats, GameStats)</code> and have that schema backed by a view.</p>
<p>Refreshing a materialized view could be a nice job for a GenServer. For example, when relevant changes come in, send a message to the GenServer, which checks every N seconds to see if there are any changes since the last time and refreshes the view if there are. It would be harder to implement that kind of rate limiting if you refreshed it using a database trigger, and refreshing it too often could burden the database.</p>
<p>You can create a materialized view in a migration, you just have to do</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">execute("""
    CREATE MATERIALIZED VIEW game_stats
    AS 
    SELECT
    -- ...
""")
</code></pre>
<p>The only way I know to update that later is to write a new migration where you drop the old one and create the new one - copy, paste, and edit from the prior migration.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="211492" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-use-postgresql-temporary-tables-with-ecto/37126/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-211492" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="211492"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="211513" data-post-id="211513">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kenny-evitt" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kenny-evitt
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks!</p>
<p>I think I knew that you could use views with Ecto schemas, but it’s good to be reminded of it – seems potentially really handy!</p>
<p>I also like your idea of using a <code>GenServer</code> to refresh the view. There’s so many things one can do with little servers like this.</p>
<p>I’ve already run into scenarios where it was <em>best</em> to use raw SQL in migrations, e.g. instead of referencing schemas (which aren’t migrated, hence subject to change, thus breaking migration code).</p>
<p>There is a <code>OR REPLACE</code> option for <a href="https://www.postgresql.org/docs/9.6/sql-createview.html" rel="noopener nofollow ugc"><code>CREATE VIEW</code></a>, but that seems pretty equivalent to <code>DROP VIEW ...</code> and then <code>CREATE VIEW ...</code> anyways.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="211513" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-use-postgresql-temporary-tables-with-ecto/37126/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-211513" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="211513"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>