1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-16 13:38:02 +00:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Karim Bouchez
2a68baf469 Merge 15337dfd71 into 44e626a086 2024-05-16 23:56:48 +01:00
jbranchaud
44e626a086 Add source link for latest TIL 2024-05-16 14:50:34 -05:00
jbranchaud
48d2ecffa0 Add Get Row Count For Most Recent Query as a Postgres TIL 2024-05-16 14:49:37 -05:00
Karim Bouchez
15337dfd71 Update the old way to capture a GitHub Actions output
See [here](https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/) for more explanations:
> We are monitoring telemetry for the usage of these commands and plan to fully disable them on 31st May 2023. Starting 1st June 2023 workflows using save-state or set-output commands via stdout will fail with an error.
2023-02-12 10:36:45 +01:00
3 changed files with 70 additions and 4 deletions

View File

@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
_1430 TILs and counting..._
_1431 TILs and counting..._
---
@@ -734,6 +734,7 @@ _1430 TILs and counting..._
- [Generate Series Of Numbers](postgres/generate-series-of-numbers.md)
- [Generating UUIDs With pgcrypto](postgres/generating-uuids-with-pgcrypto.md)
- [Get A Quick Approximate Count Of A Table](postgres/get-a-quick-approximate-count-of-a-table.md)
- [Get Row Count For Most Recent Query](postgres/get-row-count-for-most-recent-query.md)
- [Get The Size On Disk of An Index](postgres/get-the-size-on-disk-of-an-index.md)
- [Get The Size Of A Database](postgres/get-the-size-of-a-database.md)
- [Get The Size Of A Table](postgres/get-the-size-of-a-table.md)

View File

@@ -23,11 +23,11 @@ version from my `.tool-versions` file with a step that uses `set-output`.
- name: Read Node.js version to install from `.tool-versions`
id: nodejs
run: >-
echo "::set-output name=NODE_VERSION::$(
echo "NODE_VERSION=$(
cat .tool-versions |
grep nodejs |
sed 's/nodejs \(.*\)$/\1/'
)"
)" >> $GITHUB_OUTPUT
```
`echo` runs the command in the string which sets `NODE_VERSION` as an output
@@ -45,4 +45,4 @@ This output value can be referenced in a later step.
`steps` has a reference to the `nodejs` step (note the `id` above) which then
has `outputs` like the `NODE_VERSION`.
[source](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions)
[source](https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter)

View File

@@ -0,0 +1,65 @@
# Get Row Count For Most Recent Query
Anytime you execute a query in `psql`, there is a _row count_ associated with
that query. This is most naturally understood with a `select` query where a
discreet number of rows are returned. We typically see the row count (e.g. `(19
rows)`) right below the result set.
You can always reference the row count of the most recent query with [the
`:ROW_COUNT`
variable](https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES-ROW-COUNT).
Here we use `\echo` to print it out.
```sql
> select generate_series(2,20);
generate_series
-----------------
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(19 rows)
Time: 12.338 ms
> \echo :ROW_COUNT
19
```
For some queries, like one that induces a pager (e.g. `less`) to be used,
you'll lose track of the row count once the pager closes. This is where being
able to reference the row count without rerunning the query is most useful.
```sql
> select generate_series(2,2000);
Time: 9.815 ms
> \echo :ROW_COUNT
1999
```
Notice, we can also get a row count from other kinds of queries like this
`insert` statement.
```sql
> insert into users (id) values (50001), (50002), (50003);
INSERT 0 3
Time: 2.804 ms
> \echo :ROW_COUNT
3
```
[source](https://postgresql.verite.pro/blog/2024/05/13/advanced-psql-coproc.html)