-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharticle-record-temperature-postgres.html
More file actions
executable file
·386 lines (355 loc) · 22.2 KB
/
article-record-temperature-postgres.html
File metadata and controls
executable file
·386 lines (355 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!DOCTYPE html>
<html lang="en">
<head>
<!--__RIM_REDIRECT__-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Quicksand:[email protected]&display=swap" rel="stylesheet">
<title>More than one table in a database transaction in PostgreSQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="HandheldFriendly" content="True"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="canonical" href="https://rimstone-lang.com/article-record-temperature-postgres.html" />
<style>
body, html {
font-family: Quicksand,monospace,Helvetica, Arial, sans-serif;
line-height: 150%;
font-size:16px;
}
body {
padding-left:1vw;
padding-right:1vw;
}
.ncode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.shcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.sqlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.htmlcode {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
.code {
letter-spacing: 0px;
font-family: monospace;
font-size:83%;
display:inline-block;
max-width:99%;
min-width:90%;
margin:0;
padding:0;
padding-left:5px;
padding-top:3px;
padding-bottom:3px;
margin-bottom:8px;
margin-top:8px;
border: 2px solid #d6d6d6;
background-color:#f5f7f4;
white-space:nowrap;
}
/*Just like h1 but for pdf conversion it would be indented this way it's not*/
.vhub {
display: block;
font-size: 1.6em;
margin-top: 0.63em;
margin-bottom: 0.63em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
/*Just like h2 but for pdf conversion it would be indented this way it's not*/
.vsub {
display: block;
font-size: 1.25em;
margin-top: 0.53em;
margin-bottom: 0.53em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
ul {
margin-left: 0.75vw;
padding-left: 0;
}
li {
margin-left: 0.75vw;
padding-left: 0;
}
/* this must be last, as it overrides previous settings, for mobile */
@media (hover: none) {
a {
display: inline-block;
padding-top: 3px;
padding-bottom: 2px;
}
body {
padding-left:2vw;
padding-right:2vw;
letter-spacing: 1px;
}
}
/*The following is for code snippets that are highlighted by 2html vim*/
pre { overflow-x: scroll; margin:0; padding:0; font-family:monospace; }
.Identifier { color: #008b8b; }
.Statement { color: #af5f00; }
.PreProc { color: #5fd7ff; }
.Type { color: #005f00; }
.Comment { color: blue ; }
.Constant { color: #ff00ff; }
/*end of highlighted snippets*/
a {
text-decoration:none;
padding-bottom: 0px;
color:inherit;
border-bottom: 2px solid #6cb8f0;
}
a:hover {
text-decoration: none;
color:black;
border-bottom: 1px solid red;
}
/*do not underline links nor should they be active*/
pre a {
text-decoration:none;
color:black;
border-bottom: none;
pointer-events: none;
cursor: default;
}
.golfSnippet {display:none;}
ul {
list-style-type:square;
list-style-position: outside;
}
</style>
</head>
<body>
<div id="google_translate_element" style='float:right'></div>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script>
function rim_copy(gt, eid, gc) {
gt.textContent = eid.textContent;
gt.select();
document.execCommand("copy");
gc.style.visibility="visible"
setTimeout(()=>{ gc.style.visibility="hidden"; }, 1000);
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
}
</script>
<!--RIMMENU13-->
<!--RIMENDMENU13-->
<!--BEGVDOC90-->
<div class='vhub' style='margin-top:10px;margin-right:20px;text-align:left;background-color:white;'><a href='https://rimstone-lang.com' style='border-bottom:0px'><img src='https://rimstone-lang.com/rimstone.png' style='width:180px;height:auto'/></a></div><div class='vhub' style='margin-top:10px;'>More than one table in a database transaction in PostgreSQL</div><hr/><br/>
Here you'll learn how to use a database transaction to insert/update into multiple tables; so that if any of those fail, the entire transaction is cancelled (rollbacked), and if they all succeed, the transaction is committed as a whole, atomically.<br/>
<br/>
What if you wanted to record temperatures in any given zip code, and compute averages as you go along, so both historical temperatures and averages are available at will, without computation? Here's how to do it. Setting up the database is probably the most of it, so that can't be avoided. But the actual application to do the job is super simple.<br/>
<br/>
First, create a new directory for your application, and create a new application "climate":<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_276' class=notranslate>
<span class="Statement">mkdir</span> temps
<span class="Statement">cd</span> temps
rim <span class="Special">-k</span> climate</pre>
<span id=rimstone_copied_276 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_276' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_276, code_276, rimstone_copied_276)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Here's the database setup. We'll use a little "cat" trick with EOF so we can write directly to files. So in this case, all the lines between "cat << 'EOF'..." and "EOF" are written to file "setup.sql", which is the database setup. We use MariaDB here; you can use other databases too:<br/>
<div class='sqlcode' style='position:relative;padding-right:16px;'>
<pre id='code_277' class=notranslate>
cat << <span class="Special">'</span><span class="Constant">EOF</span><span class="Special">'</span> > setup.sql
<span class="Statement">create</span> <span class="Identifier">user</span> $(whoami)
<span class="Statement">create</span> database weather_pdb <span class="Special">with</span> owner=$(whoami)
<span class="Statement">grant</span> <span class="Statement">all</span> <span class="Special">on</span> database weather_pdb <span class="Special">to</span> $(whoami)
\c weather_pdb
<span class="Statement">create</span> <span class="Special">table</span> <span class="Special">if</span> <span class="Statement">not</span> <span class="Statement">exists</span> temperature_history (zip <span class="Type">varchar</span>(<span class="Constant">10</span>), temp <span class="Type">int</span>, curr_date <span class="Type">date</span>, curr_time time);
<span class="Statement">create</span> <span class="Special">table</span> <span class="Special">if</span> <span class="Statement">not</span> <span class="Statement">exists</span> climate_avg (zip <span class="Type">varchar</span>(<span class="Constant">10</span>) primary key, average_temp <span class="Type">int</span>, <span class="Identifier">count</span> <span class="Type">int</span>);
EOF</pre>
<span id=rimstone_copied_277 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_277' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_277, code_277, rimstone_copied_277)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Run the database setup:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_279' class=notranslate>
sudo <span class="Special">-u</span> postgres psql <span class="Statement"><</span> setup.sql</pre>
<span id=rimstone_copied_279 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_279' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_279, code_279, rimstone_copied_279)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
This file ("weather_pdb") is the <a href='https://rimstone-lang.com/database-config-file.html'>database-config-file</a>. It describes your database the way your database needs it, i.e. natively. So if you'd use MariaDB you'd use MariaDB format. If you'd use SQLite, you'd use SQLite format. This makes it easy, since there's no new database config file format to learn:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_280' class=notranslate>
<span class="Statement">cat</span> <span class="Statement"><< 'EOF'</span><span class="Constant"> > weather_pdb</span>
<span class="Constant">user=bear dbname=weather_pdb</span>
<span class="Statement">EOF</span></pre>
<span id=rimstone_copied_280 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_280' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_280, code_280, rimstone_copied_280)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Here's the actual code. It's a RimStone file "weather.rim", which contains two services, "/weather/insert" (to insert new temperature data) and "/weather/get-agv" (to get historical data and averages). The code is really quite self-explanatory. In RimStone, every statements always starts with the new line (watch out for "\" at end of the line, which continues the line). So you can get documentation for <a href='https://rimstone-lang.com/get-param.html'>get-param</a> (to get input parameter), or <a href='https://rimstone-lang.com/run-query.html'>run-query</a> (to run a database query), etc. <br/>
<br/>
In this case, for instance, for "/weather/insert" service, you need to provide "zip_code" and "temperature" parameters. The URL path to do that might be for instance "/weather/insert/zip_code=11111/temperature=75", or "/weather/insert?zip_code=11111&temperature=75", depending on what looks better to you, or to what practices you subscribe. If this is a web service, you can pass those parameters as a part of HTTP request body in a POST request; the point is, there's ways to supply them.<br/>
<br/>
Here's the code:<br/>
<div class='code' style='position:relative;padding-right:16px;'>
<pre id='code_275' class=notranslate>
cat << '<span class="Constant">EOF</span>' > weather-postgres.rim
<span class="Statement">%%</span> /weather-postgres/insert<span class="Identifier"> public</span>
<span class="Statement"> get-param</span> zip_code
<span class="Statement"> get-param</span> temperature
<span class="Statement"> begin-transaction</span> @weather_pdb
<span class="Statement"> run-query</span> @weather_pdb = <span class="Constant">"insert into temperature_history (zip, temp, curr_date, curr_time) </span><span class="Special">\</span>
<span class="Constant"> values ('</span><span class="Special">%s</span><span class="Constant">', '</span><span class="Special">%s</span><span class="Constant">', CURRENT_DATE, CURRENT_TIMESTAMP)"</span> \
<span class="Identifier"> input</span> zip_code, temperature<span class="Identifier"> no-loop</span><span class="Type"> error-text</span> et
<span class="Statement"> if-true</span> et<span class="Identifier"> not-equal</span> <span class="Constant">""</span>
<span class="Statement"> @</span>Error in inserting temperature history [<span class="Statement"><<print-out</span> et<span class="Statement">>></span>]
<span class="Statement"> rollback-transaction</span> @weather_pdb
<span class="Statement"> exit-handler</span>
<span class="Statement"> else-if</span>
<span class="Statement"> run-query</span> @weather_pdb = <span class="Constant">"insert into climate_avg (zip, average_temp, count) values ('</span><span class="Special">%s</span><span class="Constant">', '</span><span class="Special">%s</span><span class="Constant">', 1) </span><span class="Special">\</span>
<span class="Constant"> on conflict(zip) do update set count=climate_avg.count+1, </span><span class="Special">\</span>
<span class="Constant"> average_temp=(climate_avg.average_temp*(climate_avg.count)+'</span><span class="Special">%s</span><span class="Constant">')/(climate_avg.count+1)"</span> \
<span class="Identifier"> input</span> zip_code, temperature, temperature<span class="Identifier"> no-loop</span><span class="Type"> error-text</span> et
<span class="Statement"> if-true</span> et<span class="Identifier"> not-equal</span> <span class="Constant">""</span>
<span class="Statement"> @</span>Error in updating temperature average [<span class="Statement"><<print-out</span> et<span class="Statement">>></span>]
<span class="Statement"> rollback-transaction</span> @weather_pdb
<span class="Statement"> exit-handler</span>
<span class="Statement"> end-if</span>
<span class="Statement"> end-if</span>
<span class="Statement"> commit-transaction</span> @weather_pdb
<span class="Statement"> @</span>Data stored and average updated.
<span class="Statement">%%</span>
<span class="Statement">%%</span> /weather-postgres/get-avg<span class="Identifier"> public</span>
<span class="Statement"> get-param</span> zip_code
<span class="Statement"> run-query</span> @weather_pdb = <span class="Constant">"select temp, curr_date, curr_time from temperature_history </span><span class="Special">\</span>
<span class="Constant"> where zip='</span><span class="Special">%s</span><span class="Constant">' order by curr_date, curr_time asc"</span><span class="Identifier"> input</span> zip_code<span class="Type"> output</span> temp, date, time
<span class="Statement"> print-format</span> <span class="Constant">"Temp [</span><span class="Special">%4ld</span><span class="Constant">] Date [</span><span class="Special">%s</span><span class="Constant"> </span><span class="Special">%s</span><span class="Constant">]</span><span class="Special">\n</span><span class="Constant">"</span>, #temp, date, time
<span class="Statement"> end-query</span>
<span class="Statement"> run-query</span> @weather_pdb = <span class="Constant">"select average_temp, count from climate_avg where zip='</span><span class="Special">%s</span><span class="Constant">'"</span><span class="Identifier"> input</span> zip_code<span class="Type"> output</span> avg, count
<span class="Statement"> @</span>Average is [<span class="Statement"><<print-out</span> avg<span class="Statement">>></span>] from the total of [<span class="Statement"><<print-out</span> count<span class="Statement">>></span>] samples
<span class="Statement"> end-query</span>
<span class="Statement">%%</span>
<span class="Constant">EOF</span></pre>
<span id=rimstone_copied_275 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_275' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_275, code_275, rimstone_copied_275)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Make the application:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_281' class=notranslate>
rim <span class="Special">-q</span> <span class="Special">--db</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">postgres:weather_pdb</span><span class="Statement">"</span></pre>
<span id=rimstone_copied_281 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_281' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_281, code_281, rimstone_copied_281)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
Insert the data. Note we're using <a href='https://rimstone-lang.com/rim.html'>rim</a> utility. You can skip the "--exec" here, and see the bash code to run your native application directly from command line. <br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_282' class=notranslate>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/weather-postgres/insert/zip_code=11111/temperature=82</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/weather-postgres/insert/zip_code=11111/temperature=102</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/weather-postgres/insert/zip_code=11111/temperature=91</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span></pre>
<span id=rimstone_copied_282 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_282' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_282, code_282, rimstone_copied_282)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
The result is:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_274' class=notranslate>
Data stored and average updated.
Data stored and average updated.
Data stored and average updated.</pre>
<span id=rimstone_copied_274 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_274' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_274, code_274, rimstone_copied_274)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
And now you can query the data:<br/>
<div class="shcode" style='position:relative;padding-right:16px;'>
<pre id='code_283' class=notranslate>
rim <span class="Special">-r</span> <span class="Special">--req</span><span class="Statement">=</span><span class="Statement">"</span><span class="Constant">/weather-postgres/get-avg/zip_code=11111</span><span class="Statement">"</span> <span class="Special">--exec</span> <span class="Special">--silent-header</span></pre>
<span id=rimstone_copied_283 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_283' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_283, code_283, rimstone_copied_283)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
With the result like:<br/>
<div class='ncode' style='position:relative;padding-right:16px;'>
<pre id='code_278' class=notranslate>
Temp [ 82] Date [2025-09-07 09:47:42]
Temp [ 102] Date [2025-09-07 09:47:42]
Temp [ 91] Date [2025-09-07 09:47:42]
Average is [91] from the total of [3] sample</pre>
<span id=rimstone_copied_278 style='position:absolute;right:-14px;top:-30px; cursor: pointer;visibility:hidden;background:white;'>Copied!</span>
<textarea id='rimstonet_278' style='position: absolute;left: -500%;'></textarea>
<img src='https://rimstone-lang.com/rimstone-copy-small-1.png' id='rimstoneb' onclick='rim_copy(rimstonet_278, code_278, rimstone_copied_278)' style='position:absolute;right:0;top:0; cursor: pointer;opacity:0.5;'/>
</div><br/>
That's the application! You can run it from <a href='https://rimstone-lang.com/command-line.html'>command-line</a> or as a web <a href='https://rimstone-lang.com/service.html'>service</a> (see <a href='https://rimstone-lang.com/article-tree-web.html'>article-tree-web</a> for an example of a setup).<br/>
<!--ENDVDOC90-->
<br/><div style='width:100%;clear:both;'>
<hr/>
<!--RIMFOOT77--><span style='font-size:80%'><a href="https://rimstone-lang.com/copyright.html">Copyright</a> (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.</span>
</div><br/></body></html>