An Azure relational database service.
- Are FOREIGN KEY constraints (including a self-referencing foreign key), UNIQUE constraints and indexes, and CHECK constraints supported and enforced on append-only ledger tables? Can a ledger table be both the referencing and the referenced table?
Yes. See script below. There are several batches that are commented out. These batches fail if you uncomment them. They demonstrate some things you cannot do. For instance an FK constraint on column in a ledger table must have ON CASCADE NO ACTION (which is the default).
- Can such constraints be dropped or altered after creation (ALTER TABLE … DROP CONSTRAINT), and by which permissions?
Yes, as demonstrated by the script. The permissions required are the same as for any other DDL operation. That is, you need ALTER permission on the table. Which you will have if you are a member of db_owner or db_ddladmin.
- Under concurrent transactions, is a primary-key or foreign-key violation on insert into an append-only ledger table guaranteed to abort one of two conflicting inserts?
Yes. This is no different from any other table.
- Are reads on the primary replica strongly consistent across clients,
Two clients that submit the same query at exactly the same time are expected to get the same result. However, there is no guarantee. Due to scheduling etc, one of the processes may be delayed, and in the meanwhile a transaction may have been committed that causes the second client to get a different result. Here I am assuming that the database has READ_COMMITTED_SNAPSHOT enabled, as this is the default in Azure SQL Database. Not that there would be any guarantee even if you disabled it, but it is more prone to happen with READ_COMMITTED_SNAPSHOT.
and how is a connection guaranteed not to be routed to a readable secondary?
A connection can only be routed to a readable secondary if it has Application Intent=ReadOnly in the connection string.
- Which roles can drop a ledger database, or the logical server?
The same roles that can drop a regular database. (I'm sorry, but I don't know these roles on the top of my head.)
Is point-in-time restore always to a new database?
Yes. You certainly do not want a restore of the database at an earlier point in time to over-write the existing one when you have an insert-only ledger. I will need to add the disclaimer that I have not tested.
Here is my demo script. It has ROLLBACK at the end, as I did not want to have a permanent ledger table in my database.
DROP TABLE IF EXISTS Objects
SELECT * INTO Objects FROM sys.objects
ALTER TABLE Objects ADD CONSTRAINT pk_Objects PRIMARY KEY (object_id)
go
BEGIN TRANSACTION
CREATE TABLE insert_only_ledger (entry_id int NOT NULL,
object_id int NOT NULL,
somedata nvarchar(50) NOT NULL,
ref_entry_id int NULL,
entry_type tinyint CONSTRAINT ck_entry_type CHECK (entry_type BETWEEN 1 AND 5),
start_trans_id bigint GENERATED ALWAYS AS TRANSACTION_ID START NOT NULL,
start_seq_no bigint GENERATED ALWAYS AS SEQUENCE_NUMBER START NOT NULL,
CONSTRAINT pk_insert_only_ledger PRIMARY KEY (entry_id)
)
WITH (
LEDGER = ON (
LEDGER_VIEW = dbo.insert_only_ledger_view (
TRANSACTION_ID_COLUMN_NAME = trans_id,
SEQUENCE_NUMBER_COLUMN_NAME = seq_no,
OPERATION_TYPE_COLUMN_NAME = op_id,
OPERATION_TYPE_DESC_COLUMN_NAME = op_name
),
APPEND_ONLY= ON));
go
--ALTER TABLE insert_only_ledger ADD CONSTRAINT fk_ledger_object
-- FOREIGN KEY (object_id) REFERENCES dbo.Objects(object_id)
-- ON DELETE CASCADE
go
ALTER TABLE insert_only_ledger ADD CONSTRAINT fk_ledger_object
FOREIGN KEY (object_id) REFERENCES dbo.Objects(object_id)
ALTER TABLE insert_only_ledger ADD CONSTRAINT fk_ledger_self
FOREIGN KEY (ref_entry_id) REFERENCES dbo.insert_only_ledger(entry_id)
go
--INSERT insert_only_ledger (entry_id, object_id, somedata, entry_type)
-- SELECT 1, object_id, cast(newid() AS uniqueidentifier), 1
-- FROM sys.objects
go
-- INSERT insert_only_ledger (entry_id, object_id, somedata, entry_type)
-- SELECT row_number() OVER(ORDER BY object_id), object_id + 9, cast(newid() AS uniqueidentifier), 1
-- FROM sys.objects
go
INSERT insert_only_ledger (entry_id, object_id, somedata, entry_type)
SELECT row_number() OVER(ORDER BY object_id), object_id, cast(newid() AS uniqueidentifier), 1
FROM dbo.Objects
go
-- INSERT insert_only_ledger (entry_id, object_id, ref_entry_id, somedata, entry_type)
-- SELECT isnull(MAX(entry_id), 0) + 1, 8, -99, 'Bludder', 5
-- FROM dbo.insert_only_ledger
go
INSERT insert_only_ledger (entry_id, object_id, ref_entry_id, somedata, entry_type)
SELECT isnull(MAX(entry_id), 0) + 1, 8, 49, 'Bludder', 5
FROM dbo.insert_only_ledger
go
SELECT * FROM sys.foreign_keys WHERE is_not_trusted = 1 OR is_disabled = 1
go
ALTER TABLE insert_only_ledger DROP CONSTRAINT ck_entry_type
ALTER TABLE insert_only_ledger DROP CONSTRAINT fk_ledger_self
ALTER TABLE insert_only_ledger DROP CONSTRAINT fk_ledger_object
ALTER TABLE insert_only_ledger DROP CONSTRAINT pk_insert_only_ledger
go
-- ALTER TABLE insert_only_ledger ALTER COLUMN entry_id bigint NOT NULL
go
ROLLBACK TRANSACTION
``