Skip to content

Commit

Permalink
Use EnableQueryId()
Browse files Browse the repository at this point in the history
So that the user don't have to explicitly set compute_query_id to "on"
to make it work.
  • Loading branch information
yhuelf committed Jul 3, 2023
1 parent 9931b0d commit fcd7c81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
45 changes: 7 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ The extension can be disabled with the `pg_query_settings.enabled` parameter.
More informations on pg_query_settings
--------------------------------------

Load the library :

```
postgres=# LOAD 'pg_query_settings';
LOAD
```

Create the extension:

```
Expand All @@ -73,14 +80,6 @@ postgres=# INSERT INTO toto SELECT i, 'Ligne '||i FROM generate_series(1, 100000
INSERT 0 10000000
```

In order to retrieve the query id, you'll probably need to enable the
`compute_query_id` parameter in the same session :

```
postgres=# SET compute_query_id TO on;
SET
```

We run a query that uses a sort :

```
Expand Down Expand Up @@ -146,36 +145,6 @@ INSERT 0 1

We execute the query :

```
postgres=# EXPLAIN (COSTS OFF, ANALYZE, SETTINGS, VERBOSE) SELECT * FROM toto ORDER BY c2;
┌────────────────────────────────────────────────────────────────────────────────────┐
│ QUERY PLAN │
├────────────────────────────────────────────────────────────────────────────────────┤
│ Sort (actual time=29046.787..41640.526 rows=10000000 loops=1) │
│ Output: c1, c2 │
│ Sort Key: toto.c2 │
│ Sort Method: external merge Disk: 272920kB │
│ -> Seq Scan on public.toto (actual time=0.035..12971.535 rows=10000000 loops=1) │
│ Output: c1, c2 │
│ Settings: random_page_cost = '1.1' │
│ Query Identifier: 2507635424379213761 │
│ Planning Time: 0.162 ms │
│ Execution Time: 53110.755 ms │
└────────────────────────────────────────────────────────────────────────────────────┘
(10 rows)
```

Our configuration is not applied. That's OK, the library isn't loaded.

We load the library :

```
postgres=# LOAD 'pg_query_settings';
LOAD
```

We execute the query again:

```
postgres=# EXPLAIN (COSTS OFF, ANALYZE, SETTINGS, VERBOSE) SELECT * FROM toto ORDER BY c2;
WARNING: queryid is '2507635424379213761'
Expand Down
12 changes: 12 additions & 0 deletions pg_query_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#include <catalog/namespace.h>
#include <miscadmin.h>
#include <executor/executor.h>

#if (PG_VERSION_NUM >= 140000) && (PG_VERSION_NUM < 160000)
#include <utils/queryjumble.h>
#elif (PG_VERSION_NUM >= 160000)
#include <nodes/queryjumble.h>
#endif

#include <optimizer/planner.h>
#include <storage/bufmgr.h>
#include <utils/builtins.h>
Expand Down Expand Up @@ -351,6 +358,11 @@ _PG_init(void)
MarkGUCPrefixReserved("pg_query_settings");
#endif

#if PG_VERSION_NUM >= 140000
/* Inform core that we require a query identifier to be computed */
EnableQueryId();
#endif

if (debug) elog(DEBUG1,"Entering _PG_init()");

/* Set our two hooks */
Expand Down

0 comments on commit fcd7c81

Please sign in to comment.