Skip to content

Commit 6576f2a

Browse files
committed
PG-1532 Rewrite seqeunces using lower level functions
By using SPI and a SQL language function to force the rewrite of sequences we made life unnecessarily hard for ourselves and also intoduced a bug where ALTER TABLE could break if the pg_tde extension's functions were not in the search path. Instead we re-use the code for updating sequence persistence whe table persistance is changed. And we only need to look at the table's presistance since it is always the same as the one of the sequences.
1 parent 3381f84 commit 6576f2a

File tree

2 files changed

+13
-61
lines changed

2 files changed

+13
-61
lines changed

contrib/pg_tde/pg_tde--1.0-rc.sql

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -414,43 +414,6 @@ BEGIN ATOMIC
414414
'certPath' VALUE kmip_cert_path));
415415
END;
416416

417-
418-
CREATE FUNCTION pg_tde_internal_refresh_sequences(table_oid OID)
419-
RETURNS VOID
420-
AS
421-
$BODY$
422-
DECLARE
423-
rec RECORD;
424-
BEGIN
425-
FOR rec IN
426-
SELECT s.relname AS sequence_name,
427-
ns.nspname AS sequence_namespace,
428-
se.seqstart AS sequence_start
429-
FROM pg_class AS t
430-
JOIN pg_attribute AS a
431-
ON a.attrelid = t.oid
432-
JOIN pg_depend AS d
433-
ON d.refobjid = t.oid
434-
AND d.refobjsubid = a.attnum
435-
JOIN pg_class AS s
436-
ON s.oid = d.objid
437-
JOIN pg_sequence AS se
438-
ON se.seqrelid = d.objid
439-
JOIN pg_namespace AS ns
440-
ON ns.oid = s.relnamespace
441-
WHERE d.classid = 'pg_catalog.pg_class'::regclass
442-
AND d.refclassid = 'pg_catalog.pg_class'::regclass
443-
AND d.deptype IN ('i', 'a')
444-
AND t.relkind IN ('r', 'P')
445-
AND s.relkind = 'S'
446-
AND t.oid = table_oid
447-
LOOP
448-
EXECUTE format('ALTER SEQUENCE %s.%s START %s', rec.sequence_namespace, rec.sequence_name, rec.sequence_start);
449-
END LOOP;
450-
END
451-
$BODY$
452-
LANGUAGE plpgsql;
453-
454417
CREATE FUNCTION pg_tde_is_encrypted(relation regclass)
455418
RETURNS boolean
456419
STRICT

contrib/pg_tde/src/pg_tde_event_capture.c

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "utils/builtins.h"
1717
#include "catalog/pg_class.h"
1818
#include "commands/defrem.h"
19+
#include "commands/sequence.h"
1920
#include "access/table.h"
2021
#include "access/relation.h"
2122
#include "catalog/pg_event_trigger.h"
@@ -28,7 +29,6 @@
2829
#include "miscadmin.h"
2930
#include "access/tableam.h"
3031
#include "catalog/tde_global_space.h"
31-
#include "executor/spi.h"
3232

3333
/* Global variable that gets set at ddl start and cleard out at ddl end*/
3434
static TdeCreateEvent tdeCurrentCreateEvent = {.tid = {.value = 0},.relation = NULL};
@@ -273,35 +273,24 @@ pg_tde_ddl_command_end_capture(PG_FUNCTION_ARGS)
273273
if (IsA(parsetree, AlterTableStmt) && tdeCurrentCreateEvent.alterAccessMethodMode)
274274
{
275275
/*
276-
* sequences are not updated automatically call a helper function that
277-
* automatically alters all of them, forcing an update on the
278-
* encryption status int ret;
276+
* sequences are not updated automatically so force rewrite by
277+
* updating their persistence to be the same as before.
279278
*/
280-
char *sql = "SELECT pg_tde_internal_refresh_sequences($1);";
281-
Oid argtypes[1];
282-
SPIPlanPtr plan;
283-
Datum args[1];
284-
char nulls[1];
285-
int ret;
279+
List *seqlist = getOwnedSequences(tdeCurrentCreateEvent.baseTableOid);
280+
ListCell *lc;
281+
Relation rel = relation_open(tdeCurrentCreateEvent.baseTableOid, NoLock);
282+
char persistence = rel->rd_rel->relpersistence;
286283

287-
SPI_connect();
284+
relation_close(rel, NoLock);
288285

289-
argtypes[0] = OIDOID;
290-
plan = SPI_prepare(sql, 1, argtypes);
291-
292-
args[0] = ObjectIdGetDatum(tdeCurrentCreateEvent.baseTableOid);
293-
nulls[0] = ' ';
286+
foreach(lc, seqlist)
287+
{
288+
Oid seq_relid = lfirst_oid(lc);
294289

295-
ret = SPI_execute_plan(plan, args, nulls, false, 0);
290+
SequenceChangePersistence(seq_relid, persistence);
291+
}
296292

297293
tdeCurrentCreateEvent.alterAccessMethodMode = false;
298-
299-
SPI_finish();
300-
301-
if (ret != SPI_OK_SELECT)
302-
{
303-
elog(ERROR, "Failed to update encryption status of sequences.");
304-
}
305294
}
306295

307296
/*

0 commit comments

Comments
 (0)