Skip to content

Commit

Permalink
add pg_cron docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Stumble committed Feb 17, 2023
1 parent f6a8eba commit 40e5d68
Showing 1 changed file with 65 additions and 5 deletions.
70 changes: 65 additions & 5 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,6 @@ TBD.

TBD.

### Testsuite

TBD.

# Unit testing

Most Unit tests follows this pattern:
Expand Down Expand Up @@ -983,8 +979,72 @@ you can just commit files into git. Then, you are covered.
Files are saved in the same directory as other golden files, under the
directory of the test case, with suffix of `.var.golden`.

## Known Issues
## Known issues

1. Cannot use auto-generated loader
if the table has any `GENERATED ALWAYS AS IDENTITY` column.
Unless required by business logics, it is recommended to use `GENERATED BY DEFAULT AS IDENTITY`.

# FAQ

## Refresh materialized view using pg_cron

### Installation

See [this doc](https://docs.amazonaws.cn/en_us/AmazonRDS/latest/UserGuide/PostgreSQL_pg_cron.html) of how to enable pg_cron for RDS PostgreSQL.

### Refresh materialized views example

```sql
SELECT cron.schedule(
'hourly_refresh_all_materialized_views',
'0 * * * *',
$CRON$
REFRESH MATERIALIZED VIEW CONCURRENTLY book_metrics;
REFRESH MATERIALIZED VIEW CONCURRENTLY customer_metrics;
$CRON$
);
```

### Setup log clean-up job

```sql
SELECT cron.schedule('0 0 * * *', $CRON$ DELETE
FROM cron.job_run_details
WHERE end_time < now() - interval '7 days' $CRON$);
```

### Other Commands

List scheduled jobs:

```sql
select * from cron.job;
```

Turn ON/OFF jobs.

```sql
-- deactivate
update cron.job set active = false where jobid = $1;
-- activate
update cron.job set active = true where jobid = $1;
```

Unschedule

```sql
SELECT cron.unschedule(@jobid);
```

Get job run logs:

```sql
SELECT * FROM cron.job_run_details;
```

setting up cross-db jobs, extra steps are required:

```sql
UPDATE cron.job SET database = 'otherDB' WHERE jobid = $1;
```

0 comments on commit 40e5d68

Please sign in to comment.